Skip to content

Commit

Permalink
Avoid path issues by combining include_bytes! with tempfile.
Browse files Browse the repository at this point in the history
This should help with broken Nix checks.
  • Loading branch information
koxu1996 committed Feb 6, 2024
1 parent 65c3eb3 commit 89955b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions kairos-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ casper-types = { version = "4.0.1", features = ["std"] } # TODO: Change `std` ->
clap = "4.4.18"
hex = "0.4.3"
thiserror = "1.0.56"

[dev-dependencies]
tempfile = "3.10.0"
34 changes: 22 additions & 12 deletions kairos-cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use assert_cmd::Command;
use std::path::PathBuf;
use std::io::{self, Write};
use tempfile::NamedTempFile;

// Helper function to get the path to a fixture file
fn fixture_path(relative_path: &str) -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.extend(["tests", "fixtures", relative_path].iter());
path
fn create_temp_fixture(data: &[u8]) -> io::Result<NamedTempFile> {
let mut temp_file = NamedTempFile::new()?;
temp_file.write_all(data)?;
Ok(temp_file)
}

const ED25519_SECRET_KEY: &[u8] = include_bytes!("./fixtures/ed25519/secret_key.pem");
//const ED25519_PUBLIC_KEY: &[u8] = include_bytes!("./fixtures/ed25519/public_key.pem");
const SECP256K1_SECRET_KEY: &[u8] = include_bytes!("./fixtures/secp256k1/secret_key.pem");
//const SECP256K1_PUBLIC_KEY: &[u8] = include_bytes!("./fixtures/secp256k1/public_key.pem");

#[test]
fn deposit_successful_with_ed25519() {
let secret_key_path = fixture_path("ed25519/secret_key.pem");
let secret_key: NamedTempFile = create_temp_fixture(ED25519_SECRET_KEY).unwrap();
let secret_key_path = secret_key.path();

let mut cmd = Command::cargo_bin("kairos-cli").unwrap();
cmd.arg("deposit")
Expand All @@ -23,7 +29,8 @@ fn deposit_successful_with_ed25519() {

#[test]
fn transfer_successful_with_secp256k1() {
let secret_key_path = fixture_path("secp256k1/secret_key.pem");
let secret_key: NamedTempFile = create_temp_fixture(SECP256K1_SECRET_KEY).unwrap();
let secret_key_path = secret_key.path();
let recipient = "01a26419a7d82b2263deaedea32d35eee8ae1c850bd477f62a82939f06e80df356"; // Example recipient

let mut cmd = Command::cargo_bin("kairos-cli").unwrap();
Expand All @@ -39,7 +46,8 @@ fn transfer_successful_with_secp256k1() {

#[test]
fn withdraw_successful_with_ed25519() {
let secret_key_path = fixture_path("ed25519/secret_key.pem");
let secret_key: NamedTempFile = create_temp_fixture(ED25519_SECRET_KEY).unwrap();
let secret_key_path = secret_key.path();

let mut cmd = Command::cargo_bin("kairos-cli").unwrap();
cmd.arg("withdraw")
Expand All @@ -52,7 +60,8 @@ fn withdraw_successful_with_ed25519() {

#[test]
fn deposit_invalid_amount() {
let secret_key_path = fixture_path("ed25519/secret_key.pem");
let secret_key: NamedTempFile = create_temp_fixture(ED25519_SECRET_KEY).unwrap();
let secret_key_path = secret_key.path();

let mut cmd = Command::cargo_bin("kairos-cli").unwrap();
cmd.arg("deposit")
Expand All @@ -67,7 +76,7 @@ fn deposit_invalid_amount() {

#[test]
fn deposit_invalid_private_key_path() {
let secret_key_path = fixture_path("ed25519/non_existing.pem"); // Non-existing path
let secret_key_path = "ed25519/non_existing.pem"; // Non-existing path

let mut cmd = Command::cargo_bin("kairos-cli").unwrap();
cmd.arg("deposit")
Expand All @@ -82,7 +91,8 @@ fn deposit_invalid_private_key_path() {

#[test]
fn transfer_invalid_recipient() {
let secret_key_path = fixture_path("ed25519/secret_key.pem");
let secret_key: NamedTempFile = create_temp_fixture(ED25519_SECRET_KEY).unwrap();
let secret_key_path = secret_key.path();

let mut cmd = Command::cargo_bin("kairos-cli").unwrap();
cmd.arg("transfer")
Expand Down

0 comments on commit 89955b1

Please sign in to comment.