Skip to content

Commit f0692f4

Browse files
committed
Migrate exception logic and tests to rust-side bootstrap
1 parent f4f00b4 commit f0692f4

File tree

4 files changed

+54
-113
lines changed

4 files changed

+54
-113
lines changed

Diff for: src/bootstrap/bootstrap.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -1270,9 +1270,8 @@ def bootstrap(args):
12701270
profile = RustBuild.get_toml_static(config_toml, "profile")
12711271
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
12721272

1273-
if profile is None:
1274-
if is_non_git_source:
1275-
profile = "dist"
1273+
if profile is None and is_non_git_source:
1274+
profile = "dist"
12761275

12771276
if profile is not None:
12781277
# Allows creating alias for profile names, allowing
@@ -1294,13 +1293,6 @@ def bootstrap(args):
12941293
with open(include_path) as included_toml:
12951294
config_toml += os.linesep + included_toml.read()
12961295

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-
13041296
# Configure initial bootstrap
13051297
build = RustBuild(config_toml, args)
13061298
build.check_vendored_status()

Diff for: src/bootstrap/bootstrap_test.py

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

Diff for: src/bootstrap/src/core/config/config.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2926,10 +2926,8 @@ impl Config {
29262926
let if_unchanged = || {
29272927
if self.rust_info.is_from_tarball() {
29282928
// Git is needed for running "if-unchanged" logic.
2929-
println!(
2930-
"WARNING: 'if-unchanged' has no effect on tarball sources; ignoring `download-ci-llvm`."
2931-
);
2932-
return false;
2929+
println!("ERROR: 'if-unchanged' is only compatible with Git managed sources.");
2930+
crate::exit!(1);
29332931
}
29342932

29352933
// Fetching the LLVM submodule is unnecessary for self-tests.

Diff for: src/bootstrap/src/core/config/tests.rs

+50
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,53 @@ fn check_rustc_if_unchanged_paths() {
454454
assert!(config.src.join(p).exists(), "{p} doesn't exist.");
455455
}
456456
}
457+
458+
#[test]
459+
fn override_profile_for_non_git_sources() {
460+
let source_path = "path/to/repo.tarball";
461+
let is_git_source = source_path.contains(".git");
462+
463+
if !is_git_source {
464+
let mut config = parse(&format!("rust_info.source_path = \"{}\"", source_path));
465+
466+
if config.profile.is_none() {
467+
config.profile = Some("dist".to_string());
468+
}
469+
assert_eq!(config.profile, Some("dist".to_string()));
470+
471+
if !is_git_source {
472+
assert_eq!(config.profile, Some("dist".to_string()));
473+
}
474+
}
475+
}
476+
477+
#[test]
478+
fn check_if_unchanged_on_non_git_sources() {
479+
let source_types = ["tarball", "other-source-type", "path/to/repo.git"];
480+
481+
for &source_type in source_types.iter() {
482+
let is_git_source = source_type.contains(".git");
483+
484+
if !is_git_source {
485+
let if_unchanged_config = parse("llvm.download-ci-llvm = \"if-unchanged\"");
486+
487+
if !is_git_source {
488+
println!(
489+
"ERROR: 'if-unchanged' is not compatible with {} sources. Aborting `download-ci-llvm`.",
490+
source_type
491+
);
492+
crate::exit!(1);
493+
}
494+
495+
let rustc_if_unchanged_config = parse("rust.download-rustc = \"if-unchanged\"");
496+
497+
if !is_git_source {
498+
println!(
499+
"ERROR: 'if-unchanged' is not compatible with {} sources. Aborting `download-rustc`.",
500+
source_type
501+
);
502+
crate::exit!(1);
503+
}
504+
}
505+
}
506+
}

0 commit comments

Comments
 (0)