|
| 1 | +"""Tests for vcpkg ref/branch checkout support.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from hatch_cpp.toolchains.vcpkg import ( |
| 8 | + HatchCppVcpkgConfiguration, |
| 9 | + _read_vcpkg_ref_from_gitmodules, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class TestReadVcpkgRefFromGitmodules: |
| 14 | + """Tests for the _read_vcpkg_ref_from_gitmodules helper.""" |
| 15 | + |
| 16 | + def test_no_gitmodules_file(self, tmp_path, monkeypatch): |
| 17 | + monkeypatch.chdir(tmp_path) |
| 18 | + assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None |
| 19 | + |
| 20 | + def test_gitmodules_without_vcpkg_submodule(self, tmp_path, monkeypatch): |
| 21 | + monkeypatch.chdir(tmp_path) |
| 22 | + (tmp_path / ".gitmodules").write_text('[submodule "other"]\n\tpath = other\n\turl = https://github.com/example/other.git\n') |
| 23 | + assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None |
| 24 | + |
| 25 | + def test_gitmodules_vcpkg_without_branch(self, tmp_path, monkeypatch): |
| 26 | + monkeypatch.chdir(tmp_path) |
| 27 | + (tmp_path / ".gitmodules").write_text('[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n') |
| 28 | + assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None |
| 29 | + |
| 30 | + def test_gitmodules_vcpkg_with_branch(self, tmp_path, monkeypatch): |
| 31 | + monkeypatch.chdir(tmp_path) |
| 32 | + (tmp_path / ".gitmodules").write_text( |
| 33 | + '[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n' |
| 34 | + ) |
| 35 | + assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == "2024.01.12" |
| 36 | + |
| 37 | + def test_gitmodules_vcpkg_with_commit_sha_branch(self, tmp_path, monkeypatch): |
| 38 | + monkeypatch.chdir(tmp_path) |
| 39 | + sha = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2" |
| 40 | + (tmp_path / ".gitmodules").write_text( |
| 41 | + f'[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = {sha}\n' |
| 42 | + ) |
| 43 | + assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == sha |
| 44 | + |
| 45 | + def test_gitmodules_custom_vcpkg_root(self, tmp_path, monkeypatch): |
| 46 | + monkeypatch.chdir(tmp_path) |
| 47 | + (tmp_path / ".gitmodules").write_text( |
| 48 | + '[submodule "deps/vcpkg"]\n\tpath = deps/vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.06.15\n' |
| 49 | + ) |
| 50 | + # Default vcpkg root won't match |
| 51 | + assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None |
| 52 | + # Custom root matches |
| 53 | + assert _read_vcpkg_ref_from_gitmodules(Path("deps/vcpkg")) == "2024.06.15" |
| 54 | + |
| 55 | + def test_gitmodules_multiple_submodules(self, tmp_path, monkeypatch): |
| 56 | + monkeypatch.chdir(tmp_path) |
| 57 | + (tmp_path / ".gitmodules").write_text( |
| 58 | + '[submodule "other"]\n' |
| 59 | + "\tpath = other\n" |
| 60 | + "\turl = https://github.com/example/other.git\n" |
| 61 | + "\tbranch = main\n" |
| 62 | + '[submodule "vcpkg"]\n' |
| 63 | + "\tpath = vcpkg\n" |
| 64 | + "\turl = https://github.com/microsoft/vcpkg.git\n" |
| 65 | + "\tbranch = 2024.01.12\n" |
| 66 | + ) |
| 67 | + assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == "2024.01.12" |
| 68 | + |
| 69 | + |
| 70 | +class TestVcpkgRefConfig: |
| 71 | + """Tests for vcpkg_ref configuration field.""" |
| 72 | + |
| 73 | + def test_default_vcpkg_ref_is_none(self): |
| 74 | + cfg = HatchCppVcpkgConfiguration() |
| 75 | + assert cfg.vcpkg_ref is None |
| 76 | + |
| 77 | + def test_explicit_vcpkg_ref(self): |
| 78 | + cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12") |
| 79 | + assert cfg.vcpkg_ref == "2024.01.12" |
| 80 | + |
| 81 | + def test_explicit_vcpkg_ref_commit_sha(self): |
| 82 | + sha = "a1b2c3d4e5f6" |
| 83 | + cfg = HatchCppVcpkgConfiguration(vcpkg_ref=sha) |
| 84 | + assert cfg.vcpkg_ref == sha |
| 85 | + |
| 86 | + |
| 87 | +class TestResolveVcpkgRef: |
| 88 | + """Tests for _resolve_vcpkg_ref priority logic.""" |
| 89 | + |
| 90 | + def test_explicit_ref_takes_priority_over_gitmodules(self, tmp_path, monkeypatch): |
| 91 | + monkeypatch.chdir(tmp_path) |
| 92 | + (tmp_path / ".gitmodules").write_text( |
| 93 | + '[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n' |
| 94 | + ) |
| 95 | + cfg = HatchCppVcpkgConfiguration(vcpkg_ref="my-custom-tag") |
| 96 | + assert cfg._resolve_vcpkg_ref() == "my-custom-tag" |
| 97 | + |
| 98 | + def test_falls_back_to_gitmodules(self, tmp_path, monkeypatch): |
| 99 | + monkeypatch.chdir(tmp_path) |
| 100 | + (tmp_path / ".gitmodules").write_text( |
| 101 | + '[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n' |
| 102 | + ) |
| 103 | + cfg = HatchCppVcpkgConfiguration() |
| 104 | + assert cfg._resolve_vcpkg_ref() == "2024.01.12" |
| 105 | + |
| 106 | + def test_returns_none_when_no_ref(self, tmp_path, monkeypatch): |
| 107 | + monkeypatch.chdir(tmp_path) |
| 108 | + cfg = HatchCppVcpkgConfiguration() |
| 109 | + assert cfg._resolve_vcpkg_ref() is None |
| 110 | + |
| 111 | + |
| 112 | +class TestVcpkgGenerate: |
| 113 | + """Tests that generate() includes the checkout command when a ref is set.""" |
| 114 | + |
| 115 | + def _make_vcpkg_env(self, tmp_path): |
| 116 | + """Create a minimal vcpkg.json so generate() produces commands.""" |
| 117 | + (tmp_path / "vcpkg.json").write_text("{}") |
| 118 | + |
| 119 | + def test_generate_with_explicit_ref(self, tmp_path, monkeypatch): |
| 120 | + monkeypatch.chdir(tmp_path) |
| 121 | + self._make_vcpkg_env(tmp_path) |
| 122 | + |
| 123 | + cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12") |
| 124 | + commands = cfg.generate(None) |
| 125 | + |
| 126 | + assert any("git clone" in cmd for cmd in commands) |
| 127 | + assert any("git -C vcpkg checkout 2024.01.12" in cmd for cmd in commands) |
| 128 | + # checkout must come after clone but before bootstrap |
| 129 | + clone_idx = next(i for i, c in enumerate(commands) if "git clone" in c) |
| 130 | + checkout_idx = next(i for i, c in enumerate(commands) if "checkout" in c) |
| 131 | + bootstrap_idx = next(i for i, c in enumerate(commands) if "bootstrap" in c) |
| 132 | + assert clone_idx < checkout_idx < bootstrap_idx |
| 133 | + |
| 134 | + def test_generate_with_gitmodules_ref(self, tmp_path, monkeypatch): |
| 135 | + monkeypatch.chdir(tmp_path) |
| 136 | + self._make_vcpkg_env(tmp_path) |
| 137 | + (tmp_path / ".gitmodules").write_text( |
| 138 | + '[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.06.15\n' |
| 139 | + ) |
| 140 | + |
| 141 | + cfg = HatchCppVcpkgConfiguration() |
| 142 | + commands = cfg.generate(None) |
| 143 | + |
| 144 | + assert any("git -C vcpkg checkout 2024.06.15" in cmd for cmd in commands) |
| 145 | + |
| 146 | + def test_generate_without_ref(self, tmp_path, monkeypatch): |
| 147 | + monkeypatch.chdir(tmp_path) |
| 148 | + self._make_vcpkg_env(tmp_path) |
| 149 | + |
| 150 | + cfg = HatchCppVcpkgConfiguration() |
| 151 | + commands = cfg.generate(None) |
| 152 | + |
| 153 | + assert not any("checkout" in cmd for cmd in commands) |
| 154 | + assert any("git clone" in cmd for cmd in commands) |
| 155 | + |
| 156 | + def test_generate_skips_clone_when_vcpkg_root_exists(self, tmp_path, monkeypatch): |
| 157 | + monkeypatch.chdir(tmp_path) |
| 158 | + self._make_vcpkg_env(tmp_path) |
| 159 | + (tmp_path / "vcpkg").mkdir() |
| 160 | + |
| 161 | + cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12") |
| 162 | + commands = cfg.generate(None) |
| 163 | + |
| 164 | + # When vcpkg_root already exists, no clone or checkout happens |
| 165 | + assert not any("git clone" in cmd for cmd in commands) |
| 166 | + assert not any("checkout" in cmd for cmd in commands) |
| 167 | + assert any("vcpkg" in cmd and "install" in cmd for cmd in commands) |
| 168 | + |
| 169 | + def test_generate_no_vcpkg_json(self, tmp_path, monkeypatch): |
| 170 | + monkeypatch.chdir(tmp_path) |
| 171 | + # No vcpkg.json => no commands at all |
| 172 | + cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12") |
| 173 | + commands = cfg.generate(None) |
| 174 | + assert commands == [] |
0 commit comments