use std::{
fs::remove_dir_all,
path::{Path, PathBuf},
};
use crate::utils::io::{read_toml, write_new_sensitive_file};
use serde::Deserialize;
fn cleanup_file(path: &Path) {
remove_dir_all(path).expect("cleanup_file() path: {:?} failed: {:?}");
}
#[test]
fn write_to_file_to_path() {
let msg = "Hello World!".as_bytes();
let path = PathBuf::from("./test-write");
let file = "test.txt";
match write_new_sensitive_file(msg, &path.join(file)) {
Ok(_) => cleanup_file(&path),
Err(e) => {
cleanup_file(&path);
panic!("{}", e);
}
}
}
#[test]
fn write_to_file_nested_dir() {
let msg = "Hello World!".as_bytes();
let root = PathBuf::from("./test_missing");
match write_new_sensitive_file(msg, &root.join("test_write_string").join("test-file")) {
Ok(_) => cleanup_file(&root),
Err(e) => {
cleanup_file(&root);
panic!("{}", e);
}
}
}
#[test]
fn read_from_file_vec() {
let msg = "Hello World!".as_bytes();
let path = PathBuf::from("./test_read_file");
let file_name = "out.keystore";
write_new_sensitive_file(msg, &path.join(file_name)).unwrap();
match std::fs::read(path.join(file_name)) {
Ok(contents) => {
cleanup_file(&path);
assert_eq!(contents, msg)
}
Err(e) => {
cleanup_file(&path);
panic!("{}", e);
}
}
}
#[test]
fn read_from_file_string() {
let msg = "Hello World!";
let path = PathBuf::from("./test_string_read_file");
let file_name = "out.keystore";
write_new_sensitive_file(msg.as_bytes(), &path.join(file_name)).unwrap();
match std::fs::read_to_string(path.join(file_name)) {
Ok(contents) => {
cleanup_file(&path);
assert_eq!(contents, msg)
}
Err(e) => {
cleanup_file(&path);
panic!("{}", e);
}
}
}
#[derive(Deserialize)]
struct Config {
ip: String,
port: Option<u16>,
keys: Keys,
}
#[derive(Deserialize)]
struct Keys {
github: String,
travis: Option<String>,
}
#[test]
fn read_from_toml() {
let toml_str = r#"
ip = '127.0.0.1'
[keys]
github = 'xxxxxxxxxxxxxxxxx'
travis = 'yyyyyyyyyyyyyyyyy'
"#;
let config: Config = read_toml(toml_str).unwrap();
assert_eq!(config.ip, "127.0.0.1");
assert_eq!(config.port, None);
assert_eq!(config.keys.github, "xxxxxxxxxxxxxxxxx");
assert_eq!(config.keys.travis.as_ref().unwrap(), "yyyyyyyyyyyyyyyyy");
}