Skip to content

Commit

Permalink
Merge pull request #112 from jmcconnell26/ISSUE-16-AddUnitTests
Browse files Browse the repository at this point in the history
ISSUE-16 - Add unit tests
  • Loading branch information
anderejd authored Sep 9, 2020
2 parents ac71566 + e360dc0 commit 4113000
Show file tree
Hide file tree
Showing 12 changed files with 1,099 additions and 298 deletions.
36 changes: 36 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions cargo-geiger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ env_logger = "0.7.1"
geiger = { path = "../geiger", version = "0.4.5" }
petgraph = "0.5.1"
pico-args = "0.3.3"
strum = "0.19.2"
strum_macros = "0.19.2"
walkdir = "2.3.1"
anyhow = "1.0.31"

Expand All @@ -32,5 +34,6 @@ vendored-openssl = ["cargo/vendored-openssl"]
assert_cmd = "1.0.1"
better-panic = "0.2.0"
insta = "0.16.1"
rand = "0.7.3"
regex = "1.3.9"
rstest = "0.6.4"
75 changes: 75 additions & 0 deletions cargo-geiger/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,78 @@ pub fn resolve<'a, 'cfg>(
}

// TODO: Make a wrapper type for canonical paths and hide all mutable access.

#[cfg(test)]
mod cli_tests {
use super::*;

#[test]
fn get_cfgs_test() {
let config = Config::default().unwrap();

let target: Option<String> = None;

let root =
important_paths::find_root_manifest_for_wd(config.cwd()).unwrap();
let workspace = Workspace::new(&root, &config).unwrap();

let cfgs = get_cfgs(&config, &target, &workspace);

assert!(cfgs.is_ok());
let cfg_vec_option = cfgs.unwrap();
assert!(cfg_vec_option.is_some());
let cfg_vec = cfg_vec_option.unwrap();

let names: Vec<&Cfg> = cfg_vec
.iter()
.filter(|cfg| matches!(cfg, Cfg::Name(_)))
.collect();

let key_pairs: Vec<&Cfg> = cfg_vec
.iter()
.filter(|cfg| matches!(cfg, Cfg::KeyPair(_, _)))
.collect();

assert!(names.len() > 0);
assert!(key_pairs.len() > 0);
}

#[test]
fn get_workspace_test() {
let config = Config::default().unwrap();
let manifest_path: Option<PathBuf> = None;

let workspace_cargo_result = get_workspace(&config, manifest_path);
assert!(workspace_cargo_result.is_ok());
let workspace = workspace_cargo_result.unwrap();

let package_result = workspace.current();
assert!(package_result.is_ok());
let package = package_result.unwrap();

assert_eq!(package.package_id().name(), "cargo-geiger");
}

#[test]
fn get_registry_test() {
let config = Config::default().unwrap();
let workspace = Workspace::new(
&important_paths::find_root_manifest_for_wd(config.cwd()).unwrap(),
&config,
)
.unwrap();
let package = workspace.current().unwrap();

let registry_result = get_registry(&config, &package);

assert!(registry_result.is_ok());
let registry = registry_result.unwrap();

let package_ids = vec![package.package_id()];
let package_set_result = registry.get(&package_ids);
assert!(package_set_result.is_ok());
let package_set = package_set_result.unwrap();

assert_eq!(package_set.sources().len(), 1);
}
}
61 changes: 46 additions & 15 deletions cargo-geiger/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use colored::Colorize;
use std::error::Error;
use std::fmt;
use std::str::{self, FromStr};
use strum_macros::EnumIter;

use self::parse::{Parser, RawChunk};

#[derive(Clone, Copy, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Charset {
Utf8,
Ascii,
Expand All @@ -31,6 +32,7 @@ impl FromStr for Charset {
}
}

