Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ff9bae0
testing bare git repo error using git-testtools script #700
atluft Oct 12, 2022
36856c7
after running cargo fmt
atluft Oct 13, 2022
efa3a96
refactoring new to call init for testing of bare repo detection
atluft Oct 16, 2022
993d8e5
brute force testing of format function
atluft Oct 17, 2022
3e2757d
idea for testing formatted output using git-testtools arguments
atluft Oct 19, 2022
e36397f
fix formatting
atluft Oct 19, 2022
cc72ad6
adding pretty assert and concept of match anything in expected string
atluft Oct 20, 2022
13ac3d1
fixing windows expected output failures
atluft Oct 20, 2022
218ac5d
fixing formatting, eventually I will use a hook
atluft Oct 20, 2022
7b26b88
removing color based checking, simple regex now
atluft Oct 21, 2022
9a8b3ed
Update src/info/mod.rs
atluft Oct 22, 2022
419d7e7
Update src/info/mod.rs
atluft Oct 22, 2022
9ca8a9d
updates to use external file w/ regex in it
atluft Oct 22, 2022
dfe994c
trying standard path separator to see if windows ci passes
atluft Oct 23, 2022
e34db5f
updates to regex that make it easier to read
atluft Oct 23, 2022
6a15f6d
test coverage of serializer using json
atluft Oct 23, 2022
7c143cb
fix formatting
atluft Oct 23, 2022
a88625a
better assert logic for bare repo and pretty assert to debug CI failu…
atluft Oct 23, 2022
aead7a3
fixing git version in json serializarion
atluft Oct 23, 2022
4497f02
obtains 100% code coverage by manipulating git repo and files parsed
atluft Oct 24, 2022
9a7ae44
more code coverage, trying to get those last few lines
atluft Oct 24, 2022
56bbd62
making use of snapshot name in testing
atluft Oct 26, 2022
8ab0bb2
changing verification to string based test
atluft Oct 26, 2022
6486d50
switching to using inst snapshots
atluft Oct 26, 2022
75b427c
adding snap file needed for previous commit
atluft Oct 26, 2022
2f04c83
refactor default into Config object to allow Default construction
atluft Oct 26, 2022
f50c2e2
better naming on function
atluft Oct 28, 2022
21fbbdc
removing snap file from ignore
atluft Oct 28, 2022
69c6229
using object serialization in test
atluft Oct 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ section = "utility"

[dev-dependencies]
git-testtools = "0.9.0"
strip-ansi-escapes = "0.1.1"

[dependencies]
anyhow = "1.0.65"
Expand Down
78 changes: 74 additions & 4 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,16 @@ impl std::fmt::Display for Info {
}

impl Info {
pub fn new(config: &Config) -> Result<Self> {
let git_repo = git_repository::discover(&config.input)?;
let repo_path = git_repo
pub fn init_repo_path(repo: &git_repository::Repository) -> Result<std::path::PathBuf> {
Ok(repo
.work_dir()
.context("please run onefetch inside of a non-bare git repository")?
.to_owned();
.to_owned())
}

pub fn new(config: &Config) -> Result<Self> {
let git_repo = git_repository::discover(&config.input)?;
let repo_path = Info::init_repo_path(&git_repo)?;

let languages_handle = std::thread::spawn({
let ignored_directories = config.exclude.clone();
Expand Down Expand Up @@ -323,7 +327,10 @@ mod tests {
use super::*;
use crate::ui::num_to_color;
use clap::Parser;
use git_repository::{open, Repository, ThreadSafeRepository};
use git_testtools;
use owo_colors::AnsiColors;
use regex::Regex;

#[test]
fn test_get_style() -> Result<()> {
Expand Down Expand Up @@ -364,4 +371,67 @@ mod tests {
);
Ok(())
}

type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;

fn test_repo(name: &str) -> Result<Repository> {
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_bare_repo() -> Result {
let repo = test_repo(&"bare_repo.sh")?;
match Info::init_repo_path(&repo) {
Ok(_info) => assert!(false, "oops, info was returned on a bare git repo"),
Err(error) => assert_eq!(
"please run onefetch inside of a non-bare git repository",
error.to_string()
),
};
Ok(())
}

#[test]
fn test_language_repo() -> Result {
let repo_path = git_testtools::scripted_fixture_repo_read_only_with_args(
"language_repo.sh",
["verilog"],
)?;
let safe_repo =
ThreadSafeRepository::open_opts(repo_path.join("verilog"), open::Options::isolated())?;
let repo = safe_repo.to_thread_local();
let mut config = Config::parse_from(&["."]);
config.input = repo.path().to_path_buf();
let info = Info::new(&config)?;
let info_str = format!("{}", info);
let info_u8 = strip_ansi_escapes::strip(&info_str)?;
let simple_info_str = std::str::from_utf8(&info_u8)?;
let expected_info = [
r"(?s)onefetch-committer-name ~ git version .+",
r"-{37,}",
r"Project: repo",
r"HEAD: .+ \(main\)",
r"Created: 22 years ago",
r"Language:",
r".+Verilog \(100.0 %\).+",
r"Author: 100% author 2",
r"Last change: 22 years ago",
r"Repo: https://github.com/user/repo.git",
r"Commits: 2",
r"Lines of code: 1",
r"Size: 6 B \(1 file\)",
]
.join(".");
let re = Regex::new(&expected_info)?;
if !re.is_match(&simple_info_str) {
assert!(
false,
"OOPS, REGEX\n{}\nDOESNT MATCH\n{}",
expected_info, simple_info_str
);
}
Ok(())
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/bare_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -eu -o pipefail

git init -q --bare
39 changes: 39 additions & 0 deletions tests/fixtures/language_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/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
git config --local --add "committer.name" "onefetch-committer-name"
git config --local --add "committer.email" "[email protected]"

git config --local --add "user.name" "onefetch-user-name"
git config --local --add "user.email" "[email protected]"

git config --local --add "author.name" "onefetch-author-name"
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}
git commit -q -m c1
echo hello >> code.${LANG_EXT}
git commit -q -am c2