Skip to content

Commit

Permalink
Implement no-cd setting
Browse files Browse the repository at this point in the history
Addresses casey#2291
  • Loading branch information
artm committed Aug 25, 2024
1 parent f25e2d7 commit 56a9831
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions GRAMMAR.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ setting : 'allow-duplicate-recipes' boolean?
| 'export' boolean?
| 'fallback' boolean?
| 'ignore-comments' boolean?
| 'no-cd' boolean?
| 'positional-arguments' boolean?
| 'script-interpreter' ':=' string_list
| 'quiet' boolean?
Expand Down
4 changes: 4 additions & 0 deletions src/execution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub(crate) struct ExecutionContext<'src: 'run, 'run> {
}

impl<'src: 'run, 'run> ExecutionContext<'src, 'run> {
pub(crate) fn change_directory(&self) -> bool {
!self.module.settings.no_cd
}

pub(crate) fn working_directory(&self) -> PathBuf {
let base = if self.module.is_submodule() {
&self.module.working_directory
Expand Down
1 change: 1 addition & 0 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) enum Keyword {
False,
If,
IgnoreComments,
NoCd,
Import,
Mod,
PositionalArguments,
Expand Down
3 changes: 2 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ impl<'src> Node<'src> for Set<'src> {
| Setting::Quiet(value)
| Setting::Unstable(value)
| Setting::WindowsPowerShell(value)
| Setting::IgnoreComments(value) => {
| Setting::IgnoreComments(value)
| Setting::NoCd(value) => {
set.push_mut(value.to_string());
}
Setting::ScriptInterpreter(Interpreter { command, arguments })
Expand Down
1 change: 1 addition & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ impl<'run, 'src> Parser<'run, 'src> {
Keyword::Export => Some(Setting::Export(self.parse_set_bool()?)),
Keyword::Fallback => Some(Setting::Fallback(self.parse_set_bool()?)),
Keyword::IgnoreComments => Some(Setting::IgnoreComments(self.parse_set_bool()?)),
Keyword::NoCd => Some(Setting::NoCd(self.parse_set_bool()?)),
Keyword::PositionalArguments => Some(Setting::PositionalArguments(self.parse_set_bool()?)),
Keyword::Quiet => Some(Setting::Quiet(self.parse_set_bool()?)),
Keyword::Unstable => Some(Setting::Unstable(self.parse_set_bool()?)),
Expand Down
2 changes: 1 addition & 1 deletion src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'src, D> Recipe<'src, D> {
}

fn working_directory<'a>(&'a self, context: &'a ExecutionContext) -> Option<PathBuf> {
if self.change_directory() {
if self.change_directory() && context.change_directory() {
Some(context.working_directory())
} else {
None
Expand Down
2 changes: 2 additions & 0 deletions src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) enum Setting<'src> {
Export(bool),
Fallback(bool),
IgnoreComments(bool),
NoCd(bool),
PositionalArguments(bool),
Quiet(bool),
ScriptInterpreter(Interpreter<'src>),
Expand All @@ -32,6 +33,7 @@ impl<'src> Display for Setting<'src> {
| Self::Export(value)
| Self::Fallback(value)
| Self::IgnoreComments(value)
| Self::NoCd(value)
| Self::PositionalArguments(value)
| Self::Quiet(value)
| Self::Unstable(value)
Expand Down
4 changes: 4 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) struct Settings<'src> {
pub(crate) export: bool,
pub(crate) fallback: bool,
pub(crate) ignore_comments: bool,
pub(crate) no_cd: bool,
pub(crate) positional_arguments: bool,
pub(crate) quiet: bool,
#[serde(skip)]
Expand Down Expand Up @@ -61,6 +62,9 @@ impl<'src> Settings<'src> {
Setting::IgnoreComments(ignore_comments) => {
settings.ignore_comments = ignore_comments;
}
Setting::NoCd(no_cd) => {
settings.no_cd = no_cd;
}
Setting::PositionalArguments(positional_arguments) => {
settings.positional_arguments = positional_arguments;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn alias() {
"tempdir" : null,
"unstable": false,
"ignore_comments": false,
"no_cd": false,
"unstable": false,
"windows_powershell": false,
"windows_shell": null,
Expand Down Expand Up @@ -99,6 +100,7 @@ fn assignment() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -153,6 +155,7 @@ fn private_assignment() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -211,6 +214,7 @@ fn body() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -281,6 +285,7 @@ fn dependencies() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -389,6 +394,7 @@ fn dependency_argument() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -459,6 +465,7 @@ fn duplicate_recipes() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -507,6 +514,7 @@ fn duplicate_variables() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -558,6 +566,7 @@ fn doc_comment() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -595,6 +604,7 @@ fn empty_justfile() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -753,6 +763,7 @@ fn parameters() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -844,6 +855,7 @@ fn priors() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -895,6 +907,7 @@ fn private() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -946,6 +959,7 @@ fn quiet() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -1009,6 +1023,7 @@ fn settings() {
"export": true,
"fallback": true,
"ignore_comments": true,
"no_cd": false,
"positional_arguments": true,
"quiet": true,
"shell": {
Expand Down Expand Up @@ -1066,6 +1081,7 @@ fn shebang() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -1117,6 +1133,7 @@ fn simple() {
"export": false,
"fallback": false,
"ignore_comments": false,
"no_cd": false,
"positional_arguments": false,
"quiet": false,
"shell": null,
Expand Down Expand Up @@ -1176,6 +1193,7 @@ fn attribute() {
"tempdir" : null,
"unstable": false,
"ignore_comments": false,
"no_cd": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
Expand Down Expand Up @@ -1245,6 +1263,7 @@ fn module() {
"tempdir" : null,
"unstable": false,
"ignore_comments": false,
"no_cd": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
Expand All @@ -1269,6 +1288,7 @@ fn module() {
"tempdir" : null,
"unstable": false,
"ignore_comments": false,
"no_cd": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
Expand Down Expand Up @@ -1340,6 +1360,7 @@ fn module_group() {
"tempdir" : null,
"unstable": false,
"ignore_comments": false,
"no_cd": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
Expand All @@ -1364,6 +1385,7 @@ fn module_group() {
"tempdir" : null,
"unstable": false,
"ignore_comments": false,
"no_cd": false,
"windows_powershell": false,
"windows_shell": null,
"working_directory" : null,
Expand Down
20 changes: 20 additions & 0 deletions tests/working_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,26 @@ fn no_cd_overrides_setting() {
.run();
}

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

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

0 comments on commit 56a9831

Please sign in to comment.