Skip to content

Commit

Permalink
Use a single hardcoded_tmp_directory in bandit internal settings
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Jan 5, 2023
1 parent 86b702b commit 0607199
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,7 @@ where
if let Some(check) = flake8_bandit::checks::hardcoded_tmp_dir(
expr,
value,
&mut self.settings.flake8_bandit.all_hardcoded_tmp_directories(),
&self.settings.flake8_bandit.hardcoded_tmp_directory,
) {
self.add_check(check);
}
Expand Down
8 changes: 2 additions & 6 deletions src/flake8_bandit/checks/hardcoded_tmp_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ use crate::ast::types::Range;
use crate::registry::{Check, CheckKind};

/// S108
pub fn hardcoded_tmp_dir<'a>(
expr: &Expr,
value: &str,
prefixes: &mut impl Iterator<Item = &'a String>,
) -> Option<Check> {
if prefixes.any(|prefix| value.starts_with(prefix)) {
pub fn hardcoded_tmp_dir(expr: &Expr, value: &str, prefixes: &[String]) -> Option<Check> {
if prefixes.iter().any(|prefix| value.starts_with(prefix)) {
Some(Check::new(
CheckKind::HardcodedTempFile(value.to_string()),
Range::from_located(expr),
Expand Down
71 changes: 32 additions & 39 deletions src/flake8_bandit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,47 @@ mod tests {
use anyhow::Result;
use test_case::test_case;

use crate::flake8_bandit::settings::Settings;
use crate::linter::test_path;
use crate::registry::CheckCode;
use crate::settings;
use crate::{flake8_bandit, Settings};

#[test_case(CheckCode::S101, Path::new("S101.py"), Settings::default(), "S101"; "S101")]
#[test_case(CheckCode::S102, Path::new("S102.py"), Settings::default(), "S102"; "S102")]
#[test_case(CheckCode::S103, Path::new("S103.py"), Settings::default(), "S103"; "S103")]
#[test_case(CheckCode::S104, Path::new("S104.py"), Settings::default(), "S104"; "S104")]
#[test_case(CheckCode::S105, Path::new("S105.py"), Settings::default(), "S105"; "S105")]
#[test_case(CheckCode::S106, Path::new("S106.py"), Settings::default(), "S106"; "S106")]
#[test_case(CheckCode::S107, Path::new("S107.py"), Settings::default(), "S107"; "S107")]
#[test_case(CheckCode::S108, Path::new("S108.py"), Settings::default(), "S108_default"; "S108_0")]
#[test_case(
CheckCode::S108, Path::new("S108.py"),
Settings {
hardcoded_tmp_directory: vec!["/foo".to_string()],
..Settings::default()
},
"S108_override";
"S108_1"
)]
#[test_case(
CheckCode::S108,
Path::new("S108.py"),
Settings {
hardcoded_tmp_directory_extend: vec!["/foo".to_string()],
..Settings::default()
},
"S108_extend";
"S108_2"
)]
fn checks(
check_code: CheckCode,
path: &Path,
plugin_settings: Settings,
label: &str,
) -> Result<()> {
#[test_case(CheckCode::S101, Path::new("S101.py"); "S101")]
#[test_case(CheckCode::S102, Path::new("S102.py"); "S102")]
#[test_case(CheckCode::S103, Path::new("S103.py"); "S103")]
#[test_case(CheckCode::S104, Path::new("S104.py"); "S104")]
#[test_case(CheckCode::S105, Path::new("S105.py"); "S105")]
#[test_case(CheckCode::S106, Path::new("S106.py"); "S106")]
#[test_case(CheckCode::S107, Path::new("S107.py"); "S107")]
#[test_case(CheckCode::S108, Path::new("S108.py"); "S108")]
fn checks(check_code: CheckCode, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", check_code.as_ref(), path.to_string_lossy());
let checks = test_path(
Path::new("./resources/test/fixtures/flake8_bandit")
.join(path)
.as_path(),
&settings::Settings {
flake8_bandit: plugin_settings,
..settings::Settings::for_rule(check_code)
&Settings::for_rule(check_code),
)?;
insta::assert_yaml_snapshot!(snapshot, checks);
Ok(())
}

#[test]
fn check_hardcoded_tmp_additional_dirs() -> Result<()> {
let checks = test_path(
Path::new("./resources/test/fixtures/flake8_bandit/S108.py"),
&Settings {
flake8_bandit: flake8_bandit::settings::Settings {
hardcoded_tmp_directory: vec![
"/tmp".to_string(),
"/var/tmp".to_string(),
"/dev/shm".to_string(),
"/foo".to_string(),
],
},
..Settings::for_rule(CheckCode::S108)
},
)?;
insta::assert_yaml_snapshot!(label, checks);
insta::assert_yaml_snapshot!("S108_extend", checks);
Ok(())
}
}
30 changes: 5 additions & 25 deletions src/flake8_bandit/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,16 @@ pub struct Options {
#[derive(Debug, Hash)]
pub struct Settings {
pub hardcoded_tmp_directory: Vec<String>,
pub hardcoded_tmp_directory_extend: Vec<String>,
}

impl From<Options> for Settings {
fn from(options: Options) -> Self {
let mut hardcoded_tmp_directory = options
.hardcoded_tmp_directory
.unwrap_or_else(default_tmp_dirs);
hardcoded_tmp_directory.extend(options.hardcoded_tmp_directory_extend.unwrap_or_default());
Self {
hardcoded_tmp_directory: options
.hardcoded_tmp_directory
.unwrap_or_else(default_tmp_dirs),
hardcoded_tmp_directory_extend: options
.hardcoded_tmp_directory_extend
.unwrap_or_default(),
}
}
}
impl From<Settings> for Options {
fn from(settings: Settings) -> Self {
Self {
hardcoded_tmp_directory: Some(settings.hardcoded_tmp_directory),
hardcoded_tmp_directory_extend: Some(settings.hardcoded_tmp_directory_extend),
hardcoded_tmp_directory,
}
}
}
Expand All @@ -68,16 +58,6 @@ impl Default for Settings {
fn default() -> Self {
Self {
hardcoded_tmp_directory: default_tmp_dirs(),
hardcoded_tmp_directory_extend: Vec::new(),
}
}
}

impl Settings {
/// Returns an iterator over all directories that are considered temporary.
pub fn all_hardcoded_tmp_directories(&'_ self) -> impl Iterator<Item = &'_ String> {
self.hardcoded_tmp_directory
.iter()
.chain(self.hardcoded_tmp_directory_extend.iter())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: src/flake8_bandit/mod.rs
expression: checks
---
- kind:
HardcodedTempFile: /tmp/abc
location:
row: 5
column: 10
end_location:
row: 5
column: 20
fix: ~
parent: ~
- kind:
HardcodedTempFile: /var/tmp/123
location:
row: 8
column: 10
end_location:
row: 8
column: 24
fix: ~
parent: ~
- kind:
HardcodedTempFile: /dev/shm/unit/test
location:
row: 11
column: 10
end_location:
row: 11
column: 30
fix: ~
parent: ~

0 comments on commit 0607199

Please sign in to comment.