From 22624a7493727c24aa6fb2f347ae11d68e048266 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Fri, 22 Sep 2023 15:42:43 +0530 Subject: [PATCH 01/14] Fix launching of discovery service during fresh install --- .../_internal/discovery_client.py | 9 ++++++--- tests/unit/test_discovery_client.py | 20 ++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/ni_measurementlink_service/_internal/discovery_client.py b/ni_measurementlink_service/_internal/discovery_client.py index 614bf7356..c2ef57832 100644 --- a/ni_measurementlink_service/_internal/discovery_client.py +++ b/ni_measurementlink_service/_internal/discovery_client.py @@ -329,7 +329,7 @@ def _start_service( try: with _open_key_file(str(key_file_path)) as _: return discovery_service_subprocess - except IOError: + except OSError: pass if time.time() >= timeout_time: raise TimeoutError("Timed out waiting for discovery service to start") @@ -390,14 +390,17 @@ def _open_key_file(path: str) -> typing.TextIO: None, ) except win32file.error as e: - if e.winerror == winerror.ERROR_FILE_NOT_FOUND: + if ( + e.winerror == winerror.ERROR_FILE_NOT_FOUND + or e.winerror == winerror.ERROR_PATH_NOT_FOUND + ): raise FileNotFoundError(errno.ENOENT, e.strerror, path) from e elif ( e.winerror == winerror.ERROR_ACCESS_DENIED or e.winerror == winerror.ERROR_SHARING_VIOLATION ): raise PermissionError(errno.EACCES, e.strerror, path) from e - raise + raise WindowsError(errno.ENONET, e.strerror, path) from e # The CRT file descriptor takes ownership of the Win32 file handle. # os.O_TEXT is unnecessary because Python handles newline conversion. diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index 8d3c81cf0..2b359c7a0 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -9,6 +9,8 @@ import grpc import pytest +import win32file +import winerror from pytest_mock import MockerFixture from ni_measurementlink_service._channelpool import GrpcChannelPool @@ -16,6 +18,7 @@ DiscoveryClient, ServiceLocation, _get_discovery_service_address, + _open_key_file, _start_service, ) from ni_measurementlink_service._internal.stubs.ni.measurementlink.discovery.v1.discovery_service_pb2 import ( @@ -223,10 +226,12 @@ def test___get_discovery_service_address___start_service_jit___returns_expected_ assert _TEST_SERVICE_PORT in discovery_service_address +@pytest.mark.parametrize("key_file_error", [IOError, WindowsError]) def test___get_discovery_service_address___key_file_not_exist___throws_timeouterror( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, temp_registration_json_file_path: pathlib.Path, + key_file_error, ): mocker.patch( "ni_measurementlink_service._internal.discovery_client._get_key_file_path", @@ -236,7 +241,8 @@ def test___get_discovery_service_address___key_file_not_exist___throws_timeouter "ni_measurementlink_service._internal.discovery_client._START_SERVICE_TIMEOUT", 5.0 ) mocker.patch( - "ni_measurementlink_service._internal.discovery_client._open_key_file", side_effect=IOError + "ni_measurementlink_service._internal.discovery_client._open_key_file", + side_effect=key_file_error, ) mocker.patch( "ni_measurementlink_service._internal.discovery_client._get_registration_json_file_path", @@ -249,6 +255,18 @@ def test___get_discovery_service_address___key_file_not_exist___throws_timeouter assert exc_info.type is TimeoutError +def test___key_file_not_exist___open_key_file___raises_file_not_found_error( + mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path +): + mocker.patch( + "win32file.CreateFile", + side_effect=win32file.error(winerror.ERROR_PATH_NOT_FOUND, None, None), + ) + + with pytest.raises(FileNotFoundError): + _open_key_file(temp_discovery_key_file_path) + + def test___start_discovery_service___key_file_exist_after_poll___service_start_success( mocker: MockerFixture, temp_directory: pathlib.Path, From a23814a12bf01a6bf27feb4e765ec03b4b2e8294 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Fri, 22 Sep 2023 16:14:16 +0530 Subject: [PATCH 02/14] Fix test failure --- tests/unit/test_discovery_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index 2b359c7a0..c54762d74 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -264,7 +264,7 @@ def test___key_file_not_exist___open_key_file___raises_file_not_found_error( ) with pytest.raises(FileNotFoundError): - _open_key_file(temp_discovery_key_file_path) + _open_key_file(str(temp_discovery_key_file_path)) def test___start_discovery_service___key_file_exist_after_poll___service_start_success( From 0c36bc5e6acc2ede735e58cc9c85687abd4ac5b9 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Fri, 22 Sep 2023 16:14:16 +0530 Subject: [PATCH 03/14] Fix import failure for win32file --- tests/unit/test_discovery_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index 2b359c7a0..f2c2858d8 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -9,7 +9,7 @@ import grpc import pytest -import win32file +from win32 import win32file import winerror from pytest_mock import MockerFixture @@ -264,7 +264,7 @@ def test___key_file_not_exist___open_key_file___raises_file_not_found_error( ) with pytest.raises(FileNotFoundError): - _open_key_file(temp_discovery_key_file_path) + _open_key_file(str(temp_discovery_key_file_path)) def test___start_discovery_service___key_file_exist_after_poll___service_start_success( From c3b69586fc6cc3336111fd14559bc04f9ebd84de Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:18:26 +0530 Subject: [PATCH 04/14] Fix test failure by adding if checks for win32 platform --- tests/unit/test_discovery_client.py | 58 ++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index f2c2858d8..aa5b5a536 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -9,8 +9,6 @@ import grpc import pytest -from win32 import win32file -import winerror from pytest_mock import MockerFixture from ni_measurementlink_service._channelpool import GrpcChannelPool @@ -36,6 +34,10 @@ from ni_measurementlink_service.measurement.info import MeasurementInfo, ServiceInfo from tests.utilities.fake_rpc_error import FakeRpcError +if sys.platform == "win32": + import win32file + import winerror + _PROVIDED_MEASUREMENT_SERVICES = [ "ni.measurementlink.measurement.v1.MeasurementService", "ni.measurementlink.measurement.v2.MeasurementService", @@ -226,12 +228,10 @@ def test___get_discovery_service_address___start_service_jit___returns_expected_ assert _TEST_SERVICE_PORT in discovery_service_address -@pytest.mark.parametrize("key_file_error", [IOError, WindowsError]) -def test___get_discovery_service_address___key_file_not_exist___throws_timeouterror( +def test___open_key_file_that_throws_IOError___get_discovery_service_address___throws_timeouterror( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, temp_registration_json_file_path: pathlib.Path, - key_file_error, ): mocker.patch( "ni_measurementlink_service._internal.discovery_client._get_key_file_path", @@ -241,8 +241,7 @@ def test___get_discovery_service_address___key_file_not_exist___throws_timeouter "ni_measurementlink_service._internal.discovery_client._START_SERVICE_TIMEOUT", 5.0 ) mocker.patch( - "ni_measurementlink_service._internal.discovery_client._open_key_file", - side_effect=key_file_error, + "ni_measurementlink_service._internal.discovery_client._open_key_file", side_effect=IOError ) mocker.patch( "ni_measurementlink_service._internal.discovery_client._get_registration_json_file_path", @@ -255,16 +254,49 @@ def test___get_discovery_service_address___key_file_not_exist___throws_timeouter assert exc_info.type is TimeoutError +def test___open_key_file_that_throws_windowserror___get_discovery_service_address___throws_timeouterror( + mocker: MockerFixture, + temp_discovery_key_file_path: pathlib.Path, + temp_registration_json_file_path: pathlib.Path, +): + if sys.platform != "win32": + pytest.skip(f"Platform {sys.platform} is not supported") + else: + mocker.patch( + "ni_measurementlink_service._internal.discovery_client._get_key_file_path", + return_value=temp_discovery_key_file_path, + ) + mocker.patch( + "ni_measurementlink_service._internal.discovery_client._START_SERVICE_TIMEOUT", 5.0 + ) + mocker.patch( + "ni_measurementlink_service._internal.discovery_client._open_key_file", + side_effect=WindowsError, + ) + mocker.patch( + "ni_measurementlink_service._internal.discovery_client._get_registration_json_file_path", + return_value=temp_registration_json_file_path, + ) + mocker.patch("subprocess.Popen") + + with pytest.raises(IOError) as exc_info: + _get_discovery_service_address() + assert exc_info.type is TimeoutError + + def test___key_file_not_exist___open_key_file___raises_file_not_found_error( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path ): - mocker.patch( - "win32file.CreateFile", - side_effect=win32file.error(winerror.ERROR_PATH_NOT_FOUND, None, None), - ) + if sys.platform != "win32": + pytest.skip(f"Platform {sys.platform} is not supported") + else: + mocker.patch( + "win32file.CreateFile", + side_effect=win32file.error(winerror.ERROR_PATH_NOT_FOUND, None, None), + ) - with pytest.raises(FileNotFoundError): - _open_key_file(str(temp_discovery_key_file_path)) + with pytest.raises(FileNotFoundError): + _open_key_file(str(temp_discovery_key_file_path)) def test___start_discovery_service___key_file_exist_after_poll___service_start_success( From 1b4d4a2f3f53717ba41b826de4e6ece4e9b4e004 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:26:36 +0530 Subject: [PATCH 05/14] Fix lint error --- tests/unit/test_discovery_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index aa5b5a536..a75b450cb 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -228,7 +228,7 @@ def test___get_discovery_service_address___start_service_jit___returns_expected_ assert _TEST_SERVICE_PORT in discovery_service_address -def test___open_key_file_that_throws_IOError___get_discovery_service_address___throws_timeouterror( +def test___open_key_file_that_throws_ioError___get_discovery_service_address___throws_timeouterror( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, temp_registration_json_file_path: pathlib.Path, From 75262fa3231f548481a4f387b6fa3090698eeb24 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:29:29 +0530 Subject: [PATCH 06/14] Fix lint error --- tests/unit/test_discovery_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index a75b450cb..f2941fbb6 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -228,7 +228,7 @@ def test___get_discovery_service_address___start_service_jit___returns_expected_ assert _TEST_SERVICE_PORT in discovery_service_address -def test___open_key_file_that_throws_ioError___get_discovery_service_address___throws_timeouterror( +def test___open_key_file_that_throws_ioerror___get_discovery_service_address___throws_timeouterror( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, temp_registration_json_file_path: pathlib.Path, From ab2529934cc658903dc6d737afa6aba79505c746 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Fri, 22 Sep 2023 18:26:24 +0530 Subject: [PATCH 07/14] Rename test methods --- tests/unit/test_discovery_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index f2941fbb6..b75850476 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -228,7 +228,7 @@ def test___get_discovery_service_address___start_service_jit___returns_expected_ assert _TEST_SERVICE_PORT in discovery_service_address -def test___open_key_file_that_throws_ioerror___get_discovery_service_address___throws_timeouterror( +def test___get_discovery_service_address___open_key_file_throws_ioerror___throws_timeouterror( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, temp_registration_json_file_path: pathlib.Path, @@ -254,7 +254,7 @@ def test___open_key_file_that_throws_ioerror___get_discovery_service_address___t assert exc_info.type is TimeoutError -def test___open_key_file_that_throws_windowserror___get_discovery_service_address___throws_timeouterror( +def test___get_discovery_service_address___open_key_file_throws_windowserror___throws_timeouterror( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, temp_registration_json_file_path: pathlib.Path, From 0f09ca23e8d38aa31ed98da7f263150c966df5fd Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Mon, 25 Sep 2023 17:29:59 +0530 Subject: [PATCH 08/14] Resolve comments --- .../_internal/discovery_client.py | 8 +-- tests/unit/test_discovery_client.py | 59 +++++-------------- 2 files changed, 19 insertions(+), 48 deletions(-) diff --git a/ni_measurementlink_service/_internal/discovery_client.py b/ni_measurementlink_service/_internal/discovery_client.py index c2ef57832..8bc9ce108 100644 --- a/ni_measurementlink_service/_internal/discovery_client.py +++ b/ni_measurementlink_service/_internal/discovery_client.py @@ -339,7 +339,7 @@ def _start_service( def _service_already_running(key_file_path: pathlib.Path) -> bool: try: _delete_existing_key_file(key_file_path) - except IOError: + except OSError: return True return False @@ -394,13 +394,13 @@ def _open_key_file(path: str) -> typing.TextIO: e.winerror == winerror.ERROR_FILE_NOT_FOUND or e.winerror == winerror.ERROR_PATH_NOT_FOUND ): - raise FileNotFoundError(errno.ENOENT, e.strerror, path) from e + raise OSError(errno.ENOENT, e.strerror, path, e.winerror) from e elif ( e.winerror == winerror.ERROR_ACCESS_DENIED or e.winerror == winerror.ERROR_SHARING_VIOLATION ): - raise PermissionError(errno.EACCES, e.strerror, path) from e - raise WindowsError(errno.ENONET, e.strerror, path) from e + raise OSError(errno.EACCES, e.strerror, path, e.winerror) from e + raise OSError(None, e.strerror, path, winerror.ERROR_INVALID_FUNCTION) from e # The CRT file descriptor takes ownership of the Win32 file handle. # os.O_TEXT is unnecessary because Python handles newline conversion. diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index b75850476..2874219c4 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -228,7 +228,7 @@ def test___get_discovery_service_address___start_service_jit___returns_expected_ assert _TEST_SERVICE_PORT in discovery_service_address -def test___get_discovery_service_address___open_key_file_throws_ioerror___throws_timeouterror( +def test___get_discovery_service_address___key_file_not_exist___throws_timeouterror( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, temp_registration_json_file_path: pathlib.Path, @@ -241,7 +241,7 @@ def test___get_discovery_service_address___open_key_file_throws_ioerror___throws "ni_measurementlink_service._internal.discovery_client._START_SERVICE_TIMEOUT", 5.0 ) mocker.patch( - "ni_measurementlink_service._internal.discovery_client._open_key_file", side_effect=IOError + "ni_measurementlink_service._internal.discovery_client._open_key_file", side_effect=OSError ) mocker.patch( "ni_measurementlink_service._internal.discovery_client._get_registration_json_file_path", @@ -249,54 +249,25 @@ def test___get_discovery_service_address___open_key_file_throws_ioerror___throws ) mocker.patch("subprocess.Popen") - with pytest.raises(IOError) as exc_info: + with pytest.raises(OSError) as exc_info: _get_discovery_service_address() assert exc_info.type is TimeoutError -def test___get_discovery_service_address___open_key_file_throws_windowserror___throws_timeouterror( - mocker: MockerFixture, - temp_discovery_key_file_path: pathlib.Path, - temp_registration_json_file_path: pathlib.Path, -): - if sys.platform != "win32": - pytest.skip(f"Platform {sys.platform} is not supported") - else: - mocker.patch( - "ni_measurementlink_service._internal.discovery_client._get_key_file_path", - return_value=temp_discovery_key_file_path, - ) - mocker.patch( - "ni_measurementlink_service._internal.discovery_client._START_SERVICE_TIMEOUT", 5.0 - ) - mocker.patch( - "ni_measurementlink_service._internal.discovery_client._open_key_file", - side_effect=WindowsError, - ) - mocker.patch( - "ni_measurementlink_service._internal.discovery_client._get_registration_json_file_path", - return_value=temp_registration_json_file_path, - ) - mocker.patch("subprocess.Popen") - - with pytest.raises(IOError) as exc_info: - _get_discovery_service_address() - assert exc_info.type is TimeoutError - - +@pytest.mark.skipif(sys.platform != "win32", reason="Windows-only test") +@pytest.mark.parametrize( + "windows_error_code", [2, 3] +) # ERROR_FILE_NOT_FOUND = 2, ERROR_PATH_NOT_FOUND = 3 def test___key_file_not_exist___open_key_file___raises_file_not_found_error( - mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path + mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, windows_error_code ): - if sys.platform != "win32": - pytest.skip(f"Platform {sys.platform} is not supported") - else: - mocker.patch( - "win32file.CreateFile", - side_effect=win32file.error(winerror.ERROR_PATH_NOT_FOUND, None, None), - ) + mocker.patch( + "win32file.CreateFile", + side_effect=win32file.error(windows_error_code, None, None), + ) - with pytest.raises(FileNotFoundError): - _open_key_file(str(temp_discovery_key_file_path)) + with pytest.raises(FileNotFoundError): + _open_key_file(str(temp_discovery_key_file_path)) def test___start_discovery_service___key_file_exist_after_poll___service_start_success( @@ -309,7 +280,7 @@ def test___start_discovery_service___key_file_exist_after_poll___service_start_s mocker.patch( "ni_measurementlink_service._internal.discovery_client._open_key_file", - side_effect=[IOError, IOError, IOError, temp_discovery_key_file_path], + side_effect=[OSError, OSError, OSError, temp_discovery_key_file_path], ) mock_popen = mocker.patch("subprocess.Popen") From 81444bc34f0efa509d05292c2707ef0eb052443d Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Mon, 25 Sep 2023 17:36:25 +0530 Subject: [PATCH 09/14] Fix lint errors --- tests/unit/test_discovery_client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index 2874219c4..f85cd4be6 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -36,7 +36,6 @@ if sys.platform == "win32": import win32file - import winerror _PROVIDED_MEASUREMENT_SERVICES = [ "ni.measurementlink.measurement.v1.MeasurementService", From 358b6b1d76c801ee88566eeee886a5a22330ab90 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Mon, 25 Sep 2023 19:49:26 +0530 Subject: [PATCH 10/14] Resolve comments --- .../_internal/discovery_client.py | 14 +------------- tests/unit/test_discovery_client.py | 2 +- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/ni_measurementlink_service/_internal/discovery_client.py b/ni_measurementlink_service/_internal/discovery_client.py index 8bc9ce108..02d58c6e1 100644 --- a/ni_measurementlink_service/_internal/discovery_client.py +++ b/ni_measurementlink_service/_internal/discovery_client.py @@ -21,12 +21,10 @@ from ni_measurementlink_service.measurement.info import MeasurementInfo, ServiceInfo if sys.platform == "win32": - import errno import msvcrt import win32con import win32file - import winerror _logger = logging.getLogger(__name__) # Save Popen object to avoid "ResourceWarning: subprocess N is still running" @@ -390,17 +388,7 @@ def _open_key_file(path: str) -> typing.TextIO: None, ) except win32file.error as e: - if ( - e.winerror == winerror.ERROR_FILE_NOT_FOUND - or e.winerror == winerror.ERROR_PATH_NOT_FOUND - ): - raise OSError(errno.ENOENT, e.strerror, path, e.winerror) from e - elif ( - e.winerror == winerror.ERROR_ACCESS_DENIED - or e.winerror == winerror.ERROR_SHARING_VIOLATION - ): - raise OSError(errno.EACCES, e.strerror, path, e.winerror) from e - raise OSError(None, e.strerror, path, winerror.ERROR_INVALID_FUNCTION) from e + raise OSError(None, e.strerror, path, e.winerror) from e # The CRT file descriptor takes ownership of the Win32 file handle. # os.O_TEXT is unnecessary because Python handles newline conversion. diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index f85cd4be6..7b8f0291c 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -34,7 +34,7 @@ from ni_measurementlink_service.measurement.info import MeasurementInfo, ServiceInfo from tests.utilities.fake_rpc_error import FakeRpcError -if sys.platform == "win32": +if sys.platform.startswith('win'): import win32file _PROVIDED_MEASUREMENT_SERVICES = [ From 4329a84fa96fd2cc50ceaa765fa39649df66a7d0 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Mon, 25 Sep 2023 19:54:55 +0530 Subject: [PATCH 11/14] Fix lint error --- tests/unit/test_discovery_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index 7b8f0291c..3c7fe1940 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -34,7 +34,7 @@ from ni_measurementlink_service.measurement.info import MeasurementInfo, ServiceInfo from tests.utilities.fake_rpc_error import FakeRpcError -if sys.platform.startswith('win'): +if sys.platform.startswith("win"): import win32file _PROVIDED_MEASUREMENT_SERVICES = [ From 0d33947078e431a2284c3a09cc19e9222114f627 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Mon, 25 Sep 2023 20:20:31 +0530 Subject: [PATCH 12/14] Fix test failure --- tests/unit/test_discovery_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index 3c7fe1940..f85cd4be6 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -34,7 +34,7 @@ from ni_measurementlink_service.measurement.info import MeasurementInfo, ServiceInfo from tests.utilities.fake_rpc_error import FakeRpcError -if sys.platform.startswith("win"): +if sys.platform == "win32": import win32file _PROVIDED_MEASUREMENT_SERVICES = [ From 70702ebfbbb0ad9efe34e95e0d24488d89a6029d Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Mon, 25 Sep 2023 20:28:17 +0530 Subject: [PATCH 13/14] Fix test failure using if check --- tests/unit/test_discovery_client.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index f85cd4be6..6bca0ba6e 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -253,20 +253,22 @@ def test___get_discovery_service_address___key_file_not_exist___throws_timeouter assert exc_info.type is TimeoutError -@pytest.mark.skipif(sys.platform != "win32", reason="Windows-only test") @pytest.mark.parametrize( "windows_error_code", [2, 3] ) # ERROR_FILE_NOT_FOUND = 2, ERROR_PATH_NOT_FOUND = 3 def test___key_file_not_exist___open_key_file___raises_file_not_found_error( mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, windows_error_code -): - mocker.patch( - "win32file.CreateFile", - side_effect=win32file.error(windows_error_code, None, None), - ) +) -> None: + if sys.platform != "win32": + pytest.skip("Windows-only test") + else: + mocker.patch( + "win32file.CreateFile", + side_effect=win32file.error(windows_error_code, None, None), + ) - with pytest.raises(FileNotFoundError): - _open_key_file(str(temp_discovery_key_file_path)) + with pytest.raises(FileNotFoundError): + _open_key_file(str(temp_discovery_key_file_path)) def test___start_discovery_service___key_file_exist_after_poll___service_start_success( From 9cd18da2ff80c24c2b209ff543f52d6c7802e9d7 Mon Sep 17 00:00:00 2001 From: Sam Chrisvin <104556074+samchris007@users.noreply.github.com> Date: Mon, 25 Sep 2023 20:29:28 +0530 Subject: [PATCH 14/14] Resolve comments on naming --- tests/unit/test_discovery_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_discovery_client.py b/tests/unit/test_discovery_client.py index 6bca0ba6e..1405f1734 100644 --- a/tests/unit/test_discovery_client.py +++ b/tests/unit/test_discovery_client.py @@ -257,7 +257,7 @@ def test___get_discovery_service_address___key_file_not_exist___throws_timeouter "windows_error_code", [2, 3] ) # ERROR_FILE_NOT_FOUND = 2, ERROR_PATH_NOT_FOUND = 3 def test___key_file_not_exist___open_key_file___raises_file_not_found_error( - mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, windows_error_code + mocker: MockerFixture, temp_discovery_key_file_path: pathlib.Path, windows_error_code: int ) -> None: if sys.platform != "win32": pytest.skip("Windows-only test")