diff options
| author | Tsunenobu Kai <kai2nenobu@gmail.com> | 2020-12-26 00:51:33 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-25 17:51:33 +0200 |
| commit | 5aaeb1298987265e312cd4318bea82cade39f085 (patch) | |
| tree | fdf7e85d329a5321e071067bd76a767bf2f93f5a | |
| parent | Release version 1.5.1 (diff) | |
| download | taskipy2-5aaeb1298987265e312cd4318bea82cade39f085.zip | |
Find a parent pyproject.toml from a child directory (#28)
Enable running tasks defined in parent pyproject.toml from a child directory.
| -rwxr-xr-x | taskipy/cli.py | 2 | ||||
| -rw-r--r-- | taskipy/exceptions.py | 2 | ||||
| -rw-r--r-- | taskipy/pyproject.py | 17 | ||||
| -rw-r--r-- | taskipy/task_runner.py | 9 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_from_child/child_with_pyproject/pyproject.toml | 6 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_from_child/child_without_pyproject/.gitkeep | 0 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_from_child/pyproject.toml | 6 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 17 |
8 files changed, 49 insertions, 10 deletions
diff --git a/taskipy/cli.py b/taskipy/cli.py index f8e49ee..97d8174 100755 --- a/taskipy/cli.py +++ b/taskipy/cli.py @@ -17,7 +17,7 @@ def main(): parser.add_argument('args', nargs=argparse.REMAINDER, help='arguments to pass to the task') args = parser.parse_args() try: - runner = TaskRunner(Path.cwd() / 'pyproject.toml') + runner = TaskRunner(Path.cwd()) if args.list: runner.list() diff --git a/taskipy/exceptions.py b/taskipy/exceptions.py index 8f03ed3..b171231 100644 --- a/taskipy/exceptions.py +++ b/taskipy/exceptions.py @@ -12,7 +12,7 @@ class InvalidRunnerTypeError(TaskipyError): class MissingPyProjectFileError(TaskipyError): def __str__(self): - return 'no pyproject.toml file found in this directory' + return 'no pyproject.toml file found in this directory or parent directories' class MalformedPyProjectError(TaskipyError): diff --git a/taskipy/pyproject.py b/taskipy/pyproject.py index 4b55834..4bb599b 100644 --- a/taskipy/pyproject.py +++ b/taskipy/pyproject.py @@ -12,8 +12,9 @@ from taskipy.exceptions import ( class PyProject: - def __init__(self, file_path: Union[str, Path]): - self.__items = PyProject.__load_toml_file(file_path) + def __init__(self, base_dir: Path): + pyproject_path = self.__find_pyproject_path(base_dir) + self.__items = PyProject.__load_toml_file(pyproject_path) @property def tasks(self) -> dict: @@ -40,3 +41,15 @@ class PyProject: raise MissingPyProjectFileError() except toml.TomlDecodeError: raise MalformedPyProjectError() + + @staticmethod + def __find_pyproject_path(base_dir: Path) -> Path: + def candidate_dirs(base: Path): + yield base + for parent in base.parents: + yield parent + for candidate_dir in candidate_dirs(base_dir): + pyproject = candidate_dir / 'pyproject.toml' + if pyproject.exists(): + return pyproject + raise MissingPyProjectFileError() diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py index 5c16da1..444adbd 100644 --- a/taskipy/task_runner.py +++ b/taskipy/task_runner.py @@ -11,9 +11,10 @@ from taskipy.task import Task class TaskRunner: - def __init__(self, pyproject_path: Union[str, Path]): - self.__pyproject_path = pyproject_path - self.project = PyProject(pyproject_path) + def __init__(self, cwd: Union[str, Path]): + working_dir = cwd if isinstance(cwd, Path) else Path(cwd) + self.__working_dir = working_dir + self.project = PyProject(working_dir) def list(self): """lists tasks to stdout""" @@ -28,7 +29,7 @@ class TaskRunner: if task.runner is not None: command = f'{task.runner} {command}' process = subprocess.Popen( - command, shell=True, cwd=Path(self.__pyproject_path).parent + command, shell=True, cwd=self.__working_dir ) def send_signal_to_task_process(signum: int, frame) -> None: # pylint: disable=unused-argument diff --git a/tests/fixtures/project_with_tasks_from_child/child_with_pyproject/pyproject.toml b/tests/fixtures/project_with_tasks_from_child/child_with_pyproject/pyproject.toml new file mode 100644 index 0000000..0f5b4a1 --- /dev/null +++ b/tests/fixtures/project_with_tasks_from_child/child_with_pyproject/pyproject.toml @@ -0,0 +1,6 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.tasks] +hello = 'echo "hello from child"' diff --git a/tests/fixtures/project_with_tasks_from_child/child_without_pyproject/.gitkeep b/tests/fixtures/project_with_tasks_from_child/child_without_pyproject/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/project_with_tasks_from_child/child_without_pyproject/.gitkeep diff --git a/tests/fixtures/project_with_tasks_from_child/pyproject.toml b/tests/fixtures/project_with_tasks_from_child/pyproject.toml new file mode 100644 index 0000000..1f29837 --- /dev/null +++ b/tests/fixtures/project_with_tasks_from_child/pyproject.toml @@ -0,0 +1,6 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.tasks] +print_current_dir_name = 'python -c "from pathlib import Path; print(Path.cwd().name)"' diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index bc227d4..d70b88d 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -94,7 +94,6 @@ 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') @@ -234,7 +233,7 @@ class TaskRunFailTestCase(TaskipyTestCase): 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.assertSubstr('no pyproject.toml file found in this directory or parent directories', stdout) self.assertEqual(exit_code, 1) def test_exiting_with_code_1_and_printing_if_pyproject_toml_file_is_malformed(self): @@ -340,3 +339,17 @@ class CustomRunnerTestCase(TaskipyTestCase): self.assertSubstr('invalid value: runner is not a string. please check [tool.taskipy.settings.runner]', stdout) self.assertEqual(exit_code, 1) + + +class TaskFromChildTestCase(TaskipyTestCase): + def test_running_parent_pyproject_task_from_child_directory(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_from_child') + _, stdout, _ = self.run_task('print_current_dir_name', cwd=path.join(cwd, 'child_without_pyproject')) + + self.assertSubstr('child_without_pyproject', stdout) + + def test_find_nearest_pyproject_from_child_directory(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_from_child') + _, stdout, _ = self.run_task('hello', cwd=path.join(cwd, 'child_with_pyproject')) + + self.assertSubstr('hello from child', stdout) |