Skip to content

Commit

Permalink
feat: add a flag & config for skipping path glob patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
rdaniels6813 committed May 29, 2024
1 parent fa0ec26 commit e0db261
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 12 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ squawk-parser = { version = "0.0.0", path = "../parser" }
squawk-linter = { version = "0.0.0", path = "../linter" }
squawk-github = { version = "0.0.0", path = "../github" }
toml = "0.5.9"
glob = "0.3.1"

[dev-dependencies]
insta = "0.16.0"
13 changes: 13 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ impl std::fmt::Display for ConfigError {

#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
pub excluded_paths: Vec<String>,
#[serde(default)]
pub excluded_rules: Vec<RuleViolationKind>,
#[serde(default)]
Expand Down Expand Up @@ -98,6 +100,7 @@ mod test_config {
let squawk_toml = NamedTempFile::new().expect("generate tempFile");
let file = r#"
pg_version = "19.1"
excluded_paths = ["example.sql"]
excluded_rules = ["require-concurrent-index-creation"]
assume_in_transaction = true
Expand Down Expand Up @@ -126,6 +129,16 @@ excluded_rules = ["require-concurrent-index-creation"]
assert_debug_snapshot!(Config::parse(Some(squawk_toml.path().to_path_buf())));
}
#[test]
fn test_load_excluded_paths() {
let squawk_toml = NamedTempFile::new().expect("generate tempFile");
let file = r#"
excluded_paths = ["example.sql"]
"#;
fs::write(&squawk_toml, file).expect("Unable to write file");
assert_debug_snapshot!(Config::parse(Some(squawk_toml.path().to_path_buf())));
}
#[test]
fn test_load_assume_in_transaction() {
let squawk_toml = NamedTempFile::new().expect("generate tempFile");
let file = r#"
Expand Down
26 changes: 26 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::reporter::{
use crate::subcommand::{check_and_comment_on_pr, Command};
use atty::Stream;
use config::Config;
use glob::Pattern;
use log::info;
use simplelog::CombinedLogger;
use squawk_linter::versions::Version;
Expand All @@ -40,6 +41,12 @@ struct Opt {
/// Paths to search
#[structopt(value_name = "path")]
paths: Vec<String>,
/// Paths to exclude
///
/// For example:
/// --exclude-paths 202401*.sql
#[structopt(long = "exclude-paths", use_delimiter = true)]
excluded_paths: Option<Vec<String>>,
/// Exclude specific warnings
///
/// For example:
Expand Down Expand Up @@ -117,6 +124,22 @@ fn main() {
conf.excluded_rules
};

// the --exclude-paths flag completely overrides the configuration file.
let excluded_paths = if let Some(excluded_paths) = opts.excluded_paths {
excluded_paths
} else {
conf.excluded_paths
};
let excluded_path_patterns = excluded_paths
.iter()
.map(|excluded_path| {
Pattern::new(excluded_path).unwrap_or_else(|e| {
eprintln!("Pattern error: {e}");
process::exit(1);
})
})
.collect::<Vec<Pattern>>();

let pg_version = if let Some(pg_version) = opts.pg_version {
Some(pg_version)
} else {
Expand All @@ -135,6 +158,7 @@ fn main() {

info!("pg version: {:?}", pg_version);
info!("excluded rules: {:?}", &excluded_rules);
info!("excluded paths: {:?}", &excluded_paths);
info!("assume in a transaction: {:?}", assume_in_transaction);

let mut clap_app = Opt::clap();
Expand All @@ -149,6 +173,7 @@ fn main() {
is_stdin,
opts.stdin_filepath,
&excluded_rules,
&excluded_path_patterns,
pg_version,
assume_in_transaction,
),
Expand All @@ -167,6 +192,7 @@ fn main() {
read_stdin,
opts.stdin_filepath,
&excluded_rules,
&excluded_path_patterns,
pg_version,
assume_in_transaction,
) {
Expand Down
40 changes: 30 additions & 10 deletions cli/src/reporter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use console::strip_ansi_codes;
use console::style;
use glob::Pattern;
use log::info;
use serde::Serialize;
use squawk_linter::errors::CheckSqlError;
Expand Down Expand Up @@ -164,6 +165,7 @@ pub fn check_files(
read_stdin: bool,
stdin_path: Option<String>,
excluded_rules: &[RuleViolationKind],
excluded_paths: &[Pattern],
pg_version: Option<Version>,
assume_in_transaction: bool,
) -> Result<Vec<ViolationContent>, CheckFilesError> {
Expand All @@ -188,19 +190,37 @@ pub fn check_files(
}

for path in paths {
info!("checking file path: {}", path);
let sql = get_sql_from_path(path)?;
output_violations.push(process_violations(
&sql,
path,
excluded_rules,
pg_version,
assume_in_transaction,
));
if has_match_in_paths(path, excluded_paths) {
info!("skipping exluded file path: {}", path);
output_violations.push(ViolationContent {
filename: path.into(),
sql: String::new(),
violations: vec![],
});
} else {
info!("checking file path: {}", path);
let sql = get_sql_from_path(path)?;
output_violations.push(process_violations(
&sql,
path,
excluded_rules,
pg_version,
assume_in_transaction,
));
}
}
Ok(output_violations)
}

fn has_match_in_paths(path: &str, excluded_paths: &[Pattern]) -> bool {
for excluded in excluded_paths {
if excluded.matches(path) {
return true;
}
}
false
}

fn get_sql_from_stdin() -> Result<String, io::Error> {
let mut buffer = String::new();
let stdin = io::stdin();
Expand Down Expand Up @@ -520,7 +540,7 @@ pub fn get_comment_body(files: &[ViolationContent], version: &str) -> String {
violation_count = violations_count,
file_count = files.len(),
sql_file_content = files
.into_iter()
.iter()
.filter_map(|x| get_sql_file_content(x).ok())
.collect::<Vec<String>>()
.join("\n"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Config {
excluded_paths: [],
excluded_rules: [],
pg_version: None,
assume_in_transaction: Some(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Config {
excluded_paths: [
"example.sql",
],
excluded_rules: [
RequireConcurrentIndexCreation,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Config {
excluded_paths: [
"example.sql",
],
excluded_rules: [],
pg_version: None,
assume_in_transaction: None,
},
),
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Config {
excluded_paths: [],
excluded_rules: [
RequireConcurrentIndexCreation,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Config {
excluded_paths: [],
excluded_rules: [],
pg_version: Some(
Version {
Expand Down
3 changes: 3 additions & 0 deletions cli/src/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::reporter::{check_files, get_comment_body, CheckFilesError};

use glob::Pattern;
use log::info;
use squawk_github::{actions, app, comment_on_pr, GitHubApi, GithubError};
use squawk_linter::{versions::Version, violations::RuleViolationKind};
Expand Down Expand Up @@ -157,6 +158,7 @@ pub fn check_and_comment_on_pr(
is_stdin: bool,
stdin_path: Option<String>,
root_cmd_exclude: &[RuleViolationKind],
root_cmd_exclude_paths: &[Pattern],
pg_version: Option<Version>,
assume_in_transaction: bool,
) -> Result<(), SquawkError> {
Expand Down Expand Up @@ -188,6 +190,7 @@ pub fn check_and_comment_on_pr(
is_stdin,
stdin_path,
&concat(&exclude.unwrap_or_default(), root_cmd_exclude),
root_cmd_exclude_paths,
pg_version,
assume_in_transaction,
)?;
Expand Down

0 comments on commit e0db261

Please sign in to comment.