Skip to content

Commit

Permalink
ISSUE-16 - Adding further unit testing
Browse files Browse the repository at this point in the history
ISSUE-16 - Adding in further unit tests:
* Pulling up unit test coverage to just under 70%
* Splitting out traversal module into submodules
* Making cargo clippy suggestions
* Running cargo fmt

Signed-off-by: joshmc <josh-mcc@tiscali.co.uk>
  • Loading branch information
jmcconnell26 committed Oct 25, 2020
1 parent ad87c27 commit a058a85
Show file tree
Hide file tree
Showing 20 changed files with 1,159 additions and 439 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions cargo-geiger-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ mod source;

pub use package_id::PackageId;
pub use report::{
Count, CounterBlock, DependencyKind, PackageInfo, QuickReportEntry, QuickSafetyReport,
ReportEntry, SafetyReport, UnsafeInfo,
Count, CounterBlock, DependencyKind, PackageInfo, QuickReportEntry,
QuickSafetyReport, ReportEntry, SafetyReport, UnsafeInfo,
};
pub use source::Source;

4 changes: 3 additions & 1 deletion cargo-geiger-serde/src/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use semver::Version;
use serde::{Deserialize, Serialize};

/// Identifies a package in the dependency tree
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[derive(
Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
)]
pub struct PackageId {
/// Package name
pub name: String,
Expand Down
18 changes: 6 additions & 12 deletions cargo-geiger-serde/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,9 @@ impl Entry for QuickReportEntry {
mod entry_serde {
use crate::PackageId;
use serde::{
ser::SerializeSeq,
Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
collections::HashMap,
fmt,
marker::PhantomData,
ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer,
};
use std::{collections::HashMap, fmt, marker::PhantomData};

pub(super) fn serialize<T, S>(
map: &HashMap<PackageId, T>,
Expand All @@ -219,7 +214,9 @@ mod entry_serde {
seq.end()
}

pub(super) fn deserialize<'de, T, D>(deserializer: D) -> Result<HashMap<PackageId, T>, D::Error>
pub(super) fn deserialize<'de, T, D>(
deserializer: D,
) -> Result<HashMap<PackageId, T>, D::Error>
where
T: Deserialize<'de> + super::Entry,
D: Deserializer<'de>,
Expand Down Expand Up @@ -253,10 +250,7 @@ mod entry_serde {
}

mod set_serde {
use serde::{
ser::SerializeSeq,
Serialize, Serializer,
};
use serde::{ser::SerializeSeq, Serialize, Serializer};
use std::collections::HashSet;

pub(super) fn serialize<T, S>(
Expand Down
14 changes: 5 additions & 9 deletions cargo-geiger-serde/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ use serde::{Deserialize, Serialize};
use url::Url;

/// Source of a package (where it is fetched from)
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[derive(
Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
)]
pub enum Source {
Git {
url: Url,
rev: String,
},
Registry {
name: String,
url: Url,
},
Git { url: Url, rev: String },
Registry { name: String, url: Url },
Path(Url),
}
1 change: 1 addition & 0 deletions cargo-geiger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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
25 changes: 24 additions & 1 deletion cargo-geiger/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ pub fn resolve<'a, 'cfg>(
#[cfg(test)]
mod cli_tests {
use super::*;

use rstest::*;

#[rstest]
Expand Down Expand Up @@ -179,4 +178,28 @@ mod cli_tests {

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

#[rstest]
fn resolve_test() {
let config = Config::default().unwrap();
let manifest_path: Option<PathBuf> = None;
let workspace = get_workspace(&config, manifest_path).unwrap();
let package = workspace.current().unwrap();
let mut registry = get_registry(&config, &package).unwrap();

let features: Vec<String> = vec![];
let all_features = false;
let no_default_features = false;

let resolve_cargo_result = resolve(
package.package_id(),
&mut registry,
&workspace,
&features,
all_features,
no_default_features,
);

assert!(resolve_cargo_result.is_ok());
}
}
84 changes: 19 additions & 65 deletions cargo-geiger/src/format/print_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,12 @@ mod print_config_tests {
#[rstest(
input_invert_bool,
expected_edge_direction,
case(
true,
EdgeDirection::Incoming
),
case(
false,
EdgeDirection::Outgoing
)
case(true, EdgeDirection::Incoming),
case(false, EdgeDirection::Outgoing)
)]
fn print_config_new_test_invert(
input_invert_bool: bool,
expected_edge_direction: EdgeDirection
expected_edge_direction: EdgeDirection,
) {
let mut args = create_args();
args.invert = input_invert_bool;
Expand All @@ -143,18 +137,12 @@ mod print_config_tests {
#[rstest(
input_include_tests_bool,
expected_include_tests,
case(
true,
IncludeTests::Yes
),
case(
false,
IncludeTests::No
),
case(true, IncludeTests::Yes),
case(false, IncludeTests::No)
)]
fn print_config_new_test_include_tests(
input_include_tests_bool: bool,
expected_include_tests: IncludeTests
expected_include_tests: IncludeTests,
) {
let mut args = create_args();
args.include_tests = input_include_tests_bool;
Expand All @@ -172,31 +160,15 @@ mod print_config_tests {
input_prefix_depth_bool,
input_no_indent_bool,
expected_output_prefix,
case(
true,
false,
Prefix::Depth,
),
case(
true,
false,
Prefix::Depth,
),
case(
false,
true,
Prefix::None,
),
case(
false,
false,
Prefix::Indent,
),
case(true, false, Prefix::Depth,),
case(true, false, Prefix::Depth,),
case(false, true, Prefix::None,),
case(false, false, Prefix::Indent,)
)]
fn print_config_new_test_prefix(
input_prefix_depth_bool: bool,
input_no_indent_bool: bool,
expected_output_prefix: Prefix
expected_output_prefix: Prefix,
) {
let mut args = create_args();
args.prefix_depth = input_prefix_depth_bool;
Expand All @@ -205,27 +177,15 @@ mod print_config_tests {
let print_config_result = PrintConfig::new(&args);

assert!(print_config_result.is_ok());
assert_eq!(
print_config_result.unwrap().prefix,
expected_output_prefix
);
assert_eq!(print_config_result.unwrap().prefix, expected_output_prefix);
}

#[rstest(
input_verbosity_u32,
expected_verbosity,
case(
0,
Verbosity::Normal
),
case(
1,
Verbosity::Verbose
),
case(
1,
Verbosity::Verbose
)
case(0, Verbosity::Normal),
case(1, Verbosity::Verbose),
case(1, Verbosity::Verbose)
)]
fn print_config_new_test_verbosity(
input_verbosity_u32: u32,
Expand All @@ -237,10 +197,7 @@ mod print_config_tests {
let print_config_result = PrintConfig::new(&args);

assert!(print_config_result.is_ok());
assert_eq!(
print_config_result.unwrap().verbosity,
expected_verbosity
);
assert_eq!(print_config_result.unwrap().verbosity, expected_verbosity);
}

#[rstest(
Expand All @@ -266,16 +223,13 @@ mod print_config_tests {
let string_value = String::from("string_value");

assert_eq!(
colorize(
string_value,
&input_crate_detection_status
),
colorize(string_value, &input_crate_detection_status),
expected_colorized_string
);
}

fn create_args() -> Args {
Args{
Args {
all: false,
all_deps: false,
all_features: false,
Expand Down Expand Up @@ -303,7 +257,7 @@ mod print_config_tests {
unstable_flags: vec![],
verbose: 0,
version: false,
output_format: None
output_format: None,
}
}
}
6 changes: 2 additions & 4 deletions cargo-geiger/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,7 @@ mod graph_tests {
let result = build_graph_prerequisites(&args, &config_host);

assert!(result.is_ok());

let (extra_deps, _) = result.unwrap();

assert_eq!(extra_deps, expected_extra_deps);
}

Expand Down Expand Up @@ -294,7 +292,7 @@ mod graph_tests {
}

fn create_args() -> Args {
Args{
Args {
all: false,
all_deps: false,
all_features: false,
Expand Down Expand Up @@ -322,7 +320,7 @@ mod graph_tests {
unstable_flags: vec![],
verbose: 0,
version: false,
output_format: None
output_format: None,
}
}
}
35 changes: 31 additions & 4 deletions cargo-geiger/src/rs_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ use walkdir::{DirEntry, WalkDir};
/// The wrapped PathBufs are canonicalized.
#[derive(Debug, PartialEq)]
pub enum RsFile {
/// Library entry point source file, usually src/lib.rs
LibRoot(PathBuf),

/// Executable entry point source file, usually src/main.rs
BinRoot(PathBuf),

/// Not sure if this is relevant but let's be conservative for now.
CustomBuildRoot(PathBuf),

/// Library entry point source file, usually src/lib.rs
LibRoot(PathBuf),

/// All other .rs files.
Other(PathBuf),
}
Expand Down Expand Up @@ -97,6 +97,15 @@ pub fn into_rs_code_file(kind: &TargetKind, path: PathBuf) -> RsFile {
}
}

pub fn into_is_entry_point_and_path_buf(rs_file: RsFile) -> (bool, PathBuf) {
match rs_file {
RsFile::BinRoot(pb) => (true, pb),
RsFile::CustomBuildRoot(pb) => (true, pb),
RsFile::LibRoot(pb) => (true, pb),
RsFile::Other(pb) => (false, pb),
}
}

pub fn is_file_with_ext(entry: &DirEntry, file_ext: &str) -> bool {
if !entry.file_type().is_file() {
return false;
Expand Down Expand Up @@ -307,7 +316,7 @@ mod rs_file_tests {
)]
fn into_rs_code_file_test(
input_target_kind: TargetKind,
expected_rs_file: RsFile
expected_rs_file: RsFile,
) {
let path_buf = Path::new("test_path.ext").to_path_buf();

Expand All @@ -317,6 +326,24 @@ mod rs_file_tests {
);
}

#[rstest(
input_rs_file,
expected_is_entry_point,
case(RsFile::BinRoot(PathBuf::from("test.txt")), true),
case(RsFile::CustomBuildRoot(PathBuf::from("test.txt")), true),
case(RsFile::LibRoot(PathBuf::from("test.txt")), true),
case(RsFile::Other(PathBuf::from("test.txt")), false)
)]
fn into_is_entry_point_and_path_buf_test(
input_rs_file: RsFile,
expected_is_entry_point: bool,
) {
let (is_entry_point, _path_buf) =
into_is_entry_point_and_path_buf(input_rs_file);
assert_eq!(is_entry_point, expected_is_entry_point);
assert_eq!(_path_buf, PathBuf::from("test.txt"));
}

#[rstest]
fn is_file_with_ext_test() {
let config = Config::default().unwrap();
Expand Down
Loading

0 comments on commit a058a85

Please sign in to comment.