Skip to content

Commit

Permalink
Merge test_foo.rs -> foo.rs
Browse files Browse the repository at this point in the history
I tried splitting out the tests so that changes in the tests wouldn't
trigger recompilation of the tested module, but that was an
overoptimization.

I seem to recall making this choice from matklad's excellent One Hundred
Thousand Lines of Rust [1] series, but I can't find it again, and I've
come to not liking having them split into different modules.

[1] https://matklad.github.io/2021/09/05/Rust100k.html
  • Loading branch information
Notgnoshi committed Nov 3, 2024
1 parent 2b12c43 commit fedc273
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 216 deletions.
6 changes: 3 additions & 3 deletions herostratus-tests/src/fixtures/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn with_empty_commits(messages: &[&str]) -> eyre::Result<TempRepository> {
#[cfg(test)]
mod tests {
use git2::{Index, Odb, Repository};
use herostratus::git::{rev_parse, rev_walk};
use herostratus::git;

use super::*;

Expand All @@ -79,8 +79,8 @@ mod tests {
fn test_new_repository() {
let temp_repo = simplest().unwrap();

let rev = rev_parse("HEAD", &temp_repo.repo).unwrap();
let commits: Vec<_> = rev_walk(rev, &temp_repo.repo)
let rev = git::rev::parse("HEAD", &temp_repo.repo).unwrap();
let commits: Vec<_> = git::rev::walk(rev, &temp_repo.repo)
.unwrap()
.map(|oid| temp_repo.repo.find_commit(oid.unwrap()).unwrap())
.collect();
Expand Down
2 changes: 0 additions & 2 deletions herostratus/src/achievement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#[allow(clippy::module_inception)]
mod achievement;
mod process_rules;
#[cfg(test)]
mod test_process_rules;

pub use achievement::{Achievement, LoggedRule, Rule, RuleFactory};
pub use process_rules::{grant, grant_with_rules};
53 changes: 51 additions & 2 deletions herostratus/src/achievement/process_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ pub fn grant_with_rules<'repo>(
repo: &'repo git2::Repository,
rules: Vec<Box<dyn Rule>>,
) -> eyre::Result<impl Iterator<Item = Achievement> + 'repo> {
let rev = crate::git::rev_parse(reference, repo)
let rev = crate::git::rev::parse(reference, repo)
.wrap_err(format!("Failed to rev-parse: {reference:?}"))?;
let oids =
crate::git::rev_walk(rev, repo).wrap_err(format!("Failed to rev-walk rev: {rev:?}"))?;
crate::git::rev::walk(rev, repo).wrap_err(format!("Failed to rev-walk rev: {rev:?}"))?;

// TODO: There should be better error handling than this
let oids = oids.filter_map(|o| match o {
Expand All @@ -172,3 +172,52 @@ pub fn grant_with_rules<'repo>(
});
Ok(process_rules(oids, repo, rules))
}

#[cfg(test)]
mod tests {
use herostratus_tests::fixtures;

use super::*;
use crate::rules::test_rules::{AlwaysFail, ParticipationTrophy, ParticipationTrophy2};

#[test]
fn test_no_rules() {
let temp_repo = fixtures::repository::simplest().unwrap();
let rules = Vec::new();
let achievements = grant_with_rules("HEAD", &temp_repo.repo, rules).unwrap();
let achievements: Vec<_> = achievements.collect();
assert!(achievements.is_empty());
}

#[test]
fn test_iterator_no_matches() {
let temp_repo = fixtures::repository::simplest().unwrap();
let rules = vec![Box::new(AlwaysFail) as Box<dyn Rule>];
let achievements = grant_with_rules("HEAD", &temp_repo.repo, rules).unwrap();
let achievements: Vec<_> = achievements.collect();
assert!(achievements.is_empty());
}

#[test]
fn test_iterator_all_matches() {
let temp_repo = fixtures::repository::simplest().unwrap();

let rules = vec![
Box::new(AlwaysFail) as Box<dyn Rule>,
Box::new(ParticipationTrophy) as Box<dyn Rule>,
];
let achievements = grant_with_rules("HEAD", &temp_repo.repo, rules).unwrap();
let achievements: Vec<_> = achievements.collect();
assert_eq!(achievements.len(), 1);
}

#[test]
fn test_awards_on_finalize() {
let temp_repo = fixtures::repository::simplest().unwrap();

let rules = vec![Box::new(ParticipationTrophy2) as Box<dyn Rule>];
let achievements = grant_with_rules("HEAD", &temp_repo.repo, rules).unwrap();
let achievements: Vec<_> = achievements.collect();
assert_eq!(achievements.len(), 1);
}
}
45 changes: 0 additions & 45 deletions herostratus/src/achievement/test_process_rules.rs

This file was deleted.

110 changes: 110 additions & 0 deletions herostratus/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,113 @@ pub fn write_config(data_dir: &Path, config: &Config) -> eyre::Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::path::PathBuf;

use herostratus_tests::fixtures::config::empty;

use super::*;

#[test]
fn default_config_toml_contents() {
let default = Config::default();
let contents = serialize_config(&default).unwrap();
let expected = "[repositories]\n";
assert_eq!(contents, expected);
}

#[test]
fn read_write_config() {
let mut repositories = HashMap::new();
let config = RepositoryConfig {
path: PathBuf::from("git/Notgnoshi/herostratus"),
branch: None,
url: String::from("[email protected]:Notgnoshi/herostratus.git"),
..Default::default()
};
repositories.insert(String::from("herostratus"), config);
let config = Config {
repositories,
..Default::default()
};

let fixture = empty().unwrap();
write_config(&fixture.data_dir, &config).unwrap();

let contents = std::fs::read_to_string(config_path(&fixture.data_dir)).unwrap();
let expected = "[repositories.herostratus]\n\
path = \"git/Notgnoshi/herostratus\"\n\
url = \"[email protected]:Notgnoshi/herostratus.git\"\n\
";
assert_eq!(contents, expected);

let read_config = read_config(&fixture.data_dir).unwrap();
assert_eq!(read_config, config);
}

#[test]
fn generates_default_config_if_missing() {
let fixture = empty().unwrap();
let config_file = config_path(&fixture.data_dir);
assert!(!config_file.exists());

let config = read_config(&fixture.data_dir).unwrap();
let default_config = Config::default();
assert_eq!(config, default_config);
}

#[test]
fn config_exclude_rules() {
let config_toml = "[repositories.herostratus]\n\
path = \"git/Notgnoshi/herostratus\"\n\
url = \"[email protected]:Notgnoshi/herostratus.git\"\n\
[rules]\n\
exclude = [\"H4-non-unicode\"]\n\
";

let config = deserialize_config(config_toml).unwrap();
assert_eq!(config.rules.unwrap().exclude.unwrap(), ["H4-non-unicode"]);
}

#[test]
fn rule_specific_config() {
let config_toml = "[repositories.herostratus]\n\
path = \"git/Notgnoshi/herostratus\"\n\
url = \"[email protected]:Notgnoshi/herostratus.git\"\n\
[rules]\n\
h2_shortest_subject_line.length_threshold = 80\n\
";

let config = deserialize_config(config_toml).unwrap();
assert_eq!(
config
.rules
.unwrap()
.h2_shortest_subject_line
.unwrap()
.length_threshold,
80
);

let config_toml = "[repositories.herostratus]\n\
path = \"git/Notgnoshi/herostratus\"\n\
url = \"[email protected]:Notgnoshi/herostratus.git\"\n\
[rules.h2_shortest_subject_line]\n\
length_threshold = 80\n\
";

let config = deserialize_config(config_toml).unwrap();
assert_eq!(
config
.rules
.unwrap()
.h2_shortest_subject_line
.unwrap()
.length_threshold,
80
);
}
}
2 changes: 0 additions & 2 deletions herostratus/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[allow(clippy::module_inception)]
mod config;
#[cfg(test)]
mod test_config;

pub use config::{
config_path, deserialize_config, read_config, serialize_config, write_config, Config,
Expand Down
109 changes: 0 additions & 109 deletions herostratus/src/config/test_config.rs

This file was deleted.

Loading

0 comments on commit fedc273

Please sign in to comment.