| 1 | //! Tests config files that have sometimes caused issues despite being valid. |
| 2 | |
| 3 | use std::{io::Read, process::Stdio, thread, time::Duration}; |
| 4 | #[cfg(feature = "default")] |
| 5 | use std::{io::Write, path::Path}; |
| 6 | |
| 7 | use predicates::prelude::*; |
| 8 | |
| 9 | use crate::util::{btm_command, spawn_btm_in_pty}; |
| 10 | |
| 11 | fn reader_to_string(mut reader: Box<dyn Read>) -> String { |
| 12 | let mut buf = String::default(); |
| 13 | reader.read_to_string(&mut buf).unwrap(); |
| 14 | |
| 15 | buf |
| 16 | } |
| 17 | |
| 18 | fn run_and_kill(args: &[&str]) { |
| 19 | let (master, mut handle) = spawn_btm_in_pty(args); |
| 20 | let reader = master.try_clone_reader().unwrap(); |
| 21 | let _ = master.take_writer().unwrap(); |
| 22 | |
| 23 | const TIMES_CHECKED: u64 = 6; // Check 6 times, once every 500ms, for 3 seconds total. |
| 24 | |
| 25 | for _ in 0..TIMES_CHECKED { |
| 26 | thread::sleep(Duration::from_millis(500)); |
| 27 | match handle.try_wait() { |
| 28 | Ok(Some(exit)) => { |
| 29 | println!("output: {}", reader_to_string(reader)); |
| 30 | panic!("program terminated unexpectedly (exit status: {exit:?})"); |
| 31 | } |
| 32 | Err(e) => { |
| 33 | println!("output: {}", reader_to_string(reader)); |
| 34 | panic!("error while trying to wait: {e}") |
| 35 | } |
| 36 | _ => {} |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | handle.kill().unwrap(); |
| 41 | } |
| 42 | |
| 43 | fn run_and_kill_cfg(path: &str) { |
| 44 | run_and_kill(&["-C", path]); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn test_basic() { |
| 49 | run_and_kill(&[]); |
| 50 | } |
| 51 | |
| 52 | /// A test to ensure that a bad config will fail the `run_and_kill` function. |
| 53 | #[test] |
| 54 | #[should_panic] |
| 55 | fn test_bad_basic() { |
| 56 | run_and_kill(&["--this_does_not_exist"]); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn test_empty() { |
| 61 | run_and_kill_cfg("./tests/valid_configs/empty_config.toml"); |
| 62 | } |
| 63 | |
| 64 | #[cfg(feature = "default")] |
| 65 | fn test_uncommented_default_config(original: &Path, test_name: &str) { |
| 66 | use regex::Regex; |
| 67 | |
| 68 | // Take the default config file and uncomment everything. |
| 69 | let default_config = match std::fs::File::open(original) { |
| 70 | Ok(mut default_config_file) => { |
| 71 | let mut buf = String::new(); |
| 72 | default_config_file |
| 73 | .read_to_string(&mut buf) |
| 74 | .expect("can read file"); |
| 75 | |
| 76 | buf |
| 77 | } |
| 78 | Err(err) => { |
| 79 | println!("Could not open default config, skipping {test_name}. Error: {err:?}"); |
| 80 | return; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | let default_config = Regex::new(r"(?m)^#([a-zA-Z\[])") |
| 85 | .unwrap() |
| 86 | .replace_all(&default_config, "$1"); |
| 87 | |
| 88 | let default_config = Regex::new(r"(?m)^#(\s\s+)([a-zA-Z\[])") |
| 89 | .unwrap() |
| 90 | .replace_all(&default_config, "$2"); |
| 91 | |
| 92 | let mut uncommented_config = match tempfile::NamedTempFile::new() { |
| 93 | Ok(tf) => tf, |
| 94 | Err(err) => { |
| 95 | println!("Could not create a temp file, skipping {test_name}. Error: {err:?}"); |
| 96 | return; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | if let Err(err) = uncommented_config.write_all(default_config.as_bytes()) { |
| 101 | println!("Could not write to temp file, skipping {test_name}. Error: {err:?}"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | run_and_kill(&["-C", &uncommented_config.path().to_string_lossy()]); |
| 106 | |
| 107 | uncommented_config.close().unwrap(); |
| 108 | } |
| 109 | |
| 110 | #[cfg(feature = "default")] |
| 111 | #[test] |
| 112 | fn test_default() { |
| 113 | test_uncommented_default_config( |
| 114 | Path::new("./sample_configs/default_config.toml"), |
| 115 | "test_default", |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | #[cfg(feature = "default")] |
| 120 | #[test] |
| 121 | fn test_new_default() { |
| 122 | use tempfile::TempPath; |
| 123 | |
| 124 | let new_temp_default_path = match tempfile::NamedTempFile::new() { |
| 125 | Ok(temp_file) => temp_file.into_temp_path(), |
| 126 | Err(err) => { |
| 127 | println!("Could not create a temp file, skipping test_new_default. Error: {err:?}"); |
| 128 | return; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | // This is a hack because we need a temp file that doesn't exist. |
| 133 | let actual_temp_default_path = new_temp_default_path.to_path_buf(); |
| 134 | new_temp_default_path.close().unwrap(); |
| 135 | |
| 136 | if !actual_temp_default_path.exists() { |
| 137 | run_and_kill(&["-C", &(actual_temp_default_path.to_string_lossy())]); |
| 138 | |
| 139 | // Re-take control over the temp path to ensure it gets deleted. |
| 140 | let actual_temp_default_path = |
| 141 | TempPath::try_from_path(actual_temp_default_path).expect("temp path exists"); |
| 142 | test_uncommented_default_config(&actual_temp_default_path, "test_new_default"); |
| 143 | |
| 144 | actual_temp_default_path.close().unwrap(); |
| 145 | } else { |
| 146 | println!("temp path we want to check exists, skip test_new_default test."); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn test_demo() { |
| 152 | let path: &str = "./sample_configs/demo_config.toml"; |
| 153 | if std::path::Path::new(path).exists() { |
| 154 | run_and_kill(&["-C", path]); |
| 155 | } else { |
| 156 | println!("Could not read demo config."); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn test_many_proc() { |
| 162 | run_and_kill_cfg("./tests/valid_configs/many_proc.toml"); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn test_all_proc() { |
| 167 | run_and_kill_cfg("./tests/valid_configs/all_proc.toml"); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn test_cpu_doughnut() { |
| 172 | run_and_kill_cfg("./tests/valid_configs/cpu_doughnut.toml"); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn test_theme() { |
| 177 | run_and_kill_cfg("./tests/valid_configs/theme.toml"); |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn test_styling_sanity_check() { |
| 182 | run_and_kill_cfg("./tests/valid_configs/styling.toml"); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn test_styling_sanity_check_2() { |
| 187 | run_and_kill_cfg("./tests/valid_configs/all_styling.toml"); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn test_color_spelling_is_valid() { |
| 192 | run_and_kill_cfg("./tests/valid_configs/all_styling_color.toml"); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn test_filtering() { |
| 197 | run_and_kill_cfg("./tests/valid_configs/filtering.toml"); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn test_proc_columns() { |
| 202 | run_and_kill_cfg("./tests/valid_configs/proc_columns.toml"); |
| 203 | } |
| 204 | |
| 205 | #[cfg(target_os = "linux")] |
| 206 | #[test] |
| 207 | fn test_linux_only() { |
| 208 | run_and_kill_cfg("./tests/valid_configs/os_specific/linux.toml"); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn test_temp_disk_sort_columns() { |
| 213 | run_and_kill_cfg("./tests/valid_configs/temp_disk_sort_columns.toml"); |
| 214 | } |
| 215 | |
| 216 | #[test] |
| 217 | fn test_proc_default_sort() { |
| 218 | run_and_kill_cfg("./tests/valid_configs/proc_default_sort.toml"); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn test_newer_memory() { |
| 223 | run_and_kill_cfg("./tests/valid_configs/widget/memory.toml"); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn test_newer_cpu() { |
| 228 | run_and_kill_cfg("./tests/valid_configs/widget/cpu.toml"); |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn test_newer_processes() { |
| 233 | run_and_kill_cfg("./tests/valid_configs/widget/processes.toml"); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn test_newer_network() { |
| 238 | run_and_kill_cfg("./tests/valid_configs/widget/network.toml"); |
| 239 | } |
| 240 | |
| 241 | #[test] |
| 242 | fn test_network_alias() { |
| 243 | run_and_kill_cfg("./tests/valid_configs/network_alias.toml"); |
| 244 | } |
| 245 | |
| 246 | /// This uses deprecated network settings - once they are removed, this test file should be moved to invalid configs. |
| 247 | #[test] |
| 248 | fn test_deprecated_network() { |
| 249 | run_and_kill_cfg("./tests/valid_configs/deprecated/network.toml"); |
| 250 | } |
| 251 | |
| 252 | /// This uses deprecated process settings - once they are removed, this test file should be moved to invalid configs. |
| 253 | #[test] |
| 254 | fn test_deprecated_processes() { |
| 255 | run_and_kill_cfg("./tests/valid_configs/deprecated/processes.toml"); |
| 256 | } |
| 257 | |
| 258 | /// This uses deprecated CPU settings - once they are removed, this test file should be moved to invalid configs. |
| 259 | #[test] |
| 260 | fn test_deprecated_cpu() { |
| 261 | run_and_kill_cfg("./tests/valid_configs/deprecated/cpu.toml"); |
| 262 | } |
| 263 | |
| 264 | /// This uses deprecated memory settings - once they are removed, this test file should be moved to invalid configs. |
| 265 | #[test] |
| 266 | fn test_deprecated_memory() { |
| 267 | run_and_kill_cfg("./tests/valid_configs/deprecated/memory.toml"); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn test_newer_temperature() { |
| 272 | run_and_kill_cfg("./tests/valid_configs/widget/temperature.toml"); |
| 273 | } |
| 274 | |
| 275 | #[test] |
| 276 | fn test_disk_io_graph() { |
| 277 | run_and_kill_cfg("./tests/valid_configs/widget/disk_io_graph.toml"); |
| 278 | } |
| 279 | |
| 280 | /// This uses deprecated temperature settings - once they are removed, this test file should be moved to invalid configs. |
| 281 | #[test] |
| 282 | fn test_deprecated_temperature() { |
| 283 | run_and_kill_cfg("./tests/valid_configs/deprecated/temperature.toml"); |
| 284 | } |
| 285 | |
| 286 | /// Test that deprecated warnings are not shown for config options that are not actually set, |
| 287 | /// even when a `[flags]` section is present. |
| 288 | #[test] |
| 289 | fn test_no_spurious_deprecated_warnings() { |
| 290 | let mut child = btm_command(&["-C", "./tests/valid_configs/empty_flags.toml"]) |
| 291 | .stdout(Stdio::piped()) |
| 292 | .stderr(Stdio::piped()) |
| 293 | .spawn() |
| 294 | .unwrap(); |
| 295 | |
| 296 | thread::sleep(Duration::from_secs(1)); |
| 297 | child.kill().unwrap(); |
| 298 | child.wait().unwrap(); |
| 299 | |
| 300 | let stderr_str = { |
| 301 | let mut stderr = child.stderr.take().unwrap(); |
| 302 | let mut buf = String::new(); |
| 303 | |
| 304 | stderr.read_to_string(&mut buf).unwrap(); |
| 305 | buf |
| 306 | }; |
| 307 | |
| 308 | assert!( |
| 309 | predicate::str::contains("deprecated") |
| 310 | .not() |
| 311 | .eval(&stderr_str), |
| 312 | "Expected no deprecated warnings, but got: {stderr_str}" |
| 313 | ); |
| 314 | } |