test: Added tests for new config command
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
use assert_cmd::prelude::*;
|
use assert_cmd::prelude::*;
|
||||||
use predicates::prelude::*;
|
use predicates::prelude::*;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
@@ -49,6 +50,73 @@ providers:
|
|||||||
fs::write(app_dir.join("config.yaml"), &cfg).unwrap();
|
fs::write(app_dir.join("config.yaml"), &cfg).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn cli_config_no_changes() {
|
||||||
|
let (td, xdg_cfg, xdg_cache) = setup_env();
|
||||||
|
let pw_file = td.path().join("pw.txt");
|
||||||
|
fs::write(&pw_file, b"pw\n").unwrap();
|
||||||
|
write_yaml_config(&xdg_cfg, &pw_file, None);
|
||||||
|
|
||||||
|
// Create a no-op editor script that exits successfully without modifying the file
|
||||||
|
let editor = td.path().join("noop-editor.sh");
|
||||||
|
fs::write(&editor, b"#!/bin/sh\nexit 0\n").unwrap();
|
||||||
|
let mut perms = fs::metadata(&editor).unwrap().permissions();
|
||||||
|
perms.set_mode(0o755);
|
||||||
|
fs::set_permissions(&editor, perms).unwrap();
|
||||||
|
|
||||||
|
let mut cmd = Command::cargo_bin("gman").unwrap();
|
||||||
|
cmd.env("XDG_CONFIG_HOME", &xdg_cfg)
|
||||||
|
.env("XDG_CACHE_HOME", &xdg_cache)
|
||||||
|
.env("EDITOR", &editor)
|
||||||
|
.arg("config");
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("No changes made to configuration"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn cli_config_updates_and_persists() {
|
||||||
|
let (td, xdg_cfg, xdg_cache) = setup_env();
|
||||||
|
let pw_file = td.path().join("pw.txt");
|
||||||
|
fs::write(&pw_file, b"pw\n").unwrap();
|
||||||
|
write_yaml_config(&xdg_cfg, &pw_file, None);
|
||||||
|
|
||||||
|
// Editor script appends a valid run_configs section to the YAML file
|
||||||
|
let editor = td.path().join("append-run-config.sh");
|
||||||
|
let script = r#"#!/bin/sh
|
||||||
|
FILE="$1"
|
||||||
|
cat >> "$FILE" <<'EOF'
|
||||||
|
run_configs:
|
||||||
|
- name: echo
|
||||||
|
secrets: ["api_key"]
|
||||||
|
EOF
|
||||||
|
exit 0
|
||||||
|
"#;
|
||||||
|
fs::write(&editor, script.as_bytes()).unwrap();
|
||||||
|
let mut perms = fs::metadata(&editor).unwrap().permissions();
|
||||||
|
perms.set_mode(0o755);
|
||||||
|
fs::set_permissions(&editor, perms).unwrap();
|
||||||
|
|
||||||
|
let mut cmd = Command::cargo_bin("gman").unwrap();
|
||||||
|
cmd.env("XDG_CONFIG_HOME", &xdg_cfg)
|
||||||
|
.env("XDG_CACHE_HOME", &xdg_cache)
|
||||||
|
.env("EDITOR", &editor)
|
||||||
|
.arg("config");
|
||||||
|
|
||||||
|
cmd.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::str::contains("Configuration updated successfully"));
|
||||||
|
|
||||||
|
// Verify that the config file now contains the run_configs key
|
||||||
|
let cfg_path = xdg_cfg.join("gman").join("config.yml");
|
||||||
|
let written = fs::read_to_string(&cfg_path).expect("config file readable");
|
||||||
|
assert!(written.contains("run_configs:"));
|
||||||
|
assert!(written.contains("name: echo"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cli_shows_help() {
|
fn cli_shows_help() {
|
||||||
let (_td, cfg, cache) = setup_env();
|
let (_td, cfg, cache) = setup_env();
|
||||||
|
|||||||
+22
-1
@@ -1,6 +1,6 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use gman::config::{Config, ProviderConfig, RunConfig};
|
use gman::config::{Config, ProviderConfig, RunConfig};
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
@@ -252,4 +252,25 @@ mod tests {
|
|||||||
assert_eq!(path, None);
|
assert_eq!(path, None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_config_duplicate_provider_names_is_invalid() {
|
||||||
|
let name = Some("dup".into());
|
||||||
|
let p1 = ProviderConfig {
|
||||||
|
name: name.clone(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let p2 = ProviderConfig {
|
||||||
|
name,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let cfg = Config {
|
||||||
|
default_provider: Some("dup".into()),
|
||||||
|
providers: vec![p1, p2],
|
||||||
|
run_configs: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(cfg.validate().is_err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user