From 2e9e4ab77ad8fb419d924494686ce722c8b65c77 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 2 Aug 2024 12:03:16 -0700 Subject: [PATCH 1/4] Make paths in function relative to correct working directory --- src/function.rs | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/src/function.rs b/src/function.rs index 013af683f8..a714a8d0fd 100644 --- a/src/function.rs +++ b/src/function.rs @@ -133,8 +133,7 @@ fn absolute_path(context: Context, path: &str) -> FunctionResult { let abs_path_unchecked = context .evaluator .context - .search - .working_directory + .working_directory() .join(path) .lexiclean(); match abs_path_unchecked.to_str() { @@ -164,12 +163,7 @@ fn blake3(_context: Context, s: &str) -> FunctionResult { } fn blake3_file(context: Context, path: &str) -> FunctionResult { - let path = context - .evaluator - .context - .search - .working_directory - .join(path); + let path = context.evaluator.context.working_directory().join(path); let mut hasher = blake3::Hasher::new(); hasher .update_mmap_rayon(&path) @@ -177,9 +171,9 @@ fn blake3_file(context: Context, path: &str) -> FunctionResult { Ok(hasher.finalize().to_string()) } -fn canonicalize(_context: Context, path: &str) -> FunctionResult { - let canonical = - std::fs::canonicalize(path).map_err(|err| format!("I/O error canonicalizing path: {err}"))?; +fn canonicalize(context: Context, path: &str) -> FunctionResult { + let canonical = std::fs::canonicalize(context.evaluator.context.working_directory().join(path)) + .map_err(|err| format!("I/O error canonicalizing path: {err}"))?; canonical.to_str().map(str::to_string).ok_or_else(|| { format!( @@ -522,8 +516,7 @@ fn path_exists(context: Context, path: &str) -> FunctionResult { context .evaluator .context - .search - .working_directory + .working_directory() .join(path) .exists() .to_string(), @@ -557,12 +550,7 @@ fn sha256(_context: Context, s: &str) -> FunctionResult { fn sha256_file(context: Context, path: &str) -> FunctionResult { use sha2::{Digest, Sha256}; - let path = context - .evaluator - .context - .search - .working_directory - .join(path); + let path = context.evaluator.context.working_directory().join(path); let mut hasher = Sha256::new(); let mut file = fs::File::open(&path).map_err(|err| format!("Failed to open `{}`: {err}", path.display()))?; From b2b37ec92519732065fdf033c70e9e77655e1733 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 2 Aug 2024 12:12:03 -0700 Subject: [PATCH 2/4] Add tests --- tests/functions.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/functions.rs b/tests/functions.rs index 4aaa61dd2c..0d7dee214e 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -1093,3 +1093,93 @@ fn invocation_dir_native_abbreviation_is_accepted() { ) .run(); } + +#[test] +fn absolute_path_argument_is_relative_to_submodule_working_directory() { + Test::new() + .justfile("mod foo") + .write("foo/baz", "") + .write( + "foo/mod.just", + " +bar: + @echo {{ absolute_path('baz') }} + +", + ) + .stdout_regex(r".*[/\\]foo[/\\]baz\n") + .args(["foo", "bar"]) + .run(); +} + +#[test] +fn blake3_file_argument_is_relative_to_submodule_working_directory() { + Test::new() + .justfile("mod foo") + .write("foo/baz", "") + .write( + "foo/mod.just", + " +bar: + @echo {{ blake3_file('baz') }} + +", + ) + .stdout("af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262\n") + .args(["foo", "bar"]) + .run(); +} + +#[test] +fn sha256_file_argument_is_relative_to_submodule_working_directory() { + Test::new() + .justfile("mod foo") + .write("foo/baz", "") + .write( + "foo/mod.just", + " +bar: + @echo {{ sha256_file('baz') }} + +", + ) + .stdout_regex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n") + .args(["foo", "bar"]) + .run(); +} + +#[test] +fn path_exists_argument_is_relative_to_submodule_working_directory() { + Test::new() + .justfile("mod foo") + .write("foo/baz", "") + .write( + "foo/mod.just", + " +bar: + @echo {{ path_exists('baz') }} + +", + ) + .stdout_regex("true\n") + .args(["foo", "bar"]) + .run(); +} + +#[test] +fn canonicalize_argument_is_relative_to_submodule_working_directory() { + Test::new() + .justfile("mod foo") + .write("foo/baz", "") + .write( + "foo/mod.just", + " +bar: + @echo {{ canonicalize('baz') }} + +", + ) + .stdout_regex(r".*[/\\]foo[/\\]baz\n") + .args(["foo", "bar"]) + .run(); +} From 5242c5c75b81851cf58c6c0b717deece3431a591 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 2 Aug 2024 12:14:03 -0700 Subject: [PATCH 3/4] Revise --- tests/functions.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/functions.rs b/tests/functions.rs index 0d7dee214e..999a60a578 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -1131,7 +1131,7 @@ bar: } #[test] -fn sha256_file_argument_is_relative_to_submodule_working_directory() { +fn canonicalize_argument_is_relative_to_submodule_working_directory() { Test::new() .justfile("mod foo") .write("foo/baz", "") @@ -1139,11 +1139,11 @@ fn sha256_file_argument_is_relative_to_submodule_working_directory() { "foo/mod.just", " bar: - @echo {{ sha256_file('baz') }} + @echo {{ canonicalize('baz') }} ", ) - .stdout_regex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n") + .stdout_regex(r".*[/\\]foo[/\\]baz\n") .args(["foo", "bar"]) .run(); } @@ -1167,7 +1167,7 @@ bar: } #[test] -fn canonicalize_argument_is_relative_to_submodule_working_directory() { +fn sha256_file_argument_is_relative_to_submodule_working_directory() { Test::new() .justfile("mod foo") .write("foo/baz", "") @@ -1175,11 +1175,11 @@ fn canonicalize_argument_is_relative_to_submodule_working_directory() { "foo/mod.just", " bar: - @echo {{ canonicalize('baz') }} + @echo {{ sha256_file('baz') }} ", ) - .stdout_regex(r".*[/\\]foo[/\\]baz\n") + .stdout_regex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n") .args(["foo", "bar"]) .run(); } From e5fb8b39195b76824215e5554ee9cf788ceb6bdd Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 2 Aug 2024 12:17:30 -0700 Subject: [PATCH 4/4] Adapt --- tests/functions.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/functions.rs b/tests/functions.rs index 999a60a578..76964b74b4 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -1101,11 +1101,11 @@ fn absolute_path_argument_is_relative_to_submodule_working_directory() { .write("foo/baz", "") .write( "foo/mod.just", - " + r#" bar: - @echo {{ absolute_path('baz') }} + @echo "{{ absolute_path('baz') }}" -", +"#, ) .stdout_regex(r".*[/\\]foo[/\\]baz\n") .args(["foo", "bar"]) @@ -1137,11 +1137,11 @@ fn canonicalize_argument_is_relative_to_submodule_working_directory() { .write("foo/baz", "") .write( "foo/mod.just", - " + r#" bar: - @echo {{ canonicalize('baz') }} + @echo "{{ canonicalize('baz') }}" -", +"#, ) .stdout_regex(r".*[/\\]foo[/\\]baz\n") .args(["foo", "bar"])