Skip to content

Commit

Permalink
Added service config check and test for special characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
LazeringDeath committed Jul 22, 2024
1 parent a6bc38e commit 201e820
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ def __init__(
raise RuntimeError(f"File does not exist. {service_config_path}")

with open(service_config_path) as service_config_file:
service_config = json.load(service_config_file)
try:
service_config = json.loads(service_config_file.read())
except json.decoder.JSONDecodeError:
raise NameError(f"Invalid character(s) in {service_config_path}")

if service_class is None:
service = next(iter(service_config["services"]), None)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"services": [
{
"displayName": "SampleMeasurement",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService"
],
"path": "start.bat",
"annotations": {
"ni/service.description": "Measure inrush current with a shorted load and validate results against configured limits.",
"ni/service.collection": "CurrentTests.Inrush",
"ni/service.tags": [ "powerup", "current" ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"services": [
{
"displayName": "SampleMeasurement",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService"
],
"path": "start.bat",
"annotations": {
"ni/service.description": "Measure inrush current with a shorted load and validate results against configured limits.",
"ni/service.collection": "CurrentTests.Inrush",
"ni/service.tags": [ "powerup", "current" ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"services": [
{
"displayName": "SampleMeasurement",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService"
],
"path": "start.bat",
"annotations": {
"ni/service.description": "Measure inrush current with a shorted load and validate results against configured limits.",
"ni/service.collection": "CurrentTests.Inrush",
"ni/service.tags": [ "powerup", "current" ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"services": [
{
"displayName": "SampleMeasurement",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService"
],
"path": "start.bat",
"annotations": {
"ni/service.description": """,
"ni/service.collection": "CurrentTests.Inrush",
"ni/service.tags": [ "powerup", "current" ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"services": [
{
"displayName": "SampleMeasurement\",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService"
],
"path": "start.bat",
"annotations": {
"ni/service.description": "Measure inrush current with a shorted load and validate results against configured limits.",
"ni/service.collection": "\",
"ni/service.tags": [ "powerup", "current" ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"services": [
{
"displayName": "\\SampleMeasurement\",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService"
],
"path": "start.bat",
"annotations": {
"ni/service.description": "Measure inrush current with a shorted load and validate results against configured limits.",
"ni/service.collection": "CurrentTests.Inrush",
"ni/service.tags": [ "powerup", "current" ]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"services": [
{
"displayName": "SampleMeasurement\",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService"
],
"path": "start.bat",
"annotations": {
"ni/service.description": "Measure inrush current with a shorted load and validate results against configured limits.",
"ni/service.collection": "CurrentTests.Inrush",
"ni/service.tags": [ "powerup", "\"" ]
}
}
]
}
61 changes: 59 additions & 2 deletions packages/service/tests/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@

from ni_measurement_plugin_sdk_service import _datatypeinfo
from ni_measurement_plugin_sdk_service._annotations import TYPE_SPECIALIZATION_KEY
from ni_measurement_plugin_sdk_service._internal.stubs.ni.protobuf.types import xydata_pb2
from ni_measurement_plugin_sdk_service.measurement.info import DataType, TypeSpecialization
from ni_measurement_plugin_sdk_service._internal.stubs.ni.protobuf.types import (
xydata_pb2,
)
from ni_measurement_plugin_sdk_service.measurement.info import (
DataType,
TypeSpecialization,
)
from ni_measurement_plugin_sdk_service.measurement.service import MeasurementService


Expand Down Expand Up @@ -421,6 +426,58 @@ def test___service_config___create_measurement_service___service_info_matches_se
assert measurement_service.service_info.annotations == provided_annotations


@pytest.mark.parametrize(
"service_config,have_special_char",
[
(
"Control.serviceconfig",
False,
),
(
"Control.V1.serviceconfig",
False,
),
(
"Control.V2.serviceconfig",
False,
),
(
"ErrorDisplayName.serviceconfig",
True,
),
(
"ErrorAnnotations.serviceconfig",
True,
),
(
"ErrorTags.serviceconfig",
True,
),
(
"ErrorCollections.serviceconfig",
True,
),
],
)
def test___service_config___check_service_config___validiate_no_special_characters(
test_assets_directory: pathlib.Path,
service_config: str,
have_special_char: bool,
):
char_service_config = "check_char\\example." + service_config
try:
MeasurementService(
service_config_path=test_assets_directory / char_service_config,
version="1.0.0.0",
ui_file_paths=[],
)
error_occurred = False
except NameError:
error_occurred = True

assert error_occurred == have_special_char


@pytest.mark.parametrize(
"display_name,type,default_value,enum_type",
[
Expand Down

0 comments on commit 201e820

Please sign in to comment.