Skip to content

Commit

Permalink
add another e2e test to generate and check the default config
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Sep 12, 2024
1 parent 0910147 commit c77677f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ pub(crate) const CONFIG_TEXT: &str = r#"# This is a default config file for bott
# How much data is stored at once in terms of time.
#retention = "10m"
# Where to place the legend for the memory widget. One of "none", "top-left", "top", "top-right", "left", "right", "bottom-left", "bottom", "bottom-right".
#memory_legend = "TopRight"
#memory_legend = "top-right"
# Where to place the legend for the network widget. One of "none", "top-left", "top", "top-right", "left", "right", "bottom-left", "bottom", "bottom-right".
#network_legend = "TopRight"
#network_legend = "top-right"
# Processes widget configuration
#[processes]
Expand Down
9 changes: 8 additions & 1 deletion tests/integration/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ use hashbrown::HashMap;
use portable_pty::{native_pty_system, Child, CommandBuilder, MasterPty, PtySize};

pub fn abs_path(path: &str) -> OsString {
Path::new(path).canonicalize().unwrap().into_os_string()
let path = Path::new(path);

if path.exists() {
path.canonicalize().unwrap().into_os_string()
} else {
// We are going to trust that the path given is valid...
path.to_owned().into_os_string()
}
}

/// Returns a QEMU runner target given an architecture.
Expand Down
68 changes: 56 additions & 12 deletions tests/integration/valid_config_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::{
io::{Read, Write},
path::Path,
thread,
time::Duration,
};
Expand Down Expand Up @@ -59,10 +60,9 @@ fn test_empty() {
run_and_kill(&["-C", "./tests/valid_configs/empty_config.toml"]);
}

#[test]
fn test_default() {
fn test_uncommented_default_config(original: &Path, test_name: &str) {
// Take the default config file and uncomment everything.
let default_config = match std::fs::File::open("./sample_configs/default_config.toml") {
let default_config = match std::fs::File::open(original) {
Ok(mut default_config_file) => {
let mut buf = String::new();
default_config_file
Expand All @@ -72,11 +72,13 @@ fn test_default() {
buf
}
Err(err) => {
println!("Could not open default config, skipping test_default: {err:?}");
println!("Could not open default config, skipping {test_name}. Error: {err:?}");
return;
}
};

println!("default config: {default_config}");

let default_config = Regex::new(r"(?m)^#([a-zA-Z\[])")
.unwrap()
.replace_all(&default_config, "$1");
Expand All @@ -85,20 +87,62 @@ fn test_default() {
.unwrap()
.replace_all(&default_config, "$2");

let Ok(mut uncommented_config) = tempfile::NamedTempFile::new() else {
println!("Could not create a temp file, skipping test_default.");
return;
let mut uncommented_config = match tempfile::NamedTempFile::new() {
Ok(tf) => tf,
Err(err) => {
println!("Could not create a temp file, skipping {test_name}. Error: {err:?}");
return;
}
};

if uncommented_config
.write_all(default_config.as_bytes())
.is_err()
{
println!("Could not write to temp file, skipping test_default.");
if let Err(err) = uncommented_config.write_all(default_config.as_bytes()) {
println!("Could not write to temp file, skipping {test_name}. Error: {err:?}");
return;
}

run_and_kill(&["-C", &uncommented_config.path().to_string_lossy()]);

uncommented_config.close().unwrap();
}

#[test]
fn test_default() {
test_uncommented_default_config(
Path::new("./sample_configs/default_config.toml"),
"test_default",
);
}

#[test]
fn test_new_default() {
let new_temp_default_path = match tempfile::NamedTempFile::new() {
Ok(temp_file) => temp_file.into_temp_path(),
Err(err) => {
println!("Could not create a temp file, skipping test_new_default. Error: {err:?}");
return;
}
};

// This is a hack because we need a file that doesn't exist, and this hopefully means we avoid a bit of TOCTOU...?
let actual_temp_default_path = new_temp_default_path.join("_test_test_test_test");
new_temp_default_path.close().unwrap();

if !actual_temp_default_path.exists() {
run_and_kill(&["-C", &(actual_temp_default_path.to_string_lossy())]);
test_uncommented_default_config(&actual_temp_default_path, "test_new_default");
} else {
println!("temp path we want to check exists, skip test_new_default test.");
}
}

#[test]
fn test_demo() {
let path: &str = "./sample_configs/demo_config.toml";
if std::path::Path::new(path).exists() {
run_and_kill(&["-C", path]);
} else {
println!("Could not read demo config.");
}
}

#[test]
Expand Down

0 comments on commit c77677f

Please sign in to comment.