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

Conversation

jmcconnell26
Copy link
Contributor

Adding further unit testing + small refactoring.
Waiting until #125 is merged, and can then merge into this branch.

* Update existing testing to use rstest
* Add cases for tests where appropriate
* Pulling out sub-module from format/table
* Improve general test coverage
@anderejd
Copy link
Contributor

#125 is merged now.

@jmcconnell26
Copy link
Contributor Author

Hi @anderejd,

I've merged in the branch to master, and this is now ready for review.
I was wondering if you would have any objections to integrating some code coverage metrics with the CI/CD pipeline?

Thanks,
Josh

@jmcconnell26
Copy link
Contributor Author

I've raised a PR here to add code coverage metrics to the pipeline here #127, which can hopefully be merged in any order!

Thanks,
Josh

@@ -20,9 +20,11 @@ cargo-platform = "0.1.1"
colored = "2.0.0"
console = "0.11.3"
env_logger = "0.7.1"
fake = { version = "2.2", features=["derive"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this library, seems obfuscate the code rather than add value, in my opinion.
How would the code look without the new fake dependency?

Convince me that I'm wrong :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was 50/50 on this library, I really didn't like the fact that it didn't support being added as a dev-dependency for something which was only used in testing.
I came down on the side of including it for how much simpler it made creating large structs in unit tests, for example https://github.com/rust-secure-code/cargo-geiger/pull/126/files#diff-373099d9a5cf5c2a276c879a94c76bdbd5dab804f2dca1ad72776f3b6ea4cb7bR195 for the relative cost of adding derive statement.
If I wasn't to use this, I'd probably create a fixture method with rstest to initialise the default struct, and then inject it into each test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was 50/50 on this library, I really didn't like the fact that it didn't support being added as a dev-dependency for something which was only used in testing.

This alone makes it unfit for use, imho. Making users wait for extra bloat to compile while installing is a bad user experience. Let's go for some extra manually written test code and avoid the fake crate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that definitely makes sense, I've removed it in the last commit.

Signed-off-by: joshmc <josh-mcc@tiscali.co.uk>
Comment on lines +220 to +245
#[rstest(
input_none_detected_forbids_unsafe,
input_none_detected_allows_unsafe,
input_unsafe_detected,
expected_crate_detection_status,
case(0, 0, 1, CrateDetectionStatus::UnsafeDetected),
case(1, 0, 0, CrateDetectionStatus::NoneDetectedForbidsUnsafe),
case(4, 1, 0, CrateDetectionStatus::NoneDetectedAllowsUnsafe)
)]
fn total_package_counts_get_total_detection_status_tests(
input_none_detected_forbids_unsafe: i32,
input_none_detected_allows_unsafe: i32,
input_unsafe_detected: i32,
expected_crate_detection_status: CrateDetectionStatus,
) {
let total_detection_status = TotalPackageCounts {
none_detected_forbids_unsafe: input_none_detected_forbids_unsafe,
none_detected_allows_unsafe: input_none_detected_allows_unsafe,
unsafe_detected: input_unsafe_detected,
total_counter_block: CounterBlock::default(),
total_unused_counter_block: CounterBlock::default(),
};

assert_eq!(
total_package_counts_unsafe_detected.get_total_detection_status(),
CrateDetectionStatus::UnsafeDetected
);

let total_package_counts_none_detected_forbids_unsafe =
TotalPackageCounts {
none_detected_forbids_unsafe: 1,
none_detected_allows_unsafe: 0,
unsafe_detected: 0,
total_counter_block: CounterBlock::default(),
total_unused_counter_block: CounterBlock::default(),
};

assert_eq!(
total_package_counts_none_detected_forbids_unsafe
.get_total_detection_status(),
CrateDetectionStatus::NoneDetectedForbidsUnsafe
);

let total_package_counts_none_detected_allows_unsafe =
TotalPackageCounts {
none_detected_forbids_unsafe: 4,
none_detected_allows_unsafe: 1,
unsafe_detected: 0,
total_counter_block: CounterBlock::default(),
total_unused_counter_block: CounterBlock::default(),
};

assert_eq!(
total_package_counts_none_detected_allows_unsafe
.get_total_detection_status(),
CrateDetectionStatus::NoneDetectedAllowsUnsafe
total_detection_status.get_total_detection_status(),
expected_crate_detection_status
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and compact! A clear improvement 👍

Comment on lines 33 to +35
pub fn create_table_from_text_tree_lines(
geiger_context: &GeigerContext,
package_set: &PackageSet,
print_config: &PrintConfig,
rs_files_used: &HashSet<PathBuf>,
table_parameters: &TableParameters,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better 👍

Comment on lines +52 to +71
TextTreeLine::ExtraDepsGroup {
kind: dep_kind,
tree_vines,
} => handle_text_tree_line_extra_deps_group(
dep_kind,
&mut table_lines,
tree_vines,
),
TextTreeLine::Package {
id: package_id,
tree_vines,
} => handle_text_tree_line_package(
&emoji_symbols,
&mut handle_package_parameters,
package_id,
package_set,
&mut table_lines,
table_parameters,
tree_vines,
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much easier to read with the cases factored out as functions 👍

@@ -23,6 +23,7 @@ env_logger = "0.7.1"
geiger = { path = "../geiger", version = "0.4.5" }
petgraph = "0.5.1"
pico-args = "0.3.3"
rand = "0.7.3"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a dev-dependency instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, apologies, I had meant to remove that, I have taken out in the last commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Signed-off-by: joshmc <josh-mcc@tiscali.co.uk>
@anderejd anderejd merged commit ad87c27 into geiger-rs:master Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants