feat(build-dir): Added validation for unmatched brackets in template

This commit is contained in:
Ross Sullivan 2025-04-11 20:50:54 +09:00
parent 2a11c26af2
commit ad3e593e53
No known key found for this signature in database
GPG Key ID: A7D198C212395322
3 changed files with 45 additions and 1 deletions

View File

@ -687,6 +687,16 @@ impl GlobalContext {
} => anyhow!(
"unexpected variable `{variable}` in build.build-dir path `{raw_template}`"
),
path::ResolveTemplateError::UnexpectedBracket { bracket_type, raw_template } => {
let (btype, literal) = match bracket_type {
path::BracketType::Opening => ("opening", "{"),
path::BracketType::Closing => ("closing", "}"),
};
anyhow!(
"unexpected {btype} bracket `{literal}` in build.build-dir path `{raw_template}`"
)
}
})?;
// Check if the target directory is set to an empty string in the config.toml file.

View File

@ -58,6 +58,20 @@ impl ConfigRelativePath {
});
};
if value.contains("{") {
return Err(ResolveTemplateError::UnexpectedBracket {
bracket_type: BracketType::Opening,
raw_template: self.0.val.clone(),
});
}
if value.contains("}") {
return Err(ResolveTemplateError::UnexpectedBracket {
bracket_type: BracketType::Closing,
raw_template: self.0.val.clone(),
});
}
Ok(self.0.definition.root(gctx).join(&value))
}
@ -139,4 +153,14 @@ pub enum ResolveTemplateError {
variable: String,
raw_template: String,
},
UnexpectedBracket {
bracket_type: BracketType,
raw_template: String,
},
}
#[derive(Debug)]
pub enum BracketType {
Opening,
Closing,
}

View File

@ -685,7 +685,7 @@ fn template_workspace_path_hash_should_handle_symlink() {
}
#[cargo_test]
fn template_should_handle_ignore_unmatched_brackets() {
fn template_should_handle_reject_unmatched_brackets() {
let p = project()
.file("src/lib.rs", "")
.file(
@ -699,6 +699,11 @@ fn template_should_handle_ignore_unmatched_brackets() {
p.cargo("build -Z build-dir")
.masquerade_as_nightly_cargo(&["build-dir"])
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] unexpected opening bracket `{` in build.build-dir path `foo/{bar`
"#]])
.run();
let p = project()
@ -714,6 +719,11 @@ fn template_should_handle_ignore_unmatched_brackets() {
p.cargo("build -Z build-dir")
.masquerade_as_nightly_cargo(&["build-dir"])
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] unexpected closing bracket `}` in build.build-dir path `foo/}bar`
"#]])
.run();
}