Skip to content

Commit

Permalink
Add Profile Override for Non-Git Sources
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvincible committed Jan 13, 2025
1 parent 3ff1b64 commit 375f9e1
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
19 changes: 16 additions & 3 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ def verify(path, expected, verbose):
verified = found == expected
if not verified:
eprint(
"invalid checksum:\n" " found: {}\n" " expected: {}".format(
found, expected
)
"invalid checksum:\n"
" found: {}\n"
" expected: {}".format(found, expected)
)
return verified

Expand Down Expand Up @@ -1268,6 +1268,12 @@ def bootstrap(args):
config_toml = ""

profile = RustBuild.get_toml_static(config_toml, "profile")
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))

if profile is None:
if is_non_git_source:
profile = "dist"

if profile is not None:
# Allows creating alias for profile names, allowing
# profiles to be renamed while maintaining back compatibility
Expand All @@ -1288,6 +1294,13 @@ def bootstrap(args):
with open(include_path) as included_toml:
config_toml += os.linesep + included_toml.read()

if is_non_git_source:
for option in ["download-ci-llvm", "download-rustc"]:
if RustBuild.get_toml_static(config_toml, option):
raise Exception(
"Option '{}' cannot be used with non-git sources.".format(option)
)

# Configure initial bootstrap
build = RustBuild(config_toml, args)
build.check_vendored_status()
Expand Down
102 changes: 101 additions & 1 deletion src/bootstrap/bootstrap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import absolute_import, division, print_function
import os
import unittest
from unittest.mock import patch
from unittest.mock import patch, mock_open
import tempfile
import hashlib
import sys
Expand Down Expand Up @@ -249,3 +249,103 @@ def test_warnings(self):

_, env = self.build_args(configure_args, args=["--warnings=deny"])
self.assertTrue("-Dwarnings" in env["RUSTFLAGS"])


class TestRustBuild(unittest.TestCase):

@patch("os.path.exists")
@patch("bootstrap.RustBuild.get_toml_static")
def test_profile_none_with_non_git_source(self, mock_get_toml_static, mock_exists):
mock_exists.return_value = False
mock_get_toml_static.return_value = None

rust_root = "/mock/rust_root"
config_toml = ""

profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")

is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
if profile is None and is_non_git_source:
profile = "dist"

self.assertEqual(profile, "dist")

@patch("os.path.exists")
@patch("bootstrap.RustBuild.get_toml_static")
def test_include_path_exists(self, mock_get_toml_static, mock_exists):
mock_exists.return_value = True
mock_get_toml_static.return_value = "dist"

rust_root = "/mock/rust_root"
config_toml = "mock_config"

profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")

profile_aliases = {"user": "dist"}
include_file = f"config.{profile_aliases.get(profile) or profile}.toml"
include_dir = os.path.join(rust_root, "src", "bootstrap", "defaults")
include_path = os.path.join(include_dir, include_file)

# Assert the include_path is correctly formed
self.assertEqual(
include_path, "/mock/rust_root/src/bootstrap/defaults/config.dist.toml"
)

@patch("os.path.exists")
@patch("bootstrap.RustBuild.get_toml_static")
@patch("builtins.open", new_callable=mock_open)
def test_config_toml_inclusion(self, mock_open, mock_get_toml_static, mock_exists):
mock_exists.side_effect = (
lambda path: path
== "/mock/rust_root/src/bootstrap/defaults/config.dist.toml"
)
mock_get_toml_static.return_value = "dist"
mock_open.return_value.read.return_value = "included_toml_content"

rust_root = "/mock/rust_root"
config_toml = "initial_config"

profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")

profile_aliases = {"user": "dist"}
include_file = f"config.{profile_aliases.get(profile) or profile}.toml"
include_dir = os.path.join(rust_root, "src", "bootstrap", "defaults")
include_path = os.path.join(include_dir, include_file)

with open(include_path) as included_toml:
config_toml += os.linesep + included_toml.read()

self.assertIn("initial_config\nincluded_toml_content", config_toml)

@patch("os.path.exists")
@patch("bootstrap.RustBuild.get_toml_static")
def test_invalid_profile_for_non_git_source(
self, mock_get_toml_static, mock_exists
):
mock_exists.return_value = False

def mock_get_toml_static_side_effect(config_toml, option):
if option in ["download-ci-llvm", "download-rustc"]:
return "true"
return None

mock_get_toml_static.side_effect = mock_get_toml_static_side_effect

rust_root = "/mock/rust_root"
config_toml = ""

profile = bootstrap.RustBuild.get_toml_static(config_toml, "profile")
is_non_git_source = not os.path.exists(os.path.join(rust_root, ".git"))
if profile is None and is_non_git_source:
profile = "dist"

for option in ["download-ci-llvm", "download-rustc"]:
if bootstrap.RustBuild.get_toml_static(config_toml, option):
with self.assertRaises(Exception) as context:
raise Exception(
f"Option '{option}' cannot be used with non-git sources."
)
self.assertTrue(
f"Option '{option}' cannot be used with non-git sources."
in str(context.exception)
)

0 comments on commit 375f9e1

Please sign in to comment.