diff options
18 files changed, 195 insertions, 11 deletions
@@ -25,3 +25,4 @@ MANIFEST .idea .python-version .DS_Store +.venv diff --git a/.vscode/settings.json b/.vscode/settings.json index 6e80a92..791f1ba 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,4 +4,4 @@ "python.linting.pylintEnabled": true, "python.linting.mypyEnabled": true, "python.formatting.provider": "none" -}
\ No newline at end of file +} @@ -17,6 +17,7 @@ - [String Formatting](#string-formatting) - [Always Use Variables](#always-use-variables) - [Recursive Variables](#recursive-variables) + - [Working directory](#working-directory) - [Using Taskipy Without Poetry](#using-taskipy-without-poetry) - [Installing With PIP](#installing-with-pip) - [Running Tasks](#running-tasks-1) @@ -282,6 +283,28 @@ echo = "echo {package_dir}" In this example, we could run `task echo` and we would then see `src/package`. +### Working directory + +By default, all tasks run from the directory where they are called. This makes possible to change folder and run flexible tasks depending on the current folder. + +However, some tasks may need to always run in the same working directory, regardless from where they are called. + +If you want tasks to always run relative to a specific path, you can use the `"cwd"` setting to define a current working directory of the task relative to the root of the project (where the `pyproject.toml` file is): + +```toml +[tool.taskipy.tasks] +echo = { cmd = "python -c \"import os; print(os.getcwd())\"" , cwd = "."} +``` + +In this example, running `task echo` will print the directory path of the file `pyproject.toml` regardless of the folder that you call this task. + +If you want to define `cwd` globally (setting all tasks to always run from the same working directory) you just need to declare that under taskipy's **settings** table: + +```toml +[tool.taskipy.settings] +cwd = "." +``` + ### Using Taskipy Without Poetry Taskipy was created with poetry projects in mind, but actually only requires a valid `pyproject.toml` file in your project's directory. As a result, you can use it even without poetry: diff --git a/task.bat b/task.bat new file mode 100644 index 0000000..3be1261 --- /dev/null +++ b/task.bat @@ -0,0 +1 @@ +python %~dp0taskipy\cli.py %*
diff --git a/taskipy/pyproject.py b/taskipy/pyproject.py index 1ffcb3e..e7fd74e 100644 --- a/taskipy/pyproject.py +++ b/taskipy/pyproject.py @@ -16,8 +16,8 @@ from taskipy.exceptions import ( class PyProject: def __init__(self, base_dir: Path): - pyproject_path = PyProject.__find_pyproject_path(base_dir) - self.__items = PyProject.__load_toml_file(pyproject_path) + self.__pyproject_path = PyProject.__find_pyproject_path(base_dir) + self.__items = PyProject.__load_toml_file(self.__pyproject_path) @property def tasks(self) -> Dict[str, Task]: @@ -33,6 +33,10 @@ class PyProject: return tasks @property + def dirpath(self) -> Path: + return self.__pyproject_path.parent + + @property def variables(self) -> Dict[str, Variable]: try: toml_vars = self.__items['tool']['taskipy'].get('variables', {}) diff --git a/taskipy/task.py b/taskipy/task.py index 61fd718..a065fd8 100644 --- a/taskipy/task.py +++ b/taskipy/task.py @@ -9,6 +9,7 @@ class Task: self.__task_command = self.__extract_task_command(task_toml_contents) self.__task_description = self.__extract_task_description(task_toml_contents) self.__task_use_vars = self.__extract_task_use_vars(task_toml_contents) + self.__task_workdir = self.__extract_task_workdir(task_toml_contents) @property def name(self) -> str: @@ -19,6 +20,10 @@ class Task: return self.__task_command @property + def workdir(self) -> Optional[str]: + return self.__task_workdir + + @property def description(self) -> str: return self.__task_description @@ -36,7 +41,7 @@ class Task: raise MalformedTaskError(self.__task_name, f'task\'s "use_vars" arg has to be bool type got {type(value)}') return value - raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help, use_vars }') + raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }') def __extract_task_command(self, task_toml_contents: object) -> str: if isinstance(task_toml_contents, str): @@ -48,7 +53,19 @@ class Task: except KeyError: raise MalformedTaskError(self.__task_name, 'the task item does not have the "cmd" property') - raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help, use_vars }') + raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }') + + def __extract_task_workdir(self, task_toml_contents: object) -> Optional[str]: + if isinstance(task_toml_contents, str): + return None + + if isinstance(task_toml_contents, dict): + value = task_toml_contents.get('cwd') + if value is not None and not isinstance(value, str): + raise MalformedTaskError(self.__task_name, f'task\'s "cwd" arg has to be str type got {type(value)}') + return value + + raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }') def __extract_task_description(self, task_toml_contents: object) -> str: if isinstance(task_toml_contents, str): @@ -60,4 +77,4 @@ class Task: except KeyError: return '' - raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help, use_vars}') + raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }') diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py index ab81012..d9cd6f4 100644 --- a/taskipy/task_runner.py +++ b/taskipy/task_runner.py @@ -23,9 +23,9 @@ else: class TaskRunner: 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) + cwd_as_path = cwd if isinstance(cwd, Path) else Path(cwd) + self.__project = PyProject(cwd_as_path) + self.__working_dir = self.__get_working_dir() or cwd_as_path def list(self): """lists tasks to stdout""" @@ -34,6 +34,7 @@ class TaskRunner: def run(self, task_name: str, args: List[str]) -> int: pre_command, command, post_command = self.__get_formatted_commands(task_name) + self.__working_dir = self.__get_working_dir(task_name) or self.__working_dir if pre_command is not None: exit_code = self.__run_command_and_return_exit_code(pre_command) @@ -194,3 +195,16 @@ class TaskRunner: process.send_signal(signum) return signal_handler + + def __get_working_dir(self, task_name: Optional[str] = None) -> Optional[Path]: + cwd: Optional[str] = self.__project.settings.get("cwd", None) + + if task_name is not None: + cwd = self.__project.tasks[task_name].workdir or cwd + + if cwd is not None: + path = self.__project.dirpath / cwd + if path.exists(): + return path + + return None diff --git a/tests/fixtures/project_with_cwd_under_settings/child_with_pyproject/alternative_calling_folder/.gitkeep b/tests/fixtures/project_with_cwd_under_settings/child_with_pyproject/alternative_calling_folder/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_settings/child_with_pyproject/alternative_calling_folder/.gitkeep diff --git a/tests/fixtures/project_with_cwd_under_settings/child_with_pyproject/pyproject.toml b/tests/fixtures/project_with_cwd_under_settings/child_with_pyproject/pyproject.toml new file mode 100644 index 0000000..581d7db --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_settings/child_with_pyproject/pyproject.toml @@ -0,0 +1,9 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.settings] +cwd = "../child_without_pyproject" + +[tool.taskipy.tasks] +pwd = { cmd = "python -c \"import os; print(os.getcwd())\"" } diff --git a/tests/fixtures/project_with_cwd_under_settings/child_without_pyproject/.gitkeep b/tests/fixtures/project_with_cwd_under_settings/child_without_pyproject/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_settings/child_without_pyproject/.gitkeep diff --git a/tests/fixtures/project_with_cwd_under_settings/pyproject.toml b/tests/fixtures/project_with_cwd_under_settings/pyproject.toml new file mode 100644 index 0000000..46b43c8 --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_settings/pyproject.toml @@ -0,0 +1,9 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.settings] +cwd = "." + +[tool.taskipy.tasks] +pwd = { cmd = "python -c \"import os; print(os.getcwd())\"" } diff --git a/tests/fixtures/project_with_cwd_under_specific_task/alternative_calling_folder/.gitkeep b/tests/fixtures/project_with_cwd_under_specific_task/alternative_calling_folder/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_specific_task/alternative_calling_folder/.gitkeep diff --git a/tests/fixtures/project_with_cwd_under_specific_task/pyproject.toml b/tests/fixtures/project_with_cwd_under_specific_task/pyproject.toml new file mode 100644 index 0000000..78b7b7a --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_specific_task/pyproject.toml @@ -0,0 +1,7 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.tasks] +pwd = { cmd = "python -c \"import os; print(os.getcwd())\"" } +pwdsub = { cmd = "python -c \"import os; print(os.getcwd())\"", cwd = "./subfolder" } diff --git a/tests/fixtures/project_with_cwd_under_specific_task/subfolder/.gitkeep b/tests/fixtures/project_with_cwd_under_specific_task/subfolder/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_specific_task/subfolder/.gitkeep diff --git a/tests/fixtures/project_with_cwd_under_specific_task_and_settings/global_cwd/.gitkeep b/tests/fixtures/project_with_cwd_under_specific_task_and_settings/global_cwd/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_specific_task_and_settings/global_cwd/.gitkeep diff --git a/tests/fixtures/project_with_cwd_under_specific_task_and_settings/pyproject.toml b/tests/fixtures/project_with_cwd_under_specific_task_and_settings/pyproject.toml new file mode 100644 index 0000000..443a678 --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_specific_task_and_settings/pyproject.toml @@ -0,0 +1,11 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.settings] +cwd = "./global_cwd" + +[tool.taskipy.tasks] +pwdglobalcwd = { cmd = "python -c \"import os; print(os.getcwd())\"" } +pwdpyproject = { cmd = "python -c \"import os; print(os.getcwd())\"", cwd = "." } +pwdsub = { cmd = "python -c \"import os; print(os.getcwd())\"", cwd = "./subfolder" } diff --git a/tests/fixtures/project_with_cwd_under_specific_task_and_settings/subfolder/.gitkeep b/tests/fixtures/project_with_cwd_under_specific_task_and_settings/subfolder/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/project_with_cwd_under_specific_task_and_settings/subfolder/.gitkeep diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index c8a314f..8605f59 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -1,4 +1,5 @@ import os +import platform import random import signal import subprocess @@ -43,7 +44,7 @@ class TaskipyTestCase(unittest.TestCase): args: Optional[List[str]] = None, cwd=os.curdir, ) -> subprocess.Popen: - executable_path = path.abspath('task') + executable_path = path.abspath('task.bat' if platform.system() == 'Windows' else 'task') args = args or [] return subprocess.Popen([executable_path, task] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) @@ -303,7 +304,7 @@ class TaskDescriptionTestCase(TaskipyTestCase): exit_code, stdout, _ = self.run_task('print_age', cwd=cwd) self.assertEqual(exit_code, 1) - self.assertSubstr('tasks must be strings, or dicts that contain { cmd, help, use_vars }', stdout) + self.assertSubstr('tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }', stdout) class TaskRunFailTestCase(TaskipyTestCase): @@ -760,3 +761,90 @@ class VariableSchemaTestCase(TaskipyTestCase): exit_code, stdout, _ = self.run_task('echo', cwd=cwd) self.assertSubstr('variable test is invalid', stdout) self.assertEqual(exit_code, 127) + + +class SetCWDTestCase(TaskipyTestCase): + def test_project_with_cwd_under_settings(self): + cwd = self.create_test_dir_from_fixture("project_with_cwd_under_settings") + + exit_code, stdout, _ = self.run_task("pwd", cwd=cwd) + self.assertTrue(stdout.strip().endswith(cwd)) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwd", cwd=path.join(cwd, "child_without_pyproject")) + self.assertTrue(stdout.strip().endswith(cwd)) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwd", cwd=path.join(cwd, "child_with_pyproject")) + self.assertTrue(stdout.strip().endswith("child_without_pyproject")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwd", cwd=path.join(cwd, "child_with_pyproject/alternative_calling_folder")) + self.assertTrue(stdout.strip().endswith("child_without_pyproject")) + self.assertEqual(exit_code, 0) + + def test_project_with_cwd_under_specific_task(self): + cwd = self.create_test_dir_from_fixture("project_with_cwd_under_specific_task") + + exit_code, stdout, _ = self.run_task("pwd", cwd=cwd) + self.assertTrue(stdout.strip().endswith(cwd)) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwd", cwd=path.join(cwd, "subfolder")) + self.assertTrue(stdout.strip().endswith("subfolder")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwd", cwd=path.join(cwd, "alternative_calling_folder")) + self.assertTrue(stdout.strip().endswith("alternative_calling_folder")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdsub", cwd=cwd) + self.assertTrue(stdout.strip().endswith("subfolder")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdsub", cwd=path.join(cwd, "subfolder")) + self.assertTrue(stdout.strip().endswith("subfolder")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdsub", cwd=path.join(cwd, "alternative_calling_folder")) + self.assertTrue(stdout.strip().endswith("subfolder")) + self.assertEqual(exit_code, 0) + + def test_project_with_cwd_under_specific_task_and_settings(self): + cwd = self.create_test_dir_from_fixture("project_with_cwd_under_specific_task_and_settings") + + exit_code, stdout, _ = self.run_task("pwdglobalcwd", cwd=cwd) + self.assertTrue(stdout.strip().endswith("global_cwd")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdglobalcwd", cwd=path.join(cwd, "subfolder")) + self.assertTrue(stdout.strip().endswith("global_cwd")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdglobalcwd", cwd=path.join(cwd, "global_cwd")) + self.assertTrue(stdout.strip().endswith("global_cwd")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdpyproject", cwd=cwd) + self.assertTrue(stdout.strip().endswith(cwd)) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdpyproject", cwd=path.join(cwd, "subfolder")) + self.assertTrue(stdout.strip().endswith(cwd)) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdpyproject", cwd=path.join(cwd, "global_cwd")) + self.assertTrue(stdout.strip().endswith(cwd)) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdsub", cwd=cwd) + self.assertTrue(stdout.strip().endswith("subfolder")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdsub", cwd=path.join(cwd, "subfolder")) + self.assertTrue(stdout.strip().endswith("subfolder")) + self.assertEqual(exit_code, 0) + + exit_code, stdout, _ = self.run_task("pwdsub", cwd=path.join(cwd, "global_cwd")) + self.assertTrue(stdout.strip().endswith("subfolder")) + self.assertEqual(exit_code, 0) |