Skip to content

Commit db9794c

Browse files
committed
1 parent 047bc17 commit db9794c

File tree

2 files changed

+130
-4
lines changed

2 files changed

+130
-4
lines changed

src/bootstrap/bootstrap.py

Lines changed: 16 additions & 3 deletions
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

@@ -1264,6 +1264,12 @@ def bootstrap(args):
12641264
config_toml = ""
12651265

12661266
profile = RustBuild.get_toml_static(config_toml, "profile")
1267+
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
1268+
1269+
if profile is None:
1270+
if is_non_git_source:
1271+
profile = "dist"
1272+
12671273
if profile is not None:
12681274
# Allows creating alias for profile names, allowing
12691275
# profiles to be renamed while maintaining back compatibility
@@ -1284,6 +1290,13 @@ def bootstrap(args):
12841290
with open(include_path) as included_toml:
12851291
config_toml += os.linesep + included_toml.read()
12861292

1293+
if is_non_git_source:
1294+
for option in ["download-ci-llvm", "download-rustc"]:
1295+
if RustBuild.get_toml_static(config_toml, option):
1296+
raise Exception(
1297+
"Option '{}' cannot be used with non-git sources.".format(option)
1298+
)
1299+
12871300
# Configure initial bootstrap
12881301
build = RustBuild(config_toml, args)
12891302
build.check_vendored_status()

src/bootstrap/bootstrap_test.py

Lines changed: 114 additions & 1 deletion
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,116 @@ 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 the behavior for a non-git source
260+
mock_exists.return_value = False
261+
mock_get_toml_static.return_value = None
262+
263+
rust_root = "/mock/rust_root"
264+
config_toml = ""
265+
266+
# Simulate the execution of the logic
267+
profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
268+
269+
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
270+
if profile is None and is_non_git_source:
271+
profile = "dist"
272+
273+
# Assert that profile is set to "dist"
274+
self.assertEqual(profile, "dist")
275+
276+
@patch("os.path.exists")
277+
@patch("bootstrap.RustBuild.get_toml_static")
278+
def test_include_path_exists(self, mock_get_toml_static, mock_exists):
279+
# Mock the behavior for a git source
280+
mock_exists.return_value = True
281+
mock_get_toml_static.return_value = "dist"
282+
283+
rust_root = "/mock/rust_root"
284+
config_toml = "mock_config"
285+
286+
profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
287+
288+
# Profile alias check
289+
profile_aliases = {"user": "dist"}
290+
include_file = f"config.{profile_aliases.get(profile) or profile}.toml"
291+
include_dir = os.path.join(rust_root, "src", "bootstrap", "defaults")
292+
include_path = os.path.join(include_dir, include_file)
293+
294+
# Assert the include_path is correctly formed
295+
self.assertEqual(
296+
include_path, "/mock/rust_root/src/bootstrap/defaults/config.dist.toml"
297+
)
298+
299+
@patch("os.path.exists")
300+
@patch("bootstrap.RustBuild.get_toml_static")
301+
@patch("builtins.open", new_callable=mock_open)
302+
def test_config_toml_inclusion(self, mock_open, mock_get_toml_static, mock_exists):
303+
# Mock the behavior for a git source and profile dist
304+
mock_exists.side_effect = (
305+
lambda path: path
306+
== "/mock/rust_root/src/bootstrap/defaults/config.dist.toml"
307+
)
308+
mock_get_toml_static.return_value = "dist"
309+
mock_open.return_value.read.return_value = "included_toml_content"
310+
311+
rust_root = "/mock/rust_root"
312+
config_toml = "initial_config"
313+
314+
profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
315+
316+
# Profile alias check
317+
profile_aliases = {"user": "dist"}
318+
include_file = f"config.{profile_aliases.get(profile) or profile}.toml"
319+
include_dir = os.path.join(rust_root, "src", "bootstrap", "defaults")
320+
include_path = os.path.join(include_dir, include_file)
321+
322+
with open(include_path) as included_toml:
323+
config_toml += os.linesep + included_toml.read()
324+
325+
# Assert that the content of included_toml is appended correctly
326+
self.assertIn("initial_config\nincluded_toml_content", config_toml)
327+
328+
@patch("os.path.exists")
329+
@patch("bootstrap.RustBuild.get_toml_static")
330+
def test_invalid_profile_for_non_git_source(
331+
self, mock_get_toml_static, mock_exists
332+
):
333+
# Mock a non-git source
334+
mock_exists.return_value = False
335+
336+
# Mock config to simulate presence of options
337+
def mock_get_toml_static_side_effect(config_toml, option):
338+
if option in ["download-ci-llvm", "download-rustc"]:
339+
return "true" # Simulate the option being set in the config
340+
return None
341+
342+
mock_get_toml_static.side_effect = mock_get_toml_static_side_effect
343+
344+
rust_root = "/mock/rust_root"
345+
config_toml = ""
346+
347+
# Simulate the execution of the logic
348+
profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
349+
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
350+
if profile is None and is_non_git_source:
351+
profile = "dist"
352+
353+
# Assert that non-git sources cannot use these options
354+
for option in ["download-ci-llvm", "download-rustc"]:
355+
if bootstrap.RustBuild.get_toml_static(config_toml, option):
356+
# Now, raise the exception in the test code
357+
with self.assertRaises(Exception) as context:
358+
raise Exception(
359+
f"Option '{option}' cannot be used with non-git sources."
360+
)
361+
self.assertTrue(
362+
f"Option '{option}' cannot be used with non-git sources."
363+
in str(context.exception)
364+
)

0 commit comments

Comments
 (0)