test: Fixed Windows CLI tests (forgot to add unix cfg check)
This commit is contained in:
@@ -11,11 +11,11 @@ use std::io::{self, IsTerminal, Read, Write};
|
|||||||
use std::panic::PanicHookInfo;
|
use std::panic::PanicHookInfo;
|
||||||
|
|
||||||
use crate::cli::wrap_and_run_command;
|
use crate::cli::wrap_and_run_command;
|
||||||
|
use crate::utils::persist_config_file;
|
||||||
use dialoguer::Editor;
|
use dialoguer::Editor;
|
||||||
use std::panic;
|
use std::panic;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
use crate::utils::persist_config_file;
|
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod command;
|
mod command;
|
||||||
@@ -242,7 +242,7 @@ async fn main() -> Result<()> {
|
|||||||
new_config
|
new_config
|
||||||
.validate()
|
.validate()
|
||||||
.with_context(|| "updated configuration is invalid")?;
|
.with_context(|| "updated configuration is invalid")?;
|
||||||
persist_config_file(&new_config)?;
|
persist_config_file(&new_config)?;
|
||||||
println!("✓ Configuration updated successfully");
|
println!("✓ Configuration updated successfully");
|
||||||
}
|
}
|
||||||
Commands::Sync {} => {
|
Commands::Sync {} => {
|
||||||
|
|||||||
+14
-14
@@ -20,10 +20,10 @@
|
|||||||
//! rc.validate().unwrap();
|
//! rc.validate().unwrap();
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use collections::HashSet;
|
|
||||||
use crate::providers::local::LocalProvider;
|
use crate::providers::local::LocalProvider;
|
||||||
use crate::providers::{SecretProvider, SupportedProvider};
|
use crate::providers::{SecretProvider, SupportedProvider};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use collections::HashSet;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_with::serde_as;
|
use serde_with::serde_as;
|
||||||
@@ -215,19 +215,19 @@ fn default_provider_exists(config: &Config) -> Result<(), ValidationError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn providers_names_are_unique(config: &Config) -> Result<(), ValidationError> {
|
fn providers_names_are_unique(config: &Config) -> Result<(), ValidationError> {
|
||||||
let mut names = HashSet::new();
|
let mut names = HashSet::new();
|
||||||
for provider in &config.providers {
|
for provider in &config.providers {
|
||||||
if let Some(name) = &provider.name {
|
if let Some(name) = &provider.name
|
||||||
if !names.insert(name) {
|
&& !names.insert(name)
|
||||||
let mut err = ValidationError::new("duplicate_provider_name");
|
{
|
||||||
err.message = Some(Cow::Borrowed(
|
let mut err = ValidationError::new("duplicate_provider_name");
|
||||||
"Provider names must be unique; duplicate found",
|
err.message = Some(Cow::Borrowed(
|
||||||
));
|
"Provider names must be unique; duplicate found",
|
||||||
return Err(err);
|
));
|
||||||
}
|
return Err(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
|
|||||||
@@ -140,15 +140,15 @@ pub(in crate::providers) fn resolve_git(override_path: Option<&PathBuf>) -> Resu
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(in crate::providers) fn default_git_username(git: &Path) -> Result<String> {
|
pub(in crate::providers) fn default_git_username(git: &Path) -> Result<String> {
|
||||||
debug!("Checking for default git username");
|
debug!("Checking for default git username");
|
||||||
run_git_config_capture(git, &["config", "user.name"])
|
run_git_config_capture(git, &["config", "user.name"])
|
||||||
.with_context(|| "unable to determine git user name")
|
.with_context(|| "unable to determine git user name")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(in crate::providers) fn default_git_email(git: &Path) -> Result<String> {
|
pub(in crate::providers) fn default_git_email(git: &Path) -> Result<String> {
|
||||||
debug!("Checking for default git username");
|
debug!("Checking for default git username");
|
||||||
run_git_config_capture(git, &["config", "user.email"])
|
run_git_config_capture(git, &["config", "user.email"])
|
||||||
.with_context(|| "unable to determine git user email")
|
.with_context(|| "unable to determine git user email")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(in crate::providers) fn ensure_git_available(git: &Path) -> Result<()> {
|
pub(in crate::providers) fn ensure_git_available(git: &Path) -> Result<()> {
|
||||||
|
|||||||
+43
-42
@@ -1,6 +1,7 @@
|
|||||||
use assert_cmd::prelude::*;
|
use assert_cmd::prelude::*;
|
||||||
use predicates::prelude::*;
|
use predicates::prelude::*;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
#[cfg(unix)]
|
||||||
use std::os::unix::fs::PermissionsExt;
|
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};
|
||||||
@@ -53,40 +54,40 @@ providers:
|
|||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn cli_config_no_changes() {
|
fn cli_config_no_changes() {
|
||||||
let (td, xdg_cfg, xdg_cache) = setup_env();
|
let (td, xdg_cfg, xdg_cache) = setup_env();
|
||||||
let pw_file = td.path().join("pw.txt");
|
let pw_file = td.path().join("pw.txt");
|
||||||
fs::write(&pw_file, b"pw\n").unwrap();
|
fs::write(&pw_file, b"pw\n").unwrap();
|
||||||
write_yaml_config(&xdg_cfg, &pw_file, None);
|
write_yaml_config(&xdg_cfg, &pw_file, None);
|
||||||
|
|
||||||
// Create a no-op editor script that exits successfully without modifying the file
|
// Create a no-op editor script that exits successfully without modifying the file
|
||||||
let editor = td.path().join("noop-editor.sh");
|
let editor = td.path().join("noop-editor.sh");
|
||||||
fs::write(&editor, b"#!/bin/sh\nexit 0\n").unwrap();
|
fs::write(&editor, b"#!/bin/sh\nexit 0\n").unwrap();
|
||||||
let mut perms = fs::metadata(&editor).unwrap().permissions();
|
let mut perms = fs::metadata(&editor).unwrap().permissions();
|
||||||
perms.set_mode(0o755);
|
perms.set_mode(0o755);
|
||||||
fs::set_permissions(&editor, perms).unwrap();
|
fs::set_permissions(&editor, perms).unwrap();
|
||||||
|
|
||||||
let mut cmd = Command::cargo_bin("gman").unwrap();
|
let mut cmd = Command::cargo_bin("gman").unwrap();
|
||||||
cmd.env("XDG_CONFIG_HOME", &xdg_cfg)
|
cmd.env("XDG_CONFIG_HOME", &xdg_cfg)
|
||||||
.env("XDG_CACHE_HOME", &xdg_cache)
|
.env("XDG_CACHE_HOME", &xdg_cache)
|
||||||
.env("EDITOR", &editor)
|
.env("EDITOR", &editor)
|
||||||
.arg("config");
|
.arg("config");
|
||||||
|
|
||||||
cmd.assert()
|
cmd.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::str::contains("No changes made to configuration"));
|
.stdout(predicate::str::contains("No changes made to configuration"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn cli_config_updates_and_persists() {
|
fn cli_config_updates_and_persists() {
|
||||||
let (td, xdg_cfg, xdg_cache) = setup_env();
|
let (td, xdg_cfg, xdg_cache) = setup_env();
|
||||||
let pw_file = td.path().join("pw.txt");
|
let pw_file = td.path().join("pw.txt");
|
||||||
fs::write(&pw_file, b"pw\n").unwrap();
|
fs::write(&pw_file, b"pw\n").unwrap();
|
||||||
write_yaml_config(&xdg_cfg, &pw_file, None);
|
write_yaml_config(&xdg_cfg, &pw_file, None);
|
||||||
|
|
||||||
// Editor script appends a valid run_configs section to the YAML file
|
// Editor script appends a valid run_configs section to the YAML file
|
||||||
let editor = td.path().join("append-run-config.sh");
|
let editor = td.path().join("append-run-config.sh");
|
||||||
let script = r#"#!/bin/sh
|
let script = r#"#!/bin/sh
|
||||||
FILE="$1"
|
FILE="$1"
|
||||||
cat >> "$FILE" <<'EOF'
|
cat >> "$FILE" <<'EOF'
|
||||||
run_configs:
|
run_configs:
|
||||||
@@ -95,26 +96,26 @@ run_configs:
|
|||||||
EOF
|
EOF
|
||||||
exit 0
|
exit 0
|
||||||
"#;
|
"#;
|
||||||
fs::write(&editor, script.as_bytes()).unwrap();
|
fs::write(&editor, script.as_bytes()).unwrap();
|
||||||
let mut perms = fs::metadata(&editor).unwrap().permissions();
|
let mut perms = fs::metadata(&editor).unwrap().permissions();
|
||||||
perms.set_mode(0o755);
|
perms.set_mode(0o755);
|
||||||
fs::set_permissions(&editor, perms).unwrap();
|
fs::set_permissions(&editor, perms).unwrap();
|
||||||
|
|
||||||
let mut cmd = Command::cargo_bin("gman").unwrap();
|
let mut cmd = Command::cargo_bin("gman").unwrap();
|
||||||
cmd.env("XDG_CONFIG_HOME", &xdg_cfg)
|
cmd.env("XDG_CONFIG_HOME", &xdg_cfg)
|
||||||
.env("XDG_CACHE_HOME", &xdg_cache)
|
.env("XDG_CACHE_HOME", &xdg_cache)
|
||||||
.env("EDITOR", &editor)
|
.env("EDITOR", &editor)
|
||||||
.arg("config");
|
.arg("config");
|
||||||
|
|
||||||
cmd.assert()
|
cmd.assert().success().stdout(predicate::str::contains(
|
||||||
.success()
|
"Configuration updated successfully",
|
||||||
.stdout(predicate::str::contains("Configuration updated successfully"));
|
));
|
||||||
|
|
||||||
// Verify that the config file now contains the run_configs key
|
// Verify that the config file now contains the run_configs key
|
||||||
let cfg_path = xdg_cfg.join("gman").join("config.yml");
|
let cfg_path = xdg_cfg.join("gman").join("config.yml");
|
||||||
let written = fs::read_to_string(&cfg_path).expect("config file readable");
|
let written = fs::read_to_string(&cfg_path).expect("config file readable");
|
||||||
assert!(written.contains("run_configs:"));
|
assert!(written.contains("run_configs:"));
|
||||||
assert!(written.contains("name: echo"));
|
assert!(written.contains("name: echo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -255,15 +255,15 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_config_duplicate_provider_names_is_invalid() {
|
fn test_config_duplicate_provider_names_is_invalid() {
|
||||||
let name = Some("dup".into());
|
let name = Some("dup".into());
|
||||||
let p1 = ProviderConfig {
|
let p1 = ProviderConfig {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let p2 = ProviderConfig {
|
let p2 = ProviderConfig {
|
||||||
name,
|
name,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let cfg = Config {
|
let cfg = Config {
|
||||||
default_provider: Some("dup".into()),
|
default_provider: Some("dup".into()),
|
||||||
|
|||||||
Reference in New Issue
Block a user