1
1
mirror of https://github.com/casey/just.git synced 2024-11-22 10:26:26 +03:00

review comments

This commit is contained in:
Ben Heidemann 2024-10-31 19:36:42 +00:00
parent d034fd1510
commit 18ff6b5705
3 changed files with 59 additions and 27 deletions

View File

@ -1082,6 +1082,7 @@ impl<'run, 'src> Parser<'run, 'src> {
&mut self, &mut self,
) -> CompileResult<'src, Option<(Token<'src>, BTreeSet<Attribute<'src>>)>> { ) -> CompileResult<'src, Option<(Token<'src>, BTreeSet<Attribute<'src>>)>> {
let mut attributes = BTreeMap::new(); let mut attributes = BTreeMap::new();
let mut working_directory_attribute_line = None;
let mut token = None; let mut token = None;
@ -1108,6 +1109,17 @@ impl<'run, 'src> Parser<'run, 'src> {
let attribute = Attribute::new(name, arguments)?; let attribute = Attribute::new(name, arguments)?;
if let Attribute::WorkingDirectory(_) = &attribute {
if let Some(line) = working_directory_attribute_line {
return Err(name.error(CompileErrorKind::DuplicateAttribute {
attribute: name.lexeme(),
first: line,
}));
}
working_directory_attribute_line = Some(name.line);
}
if let Some(line) = attributes.get(&attribute) { if let Some(line) = attributes.get(&attribute) {
return Err(name.error(CompileErrorKind::DuplicateAttribute { return Err(name.error(CompileErrorKind::DuplicateAttribute {
attribute: name.lexeme(), attribute: name.lexeme(),

View File

@ -135,20 +135,15 @@ impl<'src, D> Recipe<'src, D> {
return None; return None;
} }
let working_dir = self let working_directory = context.working_directory();
.attributes
.iter()
.filter_map(|attribute| match attribute {
Attribute::WorkingDirectory(dir) => Some(dir),
_ => None,
})
.last();
Some( for attribute in &self.attributes {
working_dir if let Attribute::WorkingDirectory(dir) = attribute {
.map(|dir| context.working_directory().join(dir.raw)) return Some(working_directory.join(&dir.cooked));
.unwrap_or(context.working_directory()), }
) }
Some(working_directory)
} }
fn no_quiet(&self) -> bool { fn no_quiet(&self) -> bool {

View File

@ -333,22 +333,13 @@ file := shell('cat file.txt')
} }
#[test] #[test]
fn attribute() { fn attribute_duplicate() {
Test::new() Test::new()
.justfile( .justfile(
r#" r#"
[working-directory('bar')]
print1:
echo "$(basename "$PWD")"
[working-directory('bar')] [working-directory('bar')]
[working-directory('baz')] [working-directory('baz')]
print2: print:
echo "$(basename "$PWD")"
[working-directory('bar')]
[no-cd]
print3:
echo "$(basename "$PWD")" echo "$(basename "$PWD")"
"#, "#,
) )
@ -358,14 +349,48 @@ fn attribute() {
bar: {}, bar: {},
baz: {}, baz: {},
}) })
.args(["print1", "print2", "print3"]) .args(["print"])
.stderr(
r#"error: Recipe attribute `working-directory` first used on line 1 is duplicated on line 2
justfile:2:2
2 [working-directory('baz')]
^^^^^^^^^^^^^^^^^
"#,
)
.stdout("")
.status(1)
.run();
}
#[test]
fn attribute() {
Test::new()
.justfile(
r#"
[working-directory('bar')]
print1:
echo "$(basename "$PWD")"
[working-directory('baz')]
[no-cd]
print2:
echo "$(basename "$PWD")"
"#,
)
.current_dir("foo")
.tree(tree! {
foo: {},
bar: {},
baz: {},
})
.args(["print1", "print2"])
.stderr( .stderr(
r#"echo "$(basename "$PWD")" r#"echo "$(basename "$PWD")"
echo "$(basename "$PWD")" echo "$(basename "$PWD")"
echo "$(basename "$PWD")"
"#, "#,
) )
.stdout("bar\nbaz\nfoo\n") .stdout("bar\nfoo\n")
.run(); .run();
} }