|
| 1 | +import shutil |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from click.testing import CliRunner |
| 7 | + |
| 8 | +from concore_cli.cli import cli |
| 9 | + |
| 10 | + |
| 11 | +class TestBuildForce(unittest.TestCase): |
| 12 | + def setUp(self): |
| 13 | + self.runner = CliRunner() |
| 14 | + self.temp_dir = tempfile.mkdtemp() |
| 15 | + |
| 16 | + def tearDown(self): |
| 17 | + shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 18 | + |
| 19 | + def _project(self): |
| 20 | + result = self.runner.invoke(cli, ["init", "proj"]) |
| 21 | + self.assertEqual(result.exit_code, 0, result.output) |
| 22 | + return [ |
| 23 | + "build", |
| 24 | + "proj/workflow.graphml", |
| 25 | + "--source", |
| 26 | + "proj/src", |
| 27 | + "--type", |
| 28 | + "posix", |
| 29 | + ] |
| 30 | + |
| 31 | + def test_existing_output_is_an_error_without_force(self): |
| 32 | + with self.runner.isolated_filesystem(temp_dir=self.temp_dir): |
| 33 | + args = self._project() |
| 34 | + Path("out").mkdir() |
| 35 | + (Path("out") / "keep.txt").write_text("x") |
| 36 | + |
| 37 | + result = self.runner.invoke(cli, args + ["--output", "out"]) |
| 38 | + |
| 39 | + self.assertNotEqual(result.exit_code, 0) |
| 40 | + self.assertIn("already exists", result.output) |
| 41 | + self.assertIn("--force", result.output) |
| 42 | + self.assertTrue((Path("out") / "keep.txt").exists()) |
| 43 | + |
| 44 | + def test_force_replaces_a_generated_study(self): |
| 45 | + with self.runner.isolated_filesystem(temp_dir=self.temp_dir): |
| 46 | + args = self._project() |
| 47 | + first = self.runner.invoke(cli, args + ["--output", "out"]) |
| 48 | + self.assertEqual(first.exit_code, 0, first.output) |
| 49 | + (Path("out") / "stale.txt").write_text("old") |
| 50 | + |
| 51 | + second = self.runner.invoke(cli, args + ["--output", "out", "--force"]) |
| 52 | + |
| 53 | + self.assertEqual(second.exit_code, 0, second.output) |
| 54 | + self.assertFalse((Path("out") / "stale.txt").exists()) |
| 55 | + self.assertTrue((Path("out") / "run").exists()) |
| 56 | + |
| 57 | + def test_force_refuses_a_directory_that_is_not_a_study(self): |
| 58 | + with self.runner.isolated_filesystem(temp_dir=self.temp_dir): |
| 59 | + args = self._project() |
| 60 | + Path("mydata").mkdir() |
| 61 | + (Path("mydata") / "important.txt").write_text("keep me") |
| 62 | + |
| 63 | + result = self.runner.invoke(cli, args + ["--output", "mydata", "--force"]) |
| 64 | + |
| 65 | + self.assertNotEqual(result.exit_code, 0) |
| 66 | + self.assertIn("does not look like a study", result.output) |
| 67 | + self.assertTrue((Path("mydata") / "important.txt").exists()) |
| 68 | + |
| 69 | + def test_force_refuses_the_current_directory(self): |
| 70 | + with self.runner.isolated_filesystem(temp_dir=self.temp_dir): |
| 71 | + args = self._project() |
| 72 | + # Even if it looks like a study, the current directory is never deleted |
| 73 | + (Path("build")).write_text("") |
| 74 | + (Path("run")).write_text("") |
| 75 | + |
| 76 | + result = self.runner.invoke(cli, args + ["--output", ".", "--force"]) |
| 77 | + |
| 78 | + self.assertNotEqual(result.exit_code, 0) |
| 79 | + self.assertTrue((Path("proj") / "workflow.graphml").exists()) |
| 80 | + |
| 81 | + def test_force_refuses_a_directory_containing_the_sources(self): |
| 82 | + with self.runner.isolated_filesystem(temp_dir=self.temp_dir): |
| 83 | + args = self._project() |
| 84 | + (Path("proj") / "build").write_text("") |
| 85 | + (Path("proj") / "run").write_text("") |
| 86 | + |
| 87 | + result = self.runner.invoke(cli, args + ["--output", "proj", "--force"]) |
| 88 | + |
| 89 | + self.assertNotEqual(result.exit_code, 0) |
| 90 | + self.assertIn("contains", result.output) |
| 91 | + self.assertTrue((Path("proj") / "workflow.graphml").exists()) |
| 92 | + |
| 93 | + def test_force_refuses_a_regular_file(self): |
| 94 | + with self.runner.isolated_filesystem(temp_dir=self.temp_dir): |
| 95 | + args = self._project() |
| 96 | + Path("out").write_text("not a directory") |
| 97 | + |
| 98 | + result = self.runner.invoke(cli, args + ["--output", "out", "--force"]) |
| 99 | + |
| 100 | + self.assertNotEqual(result.exit_code, 0) |
| 101 | + self.assertEqual(Path("out").read_text(), "not a directory") |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments