Skip to content

Commit

Permalink
improve config
Browse files Browse the repository at this point in the history
  • Loading branch information
EricDriussi committed Jan 20, 2024
1 parent 2bb082c commit 234f260
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 108 deletions.
10 changes: 10 additions & 0 deletions src/authors/test/fs_repo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::conf;
use crate::test_utils::file_cleanup::AfterAssert;
use crate::test_utils::set_test_env;

use std::fs;

Expand All @@ -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]);
Expand All @@ -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()]);
Expand All @@ -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]);
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down
53 changes: 30 additions & 23 deletions src/conf/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use std::env;

use config::Config;
use config::{Config, Environment, File, FileFormat};

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::<String>("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::<String>("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!"),
},
}
}
Expand All @@ -35,19 +37,24 @@ pub fn editmsg() -> String {
get_config().get::<String>("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
};

Config::builder()
.add_source(config::File::from_str(config_file, config::FileFormat::Yaml))
.build()
.unwrap()
fn get_config() -> Config {
let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "default".to_string());

match run_mode.as_str() {
"test" => Config::builder()
.add_source(File::from_str(DEFAULT_CONFIG, FileFormat::Yaml))
// optional config, overrides default
.add_source(File::from_str(TEST_CONFIG, FileFormat::Yaml))
// allow settings from the environment (with a prefix of APP)
.add_source(Environment::with_prefix("app"))
.build()
.unwrap(),

_ => Config::builder()
.add_source(File::from_str(DEFAULT_CONFIG, FileFormat::Yaml))
// allow settings from the environment (with a prefix of APP)
.add_source(Environment::with_prefix("app"))
.build()
.unwrap(),
}
}
3 changes: 2 additions & 1 deletion src/conf/configs/default.yaml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 0 additions & 1 deletion src/conf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
111 changes: 28 additions & 83 deletions src/git/test/libgit_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use co_author::test_utils::set_test_env;
use git2::{Repository, RepositoryInitOptions, Signature};
use serial_test::serial;
use std::{
Expand Down Expand Up @@ -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");

Expand All @@ -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());
}
Expand All @@ -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()
Expand All @@ -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();
Expand All @@ -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));
Expand Down Expand Up @@ -139,25 +127,17 @@ 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());

let first_line = "FIRST LINE".to_string();
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);
}
Expand All @@ -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,
&[],
Expand All @@ -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],
Expand All @@ -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();
}
3 changes: 3 additions & 0 deletions src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod file_cleanup;
pub fn set_test_env() {
std::env::set_var("RUN_MODE", "test");
}
1 change: 1 addition & 0 deletions test/integration.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down

0 comments on commit 234f260

Please sign in to comment.