mirror of
https://github.com/o2sh/onefetch
synced 2024-07-05 00:58:35 +00:00
move integration tests to separate folder
This commit is contained in:
parent
a1f198712f
commit
6b852c82fb
43
Cargo.lock
generated
43
Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -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"] }
|
||||
|
||||
|
|
|
@ -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<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
#[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(())
|
||||
}
|
||||
}
|
||||
|
|
12
src/info/test.rs
Normal file
12
src/info/test.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
#[cfg(test)]
|
||||
pub mod utils {
|
||||
use anyhow::Result;
|
||||
use git_repository::{open, Repository, ThreadSafeRepository};
|
||||
|
||||
pub fn repo(name: &str) -> Result<Repository> {
|
||||
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())
|
||||
}
|
||||
}
|
|
@ -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<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
fn basic_repo() -> Result<Repository> {
|
||||
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('~'));
|
||||
|
|
3
src/lib.rs
Normal file
3
src/lib.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod cli;
|
||||
pub mod info;
|
||||
pub mod ui;
|
14
src/main.rs
14
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;
|
||||
|
|
|
@ -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 <author1@example.org>"
|
||||
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 <author2@example.org>"
|
||||
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 <author3@example.org>"
|
||||
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 <author4@example.org>"
|
||||
echo more >> code.${LANG_EXT}
|
||||
echo more >> code.rs
|
||||
|
||||
echo "[dependencies]" > Cargo.toml
|
||||
echo 'anyhow = "1.0.65"' >> Cargo.toml
|
|
@ -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
|
45
tests/repo.rs
Normal file
45
tests/repo.rs
Normal file
|
@ -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<Repository> {
|
||||
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(())
|
||||
}
|
|
@ -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
|
||||
}
|
||||
],
|
Loading…
Reference in New Issue
Block a user