Skip to content
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

Merged
merged 13 commits into from
May 4, 2022
93 changes: 93 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ansi_term = "0.12.0"
atty = "0.2.0"
camino = "1.0.4"
clap = { version = "2.33.0", features = ["wrap_help"] }
ctrlc = { version = "3.1.1", features = ["termination"] }
ctrlc = { version = "3.1.1", features = ["termination"] }
derivative = "2.0.0"
dotenv = "0.15.0"
edit-distance = "2.0.0"
Expand All @@ -32,13 +32,15 @@ log = "0.4.4"
regex = "1.5.4"
serde = { version = "1.0.130", features = ["derive", "rc"] }
serde_json = "1.0.68"
sha2 = "0.10"
similar = { version = "2.1.0", features = ["unicode"] }
snafu = "0.7.0"
strum = { version = "0.24.0", features = ["derive"] }
strum = { version = "0.24.0", features = ["derive"] }
target = "2.0.0"
tempfile = "3.0.0"
typed-arena = "2.0.1"
unicode-width = "0.1.0"
uuid = { version = "1.0.0", features = ["v4"] }

[dev-dependencies]
cradle = "0.2.0"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,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(string)` - Return the SHA-256 hash of `string` as a hexadecimal string.
- `sha256_file(path)` - Return the SHA-256 hash of the file at `path` as a hexadecimal string.
- `uuid()` - Return a randomly generated UUID.

### Command Evaluation Using Backticks

Backticks can be used to store the result of commands:
Expand Down
27 changes: 27 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ lazy_static! {
("path_exists", Unary(path_exists)),
("quote", Unary(quote)),
("replace", Ternary(replace)),
("sha256", Unary(sha256)),
("sha256_file", Unary(sha256_file)),
("trim", Unary(trim)),
("trim_end", Unary(trim_end)),
("trim_end_match", Binary(trim_end_match)),
Expand All @@ -43,6 +45,7 @@ lazy_static! {
("trim_start_match", Binary(trim_start_match)),
("trim_start_matches", Binary(trim_start_matches)),
("uppercase", Unary(uppercase)),
("uuid", Nullary(uuid)),
("without_extension", Unary(without_extension)),
]
.into_iter()
Expand Down Expand Up @@ -247,6 +250,26 @@ fn replace(_context: &FunctionContext, s: &str, from: &str, to: &str) -> Result<
Ok(s.replace(from, to))
}

fn sha256(_context: &FunctionContext, s: &str) -> Result<String, String> {
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(s);
let hash = hasher.finalize();
Ok(format!("{:x}", hash))
}

fn sha256_file(_context: &FunctionContext, path: &str) -> Result<String, String> {
use sha2::{Digest, Sha256};
let justpath = _context.search.working_directory.join(path);
casey marked this conversation as resolved.
Show resolved Hide resolved
let mut hasher = Sha256::new();
let mut file = std::fs::File::open(&justpath)
.map_err(|err| format!("Failed to open file at `{:?}`: {}", &justpath.to_str(), err))?;
std::io::copy(&mut file, &mut hasher)
.map_err(|err| format!("Failed to read file at `{:?}`: {}", &justpath.to_str(), err))?;
let hash = hasher.finalize();
Ok(format!("{:x}", hash))
}

fn trim(_context: &FunctionContext, s: &str) -> Result<String, String> {
Ok(s.trim().to_owned())
}
Expand Down Expand Up @@ -279,6 +302,10 @@ fn uppercase(_context: &FunctionContext, s: &str) -> Result<String, String> {
Ok(s.to_uppercase())
}

fn uuid(_context: &FunctionContext) -> Result<String, String> {
Ok(uuid::Uuid::new_v4().to_string())
}

fn without_extension(_context: &FunctionContext, path: &str) -> Result<String, String> {
let parent = Utf8Path::new(path)
.parent()
Expand Down
33 changes: 33 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,36 @@ fn path_exists_subdir() {
.stdout("true")
.run();
}

#[test]
fn uuid() {
Test::new()
.justfile("x := uuid()")
.args(&["--evaluate", "x"])
.stdout_regex("........-....-....-....-............")
.run();
}

#[test]
fn sha256() {
Test::new()
.justfile("x := sha256('5943ee37-0000-1000-8000-010203040506')")
.args(&["--evaluate", "x"])
.stdout("2330d7f5eb94a820b54fed59a8eced236f80b633a504289c030b6a65aef58871")
.run();
}

#[test]
fn sha256_file() {
Test::new()
.justfile("x := sha256_file('sub/shafile')")
.tree(tree! {
sub: {
shafile: "just is great\n",
}
})
.current_dir("sub")
.args(&["--evaluate", "x"])
.stdout("177b3d79aaafb53a7a4d7aaba99a82f27c73370e8cb0295571aade1e4fea1cd2")
.run();
}
21 changes: 20 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ macro_rules! test {
$(env: { $($env_key:literal : $env_value:literal,)* },)?
$(stdin: $stdin:expr,)?
$(stdout: $stdout:expr,)?
$(stdout_regex: $stdout_regex:expr,)?
$(stderr: $stderr:expr,)?
$(stderr_regex: $stderr_regex:expr,)?
$(status: $status:expr,)?
Expand All @@ -28,6 +29,7 @@ macro_rules! test {
$(let test = test.stderr_regex($stderr_regex);)?
$(let test = test.stdin($stdin);)?
$(let test = test.stdout($stdout);)?
$(let test = test.stdout_regex($stdout_regex);)?

test.run();
}
Expand All @@ -45,6 +47,7 @@ pub(crate) struct Test {
pub(crate) stderr_regex: Option<Regex>,
pub(crate) stdin: String,
pub(crate) stdout: String,
pub(crate) stdout_regex: Option<Regex>,
pub(crate) tempdir: TempDir,
pub(crate) unindent_stdout: bool,
}
Expand All @@ -66,6 +69,7 @@ impl Test {
stderr_regex: None,
stdin: String::new(),
stdout: String::new(),
stdout_regex: None,
tempdir,
unindent_stdout: true,
}
Expand Down Expand Up @@ -137,6 +141,11 @@ impl Test {
self
}

pub(crate) fn stdout_regex(mut self, stdout_regex: impl AsRef<str>) -> Self {
self.stdout_regex = Some(Regex::new(&format!("(?m)^{}$", stdout_regex.as_ref())).unwrap());
self
}

pub(crate) fn tree(self, mut tree: Tree) -> Self {
tree.map(|_name, content| unindent(content));
tree.instantiate(self.tempdir.path()).unwrap();
Expand Down Expand Up @@ -203,8 +212,18 @@ impl Test {
equal
}

let output_stdout = str::from_utf8(&output.stdout).unwrap();
let output_stderr = str::from_utf8(&output.stderr).unwrap();

if let Some(ref stdout_regex) = self.stdout_regex {
if !stdout_regex.is_match(output_stdout) {
panic!(
"Stdout regex mismatch:\n{:?}\n!~=\n/{:?}/",
output_stderr, stdout_regex
);
}
}

if let Some(ref stderr_regex) = self.stderr_regex {
if !stderr_regex.is_match(output_stderr) {
panic!(
Expand All @@ -215,7 +234,7 @@ impl Test {
}

if !compare("status", output.status.code().unwrap(), self.status)
| !compare("stdout", str::from_utf8(&output.stdout).unwrap(), &stdout)
| (self.stdout_regex.is_none() && !compare("stdout", output_stdout, &stdout))
| (self.stderr_regex.is_none() && !compare("stderr", output_stderr, &stderr))
{
panic!("Output mismatch.");
Expand Down