From 76e06dc95f810816278b620b2a7d49837154851c Mon Sep 17 00:00:00 2001 From: Abhishek's Macbook Pro Date: Sun, 12 May 2024 03:48:28 +0530 Subject: [PATCH] fixed open file issue in test --- src/commands/annotate_and_test_runner.py | 2 +- tests/base.py | 25 +++ .../commands/test_annotate_and_test_runner.py | 197 ++++++++-------- tests/src/commands/test_copy_test_path.py | 210 +++++++++--------- 4 files changed, 224 insertions(+), 210 deletions(-) diff --git a/src/commands/annotate_and_test_runner.py b/src/commands/annotate_and_test_runner.py index 74544ad..116fba5 100644 --- a/src/commands/annotate_and_test_runner.py +++ b/src/commands/annotate_and_test_runner.py @@ -312,7 +312,7 @@ def run(self, view: View): logger.info("Test config not enabled") return - if not is_test_file(self.view.file_name()): + if self.view.file_name() is None or (self.view.file_name() and not is_test_file(self.view.file_name())): logger.info("Not a test file, returning") return diff --git a/tests/base.py b/tests/base.py index f111f32..9e16f36 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,5 +1,7 @@ +import os import sublime from unittesting import DeferrableTestCase +from PyRock.src.constants import PyRockConstants class PyRockTestBase(DeferrableTestCase): @@ -10,6 +12,7 @@ def setUp(self): self.window = self.view.window() self.sublime_settings = sublime.load_settings("Preferences.sublime-settings") self.sublime_settings.set("close_windows_when_empty", False) + self.test_file_view = None def tearDown(self): if self.view: @@ -17,3 +20,25 @@ def tearDown(self): self.view.window().focus_view(self.view) self.view.window().run_command("close_file") + def _open_test_fixture_file(self): + file_path = os.path.join( + PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, 'test_fixture.py' + ) + + for win in sublime.windows(): + test_file_view = win.find_open_file(file_path) + + if test_file_view is not None: + break + + if test_file_view is None: + test_file_view = sublime.active_window().open_file( + fname=file_path + ) + + # wait for view to open + while test_file_view.is_loading(): + pass + + self.test_file_view = test_file_view + diff --git a/tests/src/commands/test_annotate_and_test_runner.py b/tests/src/commands/test_annotate_and_test_runner.py index be4dfdf..db8ea29 100644 --- a/tests/src/commands/test_annotate_and_test_runner.py +++ b/tests/src/commands/test_annotate_and_test_runner.py @@ -1,7 +1,5 @@ -import os -from unittest.mock import patch - import sublime +from unittest.mock import patch from PyRock.src.constants import PyRockConstants from tests.base import PyRockTestBase @@ -12,67 +10,62 @@ class TestAnnotateAndTestRunnerCommand(PyRockTestBase): def setUp(self): self.maxDiff = None self.test_runner_cmd = AnnotateAndTestRunnerCommand(test=True) + self.test_file_view = None def tearDown(self): pass - def _open_test_fixture_file(self): - test_file_view = sublime.active_window().open_file( - fname=os.path.join( - PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, 'test_fixture.py' - ) - ) - - # wait for view to open - # while test_file_view.is_loading(): - # pass - - return test_file_view - - - @patch("PyRock.src.settings.SettingsTestConfigField._get_value") - def test_run(self, mocked_get_test_config): - mocked_get_test_config.return_value = { - "enabled": True, - "test_framework": "pytest", - "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, - "test_runner_command": ["pytest"] - } - test_file_view = self._open_test_fixture_file() - - region_key = f"pyrock-gutter-icon-{test_file_view.id()}" - - annotated_regions = test_file_view.get_regions(region_key) - - self.assertEqual(len(annotated_regions), 3) - - @patch("os.path.exists") - @patch("PyRock.src.commands.annotate_and_test_runner.AnnotateAndTestRunnerCommand._run_test_command") - @patch("sublime.load_settings") - def test_click_on_annotated_html( - self, - mocked_load_settings, - mocked_run_test_command, - mocked_os_path_exists, - ): - mocked_load_settings.return_value = { - "python_venv_path": "/Users/abhishek/venv/bin/activate", - "log_level": "debug", - "test_config": { - "enabled": True, - "test_framework": "pytest", - "working_directory": "/Users/abhishek/", - "test_runner_command": ["pytest"], - } - } - mocked_os_path_exists.return_value = True - - test_file_view = self._open_test_fixture_file() - - self.test_runner_cmd.run(test_file_view) - self.test_runner_cmd._execute_test("0") - - test_command = """ + def test_run(self): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + # Wait 4 second to make sure test fixture file has opened + yield 4000 + except TimeoutError as e: + pass + test_file_view = self.test_file_view + + # Mocking with local context due to usage of yield + # when yielding patch decorator doesn't work + with patch("PyRock.src.settings.SettingsTestConfigField._get_value") as mocked_get_test_config: + mocked_get_test_config.return_value = { + "enabled": True, + "test_framework": "pytest", + "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, + "test_runner_command": ["pytest"] + } + + region_key = f"pyrock-gutter-icon-{test_file_view.id()}" + + annotated_regions = test_file_view.get_regions(region_key) + + self.assertEqual(len(annotated_regions), 3) + + def test_click_on_annotated_html(self): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + yield 4000 + except TimeoutError as e: + pass + test_file_view = self.test_file_view + + with patch("os.path.exists") as mocked_os_path_exists, patch("sublime.load_settings") as mocked_load_settings, patch("PyRock.src.commands.annotate_and_test_runner.AnnotateAndTestRunnerCommand._run_test_command") as mocked_run_test_command: + + mocked_load_settings.return_value = { + "python_venv_path": "/Users/abhishek/venv/bin/activate", + "log_level": "debug", + "test_config": { + "enabled": True, + "test_framework": "pytest", + "working_directory": "/Users/abhishek/", + "test_runner_command": ["pytest"], + } + } + mocked_os_path_exists.return_value = True + + self.test_runner_cmd.run(test_file_view) + self.test_runner_cmd._execute_test("0") + + test_command = """ set -e . "/Users/abhishek/venv/bin/activate" cd "/Users/abhishek/" @@ -80,49 +73,49 @@ def test_click_on_annotated_html( deactivate """ - mocked_run_test_command.assert_called_once_with( - test_command - ) - - @patch("PyRock.src.commands.output_panel.OutputPanel.show") - @patch("PyRock.src.settings.SettingsTestConfigField._get_value") - def test_run_test_command( - self, - mocked_get_test_config, - mocked_panel_show, - ): - mocked_get_test_config.return_value = { - "enabled": True, - "test_framework": "pytest", - "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, - "test_runner_command": ["pytest"] - } - - test_file_view = self._open_test_fixture_file() - - self.test_runner_cmd.run(test_file_view) - self.test_runner_cmd._execute_test("0") - - test_command = """ - set -e - . "/Users/abhishek/venv/bin/activate" - cd "/Users/abhishek/Library/Application Support/Sublime Text/Packages/PyRock/tests/fixtures" - pytest tests/fixtures/test_fixture.py::MyTestCase - deactivate - """ - - script_success, message = self.test_runner_cmd._run_test_command(test_command) - - self.assertFalse(script_success) - self.assertIsNotNone(message) + mocked_run_test_command.assert_called_once_with( + test_command + ) - output_panel_view = test_file_view.window().find_output_panel( - name=PyRockConstants.PACKAGE_TEST_RUNNER_OUTPUT_PANEL - ) - output_text = output_panel_view.substr( - sublime.Region(0, output_panel_view.size()) - ) - self.assertTrue(test_command in output_text) + def test_run_test_command(self): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + yield 4000 + except TimeoutError as e: + pass + test_file_view = self.test_file_view + + with patch("PyRock.src.commands.output_panel.OutputPanel.show") as mocked_panel_show, patch("PyRock.src.settings.SettingsTestConfigField._get_value") as mocked_get_test_config: + mocked_get_test_config.return_value = { + "enabled": True, + "test_framework": "pytest", + "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, + "test_runner_command": ["pytest"] + } + + self.test_runner_cmd.run(test_file_view) + self.test_runner_cmd._execute_test("0") + + test_command = """ + set -e + . "/Users/abhishek/venv/bin/activate" + cd "/Users/abhishek/Library/Application Support/Sublime Text/Packages/PyRock/tests/fixtures" + pytest tests/fixtures/test_fixture.py::MyTestCase + deactivate + """ + + script_success, message = self.test_runner_cmd._run_test_command(test_command) + + self.assertFalse(script_success) + self.assertIsNotNone(message) + + output_panel_view = test_file_view.window().find_output_panel( + name=PyRockConstants.PACKAGE_TEST_RUNNER_OUTPUT_PANEL + ) + output_text = output_panel_view.substr( + sublime.Region(0, output_panel_view.size()) + ) + self.assertTrue(test_command in output_text) @patch("os.path.exists") @patch("sublime.platform") diff --git a/tests/src/commands/test_copy_test_path.py b/tests/src/commands/test_copy_test_path.py index 438fda2..4c5919b 100644 --- a/tests/src/commands/test_copy_test_path.py +++ b/tests/src/commands/test_copy_test_path.py @@ -1,25 +1,29 @@ -import os +import sublime from unittest.mock import patch -import sublime from PyRock.src.constants import PyRockConstants from tests.base import PyRockTestBase class TestCopyTestPathCommand(PyRockTestBase): + def setUp(self): + self.maxDiff = None + self.test_file_view = None + + def tearDown(self): + pass + def test_copy_django_class_test_path( self, ): - test_file_view = sublime.active_window().open_file( - fname=os.path.join( - PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, 'test_fixture.py' - ) - ) - - # wait for view to open - while test_file_view.is_loading(): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + # Wait 4 second to make sure test fixture file has opened + yield 4000 + except TimeoutError as e: pass + test_file_view = self.test_file_view test_file_view.sel().clear() test_file_view.sel().add(sublime.Region(53, 63)) @@ -38,16 +42,13 @@ def test_copy_django_class_test_path( def test_copy_django_class_method_test_path( self, ): - - test_file_view = sublime.active_window().open_file( - fname=os.path.join( - PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, 'test_fixture.py' - ) - ) - - # wait for view to open - while test_file_view.is_loading(): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + # Wait 4 second to make sure test fixture file has opened + yield 4000 + except TimeoutError as e: pass + test_file_view = self.test_file_view test_file_view.sel().clear() test_file_view.sel().add(sublime.Region(83, 105)) @@ -67,15 +68,13 @@ def test_copy_django_class_method_test_path( def test_copy_django_individual_method_test_path( self, ): - test_file_view = sublime.active_window().open_file( - fname=os.path.join( - PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, 'test_fixture.py' - ) - ) - - # wait for view to open - while test_file_view.is_loading(): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + # Wait 4 second to make sure test fixture file has opened + yield 4000 + except TimeoutError as e: pass + test_file_view = self.test_file_view test_file_view.sel().clear() test_file_view.sel().add(sublime.Region(272, 286)) @@ -91,112 +90,109 @@ def test_copy_django_individual_method_test_path( expected_import_statement, "tests.fixtures.test_fixture.test_iam_alone" ) - @patch("PyRock.src.settings.SettingsTestConfigField._get_value") + # @patch("PyRock.src.settings.SettingsTestConfigField._get_value") def test_copy_pytest_class_test_path( self, - mocked_get_test_config, + # mocked_get_test_config, ): - test_file_view = sublime.active_window().open_file( - fname=os.path.join( - PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, 'test_fixture.py' - ) - ) - - # wait for view to open - while test_file_view.is_loading(): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + # Wait 4 second to make sure test fixture file has opened + yield 4000 + except TimeoutError as e: pass + test_file_view = self.test_file_view - mocked_get_test_config.return_value = { - "enabled": True, - "test_framework": "pytest", - "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, - "test_runner_command": ["pytest"] - } + with patch("PyRock.src.settings.SettingsTestConfigField._get_value") as mocked_get_test_config: + mocked_get_test_config.return_value = { + "enabled": True, + "test_framework": "pytest", + "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, + "test_runner_command": ["pytest"] + } - test_file_view.sel().clear() - test_file_view.sel().add(sublime.Region(53, 63)) + test_file_view.sel().clear() + test_file_view.sel().add(sublime.Region(53, 63)) - selected_text = test_file_view.substr(test_file_view.sel()[0]) - self.assertEqual(selected_text, "MyTestCase") + selected_text = test_file_view.substr(test_file_view.sel()[0]) + self.assertEqual(selected_text, "MyTestCase") - test_file_view.run_command( - "py_rock", args={"action": "copy_test_path", "test": True}) + test_file_view.run_command( + "py_rock", args={"action": "copy_test_path", "test": True}) - expected_import_statement = sublime.get_clipboard() - self.assertEqual( - expected_import_statement, "tests/fixtures/test_fixture.py::MyTestCase" - ) + expected_import_statement = sublime.get_clipboard() + self.assertEqual( + expected_import_statement, "tests/fixtures/test_fixture.py::MyTestCase" + ) - @patch("PyRock.src.settings.SettingsTestConfigField._get_value") + # @patch("PyRock.src.settings.SettingsTestConfigField._get_value") def test_copy_pytest_class_method_test_path( self, - mocked_get_test_config, + # mocked_get_test_config, ): - test_file_view = sublime.active_window().open_file( - fname=os.path.join( - PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, 'test_fixture.py' - ) - ) - - # wait for view to open - while test_file_view.is_loading(): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + # Wait 4 second to make sure test fixture file has opened + yield 4000 + except TimeoutError as e: pass + test_file_view = self.test_file_view - mocked_get_test_config.return_value = { - "enabled": True, - "test_framework": "pytest", - "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, - "test_runner_command": ["pytest"] - } + with patch("PyRock.src.settings.SettingsTestConfigField._get_value") as mocked_get_test_config: + mocked_get_test_config.return_value = { + "enabled": True, + "test_framework": "pytest", + "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, + "test_runner_command": ["pytest"] + } - test_file_view.sel().clear() - test_file_view.sel().add(sublime.Region(83, 105)) + test_file_view.sel().clear() + test_file_view.sel().add(sublime.Region(83, 105)) - selected_text = test_file_view.substr(test_file_view.sel()[0]) - self.assertEqual(selected_text, "test_long_running_task") + selected_text = test_file_view.substr(test_file_view.sel()[0]) + self.assertEqual(selected_text, "test_long_running_task") - test_file_view.run_command( - "py_rock", args={"action": "copy_test_path", "test": True}) + test_file_view.run_command( + "py_rock", args={"action": "copy_test_path", "test": True}) - expected_import_statement = sublime.get_clipboard() - self.assertEqual( - expected_import_statement, - "tests/fixtures/test_fixture.py::MyTestCase::test_long_running_task" - ) + expected_import_statement = sublime.get_clipboard() + self.assertEqual( + expected_import_statement, + "tests/fixtures/test_fixture.py::MyTestCase::test_long_running_task" + ) - @patch("PyRock.src.settings.SettingsTestConfigField._get_value") + # @patch("PyRock.src.settings.SettingsTestConfigField._get_value") def test_copy_pytest_individual_method_test_path( self, - mocked_get_test_config, + # mocked_get_test_config, ): - test_file_view = sublime.active_window().open_file( - fname=os.path.join( - PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, 'test_fixture.py' - ) - ) - - # wait for view to open - while test_file_view.is_loading(): + sublime.set_timeout_async(self._open_test_fixture_file, 0) + try: + # Wait 4 second to make sure test fixture file has opened + yield 4000 + except TimeoutError as e: pass + test_file_view = self.test_file_view - mocked_get_test_config.return_value = { - "enabled": True, - "test_framework": "pytest", - "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, - "test_runner_command": ["pytest"] - } + with patch("PyRock.src.settings.SettingsTestConfigField._get_value") as mocked_get_test_config: + mocked_get_test_config.return_value = { + "enabled": True, + "test_framework": "pytest", + "working_directory": PyRockConstants.PACKAGE_TEST_FIXTURES_DIR, + "test_runner_command": ["pytest"] + } - test_file_view.sel().clear() - test_file_view.sel().add(sublime.Region(272, 286)) + test_file_view.sel().clear() + test_file_view.sel().add(sublime.Region(272, 286)) - selected_text = test_file_view.substr(test_file_view.sel()[0]) - self.assertEqual(selected_text, "test_iam_alone") + selected_text = test_file_view.substr(test_file_view.sel()[0]) + self.assertEqual(selected_text, "test_iam_alone") - test_file_view.run_command( - "py_rock", args={"action": "copy_test_path", "test": True}) + test_file_view.run_command( + "py_rock", args={"action": "copy_test_path", "test": True}) - expected_import_statement = sublime.get_clipboard() - self.assertEqual( - expected_import_statement, - "tests/fixtures/test_fixture.py::test_iam_alone" - ) + expected_import_statement = sublime.get_clipboard() + self.assertEqual( + expected_import_statement, + "tests/fixtures/test_fixture.py::test_iam_alone" + )