|
5 | 5 | from __future__ import absolute_import, division, print_function
|
6 | 6 | import os
|
7 | 7 | import unittest
|
8 |
| -from unittest.mock import patch |
| 8 | +from unittest.mock import patch, mock_open |
9 | 9 | import tempfile
|
10 | 10 | import hashlib
|
11 | 11 | import sys
|
@@ -249,3 +249,103 @@ def test_warnings(self):
|
249 | 249 |
|
250 | 250 | _, env = self.build_args(configure_args, args=["--warnings=deny"])
|
251 | 251 | 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