Skip to content

Commit

Permalink
ISSUE-151 - Add new GitHubMarkdown output type:
Browse files Browse the repository at this point in the history
* Update README integration test snapshots
* Adding report for `cargo-geiger` to README

Signed-off-by: joshmc <josh-mcc@tiscali.co.uk>
  • Loading branch information
jmcconnell26 committed Dec 19, 2020
1 parent ba85801 commit 44b1c62
Show file tree
Hide file tree
Showing 27 changed files with 678 additions and 194 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

304 changes: 304 additions & 0 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cargo-geiger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pico-args = "0.3.3"
regex = "1.4.2"
serde = { version = "1.0.116", features = ["derive"] }
serde_json = "1.0.57"
strum = "0.19.2"
strum_macros = "0.19.2"
strum = "0.20.0"
strum_macros = "0.20.1"
walkdir = "2.3.1"
anyhow = "1.0.31"
url = "2.1.1"
Expand Down
65 changes: 41 additions & 24 deletions cargo-geiger/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::format::print_config::OutputFormat;
use crate::format::Charset;

use cargo::core::shell::ColorChoice;
use cargo::{CliResult, Config};
Expand Down Expand Up @@ -29,11 +28,10 @@ OPTIONS:
than a tree), but prefixed with the depth.
-a, --all Don't truncate dependencies that have already
been displayed.
--charset <CHARSET> Character set to use in output: utf8, ascii
[default: utf8].
--format <FORMAT> Format string used for printing dependencies
[default: {p}].
--json Output in JSON format.
--output-format Output format for the report: Ascii, GitHubMarkdown,
Json, Utf8 [default: Utf8]
--update-readme Writes output to ./README.md. Looks for a Safety
Report section, replaces if found, adds if not.
Throws an error if no README.md exists.
Expand Down Expand Up @@ -67,7 +65,6 @@ OPTIONS:
#[derive(Default)]
pub struct Args {
pub all: bool,
pub charset: Charset,
pub color: Option<String>,
pub deps_args: DepsArgs,
pub features_args: FeaturesArgs,
Expand All @@ -81,6 +78,7 @@ pub struct Args {
pub manifest_path: Option<PathBuf>,
pub no_indent: bool,
pub offline: bool,
pub output_format: OutputFormat,
pub package: Option<String>,
pub prefix_depth: bool,
pub quiet: bool,
Expand All @@ -89,7 +87,6 @@ pub struct Args {
pub unstable_flags: Vec<String>,
pub verbose: u32,
pub version: bool,
pub output_format: Option<OutputFormat>,
}

impl Args {
Expand All @@ -103,11 +100,8 @@ impl Args {
pub fn parse_args(
mut raw_args: Arguments,
) -> Result<Args, Box<dyn std::error::Error>> {
let args = Args {
let mut args = Args {
all: raw_args.contains(["-a", "--all"]),
charset: raw_args
.opt_value_from_str("--charset")?
.unwrap_or(Charset::Utf8),
color: raw_args.opt_value_from_str("--color")?,
deps_args: DepsArgs {
all_deps: raw_args.contains("--all-dependencies"),
Expand Down Expand Up @@ -158,12 +152,22 @@ impl Args {
(true, _) => 2,
},
version: raw_args.contains(["-V", "--version"]),
output_format: if raw_args.contains("--json") {
Some(OutputFormat::Json)
} else {
None
},
output_format: raw_args
.opt_value_from_str("--output-format")?
.unwrap_or(OutputFormat::Utf8),
};

if args.readme_args.update_readme
&& args.output_format != OutputFormat::GitHubMarkdown
{
eprintln!(
"OutputFormat has been specified as {:?}, but the `--update-readme` flag has also been provided. \
To ensure the report written to the README.md is correct, a reduced charset will be used.",
args.output_format
);
args.output_format = OutputFormat::GitHubMarkdown
}

Ok(args)
}

Expand Down Expand Up @@ -244,50 +248,63 @@ fn parse_features(raw_features: Option<String>) -> Vec<String> {
pub mod args_tests {
use super::*;

use cargo::core::shell::ColorChoice;
use cargo::core::Verbosity;
use rstest::*;
use std::ffi::OsString;

#[rstest(
input_argument_vector,
expected_all,
expected_charset,
expected_output_format,
expected_verbose,
case(
vec![],
false,
Charset::Utf8,
OutputFormat::Utf8,
0
),
case(
vec![OsString::from("--all")],
true,
Charset::Utf8,
OutputFormat::Utf8,
0,
),
case(
vec![OsString::from("--charset"), OsString::from("ascii")],
vec![OsString::from("--output-format"), OsString::from("Ascii")],
false,
Charset::Ascii,
OutputFormat::Ascii,
0
),
case(
vec![OsString::from("-v")],
false,
Charset::Utf8,
OutputFormat::Utf8,
1
),
case(
vec![OsString::from("-vv")],
false,
Charset::Utf8,
OutputFormat::Utf8,
2
),
case(
vec![OsString::from("--update-readme")],
false,
OutputFormat::GitHubMarkdown,
0
),
case(
vec![OsString::from("--update-readme"), OsString::from("--output-format"), OsString::from("Ascii")],
false,
OutputFormat::GitHubMarkdown,
0
)
)]
fn parse_args_test(
input_argument_vector: Vec<OsString>,
expected_all: bool,
expected_charset: Charset,
expected_output_format: OutputFormat,
expected_verbose: u32,
) {
let args_result =
Expand All @@ -298,7 +315,7 @@ pub mod args_tests {
let args = args_result.unwrap();

assert_eq!(args.all, expected_all);
assert_eq!(args.charset, expected_charset);
assert_eq!(args.output_format, expected_output_format);
assert_eq!(args.verbose, expected_verbose)
}

Expand Down
24 changes: 18 additions & 6 deletions cargo-geiger/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use strum_macros::EnumIter;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Charset {
Ascii,
GitHubMarkdown,
Utf8,
}

Expand All @@ -35,8 +36,10 @@ impl FromStr for Charset {
type Err = &'static str;

fn from_str(s: &str) -> Result<Charset, &'static str> {
match s {
let comparison_string = String::from(s).to_lowercase();
match comparison_string.as_str() {
"ascii" => Ok(Charset::Ascii),
"githubmarkdown" => Ok(Charset::GitHubMarkdown),
"utf8" => Ok(Charset::Utf8),
_ => Err("invalid charset"),
}
Expand Down Expand Up @@ -94,11 +97,20 @@ mod format_tests {

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"));
#[rstest(
input_string,
expected_enum_result,
case("ascii", Ok(Charset::Ascii)),
case("githubmarkdown", Ok(Charset::GitHubMarkdown)),
case("utf8", Ok(Charset::Utf8)),
case("UTF8", Ok(Charset::Utf8)),
case("invalid_str", Err("invalid charset"))
)]
fn charset_from_str_test(
input_string: &str,
expected_enum_result: Result<Charset, &'static str>,
) {
assert_eq!(Charset::from_str(input_string), expected_enum_result);
}

#[rstest]
Expand Down
36 changes: 27 additions & 9 deletions cargo-geiger/src/format/emoji_symbols.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::format::{Charset, SymbolKind};
use crate::format::print_config::{colorize, OutputFormat};
use crate::format::{CrateDetectionStatus, SymbolKind};

use colored::Colorize;
use colored::ColoredString;

pub struct EmojiSymbols {
charset: Charset,
emojis: [&'static str; 3],
fallbacks: [colored::ColoredString; 3],
fallbacks: [ColoredString; 3],
output_format: OutputFormat,
}

impl EmojiSymbols {
Expand All @@ -18,16 +19,33 @@ impl EmojiSymbols {
}
}

pub fn new(charset: Charset) -> EmojiSymbols {
pub fn new(output_format: OutputFormat) -> EmojiSymbols {
Self {
charset,
emojis: ["🔒", "❓", "☢️"],
fallbacks: [":)".green(), "?".normal(), "!".red().bold()],
fallbacks: [
colorize(
&CrateDetectionStatus::NoneDetectedForbidsUnsafe,
output_format,
String::from(":)"),
),
colorize(
&CrateDetectionStatus::NoneDetectedAllowsUnsafe,
output_format,
String::from("?"),
),
colorize(
&CrateDetectionStatus::UnsafeDetected,
output_format,
String::from("!"),
),
],
output_format,
}
}

pub fn will_output_emoji(&self) -> bool {
self.charset == Charset::Utf8
&& console::Term::stdout().features().wants_emoji()
(self.output_format == OutputFormat::Utf8
&& console::Term::stdout().features().wants_emoji())
|| self.output_format == OutputFormat::GitHubMarkdown
}
}
Loading

0 comments on commit 44b1c62

Please sign in to comment.