diff options
| author | Roy Sommer <roy@sommer.co.il> | 2019-12-22 23:09:38 +0200 |
|---|---|---|
| committer | Roy Sommer <roy@sommer.co.il> | 2019-12-22 23:09:38 +0200 |
| commit | ea898f7bf45ca0f8667bd76d6718c6fff9961bd3 (patch) | |
| tree | a0b1ed5885ad6e97c411335fcc2c3e6e093df8f4 /tests | |
| download | taskipy2-ea898f7bf45ca0f8667bd76d6718c6fff9961bd3.zip | |
initial commit
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/__init__.py | 0 | ||||
| -rw-r--r-- | tests/fixtures/project_with_malformed_pyproject/pyproject.toml | 8 | ||||
| -rw-r--r-- | tests/fixtures/project_with_pre_post_task_hooks/pyproject.toml | 16 | ||||
| -rw-r--r-- | tests/fixtures/project_with_pyproject_and_tasks/pyproject.toml | 9 | ||||
| -rw-r--r-- | tests/fixtures/project_with_pyproject_without_tasks_section/pyproject.toml | 3 | ||||
| -rw-r--r-- | tests/fixtures/project_without_pyproject/.keep | 0 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 131 | ||||
| -rw-r--r-- | tests/utils/__init__.py | 0 | ||||
| -rw-r--r-- | tests/utils/fixture.py | 15 |
9 files changed, 182 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/__init__.py diff --git a/tests/fixtures/project_with_malformed_pyproject/pyproject.toml b/tests/fixtures/project_with_malformed_pyproject/pyproject.toml new file mode 100644 index 0000000..28794b4 --- /dev/null +++ b/tests/fixtures/project_with_malformed_pyproject/pyproject.toml @@ -0,0 +1,8 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[[[[]]]] +asdfdasfdsaf +dsafv,dsa;lfj23[5432-tywersf +c======]asfdsafs = sadf2435 diff --git a/tests/fixtures/project_with_pre_post_task_hooks/pyproject.toml b/tests/fixtures/project_with_pre_post_task_hooks/pyproject.toml new file mode 100644 index 0000000..fb9a142 --- /dev/null +++ b/tests/fixtures/project_with_pre_post_task_hooks/pyproject.toml @@ -0,0 +1,16 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskify.tasks] +pre_hello = "echo 'pre_task'" +hello = "echo 'hello'" +post_hello = "echo 'post_task'" + +pre_hello_failed_pretask = "echo 'pre_task' && exit 1" +hello_failed_pretask = "echo 'hello'" +post_hello_failed_pretask = "echo 'post_task'" + +pre_hello_failed_posttask = "echo 'pre_task'" +hello_failed_posttask = "echo 'hello'" +post_hello_failed_posttask = "echo 'post_task' && exit 1" diff --git a/tests/fixtures/project_with_pyproject_and_tasks/pyproject.toml b/tests/fixtures/project_with_pyproject_and_tasks/pyproject.toml new file mode 100644 index 0000000..289fcf4 --- /dev/null +++ b/tests/fixtures/project_with_pyproject_and_tasks/pyproject.toml @@ -0,0 +1,9 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskify.tasks] +create_hello_txt = "echo 'hello, world' > hello.txt" +exit_17 = "exit 17" +print_hello_stdout = "echo 'hello stdout'" +print_hello_stderr = ">&2 echo 'hello stderr'" diff --git a/tests/fixtures/project_with_pyproject_without_tasks_section/pyproject.toml b/tests/fixtures/project_with_pyproject_without_tasks_section/pyproject.toml new file mode 100644 index 0000000..214e16b --- /dev/null +++ b/tests/fixtures/project_with_pyproject_without_tasks_section/pyproject.toml @@ -0,0 +1,3 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" diff --git a/tests/fixtures/project_without_pyproject/.keep b/tests/fixtures/project_without_pyproject/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/project_without_pyproject/.keep diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py new file mode 100644 index 0000000..450c65a --- /dev/null +++ b/tests/test_taskipy.py @@ -0,0 +1,131 @@ +import os +import unittest +import subprocess +from os import path +from typing import List, Tuple +from tests.utils.fixture import FixtureTempDir + +class TaskipyTestCase(unittest.TestCase): + def setUp(self): + self._tmp_dirs: List[FixtureTempDir] = [] + + def tearDown(self): + for tmp_dir in self._tmp_dirs: + tmp_dir.clean() + + def run_task(self, task: str, cwd=os.curdir) -> Tuple[int, str, str]: + executable_path = path.abspath('taskipy/run.py') + + proc = subprocess.Popen([executable_path, task], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) + stdout, stderr = proc.communicate() + + return proc.returncode, str(stdout), str(stderr) + + def create_test_dir_from_fixture(self, fixture_name: str): + tmp_dir = FixtureTempDir(path.join('tests', 'fixtures', fixture_name)) + self._tmp_dirs.append(tmp_dir) + return tmp_dir.path + + # pylint: disable=invalid-name + def assertSubstr(self, substr: str, full_string: str): + self.assertTrue(substr in full_string, msg=f'Expected \n "{substr}"\nto be in\n "{full_string}"') + + # pylint: disable=invalid-name + def assertNotSubstr(self, substr: str, full_string: str): + self.assertFalse(substr in full_string, msg=f'Expected \n "{substr}"\nnot to be in\n "{full_string}"') + + # pylint: disable=invalid-name + def assertSubstrsInOrder(self, substrs: List[str], full_string: str): + self.assertGreater(len(substrs), 0, 'please provide at least one substr') + + for substr_a, substr_b in zip(substrs[:-1], substrs[1:]): + self.assertSubstr(substr_a, full_string) + self.assertSubstr(substr_b, full_string) + self.assertLess(full_string.find(substr_a), + full_string.find(substr_b), + msg=f'Expected \n "{substr_a}"\nto appear before\n "{substr_b}"\nin\n "{full_string}"') + +class RunTaskTestCase(TaskipyTestCase): + def test_running_task(self): + cwd = self.create_test_dir_from_fixture('project_with_pyproject_and_tasks') + self.run_task('create_hello_txt', cwd=cwd) + + with open(path.join(cwd, 'hello.txt'), 'r') as f: + hello_file_contents = f.readline().strip() + + self.assertEqual(hello_file_contents, 'hello, world') + + def test_exit_code_matches_task_exit_code(self): + cwd = self.create_test_dir_from_fixture('project_with_pyproject_and_tasks') + exit_code, _, _ = self.run_task('exit_17', cwd=cwd) + + self.assertEqual(exit_code, 17) + + def test_stdout_contains_task_stdout(self): + cwd = self.create_test_dir_from_fixture('project_with_pyproject_and_tasks') + _, stdout, _ = self.run_task('print_hello_stdout', cwd=cwd) + + self.assertSubstr('hello stdout', stdout) + + def test_stderr_contains_task_stderr(self): + cwd = self.create_test_dir_from_fixture('project_with_pyproject_and_tasks') + _, _, stderr = self.run_task('print_hello_stderr', cwd=cwd) + + self.assertSubstr('hello stderr', stderr) + +class TaskPrePostHooksTestCase(TaskipyTestCase): + def test_running_pre_task_hook(self): + cwd = self.create_test_dir_from_fixture('project_with_pre_post_task_hooks') + _, stdout, _ = self.run_task('hello', cwd=cwd) + + self.assertSubstrsInOrder(['pre_task', 'hello'], stdout) + + def test_running_post_task_hook(self): + cwd = self.create_test_dir_from_fixture('project_with_pre_post_task_hooks') + _, stdout, _ = self.run_task('hello', cwd=cwd) + + self.assertSubstrsInOrder(['hello', 'post_task'], stdout) + + def test_exiting_after_pre_task_hook_if_exit_code_not_0(self): + cwd = self.create_test_dir_from_fixture('project_with_pre_post_task_hooks') + exit_code, stdout, _ = self.run_task('hello_failed_pretask', cwd=cwd) + + self.assertSubstr('pre_task', stdout) + self.assertNotSubstr('hello', stdout) + self.assertEqual(exit_code, 1) + + def test_exiting_with_post_task_hook_exit_code_if_not_0(self): + cwd = self.create_test_dir_from_fixture('project_with_pre_post_task_hooks') + exit_code, stdout, _ = self.run_task('hello_failed_posttask', cwd=cwd) + + self.assertSubstr('post_task', stdout) + self.assertEqual(exit_code, 1) + +class TaskRunFailTestCase(TaskipyTestCase): + def test_exiting_with_code_127_and_printing_if_task_not_found(self): + cwd = self.create_test_dir_from_fixture('project_with_pyproject_and_tasks') + exit_code, stdout, _ = self.run_task('task_that_does_not_exist', cwd=cwd) + + self.assertSubstr('could not find task "task_that_does_not_exist"', stdout) + self.assertEqual(exit_code, 127) + + def test_exiting_with_code_127_and_printing_if_no_tasks_section(self): + cwd = self.create_test_dir_from_fixture('project_with_pyproject_without_tasks_section') + exit_code, stdout, _ = self.run_task('task_that_does_not_exist', cwd=cwd) + + self.assertSubstr('no tasks found. add a [tool.taskify.tasks] section to your pyproject.toml', stdout) + self.assertEqual(exit_code, 127) + + def test_exiting_with_code_1_and_printing_if_no_pyproject_toml_file_found(self): + cwd = self.create_test_dir_from_fixture('project_without_pyproject') + exit_code, stdout, _ = self.run_task('some_task', cwd=cwd) + + self.assertSubstr('no pyproject.toml file found in this directory', stdout) + self.assertEqual(exit_code, 1) + + def test_exiting_with_code_1_and_printing_if_pyproject_toml_file_is_malformed(self): + cwd = self.create_test_dir_from_fixture('project_with_malformed_pyproject') + exit_code, stdout, _ = self.run_task('some_task', cwd=cwd) + + self.assertSubstr('pyproject.toml file is malformed and could not be read', stdout) + self.assertEqual(exit_code, 1) diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/utils/__init__.py diff --git a/tests/utils/fixture.py b/tests/utils/fixture.py new file mode 100644 index 0000000..7bc7df3 --- /dev/null +++ b/tests/utils/fixture.py @@ -0,0 +1,15 @@ +import tempfile +import shutil + +class FixtureTempDir: + def __init__(self, fixture_original_dir: str): + self._tmpdir: str = tempfile.mktemp() + shutil.copytree(fixture_original_dir, self._tmpdir) + + @property + def path(self): + return self._tmpdir + + def clean(self): + shutil.rmtree(self._tmpdir, ignore_errors=True) +
\ No newline at end of file |