Skip to content

Commit

Permalink
test the check subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
glehmann committed Feb 9, 2024
1 parent 776fcd1 commit bf8056a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub fn check(args: &CheckArgs) -> Result<i32> {
match check_encrypted(&input_data) {
EncryptionStatus::Encrypted | EncryptionStatus::NoValue => (),
EncryptionStatus::Mixed => {
error! {"{file:?} is partially encrypted"};
error! {"{file:?}: partially encrypted"};
is_encrypted = false;
}
EncryptionStatus::NotEncrypted => {
error! {"{file:?} is not encrypted"};
error! {"{file:?}: not encrypted"};
is_encrypted = false;
}
}
Expand Down
91 changes: 91 additions & 0 deletions tests/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
mod common;

use std::{fs::OpenOptions, io::Write};

use assert_fs::prelude::*;
use predicates::prelude::predicate::str::*;

use crate::common::*;

const YAML_CONTENT: &str = "foo: bar
titi:
toto: 42
array:
- 1
- 2
- 3
empty_map: {}
empty_array: []
empty_string: ''
empty: null
";

#[test]
fn check_clear() {
let tmp = temp_dir();
let yaml_path = tmp.child("file.yaml");
write(&yaml_path, YAML_CONTENT);
yage_cmd!("check", &yaml_path)
.assert()
.failure()
.stdout(is_empty())
.stderr(contains(": not encrypted"));
}

#[test]
fn check_encrypted() {
let tmp = temp_dir();
let (_, pub_path) = create_key(&tmp);
let yaml_path = tmp.child("file.yaml");
write(&yaml_path, YAML_CONTENT);
let encrypted_path = tmp.child("file.enc.yaml");
yage!(
"encrypt",
"-R",
&pub_path,
&yaml_path,
"-o",
&encrypted_path
)
.stdout(is_empty())
.stderr(is_empty());
yage!("check", &encrypted_path)
.stdout(is_empty())
.stderr(is_empty());
}

#[test]
fn check_mixed() {
let tmp = temp_dir();
let (_, pub_path) = create_key(&tmp);
let yaml_path = tmp.child("file.yaml");
write(&yaml_path, YAML_CONTENT);
let encrypted_path = tmp.child("file.enc.yaml");
yage!(
"encrypt",
"-R",
&pub_path,
&yaml_path,
"-o",
&encrypted_path
)
.stdout(is_empty())
.stderr(is_empty());
yage!("check", &encrypted_path)
.stdout(is_empty())
.stderr(is_empty());
// append some data to the encrypted file
{
OpenOptions::new()
.append(true)
.open(&encrypted_path)
.unwrap()
.write_all(b"auie: tsrn\n")
.unwrap();
}
yage_cmd!("check", &encrypted_path)
.assert()
.failure()
.stdout(is_empty())
.stderr(contains(": partially encrypted"));
}

0 comments on commit bf8056a

Please sign in to comment.