npub1xu…g43rm on Nostr: use std::process::Command; use std::fs; use sha2::{Digest, Sha256}; fn ...
use std::process::Command;
use std::fs;
use sha2::{Digest, Sha256};
fn calculate_sha256(file_path: &str) -> String {
let content = fs::read(file_path).expect("Unable to read file");
let mut hasher = Sha256::new();
hasher.update(&content);
hasher.finalize()
.iter()
.map(|b| format!("{:02x}", b))
.collect()
}
#[test]
fn test_get_file_hash_binary_no_features() {
let output = Command::new("cargo")
.arg("run")
.arg("--bin")
.arg("get_file_hash")
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
// Assert that the command ran successfully
assert!(output.status.success(), "Command failed with stderr: {}", stderr);
// Manually calculate the hash of the binary's source file
let expected_hash = calculate_sha256("src/bin/get_file_hash.rs");
// Assert that the output contains the correct hash
// Check for the raw hash first
assert!(stdout.contains(&expected_hash), "Output did not contain raw expected hash. Expected: {}, Actual: {}", expected_hash, stdout);
// Then check for the formatted string, including backticks
// Use a regex-like check for more flexibility with newlines if needed, or refine to exact match
let expected_hash_line = format!("* **SHA-256 Hash:** `{}`", expected_hash);
assert!(stdout.contains(&expected_hash_line), "Output did not contain expected hash line. Expected line: {}, Actual: {}", expected_hash_line, stdout);
// Assert that the output contains "Integrity Verified."
assert!(stdout.contains("Integrity Verified."), "Output did not contain 'Integrity Verified.'. stdout: {}", stdout);
println!("Output from get_file_hash binary (no features):
{}", stdout);
}
#[test]
fn test_get_file_hash_binary_with_nostr_feature() {
let output = Command::new("cargo")
.arg("run")
.arg("--bin")
.arg("get_file_hash")
.arg("--features")
.arg("nostr")
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
// Assert that the command ran successfully
assert!(output.status.success(), "Command failed with stderr: {}", stderr);
// Manually calculate the hash of the binary's source file
let expected_hash = calculate_sha256("src/bin/get_file_hash.rs");
// Assert that the output contains the correct hash
assert!(stdout.contains(&expected_hash), "Output did not contain raw expected hash. Expected: {}, Actual: {}", expected_hash, stdout);
// Then check for the formatted string, including backticks
let expected_hash_line = format!("* **SHA-256 Hash:** `{}`", expected_hash);
assert!(stdout.contains(&expected_hash_line), "Output did not contain expected hash line. Expected line: {}, Actual: {}", expected_hash_line, stdout);
// Assert that the output contains "Integrity Verified."
assert!(stdout.contains("Integrity Verified."), "Output did not contain 'Integrity Verified.'. stdout: {}", stdout);
println!("Output from get_file_hash binary (with nostr feature):
{}", stdout);
}
Published at
2026-04-04 01:41:36 UTCEvent JSON
{
"id": "c9cacd08197545b75d14878e256e76a5ad01c5ac1495b9c3836ba520a5ddd2de",
"pubkey": "37306250e62aafbe17f1fd1ee25a539f268e044d7516734c6e389c6de7273ac9",
"created_at": 1775266896,
"kind": 1,
"tags": [
[
"file",
"tests/integration_tests.rs"
],
[
"version",
"0.3.3"
]
],
"content": "use std::process::Command;\nuse std::fs;\nuse sha2::{Digest, Sha256};\n\nfn calculate_sha256(file_path: \u0026str) -\u003e String {\n let content = fs::read(file_path).expect(\"Unable to read file\");\n let mut hasher = Sha256::new();\n hasher.update(\u0026content);\n hasher.finalize()\n .iter()\n .map(|b| format!(\"{:02x}\", b))\n .collect()\n}\n\n#[test]\nfn test_get_file_hash_binary_no_features() {\n let output = Command::new(\"cargo\")\n .arg(\"run\")\n .arg(\"--bin\")\n .arg(\"get_file_hash\")\n .output()\n .expect(\"Failed to execute command\");\n\n let stdout = String::from_utf8_lossy(\u0026output.stdout);\n let stderr = String::from_utf8_lossy(\u0026output.stderr);\n\n // Assert that the command ran successfully\n assert!(output.status.success(), \"Command failed with stderr: {}\", stderr);\n\n // Manually calculate the hash of the binary's source file\n let expected_hash = calculate_sha256(\"src/bin/get_file_hash.rs\");\n\n // Assert that the output contains the correct hash\n // Check for the raw hash first\n assert!(stdout.contains(\u0026expected_hash), \"Output did not contain raw expected hash. Expected: {}, Actual: {}\", expected_hash, stdout);\n\n // Then check for the formatted string, including backticks\n // Use a regex-like check for more flexibility with newlines if needed, or refine to exact match\n let expected_hash_line = format!(\"* **SHA-256 Hash:** `{}`\", expected_hash);\n assert!(stdout.contains(\u0026expected_hash_line), \"Output did not contain expected hash line. Expected line: {}, Actual: {}\", expected_hash_line, stdout);\n\n // Assert that the output contains \"Integrity Verified.\"\n assert!(stdout.contains(\"Integrity Verified.\"), \"Output did not contain 'Integrity Verified.'. stdout: {}\", stdout);\n\n println!(\"Output from get_file_hash binary (no features):\n{}\", stdout);\n}\n\n#[test]\nfn test_get_file_hash_binary_with_nostr_feature() {\n let output = Command::new(\"cargo\")\n .arg(\"run\")\n .arg(\"--bin\")\n .arg(\"get_file_hash\")\n .arg(\"--features\")\n .arg(\"nostr\")\n .output()\n .expect(\"Failed to execute command\");\n\n let stdout = String::from_utf8_lossy(\u0026output.stdout);\n let stderr = String::from_utf8_lossy(\u0026output.stderr);\n\n // Assert that the command ran successfully\n assert!(output.status.success(), \"Command failed with stderr: {}\", stderr);\n\n // Manually calculate the hash of the binary's source file\n let expected_hash = calculate_sha256(\"src/bin/get_file_hash.rs\");\n\n // Assert that the output contains the correct hash\n assert!(stdout.contains(\u0026expected_hash), \"Output did not contain raw expected hash. Expected: {}, Actual: {}\", expected_hash, stdout);\n\n // Then check for the formatted string, including backticks\n let expected_hash_line = format!(\"* **SHA-256 Hash:** `{}`\", expected_hash);\n assert!(stdout.contains(\u0026expected_hash_line), \"Output did not contain expected hash line. Expected line: {}, Actual: {}\", expected_hash_line, stdout);\n\n // Assert that the output contains \"Integrity Verified.\"\n assert!(stdout.contains(\"Integrity Verified.\"), \"Output did not contain 'Integrity Verified.'. stdout: {}\", stdout);\n\n println!(\"Output from get_file_hash binary (with nostr feature):\n{}\", stdout);\n}\n",
"sig": "c55a805cb32b997f4593c7f8abd86c4da7bd595dff24f6c2444a09264579b0ffbe06f7b6cb94a896d4951d7ffdeb88e1f604a30e932adc44eba5d933f35668f3"
}