From 6b852c82fb5bfbb10990f31c97e7359f3dfed872 Mon Sep 17 00:00:00 2001 From: o2sh Date: Sat, 29 Oct 2022 23:21:41 +0200 Subject: [PATCH] move integration tests to separate folder --- Cargo.lock | 43 --------- Cargo.toml | 1 - src/info/mod.rs | 88 +------------------ src/info/test.rs | 12 +++ src/info/title.rs | 25 ++---- src/lib.rs | 3 + src/main.rs | 14 ++- tests/fixtures/{language_repo.sh => repo.sh} | 33 ++----- tests/regex/test_verilog_repo.stdout.regex | 21 ----- tests/repo.rs | 45 ++++++++++ .../snapshots/repo__repo.snap | 4 +- 11 files changed, 86 insertions(+), 203 deletions(-) create mode 100644 src/info/test.rs create mode 100644 src/lib.rs rename tests/fixtures/{language_repo.sh => repo.sh} (79%) delete mode 100644 tests/regex/test_verilog_repo.stdout.regex create mode 100644 tests/repo.rs rename src/info/snapshots/onefetch__info__tests__language_repo.snap => tests/snapshots/repo__repo.snap (95%) diff --git a/Cargo.lock b/Cargo.lock index 3e32874d8..f3831ecb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,12 +62,6 @@ dependencies = [ "nodrop", ] -[[package]] -name = "arrayvec" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" - [[package]] name = "arrayvec" version = "0.7.2" @@ -2037,7 +2031,6 @@ dependencies = [ "serde", "serde_json", "serde_yaml", - "strip-ansi-escapes", "strum", "tera", "terminal_size 0.2.1", @@ -2626,15 +2619,6 @@ dependencies = [ "lock_api", ] -[[package]] -name = "strip-ansi-escapes" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "011cbb39cf7c1f62871aea3cc46e5817b0937b49e9447370c93cacbe93a766d8" -dependencies = [ - "vte", -] - [[package]] name = "strsim" version = "0.8.0" @@ -3039,12 +3023,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" -[[package]] -name = "utf8parse" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" - [[package]] name = "vcpkg" version = "0.2.15" @@ -3063,27 +3041,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "vte" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cbce692ab4ca2f1f3047fcf732430249c0e971bfdd2b234cf2c47ad93af5983" -dependencies = [ - "arrayvec 0.5.2", - "utf8parse", - "vte_generate_state_changes", -] - -[[package]] -name = "vte_generate_state_changes" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d257817081c7dffcdbab24b9e62d2def62e2ff7d00b1c20062551e6cccc145ff" -dependencies = [ - "proc-macro2", - "quote", -] - [[package]] name = "walkdir" version = "2.3.2" diff --git a/Cargo.toml b/Cargo.toml index 68097784d..d9fdac4e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ section = "utility" [dev-dependencies] git-testtools = "0.9.0" -strip-ansi-escapes = "0.1.1" pretty_assertions = "1.3.0" insta = { version = "1.21.0", features = ["json", "redactions"] } diff --git a/src/info/mod.rs b/src/info/mod.rs index a692e97ef..747a50c60 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -32,7 +32,9 @@ mod git; pub mod info_field; pub mod langs; mod repo; -mod title; +#[cfg(test)] +pub mod test; +pub mod title; pub struct Info { title: Title, @@ -327,9 +329,6 @@ mod tests { use super::*; use crate::ui::num_to_color; use clap::Parser; - use git_repository::{open, ThreadSafeRepository}; - use git_testtools; - use insta::assert_json_snapshot; use owo_colors::AnsiColors; use pretty_assertions::assert_eq; @@ -372,85 +371,4 @@ mod tests { ); Ok(()) } - - type Result = std::result::Result>; - - #[test] - fn test_bare_repo() -> Result { - let repo_path = git_testtools::scripted_fixture_repo_read_only("bare_repo.sh").unwrap(); - let safe_repo = - ThreadSafeRepository::open_opts(repo_path, open::Options::isolated()).unwrap(); - let repo = safe_repo.to_thread_local(); - let res = Info::init_repo_path(&repo); - assert!(res.is_err(), "oops, info was returned on a bare git repo"); - assert_eq!( - res.unwrap_err().to_string(), - "please run onefetch inside of a non-bare git repository" - ); - Ok(()) - } - - fn assert_info_str_matches(config: &Config, re: &Regex) { - let info = Info::new(config).unwrap(); - let info_str = format!("{}", info); - let info_u8 = strip_ansi_escapes::strip(&info_str).unwrap(); - let simple_info_str = std::str::from_utf8(&info_u8).unwrap(); - assert!( - re.is_match(&simple_info_str), - "OOPS, REGEX\n{}\nDOESNT MATCH\n{}", - re.to_string(), - simple_info_str - ); - } - - #[test] - fn test_language_repo() -> Result { - let repo_path = git_testtools::scripted_fixture_repo_read_only_with_args( - "language_repo.sh", - ["verilog"], - ) - .unwrap(); - let safe_repo = - ThreadSafeRepository::open_opts(repo_path.join("verilog"), open::Options::isolated())?; - let repo = safe_repo.to_thread_local(); - - // TEST JSON SERILIZER FIRST - let mut config: Config = Config { - input: repo.path().to_path_buf(), - ..Default::default() - }; - let info = Info::new(&config).unwrap(); - assert_json_snapshot!( - info, - { - ".gitVersion" => "git version", - ".head.short_commit_id" => "short commit" - } - ); - - // TEST FORMAT FUNCTION DEFAULT Config - let expected_regex = include_str!("../../tests/regex/test_verilog_repo.stdout.regex"); - let re = Regex::new(&expected_regex).unwrap(); - assert_info_str_matches(&config, &re); - - // TEST FORMAT FUNCTION Config true_color Always - config.true_color = When::Always; - assert_info_str_matches(&config, &re); - - // TEST FORMAT FUNCTION Config true_color Never - config.true_color = When::Never; - assert_info_str_matches(&config, &re); - - // TEST FORMAT FUNCTION Config no_bots default regex - config.no_bots.replace(None); - assert_info_str_matches(&config, &re); - - // TEST FORMAT FUNCTION Config no_bots user provided regex - config - .no_bots - .replace(Some(MyRegex(Regex::from_str(r"(b|B)ot")?))); - assert_info_str_matches(&config, &re); - - Ok(()) - } } diff --git a/src/info/test.rs b/src/info/test.rs new file mode 100644 index 000000000..a8422d39a --- /dev/null +++ b/src/info/test.rs @@ -0,0 +1,12 @@ +#[cfg(test)] +pub mod utils { + use anyhow::Result; + use git_repository::{open, Repository, ThreadSafeRepository}; + + pub fn repo(name: &str) -> Result { + let name = name.to_string(); + let repo_path = git_testtools::scripted_fixture_repo_read_only(name).unwrap(); + let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?; + Ok(safe_repo.to_thread_local()) + } +} diff --git a/src/info/title.rs b/src/info/title.rs index 0dc33907e..3bf06aa61 100644 --- a/src/info/title.rs +++ b/src/info/title.rs @@ -84,23 +84,13 @@ impl std::fmt::Display for Title { #[cfg(test)] mod tests { use super::*; - use git_repository::{open, Repository, ThreadSafeRepository}; - + use crate::info::test::utils::repo; + use anyhow::Result; use owo_colors::AnsiColors; - type Result = std::result::Result>; - - fn basic_repo() -> Result { - let name = "basic_repo.sh".to_string(); - let repo_path = git_testtools::scripted_fixture_repo_read_only(name)?; - let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?; - Ok(safe_repo.to_thread_local()) - } - #[test] - fn test_get_git_username() -> Result { - // See file ../tests/fixtures/basic_repo.sh for specific repo values - let repo = basic_repo()?; + fn test_get_git_username() -> Result<()> { + let repo = repo("basic_repo.sh")?; let username = get_git_username(&repo); assert_eq!( username, "onefetch-committer-name", @@ -110,8 +100,8 @@ mod tests { } #[test] - fn test_title_format() -> Result { - let repo = basic_repo()?; + fn test_title_format() -> Result<()> { + let repo = repo("basic_repo.sh")?; let mut title = Title::new( &repo, DynColors::Ansi(AnsiColors::Red), @@ -119,8 +109,7 @@ mod tests { DynColors::Ansi(AnsiColors::Blue), true, ); - // git version is collected from command line call to git --version - // setting git_version to known value + title.git_version = "git version 2.37.2".to_string(); assert!(title.to_string().contains("onefetch-committer-name")); assert!(title.to_string().contains('~')); diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 000000000..4dd8bd02f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub mod cli; +pub mod info; +pub mod ui; diff --git a/src/main.rs b/src/main.rs index a40db59d7..651b37d76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,16 +2,16 @@ use anyhow::Result; use clap::{CommandFactory, Parser}; -use cli::Config; -use info::Info; +use onefetch::cli; +use onefetch::info::Info; +use onefetch::ui::printer::Printer; use std::io; -use ui::printer::Printer; fn main() -> Result<()> { #[cfg(windows)] let _ = enable_ansi_support::enable_ansi_support(); - let config = Config::parse(); + let config = cli::Config::parse(); if config.languages { return cli::print_supported_languages(); @@ -22,7 +22,7 @@ fn main() -> Result<()> { } if let Some(generator) = config.completion { - let mut cmd = Config::command(); + let mut cmd = cli::Config::command(); cli::print_completions(generator, &mut cmd); return Ok(()); } @@ -34,7 +34,3 @@ fn main() -> Result<()> { Ok(()) } - -mod cli; -mod info; -mod ui; diff --git a/tests/fixtures/language_repo.sh b/tests/fixtures/repo.sh similarity index 79% rename from tests/fixtures/language_repo.sh rename to tests/fixtures/repo.sh index f51f40464..774b4c408 100644 --- a/tests/fixtures/language_repo.sh +++ b/tests/fixtures/repo.sh @@ -1,21 +1,6 @@ #!/bin/bash set -eu -o pipefail -case "${1}" in - verilog) - LANG_DIR="verilog" - LANG_EXT="vg" - ;; - *) - echo "OOPS, ARGUMENT EXPECTED TO BE ONE OF THESE VALUES:" - echo " verilog for language type verilog" - exit - ;; -esac - -mkdir ${LANG_DIR} -cd ${LANG_DIR} - git init -q # BOTH NAME AND EMAIL ARE NEEDED FOR RECOGNITION @@ -31,20 +16,20 @@ git config --local --add "author.email" "onefetch-author-email@onefetch.com" git remote add origin https://github.com/user/repo.git git checkout -b main -touch code.${LANG_EXT} -git add code.${LANG_EXT} +touch code.rs +git add code.rs git commit -q -m c1 --author="Author One " git tag tag1 -echo hello >> code.${LANG_EXT} -git add code.${LANG_EXT} +echo hello >> code.rs +git add code.rs git commit -q -m c2 --author="Author Two " -echo world >> code.${LANG_EXT} -git add code.${LANG_EXT} +echo world >> code.rs +git add code.rs git commit -q -m c3 --author="Author Three " -echo something >> code.${LANG_EXT} -git add code.${LANG_EXT} +echo something >> code.rs +git add code.rs git commit -q -m c4 --author="Author Four " -echo more >> code.${LANG_EXT} +echo more >> code.rs echo "[dependencies]" > Cargo.toml echo 'anyhow = "1.0.65"' >> Cargo.toml diff --git a/tests/regex/test_verilog_repo.stdout.regex b/tests/regex/test_verilog_repo.stdout.regex deleted file mode 100644 index 05c5e60c9..000000000 --- a/tests/regex/test_verilog_repo.stdout.regex +++ /dev/null @@ -1,21 +0,0 @@ -(?x)# ESCAPED SPACES ARE USED WHEN A SPACE MATCH IS EXPLICITLY NEEDED -onefetch-committer-name\ ~\ git\ version\ ([^\r\n]+)\r?\n # MATCH LINE 1 CAPTURE GIT VERSION --{37,}\r?\n # MATCH LINE 2 -Project:\ repo\ \(1\ tag\)\r?\n # MATCH LINE 3 -HEAD:\ ([^\ ]+)\ \(main\)\r?\n # MATCH LINE 4 CAPTURE HEAD SHORT HASH -Pending:\ 1\+-\ 2\+\r?\n # MATCH LINE 5 -Version:\ tag1\r?\n # MATCH LINE 6 -Created:\ 22\ years\ ago\r?\n # MATCH LINE 7 -Language:[^\r\n]+\r?\n # MATCH LINE 8 SKIP JUNK AFTER COLON -[^V]+Verilog\ \(100.0\ %\)[^\r\n]+\r?\n # MATCH LINE 9 SKIP JUNK BEFORE VERILOG -Dependencies:\ 1\ \(cargo\)\r?\n # MATCH LINE 10 -Authors:\ 25%\ Author\ ([^\ ]+)\ 1\r?\n # MATCH LINE 11 CAPTURE AUTHOR NUMBER -([^2]+)(25%\ Author\ ([^\ ]+)\ 1)\r?\n # MATCH LINE 12 CAPTURE AUTHOR NUMBER -([^2]+)(25%\ Author\ ([^\ ]+)\ 1)\r?\n # MATCH LINE 13 CAPTURE AUTHOR NUMBER -Last\ change:\ 22\ years\ ago\r?\n # MATCH LINE 14 -Contributors:\ 4\r?\n # MATCH LINE 15 -Repo:\ https://github.com/user/repo.git\r?\n # MATCH LINE 16 -Commits:\ 4\r?\n # MATCH LINE 17 -Lines\ of\ code:\ 4\r?\n # MATCH LINE 18 -Size:\ 22\ B\ \(1\ file\)\r?\n # MATCH LINE 19 -License:\ MIT # MATCH LINE 20 diff --git a/tests/repo.rs b/tests/repo.rs new file mode 100644 index 000000000..71c29c3a6 --- /dev/null +++ b/tests/repo.rs @@ -0,0 +1,45 @@ +use anyhow::Result; +use git_repository::{open, Repository, ThreadSafeRepository}; +use onefetch::cli::Config; +use onefetch::info::Info; + +fn repo(name: &str) -> Result { + let name = name.to_string(); + let repo_path = git_testtools::scripted_fixture_repo_read_only(name).unwrap(); + let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?; + Ok(safe_repo.to_thread_local()) +} + +#[test] +fn test_bare_repo() -> Result<()> { + let repo = repo("bare_repo.sh")?; + let res = Info::init_repo_path(&repo); + assert!(res.is_err(), "oops, info was returned on a bare git repo"); + assert_eq!( + res.unwrap_err().to_string(), + "please run onefetch inside of a non-bare git repository" + ); + Ok(()) +} + +#[test] +fn test_repo() -> Result<()> { + let repo_path = git_testtools::scripted_fixture_repo_read_only("repo.sh").unwrap(); + let safe_repo = ThreadSafeRepository::open_opts(repo_path, open::Options::isolated())?; + let repo = safe_repo.to_thread_local(); + + let config: Config = Config { + input: repo.path().to_path_buf(), + ..Default::default() + }; + let info = Info::new(&config).unwrap(); + insta::assert_json_snapshot!( + info, + { + ".gitVersion" => "git version", + ".head.short_commit_id" => "short commit" + } + ); + + Ok(()) +} diff --git a/src/info/snapshots/onefetch__info__tests__language_repo.snap b/tests/snapshots/repo__repo.snap similarity index 95% rename from src/info/snapshots/onefetch__info__tests__language_repo.snap rename to tests/snapshots/repo__repo.snap index 092def9fe..5006c3330 100644 --- a/src/info/snapshots/onefetch__info__tests__language_repo.snap +++ b/tests/snapshots/repo__repo.snap @@ -1,5 +1,5 @@ --- -source: src/info/mod.rs +source: tests/repo.rs expression: info --- { @@ -20,7 +20,7 @@ expression: info "created": "22 years ago", "languages": [ { - "language": "Verilog", + "language": "Rust", "percentage": 100.0 } ],