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

cli: add support for path globbing to ignore_paths #89

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pulldown-cmark = "0.9.6"
toml = "0.8.13"
serde = { version = "1.0.202", features = ["derive"] }
url-escape = "0.1.1"
glob = "0.3.1"

[dev-dependencies]
ntest = "0.9.2"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ The following arguments are available:
| `--offline` | `-o` | Do not check any web links. Renamed from `--no-web-links` which is still an alias for downwards compatibility |
| `--match-file-extension` | `-e` | Set the flag, if the file extension shall be checked as well. For example the following markup link `[link](dir/file)` matches if for example a file called `file.md` exists in `dir`, but would fail when the `--match-file-extension` flag is set. |
| `--version` | `-V` | Print current version of mlc |
| `--ignore-path` | `-p` | Comma separated list of directories or files which shall be ignored. For example |
| `--ignore-path` | `-p` | Comma separated list of directories or files which shall be ignored. Can use globbing. For example: `--ignore-path "./ignore-me","./src","./build-*"` |
| `--ignore-links` | `-i` | Comma separated list of links which shall be ignored. Use simple `?` and `*` wildcards. For example `--ignore-links "http*://crates.io*"` will skip all links to the crates.io website. See the [used lib](https://github.com/becheran/wildmatch) for more information. |
| `--markup-types` | `-t` | Comma separated list list of markup types which shall be checked [possible values: md, html] |
| `--root-dir` | `-r` | All links to the file system starting with a slash on linux or backslash on windows will use another virtual root dir. For example the link in a file `[link](/dir/other/file.md)` checked with the cli arg `--root-dir /env/another/dir` will let *mlc* check the existence of `/env/another/dir/dir/other/file.md`. |
Expand All @@ -137,7 +137,7 @@ offline = true
# Check the exact file extension when searching for a file
match-file-extension= true
# List of files and directories which will be ignored
ignore-path=["./ignore-me","./src"]
ignore-path=["./ignore-me","./src","./build-*"]
# List of links which will be ignored
ignore-links=["http://ignore-me.de/*","http://*.ignoresub-domain/*"]
# List of markup types which shall be checked
Expand Down
50 changes: 49 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
use crate::OptionalConfig;
use clap::Arg;
use clap::ArgAction;
use glob::glob_with;
use glob::MatchOptions;
use std::fs;
use std::path::Path;
use std::path::MAIN_SEPARATOR;
use std::path::{Path, PathBuf};

const CONFIG_FILE_PATH: &str = "./.mlc.toml";

Expand Down Expand Up @@ -146,6 +148,11 @@
if let Some(ignore_links) = matches.get_many::<String>("ignore-links") {
opt.ignore_links = Some(ignore_links.map(|x| x.to_string()).collect());
}
let options = MatchOptions {

Check failure on line 151 in src/cli.rs

View workflow job for this annotation

GitHub Actions / build_windows

unused variable: `options`

Check failure on line 151 in src/cli.rs

View workflow job for this annotation

GitHub Actions / build_osx

unused variable: `options`
case_sensitive: true,
require_literal_separator: false,
require_literal_leading_dot: false,
};

if let Some(ignore_path) = matches.get_many::<String>("ignore-path") {
opt.ignore_path = Some(ignore_path.map(|x| Path::new(x).to_path_buf()).collect());
Expand Down Expand Up @@ -178,3 +185,44 @@
optional: opt,
}
}

pub fn collect_ignore_paths<'a, I>(ignore_paths: I, options: MatchOptions) -> Vec<PathBuf>
where
I: Iterator<Item = &'a String>,
{
let mut collected_paths = Vec::new();

for x in ignore_paths {
if x.contains('*') {
collected_paths.extend(handle_glob_path(x, options));
} else {
collected_paths.push(handle_literal_path(x));
}
}

collected_paths
}

fn handle_glob_path(pattern: &str, options: MatchOptions) -> Vec<PathBuf> {
let mut paths = Vec::new();

for entry in glob_with(pattern, options).unwrap() {
match entry {
Ok(p) => match fs::canonicalize(&p) {
Ok(pa) => paths.push(pa),
Err(e) => panic!("Ignore path {:?} not found. {:?}.", &p, e),
},
Err(e) => panic!("Ignore path not found. {:?}.", e),
}
}

paths
}

fn handle_literal_path(path_str: &str) -> PathBuf {
let path = Path::new(path_str).to_path_buf();
match fs::canonicalize(&path) {
Ok(p) => p,
Err(e) => panic!("Ignore path {:?} not found. {:?}.", &path, e),
}
}
26 changes: 25 additions & 1 deletion tests/file_traversal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[cfg(test)]
use mlc::cli::collect_ignore_paths;
use mlc::file_traversal;
use mlc::markup::{MarkupFile, MarkupType};
use mlc::Config;
use mlc::OptionalConfig;
use std::path::Path;
use std::path::{Path, PathBuf};

#[test]
fn find_markdown_files() {
Expand Down Expand Up @@ -36,3 +37,26 @@ fn empty_folder() {
file_traversal::find(&config, &mut result);
assert!(result.is_empty());
}

#[test]
fn glob_test() {
let options = glob::MatchOptions {
case_sensitive: true,
require_literal_separator: false,
require_literal_leading_dot: false,
};

let dir = PathBuf::from("./benches/benchmark");
let c_dir = std::fs::canonicalize(dir).expect("Canonicalize failed");

let glob_dir = "./benches/ben*".to_string();
let ignore_paths = vec![&glob_dir];

let collected_paths = collect_ignore_paths(ignore_paths.into_iter(), options);

assert!(
collected_paths.contains(&c_dir),
"The expected globbed path is not in the collected paths: {:?}",
collected_paths
);
}
Loading