Skip to content

Commit

Permalink
move integration tests to separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Oct 29, 2022
1 parent a1f1987 commit 6b852c8
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 203 deletions.
43 changes: 0 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
88 changes: 3 additions & 85 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 changes: 12 additions & 0 deletions src/info/test.rs
Original file line number Diff line number Diff line change
@@ -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())
}
}
25 changes: 7 additions & 18 deletions src/info/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -110,17 +100,16 @@ 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),
DynColors::Ansi(AnsiColors::White),
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('~'));
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod cli;
pub mod info;
pub mod ui;
14 changes: 5 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(());
}
Expand All @@ -34,7 +34,3 @@ fn main() -> Result<()> {

Ok(())
}

mod cli;
mod info;
mod ui;
33 changes: 9 additions & 24 deletions tests/fixtures/language_repo.sh → tests/fixtures/repo.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -31,20 +16,20 @@ git config --local --add "author.email" "[email protected]"
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 <[email protected]>"
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 <[email protected]>"
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 <[email protected]>"
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 <[email protected]>"
echo more >> code.${LANG_EXT}
echo more >> code.rs

echo "[dependencies]" > Cargo.toml
echo 'anyhow = "1.0.65"' >> Cargo.toml
Expand Down
21 changes: 0 additions & 21 deletions tests/regex/test_verilog_repo.stdout.regex

This file was deleted.

Loading

0 comments on commit 6b852c8

Please sign in to comment.