Skip to content

Commit

Permalink
Override no-cd setting with [cd] attribute
Browse files Browse the repository at this point in the history
Addresses casey#2291
  • Loading branch information
artm committed Aug 25, 2024
1 parent 56a9831 commit dcc3be3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::*;
pub(crate) enum Attribute<'src> {
Confirm(Option<StringLiteral<'src>>),
Doc(Option<StringLiteral<'src>>),
Cd,
Extension(StringLiteral<'src>),
Group(StringLiteral<'src>),
Linux,
Expand All @@ -32,6 +33,7 @@ impl AttributeDiscriminant {
Self::Group | Self::Extension => 1..=1,
Self::Linux
| Self::Macos
| Self::Cd
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
Expand Down Expand Up @@ -79,6 +81,7 @@ impl<'src> Attribute<'src> {
AttributeDiscriminant::Group => Self::Group(arguments.into_iter().next().unwrap()),
AttributeDiscriminant::Linux => Self::Linux,
AttributeDiscriminant::Macos => Self::Macos,
AttributeDiscriminant::Cd => Self::Cd,
AttributeDiscriminant::NoCd => Self::NoCd,
AttributeDiscriminant::NoExitMessage => Self::NoExitMessage,
AttributeDiscriminant::NoQuiet => Self::NoQuiet,
Expand Down Expand Up @@ -115,6 +118,7 @@ impl<'src> Display for Attribute<'src> {
| Self::Doc(None)
| Self::Linux
| Self::Macos
| Self::Cd
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
Expand Down
8 changes: 3 additions & 5 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ impl<'src, D> Recipe<'src, D> {
settings.positional_arguments || self.attributes.contains(&Attribute::PositionalArguments)
}

pub(crate) fn change_directory(&self) -> bool {
!self.attributes.contains(&Attribute::NoCd)
}

pub(crate) fn enabled(&self) -> bool {
let windows = self.attributes.contains(&Attribute::Windows);
let linux = self.attributes.contains(&Attribute::Linux);
Expand All @@ -133,7 +129,9 @@ impl<'src, D> Recipe<'src, D> {
}

fn working_directory<'a>(&'a self, context: &'a ExecutionContext) -> Option<PathBuf> {
if self.change_directory() && context.change_directory() {
if context.change_directory() && !self.attributes.contains(&Attribute::NoCd)
|| self.attributes.contains(&Attribute::Cd)
{
Some(context.working_directory())
} else {
None
Expand Down
22 changes: 22 additions & 0 deletions tests/working_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,28 @@ fn no_cd_setting() {
.run();
}

#[test]
fn cd_attr_overrides_no_cd_setting() {
Test::new()
.current_dir("justfile_dir/start_dir")
.tree(tree! {
justfile_dir: {
"justfile": "
set no-cd
[cd]
default:
basename $PWD
",
start_dir: {
}
}
})
.stderr("basename $PWD\n")
.stdout("justfile_dir\n")
.run();
}

#[test]
fn working_dir_in_submodule_is_relative_to_module_path() {
Test::new()
Expand Down

0 comments on commit dcc3be3

Please sign in to comment.