-
Notifications
You must be signed in to change notification settings - Fork 510
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
UUID and sha256 digest functions #1170
Conversation
Thanks for this PR! There's already For the hash functions, can you use the implementation from https://github.com/RustCrypto/hashes? I think it's better maintained. |
Hi, thanks for your review. I have now added stdout_regex in the sense of the existing stderr_regex. I agree, much less complexity that way. Also I have replaced the hashing dependency with rust-crypto. Hope this helps. |
I'm not sure whether the general name digest()/digest_file() for the functions is too broad. Any requests for a better more concise name? :-) |
I think I agree, |
Ok, they are now renamed to the more specific |
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.
Nice, looking good. I added a few minor comments.
README.md
Outdated
@@ -1016,6 +1016,12 @@ These functions can fail, for example if a path does not have an extension, whic | |||
|
|||
- `error(message)` - Abort execution and report error `message` to user. | |||
|
|||
#### UUID and Hash Generation | |||
|
|||
- `sha256(textdata)` Generates a sha256 hash of the given text data. Example: `sha256("just hello")` |
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.
- `sha256(textdata)` Generates a sha256 hash of the given text data. Example: `sha256("just hello")` | |
- `sha256(string)` Return the SHA-256 hash of `string` as a hexadecimal string. |
README.md
Outdated
#### UUID and Hash Generation | ||
|
||
- `sha256(textdata)` Generates a sha256 hash of the given text data. Example: `sha256("just hello")` | ||
- `sha256_file(path)` Generates a sha256 hash of the given file. Example: `sha256_file("/path/to/file")` |
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.
- `sha256_file(path)` Generates a sha256 hash of the given file. Example: `sha256_file("/path/to/file")` | |
- `sha256_file(path)` Return the SHA-256 hash of the file at `path` as a hexadecimal string. |
README.md
Outdated
|
||
- `sha256(textdata)` Generates a sha256 hash of the given text data. Example: `sha256("just hello")` | ||
- `sha256_file(path)` Generates a sha256 hash of the given file. Example: `sha256_file("/path/to/file")` | ||
- `uuid()` - Randomly generates a hyphenated UUID string. |
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.
- `uuid()` - Randomly generates a hyphenated UUID string. | |
- `uuid()` - Return a randomly generated UUID. |
src/function.rs
Outdated
let mut file = std::fs::File::open(path).unwrap(); | ||
std::io::copy(&mut file, &mut hasher).unwrap(); |
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.
These unwraps should both be handled. Something like:
let mut file = std::fs::File::open(path).unwrap(); | |
std::io::copy(&mut file, &mut hasher).unwrap(); | |
let mut file = std::fs::File::open(path).map_err(|err| format!("Failed to open file at `{}`: {}", path, err))?; | |
std::io::copy(&mut file, &mut hasher).map_err(|err| format!("Failed to read file at `{}`: {}", path, err)?; |
tests/functions.rs
Outdated
#[test] | ||
fn sha256_file() { | ||
let mut tmpfile = tempfile::NamedTempFile::new().unwrap(); | ||
writeln!(tmpfile, "just is great").unwrap(); |
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.
❤️
tests/functions.rs
Outdated
let justfile_content = format!("x := sha256_file('{}')", tmppath.to_str().unwrap()); | ||
|
||
Test::new() | ||
.justfile(justfile_content) |
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.
You can use the somewhat odd tree
function to populate the tempdir with files:
.justfile(justfile_content) | |
.justfile("x := sha256_file('file')") | |
.tree(tree! { | |
file: "just is great", | |
}) |
Thanks for the extensive review, I think I've covered all your issues now, feel free to check again |
Signed-off-by: mbodmer <[email protected]>
Signed-off-by: mbodmer <[email protected]>
Signed-off-by: mbodmer <[email protected]>
Signed-off-by: mbodmer <[email protected]>
Signed-off-by: mbodmer <[email protected]>
Signed-off-by: mbodmer <[email protected]>
Signed-off-by: Marc Bodmer <[email protected]>
according to casey's review Signed-off-by: mbodmer <[email protected]>
according to casey's review Signed-off-by: mbodmer <[email protected]>
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.
Nice, looks good, although I just realized that there's a bug. just
doesn't change the working directory, so you'll need to join the path
in sha256_file
with context.search.working_directory
, i.e. context.search.working_directory.join(path)
.
Could you also add a test for this? It should fail on the current PR, and then pass once path
is joined with the working directory. You can use Test::current_dir
to make just start in a sub-directory of the tmpdir. You can try to do sha256_file
on a file in tempdir.
Sorry for force pushing, I've had to rewrite the history to rebase the branch, since you've also modified the lockfile and stupid me checked it in, in a commit together with other changes. |
Lets say you have the following files:
And you're currently in
You do:
The |
Ok, I think now I understand, thanks. Will try |
Signed-off-by: mbodmer <[email protected]>
Signed-off-by: mbodmer <[email protected]>
Signed-off-by: mbodmer <[email protected]>
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.
Looks good! I went ahead and removed a _
that is now unnecessary.
Thanks |
issue #1018 mentioned a uuid function would be helpful.
Here is my attempt to deliver this. Also I've included sha256 hashing functions for strings and files.
To create a meaningful unittest for uuid() I had to refactor the Test struct in test.rs, I hope this is acceptable. The challenge was to keep the current interface but allow for custom verification. Well, here is my attempt. I'm new to rust, so bear with me.