From c5ddad39dfe229db2447b7946bfe7e8ebf1c82e7 Mon Sep 17 00:00:00 2001 From: Matthew J Morrison Date: Wed, 18 May 2022 10:22:14 -0500 Subject: [PATCH] Feature: Add ability to run tests via docker and Dispatch --- autoload/vim_python_test_runner.py | 34 +++++++++++++- autoload/vim_python_test_runner.vim | 8 +++- doc/vim-python-test-runner.txt | 10 ++++- plugin/vim_python_test_runner.vim | 4 ++ tests/test_run_docker_in_vim_tests.py | 65 +++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 tests/test_run_docker_in_vim_tests.py diff --git a/autoload/vim_python_test_runner.py b/autoload/vim_python_test_runner.py index 70bc17e..38d1660 100644 --- a/autoload/vim_python_test_runner.py +++ b/autoload/vim_python_test_runner.py @@ -23,7 +23,7 @@ def get_command_to_run_the_current_app(current_dir): flags = get_flags(current_dir) command = "{0}{1}test {2}{3}".format(path_to_manage, env_name, flags, app_name) write_test_command_to_cache_file(command) - return (command) + return command def get_command_to_run_the_current_file(current_dir): @@ -111,6 +111,38 @@ def get_command_to_run_current_base_method_with_pytests(path_to_current_file, cu return command +def get_command_to_run_python_app_with_docker(current_dir): + app_name = get_app_name(current_dir) + command = "Dispatch docker compose run test {}".format(app_name) + write_test_command_to_cache_file(command) + return command + + +def get_command_to_run_python_file_with_docker(current_dir): + app_command = get_command_to_run_python_app_with_docker(current_dir) + path_to_tests = get_dot_notation_path_to_test(current_dir) + file_name = get_file_name(current_dir) + command = "{}.{}.{}".format(app_command, path_to_tests, file_name) + write_test_command_to_cache_file(command) + return command + + +def get_command_to_run_python_class_with_docker(current_dir, current_line, current_buffer): + file_command = get_command_to_run_python_file_with_docker(current_dir) + current_class = get_current_method_and_class(current_line, current_buffer)[0] + command = "{}:{}".format(file_command, current_class) + write_test_command_to_cache_file(command) + return command + + +def get_command_to_run_python_method_with_docker(current_dir, current_line, current_buffer): + class_command = get_command_to_run_python_class_with_docker(current_dir, current_line, current_buffer) + current_method = get_current_method_and_class(current_line, current_buffer)[1] + command = "{}.{}".format(class_command, current_method) + write_test_command_to_cache_file(command) + return command + + def get_command_to_rerun_last_tests(): with open("/tmp/vim_python_test_runner_cache", "r") as f: return f.read() diff --git a/autoload/vim_python_test_runner.vim b/autoload/vim_python_test_runner.vim index 026d776..2d7c8cc 100644 --- a/autoload/vim_python_test_runner.vim +++ b/autoload/vim_python_test_runner.vim @@ -32,12 +32,16 @@ def get_proper_command(desired_command, current_directory): "pytest_class": lambda: get_command_to_run_current_class_with_pytests(vim.current.buffer.name, current_line_index, vim.current.buffer), "pytest_method": lambda: get_command_to_run_current_method_with_pytests(vim.current.buffer.name, current_line_index, vim.current.buffer), "pytest_base_method": lambda: get_command_to_run_current_base_method_with_pytests(vim.current.buffer.name, current_line_index, vim.current.buffer), - "rerun": lambda: get_command_to_rerun_last_tests() + "rerun": lambda: get_command_to_rerun_last_tests(), + "docker_python_app": lambda: get_command_to_run_python_app_with_docker(current_directory), + "docker_python_file": lambda: get_command_to_run_python_file_with_docker(current_directory), + "docker_python_class": lambda: get_command_to_run_python_class_with_docker(current_directory, current_line_index, vim.current.buffer), + "docker_python_method": lambda: get_command_to_run_python_method_with_docker(current_directory, current_line_index, vim.current.buffer), } return FUNCTIONS[desired_command]() def run_desired_command_for_os(command_to_run): - if "nose" in vim.eval("a:command_to_run") or "nose" in command_to_run: + if "nose" in vim.eval("a:command_to_run") or "nose" in command_to_run or "docker" in command_to_run: # Run nosetests for Python. vim.command("{0} 2>&1 | tee /tmp/test_results.txt".format(command_to_run)) elif "pytest" in vim.eval("a:command_to_run") or "pytest" in command_to_run: diff --git a/doc/vim-python-test-runner.txt b/doc/vim-python-test-runner.txt index 3f4ce51..e877c2f 100644 --- a/doc/vim-python-test-runner.txt +++ b/doc/vim-python-test-runner.txt @@ -43,7 +43,7 @@ pip installed in order for the plugin to function properly. =============================================================================== 3. Usage *test-runner-usage* -The plugin provides nine commands: +The plugin provides thirteen commands: - `DjangoTestApp`: Run all tests for the current app - `DjangoTestFile`: Run all tests in the current file @@ -53,6 +53,10 @@ The plugin provides nine commands: - `NosetestClass`: Run all tests in the current class - `NosetestMethod`: Run the current test method (inside of a class) - `NosetestBaseMethod`: Run the current test method (outside of a class) +- `DockerPythonApp`: Run the current app in docker +- `DockerPythonFile`: Run the current file in docker +- `DockerPythonClass`: Run the current class in docker +- `DockerPythonMethod`: Run the current method in docker - `RerunLastTests`: Rerun the last tests All arguments can be tab-completed. Ensure that your cursor is within a @@ -74,6 +78,10 @@ your vimrc: nnoremaptc :PytestClass nnoremaptm :PytestMethod nnoremaptb :PytestBaseMethod + nnoremapka :DockerPythonApp + nnoremapkf :DockerPythonFile + nnoremapkc :DockerPythonClass + nnoremapkm :DockerPythonMethod nnoremaprr :RerunLastTests Your tests results will be available in the quickfix window after they finish diff --git a/plugin/vim_python_test_runner.vim b/plugin/vim_python_test_runner.vim index 5ff1fb9..66b5c6e 100644 --- a/plugin/vim_python_test_runner.vim +++ b/plugin/vim_python_test_runner.vim @@ -11,3 +11,7 @@ command! PytestClass call vim_python_test_runner#RunDesiredTests("pytest_class") command! PytestMethod call vim_python_test_runner#RunDesiredTests("pytest_method") command! PytestBaseMethod call vim_python_test_runner#RunDesiredTests("pytest_base_method") command! RerunLastTests call vim_python_test_runner#RunDesiredTests("rerun") +command! DockerPythonApp call vim_python_test_runner#RunDesiredTests("docker_python_app") +command! DockerPythonFile call vim_python_test_runner#RunDesiredTests("docker_python_file") +command! DockerPythonClass call vim_python_test_runner#RunDesiredTests("docker_python_class") +command! DockerPythonMethod call vim_python_test_runner#RunDesiredTests("docker_python_method") diff --git a/tests/test_run_docker_in_vim_tests.py b/tests/test_run_docker_in_vim_tests.py new file mode 100644 index 0000000..627f82c --- /dev/null +++ b/tests/test_run_docker_in_vim_tests.py @@ -0,0 +1,65 @@ +import os +import glob +import shutil +import unittest +from inspect import getfile, currentframe + +import autoload.vim_python_test_runner as sut + + +class RunDockerInVimTests(unittest.TestCase): + + def setUp(self): + dirs_to_make = [ + "/tmp/project_app_only/example_app1/tests/", + ] + + contents_to_write = [ + ("/tmp/project_app_only/.vim-django", '{"app_name": "example_app1"}'), + ("/tmp/project_app_only/manage.py", "#Place holder"), + ] + + for directory in dirs_to_make: + os.makedirs(directory) + + for needed_file in contents_to_write: + with open(needed_file[0], "w") as f: + f.write(needed_file[1]) + + def tearDown(self): + for a_dir in glob.glob("/tmp/*project_*"): + shutil.rmtree(a_dir) + + def test_create_command_to_run_current_app_with_docker(self): + current_dir = '/tmp/project_app_only/example_app1/tests/test_file.py' + expected_return_value = "Dispatch docker compose run test example_app1" + command_returned = sut.get_command_to_run_python_app_with_docker(current_dir) + self.assertEqual(command_returned, expected_return_value) + + def test_create_command_to_run_current_file_with_docker(self): + current_dir = '/tmp/project_app_only/example_app1/tests/test_file.py' + expected_return_value = "Dispatch docker compose run test example_app1.tests.test_file" + command_returned = sut.get_command_to_run_python_file_with_docker(current_dir) + self.assertEqual(command_returned, expected_return_value) + + def test_create_command_to_run_current_class_with_docker(self): + current_dir = '/tmp/project_app_only/example_app1/tests/test_file.py' + current_line = 17 + current_buffer = self.build_buffer_helper() + expected_return_value = "Dispatch docker compose run test example_app1.tests.test_file:Example1" + self.assertEqual(expected_return_value, sut.get_command_to_run_python_class_with_docker(current_dir, current_line, current_buffer)) + + def test_create_command_to_run_current_method(self): + current_dir = '/tmp/project_app_only/example_app1/tests/test_file.py' + current_line = 17 + current_buffer = self.build_buffer_helper() + expected_return_value = "Dispatch docker compose run test example_app1.tests.test_file:Example1.dummy2" + self.assertEqual(expected_return_value, sut.get_command_to_run_python_method_with_docker(current_dir, current_line, current_buffer)) + + def build_buffer_helper(self): + current_dir = os.path.dirname(os.path.abspath(getfile(currentframe()))) + with open("{}/dummy_test_file.py".format(current_dir), "r") as f: + current_buffer = [] + for line in f.readlines(): + current_buffer.append(line) + return current_buffer