From 58a199d1313bf52cc74776fbde0bef245ba72329 Mon Sep 17 00:00:00 2001 From: Roy Sommer Date: Sat, 7 Mar 2020 16:52:12 +0200 Subject: Custom Runner (#11) --- .pylintrc | 4 ++- README.md | 3 +++ docs/ADVANCED_FEATURES.md | 58 +++++++++++++++++++++++++++++++++++++++++++ taskipy/run.py | 15 +++++++++-- tests/test_taskipy.py | 63 ++++++++++++++++++++++++++++++++++++++++++++--- tests/utils/fixture.py | 15 ----------- tests/utils/project.py | 42 +++++++++++++++++++++++++++++++ 7 files changed, 179 insertions(+), 21 deletions(-) create mode 100644 docs/ADVANCED_FEATURES.md delete mode 100644 tests/utils/fixture.py create mode 100644 tests/utils/project.py diff --git a/.pylintrc b/.pylintrc index 02659cb..68d2e58 100644 --- a/.pylintrc +++ b/.pylintrc @@ -3,7 +3,9 @@ disable= C0114, R0201, W0702, - C0411 + C0411, + R0903, + R0915 [FORMAT] max-line-length=1000 diff --git a/README.md b/README.md index c8c0ef0..05f84f5 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,9 @@ task TASK ``` Where `TASK` is the name of your task. +### Advanced Use Cases +If you have a more specific use case, you might not be the first to run into it! Head over to the [ADVANCED_FEATURES](./docs/ADVANCED_FEATURES.md) doc, and look it up. + ## Contributors ✨ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): diff --git a/docs/ADVANCED_FEATURES.md b/docs/ADVANCED_FEATURES.md new file mode 100644 index 0000000..c6ea68a --- /dev/null +++ b/docs/ADVANCED_FEATURES.md @@ -0,0 +1,58 @@ +# Advanced Use Cases +This is a list of quality of life features that might be useful to you if you have a specific use case that you want to easily solve. + +You can go over the use cases list and try to find the specific use case which you're trying to solve. If you cannot find your specific use case, feel free to open an issue or a PR! + +## Use Cases +For each of the use cases below, click on the arrow in order to get to the relevant feature. +1. I want to load environment variables before every task ([⏩](custom-runners)) +2. I want to run all tasks in a specific virtualenv ([⏩](custom-runners)) +3. I want to run all tasks in a specific shell \ ssh ([⏩](custom-runners)) + +## Features +### Custom Runners +#### Requirement +Under some circumstances, we would like to run all of our tasks in a consistent context; e.g., in a specific shell, a remote shell with `ssh`, some custom `virtualenv` wrapper, or even `dotenv`. + +Currently, we can configure this on a task to task basis, e.g.: +```toml +[tool.taskipy.tasks] +test = "dotenv run unittest ." +``` + +But if we want all of our tasks to use the same runner (such as `dotenv` in the above example) we would have to manually write all of them as such: +```toml +[tool.taskipy.settings] +runner = "dotenv run" + +[tool.taskipy.tasks] +test = "dotenv run unittest ." +lint = "dotenv run pylint taskipy" +release = "dotenv run release-it" +start = "dotenv run python ." +... +``` + +And so on. + +Such cases are where we need a global wrapper, so we wouldn't have to repeat the prefix with every command. + +#### Solution +The `tool.taskipy.settings.runner` option: when configured, adds the given runner command as a prefix to every task we run. + +Let's take a look at the following example, where we want to load environment variables before every task: +```toml +[tool.taskipy.settings] +runner = "dotenv run" + +[tool.taskipy.tasks] +test = "unittest ." +lint = "pylint taskipy" +``` + +Now, running `task test` will actually run: +```bash +dotenv run unittest . +``` + +Which means that we implicitly initialize the env before every task. diff --git a/taskipy/run.py b/taskipy/run.py index ab7824d..88c2ba9 100644 --- a/taskipy/run.py +++ b/taskipy/run.py @@ -6,8 +6,11 @@ from os import path from typing import List def run_task(task_name: str, args: List[str], cwd=os.curdir): - def run_commands_and_bail_on_first_fail(cmds: List[str]) -> int: + def run_commands_and_bail_on_first_fail(cmds: List[str], runner: str = None) -> int: for cmd in cmds: + if runner is not None: + cmd = f'{runner} {cmd}' + p = subprocess.Popen(cmd, shell=True, cwd=cwd) try: @@ -58,5 +61,13 @@ def run_task(task_name: str, args: List[str], cwd=os.curdir): except KeyError: pass - exit_code = run_commands_and_bail_on_first_fail(commands) + try: + runner = pyproject['tool']['taskipy']['settings']['runner'].strip() + except KeyError: + runner = None + except AttributeError: + print('invalid value: runner is not a string. please check [tool.taskipy.settings.runner]') + sys.exit(1) + + exit_code = run_commands_and_bail_on_first_fail(commands, runner=runner) sys.exit(exit_code) diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 87b62a2..1764504 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -8,11 +8,12 @@ import psutil # type: ignore import warnings from os import path from typing import List, Tuple -from tests.utils.fixture import FixtureTempDir +from tests.utils.project import TempProjectDir, GenerateProjectFromFixture, GenerateProjectWithPyProjectToml + class TaskipyTestCase(unittest.TestCase): def setUp(self): - self._tmp_dirs: List[FixtureTempDir] = [] + self._tmp_dirs: List[TempProjectDir] = [] def tearDown(self): for tmp_dir in self._tmp_dirs: @@ -31,7 +32,14 @@ class TaskipyTestCase(unittest.TestCase): return subprocess.Popen([executable_path, task] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) def create_test_dir_from_fixture(self, fixture_name: str): - tmp_dir = FixtureTempDir(path.join('tests', 'fixtures', fixture_name)) + project_generator = GenerateProjectFromFixture(path.join('tests', 'fixtures', fixture_name)) + tmp_dir = TempProjectDir(project_generator) + self._tmp_dirs.append(tmp_dir) + return tmp_dir.path + + def create_test_dir_with_py_project_toml(self, py_project_toml: str): + project_generator = GenerateProjectWithPyProjectToml(py_project_toml) + tmp_dir = TempProjectDir(project_generator) self._tmp_dirs.append(tmp_dir) return tmp_dir.path @@ -54,6 +62,7 @@ class TaskipyTestCase(unittest.TestCase): 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') @@ -82,6 +91,7 @@ class RunTaskTestCase(TaskipyTestCase): 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') @@ -110,6 +120,7 @@ class TaskPrePostHooksTestCase(TaskipyTestCase): self.assertSubstr('post_task', stdout) self.assertEqual(exit_code, 1) + class PassArgumentsTestCase(TaskipyTestCase): def test_running_task_with_positional_arguments(self): cwd = self.create_test_dir_from_fixture('project_with_tasks_that_accept_arguments') @@ -152,6 +163,7 @@ class PassArgumentsTestCase(TaskipyTestCase): self.assertNotSubstr(f'the number in posthook is {some_random_number}', stdout) self.assertEqual(exit_code, 0) + 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') @@ -181,6 +193,7 @@ class TaskRunFailTestCase(TaskipyTestCase): self.assertSubstr('pyproject.toml file is malformed and could not be read', stdout) self.assertEqual(exit_code, 1) + class InterruptingTaskTestCase(TaskipyTestCase): def setUp(self): super().setUp() @@ -218,3 +231,47 @@ class InterruptingTaskTestCase(TaskipyTestCase): exit_code = process.wait() self.assertEqual(exit_code, 130) + + +class CustomRunnerTestCase(TaskipyTestCase): + def test_running_command_with_custom_runner(self): + py_project_toml = ''' + [tool.taskipy.settings] + runner = "time" + + [tool.taskipy.tasks] + print_with_python = "python -c 'print(1337)'" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + _, _, stderr = self.run_task('print_with_python', cwd=cwd) + + time_cmd_output_format = 'user' + self.assertSubstr(time_cmd_output_format, stderr) + + def test_running_command_with_custom_runner_with_trailing_space(self): + py_project_toml = ''' + [tool.taskipy.settings] + runner = "time " + + [tool.taskipy.tasks] + print_with_python = "python -c 'print(1337)'" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + _, _, stderr = self.run_task('print_with_python', cwd=cwd) + + time_cmd_output_format = 'user' + self.assertSubstr(time_cmd_output_format, stderr) + + def test_running_command_with_custom_runner_fails_if_custom_runner_is_not_string(self): + py_project_toml = ''' + [tool.taskipy.settings] + runner = 55 + + [tool.taskipy.tasks] + print_with_python = "python -c 'print(1337)'" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('print_with_python', cwd=cwd) + + self.assertSubstr('invalid value: runner is not a string. please check [tool.taskipy.settings.runner]', stdout) + self.assertEqual(exit_code, 1) diff --git a/tests/utils/fixture.py b/tests/utils/fixture.py deleted file mode 100644 index 7bc7df3..0000000 --- a/tests/utils/fixture.py +++ /dev/null @@ -1,15 +0,0 @@ -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 diff --git a/tests/utils/project.py b/tests/utils/project.py new file mode 100644 index 0000000..d6c151b --- /dev/null +++ b/tests/utils/project.py @@ -0,0 +1,42 @@ +import tempfile +import shutil +import os +from os import path +from abc import abstractmethod + +class ProjectDirGenerator: + @abstractmethod + def generate_project(self, temp_dir: str): + pass + + +class GenerateProjectFromFixture(ProjectDirGenerator): + def __init__(self, fixture_dir: str): + self._fixture_dir: str = fixture_dir + + def generate_project(self, temp_dir: str): + shutil.copytree(self._fixture_dir, temp_dir) + + +class GenerateProjectWithPyProjectToml(ProjectDirGenerator): + def __init__(self, py_project_toml: str): + self._py_project_toml: str = py_project_toml + + def generate_project(self, temp_dir: str): + os.makedirs(temp_dir) + with open(path.join(temp_dir, 'pyproject.toml'), 'w') as f: + f.write(self._py_project_toml) + + +class TempProjectDir: + def __init__(self, project_dir_generator: ProjectDirGenerator): + self._tmpdir: str = tempfile.mktemp() + project_dir_generator.generate_project(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 -- cgit v1.2.3