Skip to content

Commit

Permalink
Add workdir setting
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Jul 29, 2024
1 parent b70546a commit 576308d
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions GRAMMAR.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ setting : 'allow-duplicate-recipes' boolean?
| 'unstable' boolean?
| 'windows-powershell' boolean?
| 'windows-shell' ':=' string_list
| 'workdir' ':=' string
boolean : ':=' ('true' | 'false')
Expand Down
1 change: 1 addition & 0 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) enum Keyword {
Unstable,
WindowsPowershell,
WindowsShell,
Workdir,
X,
}

Expand Down
5 changes: 4 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ impl<'src> Node<'src> for Set<'src> {
set.push_mut(Tree::string(&argument.cooked));
}
}
Setting::DotenvFilename(value) | Setting::DotenvPath(value) | Setting::Tempdir(value) => {
Setting::DotenvFilename(value)
| Setting::DotenvPath(value)
| Setting::Tempdir(value)
| Setting::Workdir(value) => {
set.push_mut(Tree::string(&value.cooked));
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ impl<'run, 'src> Parser<'run, 'src> {
Keyword::Shell => Some(Setting::Shell(self.parse_interpreter()?)),
Keyword::Tempdir => Some(Setting::Tempdir(self.parse_string_literal()?)),
Keyword::WindowsShell => Some(Setting::WindowsShell(self.parse_interpreter()?)),
Keyword::Workdir => Some(Setting::Workdir(self.parse_string_literal()?)),
_ => None,
};

Expand Down Expand Up @@ -2146,6 +2147,12 @@ mod tests {
tree: (justfile (set windows_powershell false)),
}

test! {
name: set_workdir,
text: "set workdir := 'my-work-dir'",
tree: (justfile (set workdir "my-work-dir")),
}

test! {
name: conditional,
text: "a := if b == c { d } else { e }",
Expand Down
6 changes: 5 additions & 1 deletion src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(crate) enum Setting<'src> {
Unstable(bool),
WindowsPowerShell(bool),
WindowsShell(Interpreter<'src>),
Workdir(StringLiteral<'src>),
}

impl<'src> Display for Setting<'src> {
Expand All @@ -38,7 +39,10 @@ impl<'src> Display for Setting<'src> {
Self::ScriptInterpreter(shell) | Self::Shell(shell) | Self::WindowsShell(shell) => {
write!(f, "[{shell}]")
}
Self::DotenvFilename(value) | Self::DotenvPath(value) | Self::Tempdir(value) => {
Self::DotenvFilename(value)
| Self::DotenvPath(value)
| Self::Tempdir(value)
| Self::Workdir(value) => {
write!(f, "{value}")
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) struct Settings<'src> {
pub(crate) unstable: bool,
pub(crate) windows_powershell: bool,
pub(crate) windows_shell: Option<Interpreter<'src>>,
pub(crate) workdir: Option<String>,
}

impl<'src> Settings<'src> {
Expand Down Expand Up @@ -84,6 +85,9 @@ impl<'src> Settings<'src> {
Setting::Tempdir(tempdir) => {
settings.tempdir = Some(tempdir.cooked);
}
Setting::Workdir(workdir) => {
settings.workdir = Some(workdir.cooked);
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn alias() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -105,6 +106,7 @@ fn assignment() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -162,6 +164,7 @@ fn body() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -231,6 +234,7 @@ fn dependencies() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -338,6 +342,7 @@ fn dependency_argument() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -407,6 +412,7 @@ fn duplicate_recipes() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -454,6 +460,7 @@ fn duplicate_variables() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -504,6 +511,7 @@ fn doc_comment() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -540,6 +548,7 @@ fn empty_justfile() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -697,6 +706,7 @@ fn parameters() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -787,6 +797,7 @@ fn priors() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -837,6 +848,7 @@ fn private() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -887,6 +899,7 @@ fn quiet() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -952,6 +965,7 @@ fn settings() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1005,6 +1019,7 @@ fn shebang() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1055,6 +1070,7 @@ fn simple() {
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1108,6 +1124,7 @@ fn attribute() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1176,6 +1193,7 @@ fn module() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand All @@ -1199,6 +1217,7 @@ fn module() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down Expand Up @@ -1269,6 +1288,7 @@ fn module_group() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand All @@ -1292,6 +1312,7 @@ fn module_group() {
"ignore_comments": false,
"windows_powershell": false,
"windows_shell": null,
"workdir" : null,
},
"unexports": [],
"warnings": [],
Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ mod unstable;
mod windows;
#[cfg(target_family = "windows")]
mod windows_shell;
mod workdir;
mod working_directory;

fn path(s: &str) -> String {
Expand Down
30 changes: 30 additions & 0 deletions tests/workdir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::*;

#[test]
fn workdir() {
Test::new()
.justfile(
r#"
set workdir := 'bar'
print1:
echo "$(basename "$PWD")"
[no-cd]
print2:
echo "$(basename "$PWD")"
"#,
)
.current_dir("foo")
.tree(tree! {
foo: {},
bar: {}
})
.args(["print1", "print2"])
.stderr(
r#"echo "$(basename "$PWD")"
echo "$(basename "$PWD")"
"#,
)
.stdout("bar\nfoo\n").run();
}

0 comments on commit 576308d

Please sign in to comment.