Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-16 - Further unit testing #126

Merged
merged 5 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
209 changes: 106 additions & 103 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion cargo-geiger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ assert_cmd = "1.0.1"
better-panic = "0.2.0"
fs_extra = "1.2.0"
insta = "0.16.1"
rand = "0.7.3"
regex = "1.3.9"
rstest = "0.6.4"
semver = "0.10.0"
Expand Down
128 changes: 96 additions & 32 deletions cargo-geiger/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::format::print::OutputFormat;
use crate::format::print_config::OutputFormat;
use crate::format::Charset;

use pico_args::Arguments;
use std::path::PathBuf;

pub const HELP: &str =
Expand Down Expand Up @@ -86,51 +87,52 @@ pub struct Args {
}

impl Args {
pub fn parse_args() -> Result<Args, Box<dyn std::error::Error>> {
let mut args = pico_args::Arguments::from_env();
pub fn parse_args(
mut raw_args: Arguments,
) -> Result<Args, Box<dyn std::error::Error>> {
let args = Args {
all: args.contains(["-a", "--all"]),
all_deps: args.contains("--all-dependencies"),
all_features: args.contains("--all-features"),
all_targets: args.contains("--all-targets"),
build_deps: args.contains("--build-dependencies"),
charset: args
all: raw_args.contains(["-a", "--all"]),
all_deps: raw_args.contains("--all-dependencies"),
all_features: raw_args.contains("--all-features"),
all_targets: raw_args.contains("--all-targets"),
build_deps: raw_args.contains("--build-dependencies"),
charset: raw_args
.opt_value_from_str("--charset")?
.unwrap_or(Charset::Utf8),
color: args.opt_value_from_str("--color")?,
dev_deps: args.contains("--dev-dependencies"),
features: args.opt_value_from_str("--features")?,
forbid_only: args.contains(["-f", "--forbid-only"]),
format: args
color: raw_args.opt_value_from_str("--color")?,
dev_deps: raw_args.contains("--dev-dependencies"),
features: raw_args.opt_value_from_str("--features")?,
forbid_only: raw_args.contains(["-f", "--forbid-only"]),
format: raw_args
.opt_value_from_str("--format")?
.unwrap_or_else(|| "{p}".to_string()),
frozen: args.contains("--frozen"),
help: args.contains(["-h", "--help"]),
include_tests: args.contains("--include-tests"),
invert: args.contains(["-i", "--invert"]),
locked: args.contains("--locked"),
manifest_path: args.opt_value_from_str("--manifest-path")?,
no_default_features: args.contains("--no-default-features"),
no_indent: args.contains("--no-indent"),
offline: args.contains("--offline"),
package: args.opt_value_from_str("--manifest-path")?,
prefix_depth: args.contains("--prefix-depth"),
quiet: args.contains(["-q", "--quiet"]),
target: args.opt_value_from_str("--target")?,
unstable_flags: args
frozen: raw_args.contains("--frozen"),
help: raw_args.contains(["-h", "--help"]),
include_tests: raw_args.contains("--include-tests"),
invert: raw_args.contains(["-i", "--invert"]),
locked: raw_args.contains("--locked"),
manifest_path: raw_args.opt_value_from_str("--manifest-path")?,
no_default_features: raw_args.contains("--no-default-features"),
no_indent: raw_args.contains("--no-indent"),
offline: raw_args.contains("--offline"),
package: raw_args.opt_value_from_str("--manifest-path")?,
prefix_depth: raw_args.contains("--prefix-depth"),
quiet: raw_args.contains(["-q", "--quiet"]),
target: raw_args.opt_value_from_str("--target")?,
unstable_flags: raw_args
.opt_value_from_str("-Z")?
.map(|s: String| s.split(' ').map(|s| s.to_owned()).collect())
.unwrap_or_else(Vec::new),
verbose: match (
args.contains("-vv"),
args.contains(["-v", "--verbose"]),
raw_args.contains("-vv"),
raw_args.contains(["-v", "--verbose"]),
) {
(false, false) => 0,
(false, true) => 1,
(true, _) => 2,
},
version: args.contains(["-V", "--version"]),
output_format: if args.contains("--json") {
version: raw_args.contains(["-V", "--version"]),
output_format: if raw_args.contains("--json") {
Some(OutputFormat::Json)
} else {
None
Expand All @@ -139,3 +141,65 @@ impl Args {
Ok(args)
}
}

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

use rstest::*;
use std::ffi::OsString;

#[rstest(
input_argument_vector,
expected_all,
expected_charset,
expected_verbose,
case(
vec![],
false,
Charset::Utf8,
0
),
case(
vec![OsString::from("--all")],
true,
Charset::Utf8,
0,
),
case(
vec![OsString::from("--charset"), OsString::from("ascii")],
false,
Charset::Ascii,
0
),
case(
vec![OsString::from("-v")],
false,
Charset::Utf8,
1
),
case(
vec![OsString::from("-vv")],
false,
Charset::Utf8,
2
)
)]
fn parse_args_test(
input_argument_vector: Vec<OsString>,
expected_all: bool,
expected_charset: Charset,
expected_verbose: u32,
) {
let args_result =
Args::parse_args(Arguments::from_vec(input_argument_vector));

assert!(args_result.is_ok());

let args = args_result.unwrap();

assert_eq!(args.all, expected_all);
assert_eq!(args.charset, expected_charset);
assert_eq!(args.verbose, expected_verbose)
}
}
12 changes: 7 additions & 5 deletions cargo-geiger/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ pub fn resolve<'a, 'cfg>(
mod cli_tests {
use super::*;

#[test]
use rstest::*;

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

Expand All @@ -135,11 +137,11 @@ mod cli_tests {
.filter(|cfg| matches!(cfg, Cfg::KeyPair(_, _)))
.collect();

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

#[test]
#[rstest]
fn get_registry_test() {
let config = Config::default().unwrap();
let workspace = Workspace::new(
Expand All @@ -162,7 +164,7 @@ mod cli_tests {
assert_eq!(package_set.sources().len(), 1);
}

#[test]
#[rstest]
fn get_workspace_test() {
let config = Config::default().unwrap();
let manifest_path: Option<PathBuf> = None;
Expand Down
10 changes: 7 additions & 3 deletions cargo-geiger/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod emoji_symbols;
pub mod pattern;
pub mod print;
pub mod print_config;
pub mod table;

mod display;
Expand All @@ -17,6 +17,7 @@ pub enum Charset {
Utf8,
}

#[derive(Debug, PartialEq)]
pub enum Chunk {
License,
Package,
Expand All @@ -43,6 +44,7 @@ pub enum CrateDetectionStatus {
UnsafeDetected,
}

#[derive(Debug, PartialEq)]
pub enum RawChunk<'a> {
Argument(&'a str),
Error(&'static str),
Expand Down Expand Up @@ -83,14 +85,16 @@ pub fn get_kind_group_name(dep_kind: DepKind) -> Option<&'static str> {
mod format_tests {
use super::*;

#[test]
use rstest::*;

#[rstest]
fn charset_from_str_test() {
assert_eq!(Charset::from_str("ascii"), Ok(Charset::Ascii));
assert_eq!(Charset::from_str("utf8"), Ok(Charset::Utf8));
assert_eq!(Charset::from_str("invalid_str"), Err("invalid charset"));
}

#[test]
#[rstest]
fn get_kind_group_name_test() {
assert_eq!(
get_kind_group_name(DepKind::Build),
Expand Down
84 changes: 77 additions & 7 deletions cargo-geiger/src/format/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ impl<'a> fmt::Display for Display<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
for chunk in &self.pattern.0 {
match *chunk {
Chunk::Raw(ref s) => (fmt.write_str(s))?,
Chunk::License => {
if let Some(ref license) = self.metadata.license {
(write!(fmt, "{}", license))?
}
}
Chunk::Package => {
(write!(
fmt,
Expand All @@ -24,19 +28,85 @@ impl<'a> fmt::Display for Display<'a> {
self.package.version()
))?
}
Chunk::License => {
if let Some(ref license) = self.metadata.license {
(write!(fmt, "{}", license))?
}
}
Chunk::Raw(ref s) => (fmt.write_str(s))?,
Chunk::Repository => {
if let Some(ref repository) = self.metadata.repository {
(write!(fmt, "{}", repository))?
}
}
}
}

Ok(())
}
}

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

use crate::format::pattern::Pattern;
use crate::format::Chunk;

use cargo::core::manifest::ManifestMetadata;
use cargo::core::{PackageId, SourceId};
use cargo::util::ToSemver;
use rstest::*;

#[rstest(
input_pattern,
expected_formatted_string,
case(
Pattern(vec![Chunk::License]),
"licence_string"
),
case(
Pattern(vec![Chunk::Package]),
"package_name 1.2.3"
),
case(
Pattern(vec![Chunk::Raw(String::from("chunk_value"))]),
"chunk_value"
),
case(
Pattern(vec![Chunk::Repository]),
"repository_string"
)
)]
fn display_format_fmt_test(
input_pattern: Pattern,
expected_formatted_string: &str,
) {
let package_id = PackageId::new(
"package_name",
"1.2.3".to_semver().unwrap(),
SourceId::from_url(
"git+https://github.com/rust-secure-code/cargo-geiger",
)
.unwrap(),
)
.unwrap();

let manifest_metadata = ManifestMetadata {
authors: vec![],
keywords: vec![],
categories: vec![],
license: Some(String::from("licence_string")),
license_file: None,
description: None,
readme: None,
homepage: None,
repository: Some(String::from("repository_string")),
documentation: None,
badges: Default::default(),
links: None,
};

let display = Display {
pattern: &input_pattern,
package: &package_id,
metadata: &manifest_metadata,
};

assert_eq!(format!("{}", display), expected_formatted_string);
}
}
39 changes: 39 additions & 0 deletions cargo-geiger/src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,42 @@ impl<'a> Iterator for Parser<'a> {
}
}
}

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

use rstest::*;

#[rstest]
fn parser_new_test() {
let parser_s = "parser_string";
let parser = Parser::new(parser_s);
assert_eq!(parser.s, parser_s);
}

#[rstest]
fn parser_argument_test() {
let mut parser = Parser::new("parser 1.2.3");
let raw_chunk = parser.argument();
assert_eq!(raw_chunk, RawChunk::Argument("parser"));
}

#[rstest(
input_s_string,
expected_name_string,
case("parser 1.2.3", "parser"),
case("1.2.3 parser", "")
)]
fn parser_name_test(input_s_string: &str, expected_name_string: &str) {
let mut parser = Parser::new(input_s_string);
assert_eq!(parser.name(), expected_name_string)
}

#[rstest]
fn parser_text_test() {
let parser_s = "parser 1.2.3";
let mut parser = Parser::new(parser_s);
assert_eq!(parser.text(0), RawChunk::Text(parser_s));
}
}
Loading