Skip to content

Commit 6b852c8

Browse files
committed
move integration tests to separate folder
1 parent a1f1987 commit 6b852c8

File tree

11 files changed

+86
-203
lines changed

11 files changed

+86
-203
lines changed

Cargo.lock

-43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ section = "utility"
2424

2525
[dev-dependencies]
2626
git-testtools = "0.9.0"
27-
strip-ansi-escapes = "0.1.1"
2827
pretty_assertions = "1.3.0"
2928
insta = { version = "1.21.0", features = ["json", "redactions"] }
3029

src/info/mod.rs

+3-85
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ mod git;
3232
pub mod info_field;
3333
pub mod langs;
3434
mod repo;
35-
mod title;
35+
#[cfg(test)]
36+
pub mod test;
37+
pub mod title;
3638

3739
pub struct Info {
3840
title: Title,
@@ -327,9 +329,6 @@ mod tests {
327329
use super::*;
328330
use crate::ui::num_to_color;
329331
use clap::Parser;
330-
use git_repository::{open, ThreadSafeRepository};
331-
use git_testtools;
332-
use insta::assert_json_snapshot;
333332
use owo_colors::AnsiColors;
334333
use pretty_assertions::assert_eq;
335334

@@ -372,85 +371,4 @@ mod tests {
372371
);
373372
Ok(())
374373
}
375-
376-
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
377-
378-
#[test]
379-
fn test_bare_repo() -> Result {
380-
let repo_path = git_testtools::scripted_fixture_repo_read_only("bare_repo.sh").unwrap();
381-
let safe_repo =
382-
ThreadSafeRepository::open_opts(repo_path, open::Options::isolated()).unwrap();
383-
let repo = safe_repo.to_thread_local();
384-
let res = Info::init_repo_path(&repo);
385-
assert!(res.is_err(), "oops, info was returned on a bare git repo");
386-
assert_eq!(
387-
res.unwrap_err().to_string(),
388-
"please run onefetch inside of a non-bare git repository"
389-
);
390-
Ok(())
391-
}
392-
393-
fn assert_info_str_matches(config: &Config, re: &Regex) {
394-
let info = Info::new(config).unwrap();
395-
let info_str = format!("{}", info);
396-
let info_u8 = strip_ansi_escapes::strip(&info_str).unwrap();
397-
let simple_info_str = std::str::from_utf8(&info_u8).unwrap();
398-
assert!(
399-
re.is_match(&simple_info_str),
400-
"OOPS, REGEX\n{}\nDOESNT MATCH\n{}",
401-
re.to_string(),
402-
simple_info_str
403-
);
404-
}
405-
406-
#[test]
407-
fn test_language_repo() -> Result {
408-
let repo_path = git_testtools::scripted_fixture_repo_read_only_with_args(
409-
"language_repo.sh",
410-
["verilog"],
411-
)
412-
.unwrap();
413-
let safe_repo =
414-
ThreadSafeRepository::open_opts(repo_path.join("verilog"), open::Options::isolated())?;
415-
let repo = safe_repo.to_thread_local();
416-
417-
// TEST JSON SERILIZER FIRST
418-
let mut config: Config = Config {
419-
input: repo.path().to_path_buf(),
420-
..Default::default()
421-
};
422-
let info = Info::new(&config).unwrap();
423-
assert_json_snapshot!(
424-
info,
425-
{
426-
".gitVersion" => "git version",
427-
".head.short_commit_id" => "short commit"
428-
}
429-
);
430-
431-
// TEST FORMAT FUNCTION DEFAULT Config
432-
let expected_regex = include_str!("../../tests/regex/test_verilog_repo.stdout.regex");
433-
let re = Regex::new(&expected_regex).unwrap();
434-
assert_info_str_matches(&config, &re);
435-
436-
// TEST FORMAT FUNCTION Config true_color Always
437-
config.true_color = When::Always;
438-
assert_info_str_matches(&config, &re);
439-
440-
// TEST FORMAT FUNCTION Config true_color Never
441-
config.true_color = When::Never;
442-
assert_info_str_matches(&config, &re);
443-
444-
// TEST FORMAT FUNCTION Config no_bots default regex
445-
config.no_bots.replace(None);
446-
assert_info_str_matches(&config, &re);
447-
448-
// TEST FORMAT FUNCTION Config no_bots user provided regex
449-
config
450-
.no_bots
451-
.replace(Some(MyRegex(Regex::from_str(r"(b|B)ot")?)));
452-
assert_info_str_matches(&config, &re);
453-
454-
Ok(())
455-
}
456374
}

src/info/test.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[cfg(test)]
2+
pub mod utils {
3+
use anyhow::Result;
4+
use git_repository::{open, Repository, ThreadSafeRepository};
5+
6+
pub fn repo(name: &str) -> Result<Repository> {
7+
let name = name.to_string();
8+
let repo_path = git_testtools::scripted_fixture_repo_read_only(name).unwrap();
9+
let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?;
10+
Ok(safe_repo.to_thread_local())
11+
}
12+
}

