Skip to content

Commit

Permalink
Merge pull request #192 from jmcconnell26/NOISSUE-AddFutherTesting
Browse files Browse the repository at this point in the history
NOISSUE - Add futher testing
  • Loading branch information
anderejd authored Feb 28, 2021
2 parents 917dfc3 + 0cf1f5f commit fcfa610
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 103 deletions.
1 change: 0 additions & 1 deletion 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-geiger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ cargo_metadata = "0.12.3"
cargo-platform = "0.1.1"
colored = "2.0.0"
console = "0.11.3"
env_logger = "0.8.2"
geiger = { path = "../geiger", version = "0.4.6" }
krates = "0.5.0"
petgraph = "0.5.1"
Expand Down
48 changes: 29 additions & 19 deletions cargo-geiger/src/format/print_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cargo::core::shell::Verbosity;
use cargo::util::errors::CliError;
use colored::{ColoredString, Colorize};
use geiger::IncludeTests;
use petgraph::EdgeDirection;
use petgraph::{Direction, EdgeDirection};
use strum_macros::EnumString;

#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down Expand Up @@ -54,10 +54,9 @@ impl PrintConfig {
// TODO: Add command line flag for this and make it default to false?
let allow_partial_results = true;

let direction = if args.invert {
EdgeDirection::Incoming
} else {
EdgeDirection::Outgoing
let direction = match args.invert {
true => EdgeDirection::Incoming,
false => EdgeDirection::Outgoing,
};

let format = Pattern::try_build(&args.format).map_err(|e| {
Expand All @@ -70,24 +69,20 @@ impl PrintConfig {
)
})?;

let include_tests = if args.include_tests {
IncludeTests::Yes
} else {
IncludeTests::No
let include_tests = match args.include_tests {
true => IncludeTests::Yes,
false => IncludeTests::No,
};

let prefix = if args.prefix_depth {
Prefix::Depth
} else if args.no_indent {
Prefix::None
} else {
Prefix::Indent
let prefix = match (args.prefix_depth, args.no_indent) {
(true, _) => Prefix::Depth,
(false, true) => Prefix::None,
(false, false) => Prefix::Indent,
};

let verbosity = if args.verbose == 0 {
Verbosity::Normal
} else {
Verbosity::Verbose
let verbosity = match args.verbose {
0 => Verbosity::Normal,
_ => Verbosity::Verbose,
};

Ok(PrintConfig {
Expand All @@ -103,6 +98,21 @@ impl PrintConfig {
}
}

impl Default for PrintConfig {
fn default() -> Self {
PrintConfig {
all: false,
allow_partial_results: false,
direction: Direction::Outgoing,
format: Pattern::try_build("p").unwrap(),
include_tests: IncludeTests::Yes,
prefix: Prefix::Depth,
output_format: Default::default(),
verbosity: Verbosity::Verbose,
}
}
}

pub fn colorize(
crate_detection_status: &CrateDetectionStatus,
output_format: OutputFormat,
Expand Down
79 changes: 78 additions & 1 deletion cargo-geiger/src/format/table/handle_text_tree_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use super::TableParameters;
use super::{table_row, table_row_empty};

use cargo_metadata::{DependencyKind, PackageId};
use colored::ColoredString;
use std::collections::HashSet;
use std::fmt::Display;

pub struct HandlePackageParameters<'a> {
pub total_package_counts: &'a mut TotalPackageCounts,
Expand Down Expand Up @@ -112,6 +114,28 @@ pub fn handle_text_tree_line_package(
),
);

let line = construct_package_text_tree_line(
crate_detection_status,
emoji_symbols,
icon,
package_name,
table_parameters,
tree_vines,
unsafe_info,
);

table_lines.push(line);
}

fn construct_package_text_tree_line(
crate_detection_status: CrateDetectionStatus,
emoji_symbols: &EmojiSymbols,
icon: Box<dyn Display>,
package_name: ColoredString,
table_parameters: &TableParameters,
tree_vines: String,
unsafe_info: ColoredString,
) -> String {
let shift_chars = unsafe_info.chars().count() + 4;

let mut line = String::new();
Expand Down Expand Up @@ -139,7 +163,7 @@ pub fn handle_text_tree_line_package(
line.push(' ');
}

table_lines.push(format!("{} {}{}", line, tree_vines, package_name));
format!("{} {}{}", line, tree_vines, package_name)
}

fn get_crate_detection_status_and_update_package_counts(
Expand Down Expand Up @@ -174,6 +198,8 @@ fn get_crate_detection_status_and_update_package_counts(
mod handle_text_tree_line_tests {
use super::*;

use crate::format::print_config::PrintConfig;
use colored::Colorize;
use rstest::*;

#[rstest(
Expand Down Expand Up @@ -219,6 +245,57 @@ mod handle_text_tree_line_tests {
}
}

#[rstest(
input_crate_detection_status,
input_output_format,
input_symbol_kind,
expected_package_text_tree_line,
case(
CrateDetectionStatus::NoneDetectedForbidsUnsafe,
OutputFormat::GitHubMarkdown,
SymbolKind::Lock,
String::from("unsafe_info 🔒 tree_vinespackage_name")
),
case(
CrateDetectionStatus::UnsafeDetected,
OutputFormat::GitHubMarkdown,
SymbolKind::Rads,
String::from("unsafe_info ☢\u{fe0f} tree_vinespackage_name")
)
)]
fn construct_package_text_tree_line_test(
input_crate_detection_status: CrateDetectionStatus,
input_output_format: OutputFormat,
input_symbol_kind: SymbolKind,
expected_package_text_tree_line: String,
) {
let emoji_symbols = EmojiSymbols::new(input_output_format);
let icon = emoji_symbols.emoji(input_symbol_kind);
let package_name = String::from("package_name").normal();
let table_parameters = TableParameters {
geiger_context: &Default::default(),
print_config: &PrintConfig {
output_format: input_output_format,
..Default::default()
},
rs_files_used: &Default::default(),
};
let tree_vines = String::from("tree_vines");
let unsafe_info = ColoredString::from("unsafe_info").normal();

let package_text_tree_line = construct_package_text_tree_line(
input_crate_detection_status,
&emoji_symbols,
icon,
package_name,
&table_parameters,
tree_vines,
unsafe_info,
);

assert_eq!(package_text_tree_line, expected_package_text_tree_line);
}

#[rstest(
input_crate_forbids_unsafe,
input_total_inc,
Expand Down
21 changes: 21 additions & 0 deletions cargo-geiger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ pub mod scan;
mod format;
/// Tree construction
mod tree;

#[cfg(test)]
mod lib_tests {
use cargo_metadata::{CargoOpt, Metadata, MetadataCommand};
use krates::Builder as KratesBuilder;
use krates::Krates;

pub fn construct_krates_and_metadata() -> (Krates, Metadata) {
let metadata = MetadataCommand::new()
.manifest_path("./Cargo.toml")
.features(CargoOpt::AllFeatures)
.exec()
.unwrap();

let krates = KratesBuilder::new()
.build_with_metadata(metadata.clone(), |_| ())
.unwrap();

(krates, metadata)
}
}
1 change: 0 additions & 1 deletion cargo-geiger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ fn real_main(args: &Args, config: &mut Config) -> CliResult {
}

fn main() {
env_logger::init();
let mut config = match Config::default() {
Ok(cfg) => cfg,
Err(e) => {
Expand Down
4 changes: 3 additions & 1 deletion cargo-geiger/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use cargo_metadata::Metadata;
use std::collections::HashSet;
use std::path::PathBuf;

use cargo_geiger_serde::DependencyKind as CargoGeigerSerdeDependencyKind;

/// Holds a pointer to both a `Krates` graph, and the `Metadata` struct
/// which are often required together
pub struct CargoMetadataParameters<'a> {
Expand Down Expand Up @@ -71,7 +73,7 @@ pub trait ToCargoCoreDepKind {
pub trait ToCargoGeigerDependencyKind {
fn to_cargo_geiger_dependency_kind(
&self,
) -> Option<cargo_geiger_serde::DependencyKind>;
) -> Option<CargoGeigerSerdeDependencyKind>;
}

pub trait ToCargoGeigerPackageId {
Expand Down
29 changes: 16 additions & 13 deletions cargo-geiger/src/mapping/geiger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use super::{ToCargoGeigerSource, ToCargoMetadataPackage};
use cargo_metadata::Metadata;
use url::Url;

impl ToCargoGeigerSource for cargo_metadata::PackageId {
use cargo_geiger_serde::Source as CargoGeigerSerdeSource;
use cargo_metadata::PackageId as CargoMetadataPackageId;

impl ToCargoGeigerSource for CargoMetadataPackageId {
fn to_cargo_geiger_source(
&self,
metadata: &Metadata,
) -> cargo_geiger_serde::Source {
) -> CargoGeigerSerdeSource {
let package = self.to_cargo_metadata_package(metadata).unwrap();

match package.source {
Expand All @@ -17,14 +20,14 @@ impl ToCargoGeigerSource for cargo_metadata::PackageId {
}
}

fn handle_source_repr(source_repr: &str) -> cargo_geiger_serde::Source {
fn handle_source_repr(source_repr: &str) -> CargoGeigerSerdeSource {
let mut source_repr_vec = source_repr.split('+').collect::<Vec<&str>>();

let source_type = source_repr_vec[0];

match source_type {
"registry" => {
cargo_geiger_serde::Source::Registry {
CargoGeigerSerdeSource::Registry {
// It looks like cargo metadata drops this information
name: String::from("crates.io"),
url: Url::parse(source_repr_vec.pop().unwrap()).unwrap(),
Expand All @@ -45,7 +48,7 @@ fn handle_source_repr(source_repr: &str) -> cargo_geiger_serde::Source {
.unwrap()
.1;

cargo_geiger_serde::Source::Git {
CargoGeigerSerdeSource::Git {
url: Url::parse(&git_url_without_query).unwrap(),
rev: String::from(revision),
}
Expand All @@ -57,8 +60,8 @@ fn handle_source_repr(source_repr: &str) -> cargo_geiger_serde::Source {
}

fn handle_path_source(
package_id: &cargo_metadata::PackageId,
) -> cargo_geiger_serde::Source {
package_id: &CargoMetadataPackageId,
) -> CargoGeigerSerdeSource {
let raw_repr = package_id.repr.clone();
let raw_path_repr = raw_repr[1..raw_repr.len() - 1]
.split("+file://")
Expand All @@ -74,7 +77,7 @@ fn handle_path_source(
source_url = Url::from_file_path(raw_path_repr).unwrap();
}

cargo_geiger_serde::Source::Path(source_url)
CargoGeigerSerdeSource::Path(source_url)
}

#[cfg(test)]
Expand All @@ -89,22 +92,22 @@ mod geiger_tests {
expected_source,
case(
"registry+https://github.com/rust-lang/crates.io-index",
cargo_geiger_serde::Source::Registry {
CargoGeigerSerdeSource::Registry {
name: String::from("crates.io"),
url: Url::parse("https://github.com/rust-lang/crates.io-index").unwrap()
}
),
case(
"git+https://github.com/rust-itertools/itertools.git?rev=8761fbefb3b209",
cargo_geiger_serde::Source::Git {
CargoGeigerSerdeSource::Git {
url: Url::parse("https://github.com/rust-itertools/itertools.git").unwrap(),
rev: String::from("8761fbefb3b209")
}
)
)]
fn handle_source_repr_test(
input_source_repr: &str,
expected_source: cargo_geiger_serde::Source,
expected_source: CargoGeigerSerdeSource,
) {
let source = handle_source_repr(input_source_repr);
assert_eq!(source, expected_source);
Expand All @@ -113,11 +116,11 @@ mod geiger_tests {
#[rstest]
fn handle_path_source_test() {
if !cfg!(windows) {
let package_id = cargo_metadata::PackageId {
let package_id = CargoMetadataPackageId {
repr: String::from("(path+file:///cargo_geiger/test_crates/test1_package_with_no_deps)"),
};

let expected_source = cargo_geiger_serde::Source::Path(
let expected_source = CargoGeigerSerdeSource::Path(
Url::from_file_path(
"/cargo_geiger/test_crates/test1_package_with_no_deps",
)
Expand Down
18 changes: 2 additions & 16 deletions cargo-geiger/src/mapping/krates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ impl QueryResolve for Krates {
#[cfg(test)]
mod krates_tests {
use super::*;
use crate::lib_tests::construct_krates_and_metadata;

use cargo_metadata::{CargoOpt, Metadata, MetadataCommand, Version};
use krates::Builder as KratesBuilder;
use cargo_metadata::Version;
use rstest::*;

#[rstest]
Expand Down Expand Up @@ -157,18 +157,4 @@ mod krates_tests {

assert_eq!(package_version, expected_package_version);
}

fn construct_krates_and_metadata() -> (Krates, Metadata) {
let metadata = MetadataCommand::new()
.manifest_path("./Cargo.toml")
.features(CargoOpt::AllFeatures)
.exec()
.unwrap();

let krates = KratesBuilder::new()
.build_with_metadata(metadata.clone(), |_| ())
.unwrap();

(krates, metadata)
}
}
Loading

0 comments on commit fcfa610

Please sign in to comment.