def test_transform_basename_prefix_only(self): self.assertEqual( output_cacher.TempFileTransform( basename_prefix="fake.").transform("bar.o"), "fake.bar.o") self.assertEqual( output_cacher.TempFileTransform( basename_prefix="fake.").transform("foo/bar.o"), "foo/fake.bar.o")
def test_command_passed_and_rerun_differs(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action( command=["run.sh", "in.put", "out.put"], outputs={"out.put"}) with mock.patch.object(subprocess, "call", return_value=0) as mock_call: with mock.patch.object(output_cacher, "files_match", return_value=False) as mock_match: with mock.patch.object(os.path, "isfile", return_value=True) as mock_isfile: with mock.patch.object(os, "remove") as mock_remove: with mock.patch.object(os, "makedirs") as mock_mkdir: with mock.patch.object(shutil, "copy2") as mock_copy: self.assertEqual( action.run_twice_and_compare_outputs( transform), 1) mock_call.assert_has_calls( [ mock.call(["run.sh", "in.put", "out.put"]), mock.call(["run.sh", "in.put", "out.put"]), ], any_order=True) mock_match.assert_called_with("out.put", "out.put.tmp") mock_remove.assert_not_called() mock_isfile.assert_called() mock_mkdir.assert_not_called() # using suffix, not temp_dir mock_copy.assert_called_once_with( "out.put", "out.put.tmp", follow_symlinks=False)
def test_require_flag_match_have_flag(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action(command=["run.sh", "-f", "out.txt"], substitutions={"out.txt": "-f"}) repl, renamed = action.substitute_command(transform) self.assertEqual(repl, ["run.sh", "-f", "out.txt.tmp"]) self.assertEqual(renamed, {"out.txt": "out.txt.tmp"})
def test_command_passed_and_some_outptus_differ(self): def fake_match(file1: str, file2: str) -> bool: if file1 == "out.put": return True elif file1 == "out2.put": return False raise ValueError(f"Unhandled file name: {file1}") transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action( command=["run.sh", "in.put", "out.put", "out2.put"], outputs={"out.put", "out2.put"}) with mock.patch.object(subprocess, "call", return_value=0) as mock_call: with mock.patch.object(output_cacher, "files_match", wraps=fake_match) as mock_match: with mock.patch.object(os, "remove") as mock_remove: self.assertEqual( action.run_twice_with_substitution_and_compare_outputs( transform), 1) mock_call.assert_has_calls( [ mock.call(["run.sh", "in.put", "out.put", "out2.put"]), mock.call(["run.sh", "in.put", "out.put.tmp", "out2.put.tmp"]), ], any_order=True) mock_match.assert_has_calls( [ mock.call("out.put", "out.put.tmp"), mock.call("out2.put", "out2.put.tmp"), ], any_order=True) mock_remove.assert_has_calls([mock.call("out.put.tmp")])
def test_match_only_one_flag_first_occurrence(self): transform = output_cacher.TempFileTransform(basename_prefix="tmp-") action = output_cacher.Action( command=["run.sh", "out.txt", "-f", "out.txt", "out.txt"], substitutions={"out.txt": "-f"}) repl, renamed = action.substitute_command(transform) self.assertEqual(repl, ["run.sh", "out.txt", "-f", "tmp-out.txt", "out.txt"]) self.assertEqual(renamed, {"out.txt": "tmp-out.txt"})
def test_command_failed(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action(command=["run.sh"], outputs={}) with mock.patch.object(subprocess, "call", return_value=1) as mock_call: with mock.patch.object(output_cacher, "move_if_different") as mock_update: self.assertEqual(action.run_cached(transform), 1) mock_call.assert_called_with(["run.sh"]) mock_update.assert_not_called()
def test_command_failed(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action(command=["run.sh"], outputs={}) with mock.patch.object(subprocess, "call", return_value=1) as mock_call: with mock.patch.object(output_cacher, "files_match") as mock_match: self.assertEqual( action.run_twice_with_substitution_and_compare_outputs( transform), 1) mock_call.assert_called_once_with(["run.sh"]) mock_match.assert_not_called()
def test_command_passed_using_suffix(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action( command=["run.sh", "in.put", "out.put"], outputs={"out.put"}) with mock.patch.object(subprocess, "call", return_value=0) as mock_call: with mock.patch.object(output_cacher, "move_if_different") as mock_update: self.assertEqual(action.run_cached(transform), 0) mock_call.assert_called_with(["run.sh", "in.put", "out.put.tmp"]) mock_update.assert_called_with( src="out.put.tmp", dest="out.put", verbose=False)
def test_command_passed_using_relative_temp_dir(self): transform = output_cacher.TempFileTransform(temp_dir="temp/temp") action = output_cacher.Action( command=["run.sh", "in.put", "foo/out.put"], outputs={"foo/out.put"}) with mock.patch.object(subprocess, "call", return_value=0) as mock_call: with mock.patch.object(output_cacher, "move_if_different") as mock_update: with mock.patch.object(os, "makedirs") as mock_mkdir: self.assertEqual(action.run_cached(transform), 0) mock_mkdir.assert_called_with("temp/temp/foo", exist_ok=True) mock_call.assert_called_with( ["run.sh", "in.put", "temp/temp/foo/out.put"]) mock_update.assert_called_with( src="temp/temp/foo/out.put", dest="foo/out.put", verbose=False)
def test_command_passed_and_some_outptus_differ(self): def fake_match(file1: str, file2: str) -> bool: if file1 == "out.put": return True elif file1 == "out2.put": return False raise ValueError(f"Unhandled file name: {file1}") transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action( command=["run.sh", "in.put", "out.put", "out2.put"], substitutions={ "out.put": "", "out2.put": "" }) with mock.patch.object(subprocess, "call", return_value=0) as mock_call: with mock.patch.object(output_cacher, "files_match", wraps=fake_match) as mock_match: with mock.patch.object(os.path, "isfile", return_value=True) as mock_isfile: with mock.patch.object(os, "remove") as mock_remove: with mock.patch.object(os, "makedirs") as mock_mkdir: with mock.patch.object(shutil, "copy2") as mock_copy: self.assertEqual( action.run_twice_and_compare_outputs( transform), 1) mock_call.assert_has_calls([ mock.call(["run.sh", "in.put", "out.put", "out2.put"]), mock.call(["run.sh", "in.put", "out.put", "out2.put"]), ], any_order=True) mock_match.assert_has_calls([ mock.call("out.put", "out.put.tmp"), mock.call("out2.put", "out2.put.tmp"), ], any_order=True) mock_remove.assert_has_calls([mock.call("out.put.tmp")]) mock_isfile.assert_called() mock_mkdir.assert_not_called() # using suffix, not temp_dir mock_copy.assert_has_calls([ mock.call("out.put", "out.put.tmp", follow_symlinks=False), mock.call("out2.put", "out2.put.tmp", follow_symlinks=False), ], any_order=True)
def test_command_failed(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action(command=["run.sh"], outputs={}) with mock.patch.object(subprocess, "call", return_value=1) as mock_call: with mock.patch.object(output_cacher, "files_match") as mock_match: with mock.patch.object(os.path, "exists", return_value=True) as mock_exists: with mock.patch.object(shutil, "copy2") as mock_copy: with mock.patch.object(os, "makedirs") as mock_mkdir: self.assertEqual( action.run_twice_and_compare_outputs(transform), 1) mock_call.assert_called_once_with(["run.sh"]) mock_match.assert_not_called() mock_exists.assert_not_called() mock_mkdir.assert_not_called() mock_copy.assert_not_called()
def test_command_passed_and_rerun_differs(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action( command=["run.sh", "in.put", "out.put"], outputs={"out.put"}) with mock.patch.object(subprocess, "call", return_value=0) as mock_call: with mock.patch.object(output_cacher, "files_match", return_value=False) as mock_match: with mock.patch.object(os, "remove") as mock_remove: self.assertEqual( action.run_twice_with_substitution_and_compare_outputs( transform), 1) mock_call.assert_has_calls( [ mock.call(["run.sh", "in.put", "out.put"]), mock.call(["run.sh", "in.put", "out.put.tmp"]), ], any_order=True) mock_match.assert_called_with("out.put", "out.put.tmp") mock_remove.assert_not_called()
def test_invalid(self): self.assertFalse(output_cacher.TempFileTransform().valid)
def test_transform_suffix_only(self): self.assertEqual( output_cacher.TempFileTransform( suffix=".tmp").transform("foo/bar.o"), "foo/bar.o.tmp")
def test_dry_run(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action(command=["run.sh"], substitutions={}) with mock.patch.object(subprocess, "call") as mock_call: self.assertEqual(action.run_cached(transform, dry_run=True), 0) mock_call.assert_not_called()
def test_temp_dir_and_suffix(self): self.assertTrue( output_cacher.TempFileTransform( temp_dir="throw/away", suffix=".tmp").valid)
def test_suffix_only(self): self.assertTrue(output_cacher.TempFileTransform(suffix=".tmp").valid)
def test_temp_dir_only(self): self.assertTrue( output_cacher.TempFileTransform(temp_dir="my_tmp").valid) self.assertTrue( output_cacher.TempFileTransform(temp_dir="/my_tmp").valid)
def test_basename_prefix_only(self): self.assertTrue( output_cacher.TempFileTransform(basename_prefix="temp-").valid)
def test_transform_temp_dir_only(self): self.assertEqual( output_cacher.TempFileTransform( temp_dir="t/m/p").transform("foo/bar.o"), "t/m/p/foo/bar.o")
def test_transform_basename_prefix_and_temp_dir(self): self.assertEqual( output_cacher.TempFileTransform( temp_dir="/t/m/p", basename_prefix="xyz-").transform("foo/bar.o"), "/t/m/p/foo/xyz-bar.o")
def test_transform_suffix_and_temp_dir(self): self.assertEqual( output_cacher.TempFileTransform( temp_dir="/t/m/p", suffix=".foo").transform("foo/bar.o"), "/t/m/p/foo/bar.o.foo")
def test_no_substitutions(self): transform = output_cacher.TempFileTransform(suffix=".tmp") action = output_cacher.Action(command=["run.sh"], substitutions={}) repl, renamed = action.substitute_command(transform) self.assertEqual(repl, ["run.sh"]) self.assertEqual(renamed, {})