diff --git a/src/authors/test/fs_repo.rs b/src/authors/test/fs_repo.rs index 231842d..a6b1878 100644 --- a/src/authors/test/fs_repo.rs +++ b/src/authors/test/fs_repo.rs @@ -1,5 +1,6 @@ use crate::conf; use crate::test_utils::file_cleanup::AfterAssert; +use crate::test_utils::set_test_env; use std::fs; @@ -12,6 +13,7 @@ use crate::authors::fs_provider::FSProvider; #[test] #[serial] fn should_connect_to_an_authors_file_in_cwd_if_available() { + set_test_env(); let cwd_authors_file_path = conf::authors_file_name(); fs::File::create(cwd_authors_file_path.clone()).unwrap(); let _after = AfterAssert::cleanup(&[&cwd_authors_file_path]); @@ -22,6 +24,7 @@ fn should_connect_to_an_authors_file_in_cwd_if_available() { #[test] #[serial] fn should_connect_to_the_default_authors_file_if_no_file_is_available_in_cwd() { + set_test_env(); let default_authors_file_path = conf::authors_file_path(); fs::File::create(&default_authors_file_path).unwrap(); let _after = AfterAssert::cleanup(&[default_authors_file_path.as_str()]); @@ -32,11 +35,13 @@ fn should_connect_to_the_default_authors_file_if_no_file_is_available_in_cwd() { #[test] #[serial] fn should_error_when_neither_cwd_or_default_authors_file_are_available() { + set_test_env(); assert!(FSProvider::from_cwd_with_home_fallback().is_err()); } #[test] fn should_connect_to_a_given_existing_authors_file() { + set_test_env(); let an_authors_file_path = "/tmp/an_authors_file"; fs::File::create(an_authors_file_path).unwrap(); let _after = AfterAssert::cleanup(&[an_authors_file_path]); @@ -46,11 +51,13 @@ fn should_connect_to_a_given_existing_authors_file() { #[test] fn should_not_connect_to_a_given_non_existing_file() { + set_test_env(); assert!(FSProvider::from("/tmp/no_file_here".to_string()).is_err()); } #[test] fn should_fetch_all_available_authors() { + set_test_env(); let an_authors_file_path = conf::dummy_data(); let repo = FSProvider::from(an_authors_file_path.to_string()).unwrap(); @@ -69,6 +76,7 @@ fn should_fetch_all_available_authors() { #[test] fn should_fetch_authors_based_on_alias() { + set_test_env(); let an_authors_file_path = conf::dummy_data(); let repo = FSProvider::from(an_authors_file_path.to_string()).unwrap(); @@ -83,6 +91,7 @@ fn should_fetch_authors_based_on_alias() { #[test] fn should_fetch_all_authors_for_a_given_alias() { + set_test_env(); let an_authors_file_path = conf::dummy_data(); let repo = FSProvider::from(an_authors_file_path.to_string()).unwrap(); @@ -100,6 +109,7 @@ fn should_fetch_all_authors_for_a_given_alias() { #[test] fn should_return_an_empty_list_if_no_author_mathces_alias() { + set_test_env(); let an_authors_file_path = conf::dummy_data(); let repo = FSProvider::from(an_authors_file_path.to_string()).unwrap(); diff --git a/src/conf/config.rs b/src/conf/config.rs index e83c6eb..3152a76 100644 --- a/src/conf/config.rs +++ b/src/conf/config.rs @@ -2,19 +2,18 @@ use std::env; use config::Config; -const DEFAULT_CONFIG: &str = include_str!("configs/default.yaml"); -const TEST_CONFIG: &str = include_str!("configs/test.yaml"); - pub fn authors_file_path() -> String { - let path = get_config().get::("authors_file_path").unwrap(); - let file = authors_file_name(); - let raw_config_string = format!("{}{}", path, file); - let base = "BASE_PATH"; + let path_to_config_dir = get_config().get::("authors_file_path").unwrap(); + let file_name = authors_file_name(); + let full_file_path = format!("{}{}", path_to_config_dir, file_name); + let home_placeholder = "PLACEHOLDER"; match env::var("XDG_CONFIG_HOME") { - Ok(env_var) => raw_config_string.replace(&format!("${}", base), &env_var), + Ok(env_home_var) => full_file_path.replace(&format!("${}", home_placeholder), &env_home_var), Err(_) => match env::var("HOME") { - Ok(env_var) => raw_config_string.replace(&format!("${}", base), &format!("{}/.config", env_var)), - Err(_) => panic!("Your $HOME is not set, can't locate authors file!"), + Ok(env_home_var) => { + full_file_path.replace(&format!("${}", home_placeholder), &format!("{}/.config", env_home_var)) + } + Err(_) => panic!("Your $HOME is not set, can't locate default authors file!"), }, } } @@ -35,19 +34,18 @@ pub fn editmsg() -> String { get_config().get::("editmsg").unwrap() } -pub fn get_config() -> Config { - let config_file = if let Ok(test_env) = env::var("COA_ENV") { - if test_env == "test" { - TEST_CONFIG - } else { - DEFAULT_CONFIG - } - } else { - DEFAULT_CONFIG - }; +fn get_config() -> Config { + let default = "default"; + let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| default.to_string()); Config::builder() - .add_source(config::File::from_str(config_file, config::FileFormat::Yaml)) - .build() - .unwrap() -} + // always add default config + .add_source(config::File::with_name(&format!("src/conf/configs/{}", default)).required(true)) + .add_source( + config::File::with_name(&format!("src/conf/configs/{}", run_mode)) + .required(false), // optional config, overrides default + ) + // allow settings from the environment (with a prefix of APP) + .add_source(config::Environment::with_prefix("app")) + .build().unwrap() +} \ No newline at end of file diff --git a/src/conf/configs/default.yaml b/src/conf/configs/default.yaml index 03042a5..1a447e0 100644 --- a/src/conf/configs/default.yaml +++ b/src/conf/configs/default.yaml @@ -1,3 +1,4 @@ -authors_file: "$BASE_PATH/co-author/authors" +authors_file_path: "$PLACEHOLDER/co-author/" +authors_file_name: "authors.csv" hooks_path: ".git/hooks" editmsg: ".git/COMMIT_EDITMSG" diff --git a/src/conf/mod.rs b/src/conf/mod.rs index 1984f52..fa25552 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -3,7 +3,6 @@ pub use config::authors_file_name; pub use config::authors_file_path; pub use config::dummy_data; pub use config::editmsg; -pub use config::get_config; pub use config::hooks_path; #[cfg(test)] diff --git a/src/git/test/libgit_wrapper.rs b/src/git/test/libgit_wrapper.rs index 010f36b..0d46657 100644 --- a/src/git/test/libgit_wrapper.rs +++ b/src/git/test/libgit_wrapper.rs @@ -1,3 +1,4 @@ +use co_author::test_utils::set_test_env; use git2::{Repository, RepositoryInitOptions, Signature}; use serial_test::serial; use std::{ @@ -31,6 +32,7 @@ fn should_determine_if_is_valid_git_repo() { #[test] #[serial] fn should_create_a_commit_on_an_already_existing_git_repo_with_staged_changes() { + set_test_env(); let git_repo = init_repo(REPO_PATH); create_and_add_file_to_git_tree(&git_repo, "foo"); @@ -42,9 +44,7 @@ fn should_create_a_commit_on_an_already_existing_git_repo_with_staged_changes() let editmsg_path = format!("{}/.git/COMMIT_EDITMSG", REPO_PATH); std::fs::write(editmsg_path, commit_body.formatted_body()).unwrap(); - let result = repo - .unwrap() - .commit(); + let result = repo.unwrap().commit(); assert!(result.is_ok()); } @@ -63,9 +63,7 @@ fn should_error_out_if_commit_body_is_empty() { let editmsg_path = format!("{}/.git/COMMIT_EDITMSG", REPO_PATH); std::fs::write(editmsg_path, commit_body.formatted_body()).unwrap(); - let result = repo - .unwrap() - .commit(); + let result = repo.unwrap().commit(); assert!(result .unwrap_err() @@ -76,27 +74,19 @@ fn should_error_out_if_commit_body_is_empty() { #[test] #[serial] fn test_prepares_editmsg_file() { + set_test_env(); let git_repo = init_repo(REPO_PATH); create_and_add_file_to_git_tree(&git_repo, "foo"); - let mut index = git_repo - .index() - .unwrap(); - let id = index - .write_tree() - .unwrap(); - let tree = git_repo - .find_tree(id) - .unwrap(); + let mut index = git_repo.index().unwrap(); + let id = index.write_tree().unwrap(); + let tree = git_repo.find_tree(id).unwrap(); add_commit(&git_repo, tree.clone(), "IRRELEVANT"); // add bar create_and_add_file_to_git_tree(&git_repo, "bar"); // modify but don't add foo - let root = git_repo - .path() - .parent() - .unwrap(); + let root = git_repo.path().parent().unwrap(); std::fs::write(root.join("foo"), "text").unwrap(); // add baz but keep untracked std::fs::write(root.join("baz"), "text").unwrap(); @@ -105,9 +95,7 @@ fn test_prepares_editmsg_file() { let repo = LibGitWrapper::from(PathBuf::from(REPO_PATH)); assert!(repo.is_ok()); - repo.unwrap() - .add_status_to_editmsg() - .unwrap(); + repo.unwrap().add_status_to_editmsg().unwrap(); let commit_editmsg_path = "/var/tmp/coa/.git/COMMIT_EDITMSG"; let contents = std::fs::read_to_string(Path::new(commit_editmsg_path)); @@ -139,15 +127,9 @@ fn should_only_return_the_first_line_from_the_last_commit() { let git_repo = init_repo(REPO_PATH); create_and_add_file_to_git_tree(&git_repo, "foo"); - let mut index = git_repo - .index() - .unwrap(); - let id = index - .write_tree() - .unwrap(); - let tree = git_repo - .find_tree(id) - .unwrap(); + let mut index = git_repo.index().unwrap(); + let id = index.write_tree().unwrap(); + let tree = git_repo.find_tree(id).unwrap(); let repo = LibGitWrapper::from(PathBuf::from(REPO_PATH)); assert!(repo.is_ok()); @@ -155,9 +137,7 @@ fn should_only_return_the_first_line_from_the_last_commit() { let msg = format!("{}\nSECOND_LINE", first_line); add_commit(&git_repo, tree, msg.as_str()); - let result = repo - .unwrap() - .prev_commit_msg(); + let result = repo.unwrap().prev_commit_msg(); assert_eq!(result.unwrap(), first_line); } @@ -168,23 +148,13 @@ fn init_repo(path: &str) -> Repository { let repo = Repository::init_opts(&dir, &RepositoryInitOptions::new()).unwrap(); set_user_and_email(&repo); - let mut index = repo - .index() - .unwrap(); - let id = index - .write_tree() - .unwrap(); - let tree = repo - .find_tree(id) - .unwrap(); + let mut index = repo.index().unwrap(); + let id = index.write_tree().unwrap(); + let tree = repo.find_tree(id).unwrap(); repo.commit( Some("HEAD"), - &repo - .signature() - .unwrap(), - &repo - .signature() - .unwrap(), + &repo.signature().unwrap(), + &repo.signature().unwrap(), "initial commit", &tree, &[], @@ -198,36 +168,20 @@ fn set_user_and_email(repo: &Repository) { let sig = Signature::now("a_name", "an_email").unwrap(); repo.config() .unwrap() - .set_str( - "user.name", - sig.name() - .unwrap(), - ) + .set_str("user.name", sig.name().unwrap()) .unwrap(); repo.config() .unwrap() - .set_str( - "user.email", - sig.email() - .unwrap(), - ) + .set_str("user.email", sig.email().unwrap()) .unwrap(); } fn add_commit(repo: &Repository, tree: git2::Tree<'_>, msg: &str) { - let head_commit = repo - .head() - .unwrap() - .peel_to_commit() - .unwrap(); + let head_commit = repo.head().unwrap().peel_to_commit().unwrap(); repo.commit( Some("HEAD"), - &repo - .signature() - .unwrap(), - &repo - .signature() - .unwrap(), + &repo.signature().unwrap(), + &repo.signature().unwrap(), msg, &tree, &[&head_commit], @@ -236,19 +190,10 @@ fn add_commit(repo: &Repository, tree: git2::Tree<'_>, msg: &str) { } fn create_and_add_file_to_git_tree(repo: &Repository, file_name: &str) { - let root = repo - .path() - .parent() - .unwrap(); + let root = repo.path().parent().unwrap(); File::create(root.join(file_name)).unwrap(); - let mut index = repo - .index() - .unwrap(); - index - .add_path(Path::new(file_name)) - .unwrap(); - index - .write() - .unwrap(); + let mut index = repo.index().unwrap(); + index.add_path(Path::new(file_name)).unwrap(); + index.write().unwrap(); } diff --git a/src/test_utils/mod.rs b/src/test_utils/mod.rs index ab9efe5..394788c 100644 --- a/src/test_utils/mod.rs +++ b/src/test_utils/mod.rs @@ -1 +1,4 @@ pub mod file_cleanup; +pub fn set_test_env() { + std::env::set_var("RUN_MODE", "test"); +} diff --git a/test/integration.rs b/test/integration.rs index 5166f11..b6cc6e3 100644 --- a/test/integration.rs +++ b/test/integration.rs @@ -1,4 +1,5 @@ use std::error::Error; +// TODO: these tests are being ignored use co_author::{args::Args, authors::author::Author, cli::Cli, handle_authors, handle_commit_msg};