diff options
| author | Diogo <rossi.diogo@gmail.com> | 2024-08-23 17:59:31 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-23 23:59:31 +0300 |
| commit | 7cfecafdbd84c0f25b8a6c09a4cef72616968917 (patch) | |
| tree | a6ebb99681950915a503366cd4be1fc3c2e2ff0c /tests | |
| parent | Update README.md (diff) | |
| download | taskipy2-7cfecafdbd84c0f25b8a6c09a4cef72616968917.zip | |
Add option `cwd` to running tasks (#83)
* 🎨 chore: removing settings that are deprecated
Reference:
https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions#existing-python-tools-extensions
* 👷 build: add `.venv` folder to `.gitignore`
* ✨ feat: new property 'workdir' for tasks
* 🎨 chore: add "cwd" to error messages
* ✨ feat: new property `dirpath` of `PyProject`
* ✨ feat: new method to get workdir in `TaskRunner`
* 🐞 fix: allow for absolute paths
* 🐞 fix: the task cwd should takes precedence
* 📄 docs: update `README.md`
* Update README.md
Co-authored-by: Roy Sommer <illBeRoy@users.noreply.github.com>
* rename variable `working_dir` into `cwd_as_path` to emphasize what it's really used for
suggestion: https://github.com/taskipy/taskipy/pull/83/files#r1676391148
* assert that `cwd` is a string, and throw if it's not
* 👷 build: add settings to test in VSCode
* 🐞 fix: add task exec when testing on windows
* ✅ test: add new `SetCWDTestCase`
* ✅ test: fix error message in test case
* ✏️ typo: in fixture name
* 💄 style: format code
* Update settings.json
---------
Co-authored-by: Roy Sommer <illBeRoy@users.noreply.github.com>
Diffstat (limited to 'tests')
11 files changed, 126 insertions, 2 deletions
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) |