[M] Move tests

This commit is contained in:
2026-05-07 18:27:38 +00:00
parent 17e3961267
commit 7fc3ab32ad
17 changed files with 305 additions and 299 deletions
+2 -148
View File
@@ -268,151 +268,5 @@ pub fn validate_config(config: &Config) -> Result<()> {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_token_forms() {
let config: Config = toml::from_str(
r#"
[webhook]
install = true
url = "https://mirror.example.test/webhook"
secret = { env = "WEBHOOK_SECRET" }
full_sync_interval_minutes = 60
reachability_check_interval_minutes = 15
[[sites]]
name = "github"
provider = "github"
base_url = "https://github.com"
token = { env = "GITHUB_TOKEN" }
[[mirrors]]
name = "personal"
create_missing = true
visibility = "private"
allow_force = false
[[mirrors.endpoints]]
site = "github"
kind = "user"
namespace = "alice"
[[mirrors.endpoints]]
site = "github"
kind = "org"
namespace = "example"
"#,
)
.unwrap();
assert_eq!(config.sites.len(), 1);
assert_eq!(config.mirrors[0].endpoints.len(), 2);
let webhook = config.webhook.unwrap();
assert!(webhook.install);
assert_eq!(webhook.url, "https://mirror.example.test/webhook");
assert_eq!(
webhook.secret,
TokenConfig::Env("WEBHOOK_SECRET".to_string())
);
assert_eq!(webhook.full_sync_interval_minutes, Some(60));
}
#[test]
fn validation_rejects_unknown_sites_and_single_endpoint_groups() {
let config = Config {
sites: vec![site("github", ProviderKind::Github)],
mirrors: vec![MirrorConfig {
name: "broken".to_string(),
endpoints: vec![EndpointConfig {
site: "github".to_string(),
kind: NamespaceKind::User,
namespace: "alice".to_string(),
}],
create_missing: true,
visibility: Visibility::Private,
allow_force: false,
}],
webhook: None,
};
let err = validate_config(&config).unwrap_err().to_string();
assert!(err.contains("at least two endpoints"));
let config = Config {
sites: vec![site("github", ProviderKind::Github)],
mirrors: vec![MirrorConfig {
name: "broken".to_string(),
endpoints: vec![
EndpointConfig {
site: "github".to_string(),
kind: NamespaceKind::User,
namespace: "alice".to_string(),
},
EndpointConfig {
site: "missing".to_string(),
kind: NamespaceKind::User,
namespace: "alice".to_string(),
},
],
create_missing: true,
visibility: Visibility::Private,
allow_force: false,
}],
webhook: None,
};
let err = validate_config(&config).unwrap_err().to_string();
assert!(err.contains("unknown site 'missing'"));
}
#[test]
fn api_base_defaults_match_providers() {
assert_eq!(
site("github", ProviderKind::Github).api_base(),
"https://api.github.com"
);
assert_eq!(
SiteConfig {
base_url: "https://github.example.test/".to_string(),
..site("github-enterprise", ProviderKind::Github)
}
.api_base(),
"https://github.example.test/api/v3"
);
assert_eq!(
SiteConfig {
base_url: "https://gitlab.example.test".to_string(),
..site("gitlab", ProviderKind::Gitlab)
}
.api_base(),
"https://gitlab.example.test/api/v4"
);
assert_eq!(
SiteConfig {
base_url: "https://gitea.example.test".to_string(),
..site("gitea", ProviderKind::Gitea)
}
.api_base(),
"https://gitea.example.test/api/v1"
);
assert_eq!(
SiteConfig {
base_url: "https://forgejo.example.test".to_string(),
..site("forgejo", ProviderKind::Forgejo)
}
.api_base(),
"https://forgejo.example.test/api/v1"
);
}
fn site(name: &str, provider: ProviderKind) -> SiteConfig {
SiteConfig {
name: name.to_string(),
provider,
base_url: "https://github.com".to_string(),
api_url: None,
token: TokenConfig::Value("token".to_string()),
git_username: None,
}
}
}
#[path = "../tests/unit/config.rs"]
mod tests;
+1
View File
@@ -677,4 +677,5 @@ pub fn safe_remote_name(value: &str) -> String {
}
#[cfg(test)]
#[path = "../tests/unit/git.rs"]
mod tests;
+2
View File
@@ -564,6 +564,7 @@ fn pat_instruction_lines(provider: &ProviderKind, base_url: &str) -> Vec<String>
}
#[cfg(test)]
#[path = "../tests/unit/interactive_test_io.rs"]
mod test_io;
#[cfg(test)]
use test_io::*;
@@ -913,4 +914,5 @@ fn generate_webhook_secret() -> String {
}
#[cfg(test)]
#[path = "../tests/unit/interactive.rs"]
mod tests;
+2 -15
View File
@@ -230,18 +230,5 @@ macro_rules! logln {
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn repo_prefix_pads_and_truncates_to_fixed_width() {
assert_eq!(repo_prefix("api", 6), "api ");
assert_eq!(repo_prefix("very-long-repo", 8), "very-lo~");
}
#[test]
fn status_text_truncates_to_fixed_width() {
assert_eq!(truncate_status("short", 8), "short");
assert_eq!(truncate_status("very-long-status", 8), "very-lo~");
}
}
#[path = "../tests/unit/logging.rs"]
mod tests;
+2 -134
View File
@@ -223,137 +223,5 @@ fn resolve_webhook_url(config: &Config, value: Option<String>) -> Result<String>
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cli_config_opens_wizard() {
let cli = Cli::try_parse_from(["git-sync", "config"]).unwrap();
assert!(matches!(cli.command, Command::Config));
}
#[test]
fn cli_rejects_removed_config_subcommands() {
for args in [
["git-sync", "config", "wizard"].as_slice(),
["git-sync", "config", "init"].as_slice(),
["git-sync", "config", "show"].as_slice(),
["git-sync", "config", "site", "list"].as_slice(),
["git-sync", "config", "mirror", "list"].as_slice(),
] {
assert!(Cli::try_parse_from(args).is_err());
}
}
#[test]
fn cli_accepts_sync_repo_pattern() {
let cli = Cli::try_parse_from([
"git-sync",
"sync",
"--repo-pattern",
"^(foo|bar)-",
"--dry-run",
])
.unwrap();
let Command::Sync(args) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(args.repo_pattern, Some("^(foo|bar)-".to_string()));
assert!(args.dry_run);
}
#[test]
fn cli_accepts_sync_retry_failed() {
let cli = Cli::try_parse_from(["git-sync", "sync", "--retry-failed"]).unwrap();
let Command::Sync(args) = cli.command else {
panic!("parsed unexpected command");
};
assert!(args.retry_failed);
}
#[test]
fn cli_accepts_sync_jobs() {
let cli = Cli::try_parse_from(["git-sync", "sync", "--jobs", "8"]).unwrap();
let Command::Sync(args) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(args.jobs, 8);
}
#[test]
fn cli_accepts_webhook_serve() {
let cli = Cli::try_parse_from([
"git-sync",
"serve",
"--listen",
"127.0.0.1:9000",
"--secret-env",
"WEBHOOK_SECRET",
"--jobs",
"2",
"--full-sync-interval-minutes",
"30",
])
.unwrap();
let Command::Serve(args) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(args.listen, "127.0.0.1:9000");
assert_eq!(args.secret_env, Some("WEBHOOK_SECRET".to_string()));
assert_eq!(args.jobs, 2);
assert_eq!(args.full_sync_interval_minutes, Some(30));
}
#[test]
fn cli_accepts_webhook_install() {
let cli = Cli::try_parse_from([
"git-sync",
"webhook",
"install",
"--url",
"https://mirror.example.test/webhook",
"--secret",
"secret",
"--group",
"sync-1",
"--repo-pattern",
"^repo$",
])
.unwrap();
let Command::Webhook(WebhookCommand::Install(args)) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(
args.url,
Some("https://mirror.example.test/webhook".to_string())
);
assert_eq!(args.secret, Some("secret".to_string()));
assert_eq!(args.group, Some("sync-1".to_string()));
assert_eq!(args.repo_pattern, Some("^repo$".to_string()));
}
#[test]
fn cli_accepts_webhook_uninstall() {
let cli = Cli::try_parse_from([
"git-sync",
"webhook",
"uninstall",
"--group",
"sync-1",
"--dry-run",
])
.unwrap();
let Command::Webhook(WebhookCommand::Uninstall(args)) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(args.group, Some("sync-1".to_string()));
assert!(args.dry_run);
}
}
#[path = "../tests/unit/cli.rs"]
mod tests;
+1
View File
@@ -747,4 +747,5 @@ pub fn repos_by_name(repos: Vec<EndpointRepo>) -> HashMap<String, Vec<EndpointRe
}
#[cfg(test)]
#[path = "../tests/unit/provider.rs"]
mod tests;
+3 -2
View File
@@ -24,12 +24,12 @@ use self::output::{
print_branch_decisions, print_branch_deletions, print_failure, print_failure_summary,
print_tag_decisions, short_sha,
};
#[cfg(test)]
use self::state::{FailedRepo, failure_state_path};
use self::state::{
FailureState, RefState, RemoteRefState, SyncFailure, load_failure_state, load_ref_state,
save_failure_state, save_ref_state,
};
#[cfg(test)]
use self::state::{FailedRepo, failure_state_path};
pub const DEFAULT_JOBS: usize = 4;
@@ -936,4 +936,5 @@ struct RepoRefSyncResult {
}
#[cfg(test)]
#[path = "../tests/unit/sync.rs"]
mod tests;
+1
View File
@@ -769,4 +769,5 @@ fn fixed_time_eq(left: &[u8], right: &[u8]) -> bool {
}
#[cfg(test)]
#[path = "../tests/unit/webhook.rs"]
mod tests;
+132
View File
@@ -0,0 +1,132 @@
use super::*;
#[test]
fn cli_config_opens_wizard() {
let cli = Cli::try_parse_from(["git-sync", "config"]).unwrap();
assert!(matches!(cli.command, Command::Config));
}
#[test]
fn cli_rejects_removed_config_subcommands() {
for args in [
["git-sync", "config", "wizard"].as_slice(),
["git-sync", "config", "init"].as_slice(),
["git-sync", "config", "show"].as_slice(),
["git-sync", "config", "site", "list"].as_slice(),
["git-sync", "config", "mirror", "list"].as_slice(),
] {
assert!(Cli::try_parse_from(args).is_err());
}
}
#[test]
fn cli_accepts_sync_repo_pattern() {
let cli = Cli::try_parse_from([
"git-sync",
"sync",
"--repo-pattern",
"^(foo|bar)-",
"--dry-run",
])
.unwrap();
let Command::Sync(args) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(args.repo_pattern, Some("^(foo|bar)-".to_string()));
assert!(args.dry_run);
}
#[test]
fn cli_accepts_sync_retry_failed() {
let cli = Cli::try_parse_from(["git-sync", "sync", "--retry-failed"]).unwrap();
let Command::Sync(args) = cli.command else {
panic!("parsed unexpected command");
};
assert!(args.retry_failed);
}
#[test]
fn cli_accepts_sync_jobs() {
let cli = Cli::try_parse_from(["git-sync", "sync", "--jobs", "8"]).unwrap();
let Command::Sync(args) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(args.jobs, 8);
}
#[test]
fn cli_accepts_webhook_serve() {
let cli = Cli::try_parse_from([
"git-sync",
"serve",
"--listen",
"127.0.0.1:9000",
"--secret-env",
"WEBHOOK_SECRET",
"--jobs",
"2",
"--full-sync-interval-minutes",
"30",
])
.unwrap();
let Command::Serve(args) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(args.listen, "127.0.0.1:9000");
assert_eq!(args.secret_env, Some("WEBHOOK_SECRET".to_string()));
assert_eq!(args.jobs, 2);
assert_eq!(args.full_sync_interval_minutes, Some(30));
}
#[test]
fn cli_accepts_webhook_install() {
let cli = Cli::try_parse_from([
"git-sync",
"webhook",
"install",
"--url",
"https://mirror.example.test/webhook",
"--secret",
"secret",
"--group",
"sync-1",
"--repo-pattern",
"^repo$",
])
.unwrap();
let Command::Webhook(WebhookCommand::Install(args)) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(
args.url,
Some("https://mirror.example.test/webhook".to_string())
);
assert_eq!(args.secret, Some("secret".to_string()));
assert_eq!(args.group, Some("sync-1".to_string()));
assert_eq!(args.repo_pattern, Some("^repo$".to_string()));
}
#[test]
fn cli_accepts_webhook_uninstall() {
let cli = Cli::try_parse_from([
"git-sync",
"webhook",
"uninstall",
"--group",
"sync-1",
"--dry-run",
])
.unwrap();
let Command::Webhook(WebhookCommand::Uninstall(args)) = cli.command else {
panic!("parsed unexpected command");
};
assert_eq!(args.group, Some("sync-1".to_string()));
assert!(args.dry_run);
}
+146
View File
@@ -0,0 +1,146 @@
use super::*;
#[test]
fn parses_token_forms() {
let config: Config = toml::from_str(
r#"
[webhook]
install = true
url = "https://mirror.example.test/webhook"
secret = { env = "WEBHOOK_SECRET" }
full_sync_interval_minutes = 60
reachability_check_interval_minutes = 15
[[sites]]
name = "github"
provider = "github"
base_url = "https://github.com"
token = { env = "GITHUB_TOKEN" }
[[mirrors]]
name = "personal"
create_missing = true
visibility = "private"
allow_force = false
[[mirrors.endpoints]]
site = "github"
kind = "user"
namespace = "alice"
[[mirrors.endpoints]]
site = "github"
kind = "org"
namespace = "example"
"#,
)
.unwrap();
assert_eq!(config.sites.len(), 1);
assert_eq!(config.mirrors[0].endpoints.len(), 2);
let webhook = config.webhook.unwrap();
assert!(webhook.install);
assert_eq!(webhook.url, "https://mirror.example.test/webhook");
assert_eq!(
webhook.secret,
TokenConfig::Env("WEBHOOK_SECRET".to_string())
);
assert_eq!(webhook.full_sync_interval_minutes, Some(60));
}
#[test]
fn validation_rejects_unknown_sites_and_single_endpoint_groups() {
let config = Config {
sites: vec![site("github", ProviderKind::Github)],
mirrors: vec![MirrorConfig {
name: "broken".to_string(),
endpoints: vec![EndpointConfig {
site: "github".to_string(),
kind: NamespaceKind::User,
namespace: "alice".to_string(),
}],
create_missing: true,
visibility: Visibility::Private,
allow_force: false,
}],
webhook: None,
};
let err = validate_config(&config).unwrap_err().to_string();
assert!(err.contains("at least two endpoints"));
let config = Config {
sites: vec![site("github", ProviderKind::Github)],
mirrors: vec![MirrorConfig {
name: "broken".to_string(),
endpoints: vec![
EndpointConfig {
site: "github".to_string(),
kind: NamespaceKind::User,
namespace: "alice".to_string(),
},
EndpointConfig {
site: "missing".to_string(),
kind: NamespaceKind::User,
namespace: "alice".to_string(),
},
],
create_missing: true,
visibility: Visibility::Private,
allow_force: false,
}],
webhook: None,
};
let err = validate_config(&config).unwrap_err().to_string();
assert!(err.contains("unknown site 'missing'"));
}
#[test]
fn api_base_defaults_match_providers() {
assert_eq!(
site("github", ProviderKind::Github).api_base(),
"https://api.github.com"
);
assert_eq!(
SiteConfig {
base_url: "https://github.example.test/".to_string(),
..site("github-enterprise", ProviderKind::Github)
}
.api_base(),
"https://github.example.test/api/v3"
);
assert_eq!(
SiteConfig {
base_url: "https://gitlab.example.test".to_string(),
..site("gitlab", ProviderKind::Gitlab)
}
.api_base(),
"https://gitlab.example.test/api/v4"
);
assert_eq!(
SiteConfig {
base_url: "https://gitea.example.test".to_string(),
..site("gitea", ProviderKind::Gitea)
}
.api_base(),
"https://gitea.example.test/api/v1"
);
assert_eq!(
SiteConfig {
base_url: "https://forgejo.example.test".to_string(),
..site("forgejo", ProviderKind::Forgejo)
}
.api_base(),
"https://forgejo.example.test/api/v1"
);
}
fn site(name: &str, provider: ProviderKind) -> SiteConfig {
SiteConfig {
name: name.to_string(),
provider,
base_url: "https://github.com".to_string(),
api_url: None,
token: TokenConfig::Value("token".to_string()),
git_username: None,
}
}
+13
View File
@@ -0,0 +1,13 @@
use super::*;
#[test]
fn repo_prefix_pads_and_truncates_to_fixed_width() {
assert_eq!(repo_prefix("api", 6), "api ");
assert_eq!(repo_prefix("very-long-repo", 8), "very-lo~");
}
#[test]
fn status_text_truncates_to_fixed_width() {
assert_eq!(truncate_status("short", 8), "short");
assert_eq!(truncate_status("very-long-status", 8), "very-lo~");
}