Skip to content

Commit

Permalink
Set per-recipe workdir with [cd(DIR)] attribute
Browse files Browse the repository at this point in the history
Addresses casey#2082
  • Loading branch information
artm committed Aug 25, 2024
1 parent dcc3be3 commit a0c1bf9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::*;
pub(crate) enum Attribute<'src> {
Confirm(Option<StringLiteral<'src>>),
Doc(Option<StringLiteral<'src>>),
Cd,
Cd(Option<StringLiteral<'src>>),
Extension(StringLiteral<'src>),
Group(StringLiteral<'src>),
Linux,
Expand All @@ -29,11 +29,10 @@ pub(crate) enum Attribute<'src> {
impl AttributeDiscriminant {
fn argument_range(self) -> RangeInclusive<usize> {
match self {
Self::Confirm | Self::Doc => 0..=1,
Self::Confirm | Self::Doc | Self::Cd => 0..=1,
Self::Group | Self::Extension => 1..=1,
Self::Linux
| Self::Macos
| Self::Cd
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
Expand Down Expand Up @@ -77,11 +76,11 @@ impl<'src> Attribute<'src> {
Ok(match discriminant {
AttributeDiscriminant::Confirm => Self::Confirm(arguments.into_iter().next()),
AttributeDiscriminant::Doc => Self::Doc(arguments.into_iter().next()),
AttributeDiscriminant::Cd => Self::Cd(arguments.into_iter().next()),
AttributeDiscriminant::Extension => Self::Extension(arguments.into_iter().next().unwrap()),
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 @@ -111,14 +110,15 @@ impl<'src> Display for Attribute<'src> {
match self {
Self::Confirm(Some(argument))
| Self::Doc(Some(argument))
| Self::Cd(Some(argument))
| Self::Extension(argument)
| Self::Group(argument) => write!(f, "({argument})")?,
Self::Script(Some(shell)) => write!(f, "({shell})")?,
Self::Confirm(None)
| Self::Doc(None)
| Self::Cd(None)
| Self::Linux
| Self::Macos
| Self::Cd
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
Expand Down
11 changes: 8 additions & 3 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@ impl<'src, D> Recipe<'src, D> {
}

fn working_directory<'a>(&'a self, context: &'a ExecutionContext) -> Option<PathBuf> {
if context.change_directory() && !self.attributes.contains(&Attribute::NoCd)
|| self.attributes.contains(&Attribute::Cd)
{
for attribute in &self.attributes {
if let Attribute::Cd(Some(directory)) = attribute {
return Some(PathBuf::from(&directory.cooked));
} else if let Attribute::Cd(None) = attribute {
return Some(context.working_directory());
}
}
if context.change_directory() && !self.attributes.contains(&Attribute::NoCd) {
Some(context.working_directory())
} else {
None
Expand Down
22 changes: 20 additions & 2 deletions tests/working_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,33 @@ fn cd_attr_overrides_no_cd_setting() {
default:
basename $PWD
",
start_dir: {
}
start_dir: {}
}
})
.stderr("basename $PWD\n")
.stdout("justfile_dir\n")
.run();
}

#[test]
fn cd_attr_with_path() {
Test::new()
.current_dir("justfile_dir")
.tree(tree! {
justfile_dir: {
"justfile": r#"
[cd("desired_work_dir")]
default:
basename $PWD
"#,
desired_work_dir: {}
}
})
.stderr("basename $PWD\n")
.stdout("desired_work_dir\n")
.run();
}

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

0 comments on commit a0c1bf9

Please sign in to comment.