Skip to content

Commit

Permalink
test edit subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
glehmann committed Feb 10, 2024
1 parent 0dbd0a4 commit 8697270
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions tests/edit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
mod common;

use common::*;

use predicates::str::is_empty;
use serde_yaml as sy;
use yage::{check_encrypted, EncryptionStatus};

// editor command that add "hop: hop" to the file
const EDITOR: &str = "bash -c 'echo hop: hop >> $0'";

#[test]
fn edit_key_file_from_args() {
let (_tmp, key_path, _, _, encrypted_path) = generate_encrypted_file();
let before_edit_data = read(&encrypted_path);
yage!(
"edit",
"--editor",
EDITOR,
"--key-file",
&key_path,
&encrypted_path
)
.stdout(is_empty())
.stderr(is_empty());
let after_edit_data = read(&encrypted_path);
assert_ne!(before_edit_data, after_edit_data);
assert!(after_edit_data.starts_with(&before_edit_data));
assert_eq!(
check_encrypted(&sy::from_str(&after_edit_data).unwrap()),
EncryptionStatus::Encrypted
);
}

#[test]
fn edit_key_file_from_env() {
let (_tmp, key_path, _, _, encrypted_path) = generate_encrypted_file();
let before_edit_data = read(&encrypted_path);
yage_cmd!("edit", &encrypted_path)
.env("EDITOR", EDITOR)
.env("YAGE_KEY_FILE", &key_path)
.assert()
.success()
.stdout(is_empty())
.stderr(is_empty());
let after_edit_data = read(&encrypted_path);
assert_ne!(before_edit_data, after_edit_data);
assert!(after_edit_data.starts_with(&before_edit_data));
assert_eq!(
check_encrypted(&sy::from_str(&after_edit_data).unwrap()),
EncryptionStatus::Encrypted
);
}

#[test]
fn edit_key_from_env() {
let (_tmp, key_path, _, _, encrypted_path) = generate_encrypted_file();
let before_edit_data = read(&encrypted_path);
yage_cmd!("edit", "-e", EDITOR, &encrypted_path)
.env("YAGE_KEY", read(&key_path).trim())
.assert()
.success()
.stdout(is_empty())
.stderr(is_empty());
let after_edit_data = read(&encrypted_path);
assert_ne!(before_edit_data, after_edit_data);
assert!(after_edit_data.starts_with(&before_edit_data));
assert_eq!(
check_encrypted(&sy::from_str(&after_edit_data).unwrap()),
EncryptionStatus::Encrypted
);
}

#[test]
fn edit_key_from_stdin() {
let (_tmp, key_path, _, _, encrypted_path) = generate_encrypted_file();
let before_edit_data = read(&encrypted_path);
yage_cmd!("edit", "-K", "-", "-e", EDITOR, &encrypted_path)
.write_stdin(read(&key_path))
.assert()
.success()
.stdout(is_empty())
.stderr(is_empty());
let after_edit_data = read(&encrypted_path);
assert_ne!(before_edit_data, after_edit_data);
assert!(after_edit_data.starts_with(&before_edit_data));
assert_eq!(
check_encrypted(&sy::from_str(&after_edit_data).unwrap()),
EncryptionStatus::Encrypted
);
}

0 comments on commit 8697270

Please sign in to comment.