Skip to content

Commit

Permalink
fixed open file issue in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek's Macbook Pro authored and Abhishek's Macbook Pro committed May 11, 2024
1 parent 28f3cad commit 76e06dc
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 210 deletions.
2 changes: 1 addition & 1 deletion src/commands/annotate_and_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import sublime
from unittesting import DeferrableTestCase
from PyRock.src.constants import PyRockConstants


class PyRockTestBase(DeferrableTestCase):
Expand All @@ -10,10 +12,33 @@ 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:
self.view.set_scratch(True)
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

197 changes: 95 additions & 102 deletions tests/src/commands/test_annotate_and_test_runner.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,117 +10,112 @@ 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/"
pytest tests/fixtures/test_fixture.py::MyTestCase
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")
Expand Down
Loading

0 comments on commit 76e06dc

Please sign in to comment.