Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Nov 27, 2024
1 parent 89687aa commit 08546b8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 57 deletions.
5 changes: 5 additions & 0 deletions src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ impl Display for CompileError<'_> {
consist of tabs or spaces, but not both",
ShowWhitespace(whitespace)
),
NoCdAndWorkingDirectoryAttribute { recipe } => write!(
f,
"Recipe `{recipe}` has both `[no-cd]` and `[working-directory]` attributes"
),
ParameterFollowsVariadicParameter { parameter } => {
write!(f, "Parameter `{parameter}` follows variadic parameter")
}
Expand Down Expand Up @@ -286,6 +290,7 @@ impl Display for CompileError<'_> {
UnterminatedBacktick => write!(f, "Unterminated backtick"),
UnterminatedInterpolation => write!(f, "Unterminated interpolation"),
UnterminatedString => write!(f, "Unterminated string"),
UnterminatedString => write!(f, "Unterminated string"),
}
}
}
3 changes: 3 additions & 0 deletions src/compile_error_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ pub(crate) enum CompileErrorKind<'src> {
MixedLeadingWhitespace {
whitespace: &'src str,
},
NoCdAndWorkingDirectoryAttribute {
recipe: &'src str,
},
ParameterFollowsVariadicParameter {
parameter: &'src str,
},
Expand Down
16 changes: 16 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,22 @@ impl<'run, 'src> Parser<'run, 'src> {
}));
}

let working_directory = attributes
.iter()
.any(|attribute| matches!(attribute, Attribute::WorkingDirectory(_)));

if working_directory {
for attribute in &attributes {
if let Attribute::NoCd = attribute {
return Err(
name.error(CompileErrorKind::NoCdAndWorkingDirectoryAttribute {
recipe: name.lexeme(),
}),
);
}
}
}

let private = name.lexeme().starts_with('_') || attributes.contains(&Attribute::Private);

let mut doc = doc.map(ToOwned::to_owned);
Expand Down
22 changes: 21 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub(crate) struct Test {
pub(crate) args: Vec<String>,
pub(crate) current_dir: PathBuf,
pub(crate) env: BTreeMap<String, String>,
pub(crate) expected_files: BTreeMap<PathBuf, Vec<u8>>,
pub(crate) justfile: Option<String>,
pub(crate) shell: bool,
pub(crate) status: i32,
Expand All @@ -71,6 +72,7 @@ impl Test {
args: Vec::new(),
current_dir: PathBuf::new(),
env: BTreeMap::new(),
expected_files: BTreeMap::new(),
justfile: Some(String::new()),
shell: true,
status: EXIT_SUCCESS,
Expand Down Expand Up @@ -98,7 +100,7 @@ impl Test {
}

pub(crate) fn create_dir(self, path: impl AsRef<Path>) -> Self {
fs::create_dir_all(self.tempdir.path().join(path.as_ref())).unwrap();
fs::create_dir_all(self.tempdir.path().join(path)).unwrap();
self
}

Expand Down Expand Up @@ -195,6 +197,14 @@ impl Test {
fs::write(path, content).unwrap();
self
}

pub(crate) fn expect_file(mut self, path: impl AsRef<Path>, content: impl AsRef<[u8]>) -> Self {
let path = path.as_ref();
self
.expected_files
.insert(path.into(), content.as_ref().into());
self
}
}

impl Test {
Expand Down Expand Up @@ -283,6 +293,16 @@ impl Test {
panic!("Output mismatch.");
}

for (path, expected) in &self.expected_files {
let actual = fs::read(self.tempdir.path().join(path)).unwrap();
assert_eq!(
actual,
expected.as_slice(),
"mismatch for expected file at path {}",
path.display(),
);
}

if self.test_round_trip && self.status == EXIT_SUCCESS {
self.round_trip();
}
Expand Down
97 changes: 41 additions & 56 deletions tests/working_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,20 +336,12 @@ file := shell('cat file.txt')
fn attribute_duplicate() {
Test::new()
.justfile(
r#"
[working-directory('bar')]
[working-directory('baz')]
print:
echo "$(basename "$PWD")"
"#,
"
[working-directory('bar')]
[working-directory('baz')]
foo:
",
)
.current_dir("foo")
.tree(tree! {
foo: {},
bar: {},
baz: {},
})
.args(["print"])
.stderr(
"error: Recipe attribute `working-directory` first used on line 1 is duplicated on line 2
——▶ justfile:2:2
Expand All @@ -358,68 +350,61 @@ fn attribute_duplicate() {
│ ^^^^^^^^^^^^^^^^^
",
)
.stdout("")
.status(1)
.status(EXIT_FAILURE)
.run();
}

#[test]
fn attribute() {
Test::new()
.justfile(
r#"
[working-directory('bar')]
print1:
echo "$(basename "$PWD")"
"
[working-directory('foo')]
@qux:
echo baz > bar
",
)
.create_dir("foo")
.expect_file("foo/bar", "baz\n")
.run();
}

[working-directory('baz')]
[no-cd]
print2:
echo "$(basename "$PWD")"
"#,
#[test]
fn attribute_with_nocd_is_forbidden() {
Test::new()
.justfile(
"
[working-directory('foo')]
[no-cd]
bar:
",
)
.current_dir("foo")
.tree(tree! {
foo: {},
bar: {},
baz: {},
})
.args(["print1", "print2"])
.stderr(
r#"echo "$(basename "$PWD")"
echo "$(basename "$PWD")"
"#,
"
error: Recipe `bar` has both `[no-cd]` and `[working-directory]` attributes
——▶ justfile:3:1
3 │ bar:
│ ^^^
",
)
.stdout("bar\nfoo\n")
.status(EXIT_FAILURE)
.run();
}

#[test]
fn setting_and_attribute() {
Test::new()
.justfile(
r#"
set working-directory := 'bar'
"
set working-directory := 'foo'
[working-directory('baz')]
print1:
echo "$(basename "$PWD")"
echo "$(basename "$(dirname "$PWD")")"
"#,
)
.current_dir("foo")
.tree(tree! {
foo: {},
bar: {
baz: {},
},
})
.args(["print1"])
.stderr(
r#"echo "$(basename "$PWD")"
echo "$(basename "$(dirname "$PWD")")"
"#,
[working-directory('bar')]
@baz:
echo bob > fred
",
)
.stdout("baz\nbar\n")
.create_dir("foo/bar")
.expect_file("foo/bar/fred", "bob\n")
.run();
}

0 comments on commit 08546b8

Please sign in to comment.