def setUp(self): self.filenames = ["file1", "file2", "file3", "file_1", "file_2", "file_3", "file__1", "file__2"] self.should_not_be_present = ["file4", "file5", "file6", "file7", "file8", "file9"] for filename in self.should_not_be_present: if os.path.exists(filename) and os.path.isfile(filename): os.unlink(filename) for filename in self.filenames: with open(filename, "w") as f: f.close() self.task = Task(["file[0-3]", "file_*"], ["file[4-6]"], ["touch file4\n", "touch file5\n", "touch file6\n", "echo $[test_var]\n", "echo $test_var\n"], ["force"], {"test_var": "test_var_value"})
class TestTaskMethods(unittest.TestCase): def setUp(self): self.filenames = ["file1", "file2", "file3", "file_1", "file_2", "file_3", "file__1", "file__2"] self.should_not_be_present = ["file4", "file5", "file6", "file7", "file8", "file9"] for filename in self.should_not_be_present: if os.path.exists(filename) and os.path.isfile(filename): os.unlink(filename) for filename in self.filenames: with open(filename, "w") as f: f.close() self.task = Task(["file[0-3]", "file_*"], ["file[4-6]"], ["touch file4\n", "touch file5\n", "touch file6\n", "echo $[test_var]\n", "echo $test_var\n"], ["force"], {"test_var": "test_var_value"}) def test_task(self): self.task() def test_code_variable_expansion(self): self.task.expand_variables() self.assertTrue(any([line for line in self.task.code if "test_var_value" in line])) def test_outputs_do_not_exist(self): task = Task(["file[0-3]", "file_*"], ["file99", "file234"], ["touch file4\n", "touch file5\n", "touch file6\n"], ["force"], {"test_var": "test_var_value"}) with self.assertRaises(TaskFailedException): task() def test_return_code_is_not_0(self): task = Task(["file[0-3]", "file_*"], ["file99", "file234"], ["touch file4\n", "touch file5\n", "touch file6\n", "ls non_existant_dir\n"], ["force"], {"test_var": "test_var_value"}) with self.assertRaises(TaskFailedException): task() def test_use_the_force(self): f = open("file22", "w") f.close() time.sleep(0.1) f = open("file33", "w") f.close() self.assertTrue(os.path.getmtime("file33") > os.path.getmtime("file22")) task = Task(["file22"], ["file33"], ["touch file33\n"], ["force"], {"test_var": "test_var_value"}) result = self.task.dependencies_are_newer(["file33"], ["file22"]) self.assertFalse(result) self.assertTrue(task.inputs == ["file22"]) self.assertTrue(task.outputs == ["file33"]) self.assertTrue(task.code == ["touch file33\n"]) self.assertTrue(task.options == ["force"]) self.assertTrue(task.interpreter == "bash") self.assertTrue(task.force) task() os.unlink("file22") os.unlink("file33") def test_files_exist(self): self.assertTrue(self.task.files_exist(["file1", "file2", "file3"])) def test_filename_shell_expansion(self): results = self.task.expand_filenames(["file[0-3]", "file_?", "file__*"]) for result, filename in zip(results, self.filenames): self.assertEqual(result, filename) def test_filename_variable_expansion(self): results = self.task.expand_filenames(["$test_var"]) self.assertEqual(results[0], "test_var_value") def test_nonexistant_file(self): results = self.task.expand_filenames(["file[4-9]"]) self.assertEqual(results[0], "NONEXISTENT") def test_dependencies_are_newer(self): for filename in ["old_file1", "old_file2"]: with open(filename, "w") as f: f.close() time.sleep(0.1) for filename in ["new_file1", "new_file2"]: with open(filename, "w") as f: f.close() result = self.task.dependencies_are_newer(["old_file1", "old_file2"], ["new_file1", "new_file2"]) self.assertTrue(result) [os.unlink(filename) for filename in ["old_file1", "old_file2"]] [os.unlink(filename) for filename in ["new_file1", "new_file2"]] def test_dependencies_are_older(self): for filename in ["new_file1", "new_file2"]: with open(filename, "w") as f: f.close() time.sleep(0.1) for filename in ["old_file1", "old_file2"]: with open(filename, "w") as f: f.close() result = self.task.dependencies_are_newer(["old_file1", "old_file2"], ["new_file1", "new_file2"]) self.assertFalse(result) [os.unlink(filename) for filename in ["old_file1", "old_file2"]] [os.unlink(filename) for filename in ["new_file1", "new_file2"]] def tearDown(self): for filename in self.filenames: os.unlink(filename)