From edde6029745a650696773c30247b97c151079487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardas=20Ali=C5=A1auskas?= Date: Mon, 27 Sep 2021 17:12:22 +0700 Subject: Add variable support (#38) * add variable support * add use_vars task option and make command variables expand only when use_vars is True * add tests for variable feature * add setting use_vars as global use_vars for all commands --- tests/test_taskipy.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 1598f0f..4a1c3e3 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -272,7 +272,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 }', stdout) + self.assertSubstr('tasks must be strings, or dicts that contain { cmd, help, use_vars }', stdout) class TaskRunFailTestCase(TaskipyTestCase): @@ -429,3 +429,96 @@ class TaskFromChildTestCase(TaskipyTestCase): _, stdout, _ = self.run_task('hello', cwd=path.join(cwd, 'child_with_pyproject')) self.assertSubstr('hello from child', stdout) + + +class UseVarsTestCase(TaskipyTestCase): + def test_use_vars_working(self): + py_project_toml = ''' + [tool.taskipy.variables] + name = "John Doe" + + [tool.taskipy.tasks] + echo = { cmd = "echo hello {name:<10}:", use_vars=true } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('hello John Doe :', stdout) + self.assertEqual(exit_code, 0) + + def test_use_vars_no_param(self): + py_project_toml = ''' + [tool.taskipy.variables] + name = "John Doe" + + [tool.taskipy.tasks] + echo = { cmd = "echo hello {name}" } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('hello {name}', stdout) + self.assertEqual(exit_code, 0) + + def test_use_vars_param_disabled(self): + py_project_toml = ''' + [tool.taskipy.variables] + name = "John Doe" + + [tool.taskipy.tasks] + echo = { cmd = "echo hello {name}", use_vars=false} + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('hello {name}', stdout) + self.assertEqual(exit_code, 0) + + def test_use_vars_str_task_no_param(self): + py_project_toml = ''' + [tool.taskipy.variables] + name = "John Doe" + + [tool.taskipy.tasks] + echo = "echo hello {name}" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('hello {name}', stdout) + self.assertEqual(exit_code, 0) + + def test_use_vars_param_malformed(self): + py_project_toml = ''' + [tool.taskipy.variables] + name = "John Doe" + + [tool.taskipy.tasks] + echo = { cmd = "echo hello {name}", use_vars=1} + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('task\'s "use_vars" arg has to be bool', stdout) + self.assertEqual(exit_code, 1) + + def test_use_vars_missing_var(self): + py_project_toml = ''' + [tool.taskipy.tasks] + echo = { cmd = "echo hello {name}", use_vars=true} + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr("reason: 'name' variable expected in [pyproject.taskipy.variables", stdout) + self.assertEqual(exit_code, 1) + + def test_use_vars_setting(self): + py_project_toml = ''' + [tool.taskipy.settings] + use_vars = true + + [tool.taskipy.variables] + name = "John Doe" + + [tool.taskipy.tasks] + echo = "echo hello {name:<10}:" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('hello John Doe :', stdout) + self.assertEqual(exit_code, 0) -- cgit v1.2.3