|
| 1 | +import tempfile |
| 2 | +import shutil |
| 3 | +import unittest |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from click.testing import CliRunner |
| 8 | +from concore_cli.cli import cli |
| 9 | + |
| 10 | + |
| 11 | +class TestSetupCommand(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) |
| 18 | + |
| 19 | + @patch("concore_cli.commands.setup._resolve_concore_path") |
| 20 | + @patch("concore_cli.commands.setup._detect_tool") |
| 21 | + @patch("concore_cli.commands.setup._get_platform_key") |
| 22 | + def test_setup_dry_run_does_not_write(self, mock_plat, mock_detect, mock_path): |
| 23 | + mock_plat.return_value = "posix" |
| 24 | + mock_path.return_value = Path(self.temp_dir) |
| 25 | + |
| 26 | + def detect_side_effect(names): |
| 27 | + if "g++" in names: |
| 28 | + return "/usr/bin/g++", "g++" |
| 29 | + if "python3" in names: |
| 30 | + return "/usr/bin/python3", "python3" |
| 31 | + if "iverilog" in names: |
| 32 | + return "/usr/bin/iverilog", "iverilog" |
| 33 | + if "octave" in names: |
| 34 | + return "/usr/bin/octave", "octave" |
| 35 | + if "docker" in names: |
| 36 | + return "/usr/bin/docker", "docker" |
| 37 | + return None, None |
| 38 | + |
| 39 | + mock_detect.side_effect = detect_side_effect |
| 40 | + |
| 41 | + result = self.runner.invoke(cli, ["setup", "--dry-run"]) |
| 42 | + self.assertEqual(result.exit_code, 0) |
| 43 | + |
| 44 | + self.assertFalse((Path(self.temp_dir) / "concore.tools").exists()) |
| 45 | + self.assertFalse((Path(self.temp_dir) / "concore.sudo").exists()) |
| 46 | + self.assertFalse((Path(self.temp_dir) / "concore.octave").exists()) |
| 47 | + |
| 48 | + @patch("concore_cli.commands.setup._resolve_concore_path") |
| 49 | + @patch("concore_cli.commands.setup._detect_tool") |
| 50 | + @patch("concore_cli.commands.setup._get_platform_key") |
| 51 | + def test_setup_writes_files(self, mock_plat, mock_detect, mock_path): |
| 52 | + mock_plat.return_value = "posix" |
| 53 | + mock_path.return_value = Path(self.temp_dir) |
| 54 | + |
| 55 | + def detect_side_effect(names): |
| 56 | + if "g++" in names: |
| 57 | + return "/usr/bin/g++", "g++" |
| 58 | + if "python3" in names: |
| 59 | + return "/usr/bin/python3", "python3" |
| 60 | + if "iverilog" in names: |
| 61 | + return "/usr/bin/iverilog", "iverilog" |
| 62 | + if "octave" in names: |
| 63 | + return "/usr/bin/octave", "octave" |
| 64 | + if "docker" in names: |
| 65 | + return "/usr/bin/docker", "docker" |
| 66 | + return None, None |
| 67 | + |
| 68 | + mock_detect.side_effect = detect_side_effect |
| 69 | + |
| 70 | + result = self.runner.invoke(cli, ["setup"]) |
| 71 | + self.assertEqual(result.exit_code, 0) |
| 72 | + |
| 73 | + tools_file = Path(self.temp_dir) / "concore.tools" |
| 74 | + sudo_file = Path(self.temp_dir) / "concore.sudo" |
| 75 | + octave_file = Path(self.temp_dir) / "concore.octave" |
| 76 | + |
| 77 | + self.assertTrue(tools_file.exists()) |
| 78 | + self.assertTrue(sudo_file.exists()) |
| 79 | + self.assertTrue(octave_file.exists()) |
| 80 | + |
| 81 | + tools_content = tools_file.read_text() |
| 82 | + self.assertIn("CPPEXE=/usr/bin/g++", tools_content) |
| 83 | + self.assertIn("PYTHONEXE=/usr/bin/python3", tools_content) |
| 84 | + self.assertIn("VEXE=/usr/bin/iverilog", tools_content) |
| 85 | + self.assertIn("OCTAVEEXE=/usr/bin/octave", tools_content) |
| 86 | + self.assertEqual(sudo_file.read_text().strip(), "docker") |
| 87 | + |
| 88 | + @patch("concore_cli.commands.setup._resolve_concore_path") |
| 89 | + @patch("concore_cli.commands.setup._detect_tool") |
| 90 | + @patch("concore_cli.commands.setup._get_platform_key") |
| 91 | + def test_setup_no_force_keeps_existing(self, mock_plat, mock_detect, mock_path): |
| 92 | + mock_plat.return_value = "posix" |
| 93 | + mock_path.return_value = Path(self.temp_dir) |
| 94 | + |
| 95 | + tools_file = Path(self.temp_dir) / "concore.tools" |
| 96 | + tools_file.write_text("CPPEXE=/old/path\n") |
| 97 | + |
| 98 | + def detect_side_effect(names): |
| 99 | + if "g++" in names: |
| 100 | + return "/usr/bin/g++", "g++" |
| 101 | + if "python3" in names: |
| 102 | + return "/usr/bin/python3", "python3" |
| 103 | + return None, None |
| 104 | + |
| 105 | + mock_detect.side_effect = detect_side_effect |
| 106 | + |
| 107 | + result = self.runner.invoke(cli, ["setup"]) |
| 108 | + self.assertEqual(result.exit_code, 0) |
| 109 | + self.assertEqual(tools_file.read_text(), "CPPEXE=/old/path\n") |
| 110 | + |
| 111 | + @patch("concore_cli.commands.setup._resolve_concore_path") |
| 112 | + @patch("concore_cli.commands.setup._detect_tool") |
| 113 | + @patch("concore_cli.commands.setup._get_platform_key") |
| 114 | + def test_setup_force_overwrites_existing(self, mock_plat, mock_detect, mock_path): |
| 115 | + mock_plat.return_value = "posix" |
| 116 | + mock_path.return_value = Path(self.temp_dir) |
| 117 | + |
| 118 | + tools_file = Path(self.temp_dir) / "concore.tools" |
| 119 | + tools_file.write_text("CPPEXE=/old/path\n") |
| 120 | + |
| 121 | + def detect_side_effect(names): |
| 122 | + if "g++" in names: |
| 123 | + return "/usr/bin/g++", "g++" |
| 124 | + if "python3" in names: |
| 125 | + return "/usr/bin/python3", "python3" |
| 126 | + return None, None |
| 127 | + |
| 128 | + mock_detect.side_effect = detect_side_effect |
| 129 | + |
| 130 | + result = self.runner.invoke(cli, ["setup", "--force"]) |
| 131 | + self.assertEqual(result.exit_code, 0) |
| 132 | + |
| 133 | + content = tools_file.read_text() |
| 134 | + self.assertIn("CPPEXE=/usr/bin/g++", content) |
| 135 | + self.assertIn("PYTHONEXE=/usr/bin/python3", content) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + unittest.main() |
0 commit comments