Skip to content

Commit

Permalink
make sure that all check patterns have test (#110)
Browse files Browse the repository at this point in the history
* make sure that all check patterns have test
  • Loading branch information
kaplanelad committed Oct 1, 2022
1 parent c8f5488 commit f24b42a
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 6 deletions.
2 changes: 1 addition & 1 deletion shellfirm/checks/kubernetes-strict.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
description: "This command will update the given given resource."
id: kubernetes-strict:update_resource
- from: kubernetes-strict
test: \s*(kubectl|k)\s*scale
test: (kubectl|k)\s*scale
description: "This command will set a new size for a given resource."
id: kubernetes-strict:change_resource_size
- from: kubernetes-strict
Expand Down
2 changes: 1 addition & 1 deletion shellfirm/checks/kubernetes.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- from: kubernetes
test: \s*(kubectl|k)\s*delete\s*(ns|namespace)
test: (kubectl|k)\s*delete\s*(ns|namespace)
description: "Deleting the namespace also deletes all the residing components."
id: kubernetes:delete_namespace
3 changes: 2 additions & 1 deletion shellfirm/src/bin/cmd/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use clap::{App, Arg, ArgMatches, Command};
use clap::{App, AppSettings::ArgRequiredElseHelp, Arg, ArgMatches, Command};
use shellfirm::{dialog, Challenge, Config, Settings};
use strum::IntoEnumIterator;

Expand All @@ -8,6 +8,7 @@ const ALL_GROUP_CHECKS: &[&str] = &include!(concat!(env!("OUT_DIR"), "/all_the_f
pub fn command() -> Command<'static> {
Command::new("config")
.about("Manage app config")
.setting(ArgRequiredElseHelp)
.subcommand(
App::new("update-groups")
.about("enable check group")
Expand Down
28 changes: 28 additions & 0 deletions shellfirm/tests/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,31 @@ fn test_checks() {
assert_debug_snapshot!(file_name, test_file_results);
}
}

#[test]
fn test_patterns_coverage() {
let checks = get_all_checks().unwrap();

let test_files_path = fs::read_dir("./tests/checks")
.unwrap()
.filter_map(|entry| {
entry
.ok()
.and_then(|e| Some(e.file_name().to_str().unwrap().to_string()))
})
.collect::<Vec<String>>();

let mut not_covered = vec![];
for check in checks {
if !test_files_path.contains(&format!("{}.yaml", &check.id.replace(":", "-"))) {
// println!(
// "{} - > {}",
// &check.id,
// format!("{}.yaml", &check.id.replace(":", "-"))
// );
println!("{:?}", test_files_path);
not_covered.push(check.id);
}
}
assert_debug_snapshot!(not_covered)
}
7 changes: 7 additions & 0 deletions shellfirm/tests/checks/kubernetes-delete_namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- test: k delete ns
description: match command
- test: kubectl delete namespace
description: match command with spaces
- test: kubectll delete
description: invalid command
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- test: k scale
description: match command
- test: kubectl scale
description: match command with spaces
- test: kubectll scale
description: invalid command
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ expression: test_file_results
---
[
TestSensitivePatternsResult {
file_path: "heroku-remove_app_containe.yaml",
file_path: "heroku-remove_app_container.yaml",
test: "heroku container:rm",
check_detection_ids: [
"heroku:remove_app_container",
],
test_description: "match command",
},
TestSensitivePatternsResult {
file_path: "heroku-remove_app_containe.yaml",
file_path: "heroku-remove_app_container.yaml",
test: "heroku container:rm",
check_detection_ids: [
"heroku:remove_app_container",
],
test_description: "match command with spaces",
},
TestSensitivePatternsResult {
file_path: "heroku-remove_app_containe.yaml",
file_path: "heroku-remove_app_container.yaml",
test: "herokuu container:rm",
check_detection_ids: [],
test_description: "invalid command",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: shellfirm/tests/checks.rs
expression: test_file_results
---
[
TestSensitivePatternsResult {
file_path: "kubernetes-delete_namespace.yaml",
test: "k delete ns",
check_detection_ids: [
"kubernetes:delete_namespace",
"kubernetes-strict:delete_resource",
],
test_description: "match command",
},
TestSensitivePatternsResult {
file_path: "kubernetes-delete_namespace.yaml",
test: "kubectl delete namespace",
check_detection_ids: [
"kubernetes:delete_namespace",
"kubernetes-strict:delete_resource",
],
test_description: "match command with spaces",
},
TestSensitivePatternsResult {
file_path: "kubernetes-delete_namespace.yaml",
test: "kubectll delete",
check_detection_ids: [],
test_description: "invalid command",
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: shellfirm/tests/checks.rs
expression: test_file_results
---
[
TestSensitivePatternsResult {
file_path: "kubernetes-strict-change_resource_size.yaml",
test: "k scale",
check_detection_ids: [
"kubernetes-strict:change_resource_size",
],
test_description: "match command",
},
TestSensitivePatternsResult {
file_path: "kubernetes-strict-change_resource_size.yaml",
test: "kubectl scale",
check_detection_ids: [
"kubernetes-strict:change_resource_size",
],
test_description: "match command with spaces",
},
TestSensitivePatternsResult {
file_path: "kubernetes-strict-change_resource_size.yaml",
test: "kubectll scale",
check_detection_ids: [],
test_description: "invalid command",
},
]
5 changes: 5 additions & 0 deletions shellfirm/tests/snapshots/checks__patterns_coverage.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: shellfirm/tests/checks.rs
expression: not_covered
---
[]

0 comments on commit f24b42a

Please sign in to comment.