Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion autoload/vim_python_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 6 additions & 2 deletions autoload/vim_python_test_runner.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion doc/vim-python-test-runner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -74,6 +78,10 @@ your vimrc:
nnoremap<Leader>tc :PytestClass<CR>
nnoremap<Leader>tm :PytestMethod<CR>
nnoremap<Leader>tb :PytestBaseMethod<CR>
nnoremap<Leader>ka :DockerPythonApp<CR>
nnoremap<Leader>kf :DockerPythonFile<CR>
nnoremap<Leader>kc :DockerPythonClass<CR>
nnoremap<Leader>km :DockerPythonMethod<CR>
nnoremap<Leader>rr :RerunLastTests<CR>

Your tests results will be available in the quickfix window after they finish
Expand Down
4 changes: 4 additions & 0 deletions plugin/vim_python_test_runner.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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")
65 changes: 65 additions & 0 deletions tests/test_run_docker_in_vim_tests.py
Original file line number Diff line number Diff line change
@@ -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