-
Notifications
You must be signed in to change notification settings - Fork 512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow tempdir configuration (#1300) #1369
Conversation
Thanks for taking a crack at this! This should have tests. One suggestion is that the script could get the name of the script, extract the directory, write a file to that directory, and then after the tests assert that the file was created in the right location. The justfile for such a test will look something like this:
|
fb7e058
to
b87e0a8
Compare
I took your suggestion and I believe the two tests in Also not sure if these tests should have specific Let me know your thoughts about the new two tests. Thanks again for your time ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bunch of comments! Check 'em out. I think tempdir
(no dash) is better than temp_dir
.
src/analyzer.rs
Outdated
Setting::Tempdir(path) => { | ||
settings.temp_dir = Some(path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting::Tempdir(path) => { | |
settings.temp_dir = Some(path); | |
Setting::Tempdir(tempdir) => { | |
settings.tempdir = Some(tempdir); |
src/parser.rs
Outdated
@@ -786,13 +786,24 @@ impl<'tokens, 'src> Parser<'tokens, 'src> { | |||
value: Setting::WindowsShell(self.parse_shell()?), | |||
name, | |||
}) | |||
} else if name.lexeme() == Keyword::Tempdir.lexeme() { | |||
Ok(Set { | |||
value: Setting::Tempdir(self.parse_path()?), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can just be inlined, also the value of Seting::Tempdir
can be a String
:
value: Setting::Tempdir(self.parse_path()?), | |
value: Setting::Tempdir(self.parse_string_literal()?.cooked), |
src/parser.rs
Outdated
// Parse tempdir setting value | ||
fn parse_path(&mut self) -> CompileResult<'src, StringLiteral<'src>> { | ||
let path = self.parse_string_literal()?; | ||
Ok(path) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Parse tempdir setting value | |
fn parse_path(&mut self) -> CompileResult<'src, StringLiteral<'src>> { | |
let path = self.parse_string_literal()?; | |
Ok(path) | |
} |
src/recipe.rs
Outdated
let tmp = if let Some(temp_dir) = &context.settings.temp_dir { | ||
tempfile::Builder::new() | ||
.tempdir_in(temp_dir.raw) | ||
.map_err(|error| Error::TmpdirIo { | ||
recipe: self.name(), | ||
io_error: error, | ||
})? | ||
} else { | ||
tempfile::Builder::new() | ||
.prefix("just") | ||
.tempdir() | ||
.map_err(|error| Error::TmpdirIo { | ||
recipe: self.name(), | ||
io_error: error, | ||
})? | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like this to deduplicate a little bit, and preserve the temporary directory prefix:
let tmp = if let Some(temp_dir) = &context.settings.temp_dir { | |
tempfile::Builder::new() | |
.tempdir_in(temp_dir.raw) | |
.map_err(|error| Error::TmpdirIo { | |
recipe: self.name(), | |
io_error: error, | |
})? | |
} else { | |
tempfile::Builder::new() | |
.prefix("just") | |
.tempdir() | |
.map_err(|error| Error::TmpdirIo { | |
recipe: self.name(), | |
io_error: error, | |
})? | |
}; | |
let mut tempdir_builder = tempfile::Builder::new(); | |
tempdir_builder.prefix("just"); | |
let tempdir = match &context.settings.tempdir { | |
Some(tempdir) => tempdir_builder.tempdir_in(tempdir), | |
None => tempdir_builder.tempdir(), | |
}.map_err(|error| Error::TmpdirIo { | |
recipe: self.name(), | |
io_error: error, | |
})?; |
src/setting.rs
Outdated
@@ -8,6 +8,7 @@ pub(crate) enum Setting<'src> { | |||
Export(bool), | |||
PositionalArguments(bool), | |||
Shell(Shell<'src>), | |||
Tempdir(StringLiteral<'src>), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tempdir(StringLiteral<'src>), | |
Tempdir(String), |
src/setting.rs
Outdated
@@ -22,6 +23,7 @@ impl<'src> Display for Setting<'src> { | |||
| Setting::PositionalArguments(value) | |||
| Setting::WindowsPowerShell(value) => write!(f, "{}", value), | |||
Setting::Shell(shell) | Setting::WindowsShell(shell) => write!(f, "{}", shell), | |||
Setting::Tempdir(path) => write!(f, "{}", path), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting::Tempdir(path) => write!(f, "{}", path), | |
Setting::Tempdir(tempdir) => write!(f, "{}", tempdir), |
src/settings.rs
Outdated
@@ -13,6 +13,7 @@ pub(crate) struct Settings<'src> { | |||
pub(crate) ignore_comments: bool, | |||
pub(crate) positional_arguments: bool, | |||
pub(crate) shell: Option<Shell<'src>>, | |||
pub(crate) temp_dir: Option<StringLiteral<'src>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub(crate) temp_dir: Option<StringLiteral<'src>>, | |
pub(crate) tempdir: Option<String>, |
tests/json.rs
Outdated
@@ -44,6 +44,7 @@ fn alias() { | |||
"export": false, | |||
"positional_arguments": false, | |||
"shell": null, | |||
"temp_dir" : null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"temp_dir" : null, | |
"tempdir" : null, |
tests/misc.rs
Outdated
test! { | ||
name: custom_tempdir, | ||
justfile: r#" | ||
set tempdir := "." | ||
parent_tempdir := "." | ||
foo: | ||
#!/usr/bin/env bash | ||
script_tempdir=$(basename "$(dirname $0)") | ||
script_foo=`realpath {{parent_tempdir}}`/$script_tempdir/foo | ||
[[ -f $script_foo ]] && echo "foo_exists" || echo "foo_missing" | ||
"#, | ||
stdout: "foo_exists\n", | ||
} | ||
|
||
test! { | ||
name: standard_tempdir, | ||
justfile: r#" | ||
standard_parent_tempdir := "/tmp" | ||
foo: | ||
#!/usr/bin/env bash | ||
script_tempdir=$(basename "$(dirname $0)") | ||
script_foo={{standard_parent_tempdir}}/$script_tempdir/foo | ||
[[ -f $script_foo ]] && echo "foo_exists" || echo "foo_missing" | ||
"#, | ||
stdout: "foo_exists\n", | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments:
- There are still a bunch of macro-based tests, but I've been moving away from them. For new tests it's best to use the
Test
struct. Check out some of the other tests. - These tests can go in their own file, perhaps
tests/tempdir.rs
. realpath
isn't available on MacOS.- The test can be simplified with something like this:
This will only succeed if the temporary directory is the same directory as the current directory, which will only be true if the tempdir has been set:
set tempdir := "."
foo:
touch $(basename "$(dirname $0)")/bar
cat bar
- I think the
standard_tempdir
test can be removed.
Regarding running these tests on windows, we should avoid using any unix-specific commands so they can run anywhere. |
b87e0a8
to
95f67f7
Compare
95f67f7
to
08af5dd
Compare
Thanks for the feedback on all the files. I have changed them accordingly and added the tests which now should address what you have mentioned, and used the Thanks again for your time! |
Nice merged! I added a small change, which is to interpret the tempdir path relative to the working directory, not the invocation directory. |
Thanks so much for the guidance and adding the small tweaks. Hope to keep contributing in the near future to this repo :) |
You are most welcome! Thank you for the PR! |
Hi,
This PR addresses issue #1300 to allow configuring
tempdir
as an additional setting.I am fairly new at Rust, and wanted to tackle this issue as it seemed like a good challenge. Please let me know if something is just completely wrong, and I will gladly try to change it to get it right. But as far as I could see, it does what it intends on doing. Most of the changes are just to make the other tests happy again.
Looking forward to it! And thanks for this awesome tool!