Skip to content

Commit 375f9e1

Browse files
committed
Add Profile Override for Non-Git Sources
1 parent 3ff1b64 commit 375f9e1

File tree

2 files changed

+117
-4
lines changed

2 files changed

+117
-4
lines changed

src/bootstrap/bootstrap.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ def verify(path, expected, verbose):
186186
verified = found == expected
187187
if not verified:
188188
eprint(
189-
"invalid checksum:\n" " found: {}\n" " expected: {}".format(
190-
found, expected
191-
)
189+
"invalid checksum:\n"
190+
" found: {}\n"
191+
" expected: {}".format(found, expected)
192192
)
193193
return verified
194194

@@ -1268,6 +1268,12 @@ def bootstrap(args):
12681268
config_toml = ""
12691269

12701270
profile = RustBuild.get_toml_static(config_toml, "profile")
1271+
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
1272+
1273+
if profile is None:
1274+
if is_non_git_source:
1275+
profile = "dist"
1276+
12711277
if profile is not None:
12721278
# Allows creating alias for profile names, allowing
12731279
# profiles to be renamed while maintaining back compatibility
@@ -1288,6 +1294,13 @@ def bootstrap(args):
12881294
with open(include_path) as included_toml:
12891295
config_toml += os.linesep + included_toml.read()
12901296

1297+
if is_non_git_source:
1298+
for option in ["download-ci-llvm", "download-rustc"]:
1299+
if RustBuild.get_toml_static(config_toml, option):
1300+
raise Exception(
1301+
"Option '{}' cannot be used with non-git sources.".format(option)
1302+
)
1303+
12911304
# Configure initial bootstrap
12921305
build = RustBuild(config_toml, args)
12931306
build.check_vendored_status()

src/bootstrap/bootstrap_test.py

+101-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import absolute_import, division, print_function
66
import os
77
import unittest
8-
from unittest.mock import patch
8+
from unittest.mock import patch, mock_open
99
import tempfile
1010
import hashlib
1111
import sys
@@ -249,3 +249,103 @@ def test_warnings(self):
249249

250250
_, env = self.build_args(configure_args, args=["--warnings=deny"])
251251
self.assertTrue("-Dwarnings" in env["RUSTFLAGS"])
252+
253+
254+
class TestRustBuild(unittest.TestCase):
255+
256+
@patch("os.path.exists")
257+
@patch("bootstrap.RustBuild.get_toml_static")
258+
def test_profile_none_with_non_git_source(self, mock_get_toml_static, mock_exists):
259+
mock_exists.return_value = False
260+
mock_get_toml_static.return_value = None
261+
262+
rust_root = "/mock/rust_root"
263+
config_toml = ""
264+
265+
profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
266+
267+
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
268+
if profile is None and is_non_git_source:
269+
profile = "dist"
270+
271+
self.assertEqual(profile, "dist")
272+
273+
@patch("os.path.exists")
274+
@patch("bootstrap.RustBuild.get_toml_static")
275+
def test_include_path_exists(self, mock_get_toml_static, mock_exists):
276+
mock_exists.return_value = True
277+
mock_get_toml_static.return_value = "dist"
278+
279+
rust_root = "/mock/rust_root"
280+
config_toml = "mock_config"
281+
282+
profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
283+
284+
profile_aliases = {"user": "dist"}
285+
include_file = f"config.{profile_aliases.get(profile) or profile}.toml"
286+
include_dir = os.path.join(rust_root, "src", "bootstrap", "defaults")
287+
include_path = os.path.join(include_dir, include_file)
288+
289+
# Assert the include_path is correctly formed
290+
self.assertEqual(
291+
include_path, "/mock/rust_root/src/bootstrap/defaults/config.dist.toml"
292+
)
293+
294+
@patch("os.path.exists")
295+
@patch("bootstrap.RustBuild.get_toml_static")
296+
@patch("builtins.open", new_callable=mock_open)
297+
def test_config_toml_inclusion(self, mock_open, mock_get_toml_static, mock_exists):
298+
mock_exists.side_effect = (
299+
lambda path: path
300+
== "/mock/rust_root/src/bootstrap/defaults/config.dist.toml"
301+
)
302+
mock_get_toml_static.return_value = "dist"
303+
mock_open.return_value.read.return_value = "included_toml_content"
304+
305+
rust_root = "/mock/rust_root"
306+
config_toml = "initial_config"
307+
308+
profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
309+
310+
profile_aliases = {"user": "dist"}
311+
include_file = f"config.{profile_aliases.get(profile) or profile}.toml"
312+
include_dir = os.path.join(rust_root, "src", "bootstrap", "defaults")
313+
include_path = os.path.join(include_dir, include_file)
314+
315+
with open(include_path) as included_toml:
316+
config_toml += os.linesep + included_toml.read()
317+
318+
self.assertIn("initial_config\nincluded_toml_content", config_toml)
319+
320+
@patch("os.path.exists")
321+
@patch("bootstrap.RustBuild.get_toml_static")
322+
def test_invalid_profile_for_non_git_source(
323+
self, mock_get_toml_static, mock_exists
324+
):
325+
mock_exists.return_value = False
326+
327+
def mock_get_toml_static_side_effect(config_toml, option):
328+
if option in ["download-ci-llvm", "download-rustc"]:
329+
return "true"
330+
return None
331+
332+
mock_get_toml_static.side_effect = mock_get_toml_static_side_effect
333+
334+
rust_root = "/mock/rust_root"
335+
config_toml = ""
336+
337+
profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
338+
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
339+
if profile is None and is_non_git_source:
340+
profile = "dist"
341+
342+
for option in ["download-ci-llvm", "download-rustc"]:
343+
if bootstrap.RustBuild.get_toml_static(config_toml, option):
344+
with self.assertRaises(Exception) as context:
345+
raise Exception(
346+
f"Option '{option}' cannot be used with non-git sources."
347+
)
348+
self.assertTrue(
349+
f"Option '{option}' cannot be used with non-git sources."
350+
in str(context.exception)
351+
)

0 commit comments

Comments
 (0)