src/info/title.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,13 @@ impl std::fmt::Display for Title {
8484
#[cfg(test)]
8585
mod tests {
8686
use super::*;
87-
use git_repository::{open, Repository, ThreadSafeRepository};
88-
87+
use crate::info::test::utils::repo;
88+
use anyhow::Result;
8989
use owo_colors::AnsiColors;
9090

91-
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
92-
93-
fn basic_repo() -> Result<Repository> {
94-
let name = "basic_repo.sh".to_string();
95-
let repo_path = git_testtools::scripted_fixture_repo_read_only(name)?;
96-
let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?;
97-
Ok(safe_repo.to_thread_local())
98-
}
99-
10091
#[test]
101-
fn test_get_git_username() -> Result {
102-
// See file ../tests/fixtures/basic_repo.sh for specific repo values
103-
let repo = basic_repo()?;
92+
fn test_get_git_username() -> Result<()> {
93+
let repo = repo("basic_repo.sh")?;
10494
let username = get_git_username(&repo);
10595
assert_eq!(
10696
username, "onefetch-committer-name",
@@ -110,17 +100,16 @@ mod tests {
110100
}
111101

112102
#[test]
113-
fn test_title_format() -> Result {
114-
let repo = basic_repo()?;
103+
fn test_title_format() -> Result<()> {
104+
let repo = repo("basic_repo.sh")?;
115105
let mut title = Title::new(
116106
&repo,
117107
DynColors::Ansi(AnsiColors::Red),
118108
DynColors::Ansi(AnsiColors::White),
119109
DynColors::Ansi(AnsiColors::Blue),
120110
true,
121111
);
122-
// git version is collected from command line call to git --version
123-
// setting git_version to known value
112+
124113
title.git_version = "git version 2.37.2".to_string();
125114
assert!(title.to_string().contains("onefetch-committer-name"));
126115
assert!(title.to_string().contains('~'));

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod cli;
2+
pub mod info;
3+
pub mod ui;

src/main.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
use anyhow::Result;
44
use clap::{CommandFactory, Parser};
5-
use cli::Config;
6-
use info::Info;
5+
use onefetch::cli;
6+
use onefetch::info::Info;
7+
use onefetch::ui::printer::Printer;
78
use std::io;
8-
use ui::printer::Printer;
99

1010
fn main() -> Result<()> {
1111
#[cfg(windows)]
1212
let _ = enable_ansi_support::enable_ansi_support();
1313

14-
let config = Config::parse();
14+
let config = cli::Config::parse();
1515

1616
if config.languages {
1717
return cli::print_supported_languages();
@@ -22,7 +22,7 @@ fn main() -> Result<()> {
2222
}
2323

2424
if let Some(generator) = config.completion {
25-
let mut cmd = Config::command();
25+
let mut cmd = cli::Config::command();
2626
cli::print_completions(generator, &mut cmd);
2727
return Ok(());
2828
}
@@ -34,7 +34,3 @@ fn main() -> Result<()> {
3434

3535
Ok(())
3636
}
37-
38-
mod cli;
39-
mod info;
40-
mod ui;

tests/fixtures/language_repo.sh renamed to tests/fixtures/repo.sh

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
#!/bin/bash
22
set -eu -o pipefail
33

4-
case "${1}" in
5-
verilog)
6-
LANG_DIR="verilog"
7-
LANG_EXT="vg"
8-
;;
9-
*)
10-
echo "OOPS, ARGUMENT EXPECTED TO BE ONE OF THESE VALUES:"
11-
echo " verilog for language type verilog"
12-
exit
13-
;;
14-
esac
15-
16-
mkdir ${LANG_DIR}
17-
cd ${LANG_DIR}
18-
194
git init -q
205

216
# BOTH NAME AND EMAIL ARE NEEDED FOR RECOGNITION
@@ -31,20 +16,20 @@ git config --local --add "author.email" "onefetch-author-email@onefetch.com"
3116
git remote add origin https://github.com/user/repo.git
3217

3318
git checkout -b main
34-
touch code.${LANG_EXT}
35-
git add code.${LANG_EXT}
19+
touch code.rs
20+
git add code.rs
3621
git commit -q -m c1 --author="Author One <author1@example.org>"
3722
git tag tag1
38-
echo hello >> code.${LANG_EXT}
39-
git add code.${LANG_EXT}
23+
echo hello >> code.rs
24+
git add code.rs
4025
git commit -q -m c2 --author="Author Two <author2@example.org>"
41-
echo world >> code.${LANG_EXT}
42-
git add code.${LANG_EXT}
26+
echo world >> code.rs
27+
git add code.rs
4328
git commit -q -m c3 --author="Author Three <author3@example.org>"
44-
echo something >> code.${LANG_EXT}
45-
git add code.${LANG_EXT}
29+
echo something >> code.rs
30+
git add code.rs
4631
git commit -q -m c4 --author="Author Four <author4@example.org>"
47-
echo more >> code.${LANG_EXT}
32+
echo more >> code.rs
4833

4934
echo "[dependencies]" > Cargo.toml
5035
echo 'anyhow = "1.0.65"' >> Cargo.toml

tests/regex/test_verilog_repo.stdout.regex

-21
This file was deleted.

0 commit comments

Comments
 (0)