#[derive(Debug, Clone, EnumIter, PartialEq)]
pub enum CrateDetectionStatus {
NoneDetectedForbidsUnsafe,
NoneDetectedAllowsUnsafe,
Expand Down Expand Up @@ -106,6 +108,18 @@ impl EmojiSymbols {
pub struct Pattern(Vec<Chunk>);

impl Pattern {
pub fn display<'a>(
&'a self,
package: &'a PackageId,
metadata: &'a ManifestMetadata,
) -> Display<'a> {
Display {
pattern: self,
package,
metadata,
}
}

pub fn try_build(format: &str) -> Result<Pattern, Box<dyn Error>> {
let mut chunks = vec![];

Expand All @@ -125,18 +139,6 @@ impl Pattern {

Ok(Pattern(chunks))
}

pub fn display<'a>(
&'a self,
package: &'a PackageId,
metadata: &'a ManifestMetadata,
) -> Display<'a> {
Display {
pattern: self,
package,
metadata,
}
}
}

#[derive(Clone, Copy)]
Expand All @@ -146,8 +148,8 @@ pub enum SymbolKind {
Rads = 2,
}

pub fn get_kind_group_name(k: DepKind) -> Option<&'static str> {
match k {
pub fn get_kind_group_name(dep_kind: DepKind) -> Option<&'static str> {
match dep_kind {
DepKind::Normal => None,
DepKind::Build => Some("[build-dependencies]"),
DepKind::Development => Some("[dev-dependencies]"),
Expand All @@ -160,3 +162,32 @@ enum Chunk {
License,
Repository,
}

#[cfg(test)]
mod format_tests {
use super::*;

#[test]
fn charset_from_str_test() {
assert_eq!(Charset::from_str("utf8"), Ok(Charset::Utf8));

assert_eq!(Charset::from_str("ascii"), Ok(Charset::Ascii));

assert_eq!(Charset::from_str("invalid_str"), Err("invalid charset"));
}

#[test]
fn get_kind_group_name_test() {
assert_eq!(get_kind_group_name(DepKind::Normal), None);

assert_eq!(
get_kind_group_name(DepKind::Build),
Some("[build-dependencies]")
);

assert_eq!(
get_kind_group_name(DepKind::Development),
Some("[dev-dependencies]")
);
}
}
47 changes: 39 additions & 8 deletions cargo-geiger/src/format/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use petgraph::EdgeDirection;

#[derive(Clone, Copy)]
pub enum Prefix {
None,
Indent,
Depth,
Indent,
None,
}

pub struct PrintConfig<'a> {
Expand All @@ -30,12 +30,43 @@ pub struct PrintConfig<'a> {
}

pub fn colorize(
s: String,
detection_status: &CrateDetectionStatus,
string: String,
crate_detection_status: &CrateDetectionStatus,
) -> colored::ColoredString {
match detection_status {
CrateDetectionStatus::NoneDetectedForbidsUnsafe => s.green(),
CrateDetectionStatus::NoneDetectedAllowsUnsafe => s.normal(),
CrateDetectionStatus::UnsafeDetected => s.red().bold(),
match crate_detection_status {
CrateDetectionStatus::NoneDetectedForbidsUnsafe => string.green(),
CrateDetectionStatus::NoneDetectedAllowsUnsafe => string.normal(),
CrateDetectionStatus::UnsafeDetected => string.red().bold(),
}
}

#[cfg(test)]
mod print_tests {
use super::*;

#[test]
fn colorize_test() {
let string = String::from("string_value");

assert_eq!(
string.clone().green(),
colorize(
string.clone(),
&CrateDetectionStatus::NoneDetectedForbidsUnsafe
)
);

assert_eq!(
string.clone().normal(),
colorize(
string.clone(),
&CrateDetectionStatus::NoneDetectedAllowsUnsafe
)
);

assert_eq!(
string.clone().red().bold(),
colorize(string.clone(), &CrateDetectionStatus::UnsafeDetected)
);
}
}
Loading

0 comments on commit 4113000

Please sign in to comment.