summaryrefslogtreecommitdiffstats
path: root/src/tools/cargo/tests/testsuite/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/cargo/tests/testsuite/config.rs')
-rw-r--r--src/tools/cargo/tests/testsuite/config.rs109
1 files changed, 97 insertions, 12 deletions
diff --git a/src/tools/cargo/tests/testsuite/config.rs b/src/tools/cargo/tests/testsuite/config.rs
index 7078fc445..e5078bd8e 100644
--- a/src/tools/cargo/tests/testsuite/config.rs
+++ b/src/tools/cargo/tests/testsuite/config.rs
@@ -2,8 +2,9 @@
use cargo::core::{PackageIdSpec, Shell};
use cargo::util::config::{self, Config, Definition, JobsConfig, SslVersionConfig, StringList};
-use cargo::util::interning::InternedString;
-use cargo::util::toml::{self as cargo_toml, TomlDebugInfo, VecStringOrBool as VSOB};
+use cargo::util::toml::schema::TomlTrimPaths;
+use cargo::util::toml::schema::TomlTrimPathsValue;
+use cargo::util::toml::schema::{self as cargo_toml, TomlDebugInfo, VecStringOrBool as VSOB};
use cargo::CargoResult;
use cargo_test_support::compare;
use cargo_test_support::{panic_error, paths, project, symlink_supported, t};
@@ -21,6 +22,7 @@ pub struct ConfigBuilder {
unstable: Vec<String>,
config_args: Vec<String>,
cwd: Option<PathBuf>,
+ root: Option<PathBuf>,
enable_nightly_features: bool,
}
@@ -30,6 +32,7 @@ impl ConfigBuilder {
env: HashMap::new(),
unstable: Vec::new(),
config_args: Vec::new(),
+ root: None,
cwd: None,
enable_nightly_features: false,
}
@@ -60,8 +63,28 @@ impl ConfigBuilder {
}
/// Sets the current working directory where config files will be loaded.
+ ///
+ /// Default is the root from [`ConfigBuilder::root`] or [`paths::root`].
pub fn cwd(&mut self, path: impl AsRef<Path>) -> &mut Self {
- self.cwd = Some(paths::root().join(path.as_ref()));
+ let path = path.as_ref();
+ let cwd = self
+ .root
+ .as_ref()
+ .map_or_else(|| paths::root().join(path), |r| r.join(path));
+ self.cwd = Some(cwd);
+ self
+ }
+
+ /// Sets the test root directory.
+ ///
+ /// This generally should not be necessary. It is only useful if you want
+ /// to create a `Config` from within a thread. Since Cargo's testsuite
+ /// uses thread-local storage, this can be used to avoid accessing that
+ /// thread-local storage.
+ ///
+ /// Default is [`paths::root`].
+ pub fn root(&mut self, path: impl Into<PathBuf>) -> &mut Self {
+ self.root = Some(path.into());
self
}
@@ -72,14 +95,15 @@ impl ConfigBuilder {
/// Creates the `Config`, returning a Result.
pub fn build_err(&self) -> CargoResult<Config> {
- let output = Box::new(fs::File::create(paths::root().join("shell.out")).unwrap());
+ let root = self.root.clone().unwrap_or_else(|| paths::root());
+ let output = Box::new(fs::File::create(root.join("shell.out")).unwrap());
let shell = Shell::from_write(output);
- let cwd = self.cwd.clone().unwrap_or_else(|| paths::root());
- let homedir = paths::home();
+ let cwd = self.cwd.clone().unwrap_or_else(|| root.clone());
+ let homedir = root.join("home").join(".cargo");
let mut config = Config::new(shell, cwd, homedir);
config.nightly_features_allowed = self.enable_nightly_features || !self.unstable.is_empty();
config.set_env(self.env.clone());
- config.set_search_stop_path(paths::root());
+ config.set_search_stop_path(&root);
config.configure(
0,
false,
@@ -422,8 +446,8 @@ lto = false
p,
cargo_toml::TomlProfile {
lto: Some(cargo_toml::StringOrBool::Bool(false)),
- dir_name: Some(InternedString::new("without-lto")),
- inherits: Some(InternedString::new("dev")),
+ dir_name: Some(String::from("without-lto")),
+ inherits: Some(String::from("dev")),
..Default::default()
}
);
@@ -1503,7 +1527,7 @@ fn all_profile_options() {
let base_settings = cargo_toml::TomlProfile {
opt_level: Some(cargo_toml::TomlOptLevel("0".to_string())),
lto: Some(cargo_toml::StringOrBool::String("thin".to_string())),
- codegen_backend: Some(InternedString::new("example")),
+ codegen_backend: Some(String::from("example")),
codegen_units: Some(123),
debug: Some(cargo_toml::TomlDebugInfo::Limited),
split_debuginfo: Some("packed".to_string()),
@@ -1512,12 +1536,13 @@ fn all_profile_options() {
panic: Some("abort".to_string()),
overflow_checks: Some(true),
incremental: Some(true),
- dir_name: Some(InternedString::new("dir_name")),
- inherits: Some(InternedString::new("debug")),
+ dir_name: Some(String::from("dir_name")),
+ inherits: Some(String::from("debug")),
strip: Some(cargo_toml::StringOrBool::String("symbols".to_string())),
package: None,
build_override: None,
rustflags: None,
+ trim_paths: None,
};
let mut overrides = BTreeMap::new();
let key = cargo_toml::ProfilePackageSpec::Spec(PackageIdSpec::parse("foo").unwrap());
@@ -1705,3 +1730,63 @@ jobs = 2
JobsConfig::Integer(v) => assert_eq!(v, 2),
}
}
+
+#[cargo_test]
+fn trim_paths_parsing() {
+ let config = ConfigBuilder::new().build();
+ let p: cargo_toml::TomlProfile = config.get("profile.dev").unwrap();
+ assert_eq!(p.trim_paths, None);
+
+ let test_cases = [
+ (TomlTrimPathsValue::Diagnostics.into(), "diagnostics"),
+ (TomlTrimPathsValue::Macro.into(), "macro"),
+ (TomlTrimPathsValue::Object.into(), "object"),
+ ];
+ for (expected, val) in test_cases {
+ // env
+ let config = ConfigBuilder::new()
+ .env("CARGO_PROFILE_DEV_TRIM_PATHS", val)
+ .build();
+ let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
+ assert_eq!(trim_paths, expected, "failed to parse {val}");
+
+ // config.toml
+ let config = ConfigBuilder::new()
+ .config_arg(format!("profile.dev.trim-paths='{val}'"))
+ .build();
+ let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
+ assert_eq!(trim_paths, expected, "failed to parse {val}");
+ }
+
+ let test_cases = [(TomlTrimPaths::none(), false), (TomlTrimPaths::All, true)];
+
+ for (expected, val) in test_cases {
+ // env
+ let config = ConfigBuilder::new()
+ .env("CARGO_PROFILE_DEV_TRIM_PATHS", format!("{val}"))
+ .build();
+ let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
+ assert_eq!(trim_paths, expected, "failed to parse {val}");
+
+ // config.toml
+ let config = ConfigBuilder::new()
+ .config_arg(format!("profile.dev.trim-paths={val}"))
+ .build();
+ let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
+ assert_eq!(trim_paths, expected, "failed to parse {val}");
+ }
+
+ let expected = vec![
+ TomlTrimPathsValue::Diagnostics,
+ TomlTrimPathsValue::Macro,
+ TomlTrimPathsValue::Object,
+ ]
+ .into();
+ let val = r#"["diagnostics", "macro", "object"]"#;
+ // config.toml
+ let config = ConfigBuilder::new()
+ .config_arg(format!("profile.dev.trim-paths={val}"))
+ .build();
+ let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
+ assert_eq!(trim_paths, expected, "failed to parse {val}");
+}