Skip to content

Commit b2321aa

Browse files
committed
add tests
1 parent 1b66ad6 commit b2321aa

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

cpp-linter-lib/src/cli/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,13 @@ Further filtering can still be applied (see [Source options](#source-options))."
295295
)
296296
.groups([
297297
ArgGroup::new("Clang-tidy options")
298-
.args(["tidy-checks", "database", "extra-arg", "ignore-tidy"]),
299-
ArgGroup::new("Clang-format options").args(["style", "ignore-format"]),
300-
ArgGroup::new("General options").args(["verbosity", "version"]),
301-
ArgGroup::new("Source options").args(["extensions", "repo-root", "ignore", "lines-changed-only", "files-changed-only"]),
298+
.args(["tidy-checks", "database", "extra-arg", "ignore-tidy"]).multiple(true),
299+
ArgGroup::new("Clang-format options").args(["style", "ignore-format"]).multiple(true),
300+
ArgGroup::new("General options").args(["verbosity", "version"]).multiple(true),
301+
ArgGroup::new("Source options").args(["extensions", "repo-root", "ignore", "lines-changed-only", "files-changed-only"]).multiple(true),
302302
ArgGroup::new("Feedback options").args([
303303
"thread-comments", "no-lgtm", "step-summary", "file-annotations"
304-
]),
304+
]).multiple(true),
305305
])
306306
.next_line_help(true)
307307
}

cpp-linter-lib/src/cli/structs.rs

+22
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct Cli {
4545
pub no_lgtm: bool,
4646
pub step_summary: bool,
4747
pub file_annotations: bool,
48+
pub not_ignored: Option<Vec<String>>,
4849
}
4950

5051
impl From<&ArgMatches> for Cli {
@@ -101,6 +102,9 @@ impl From<&ArgMatches> for Cli {
101102
step_summary: args.get_flag("step-summary"),
102103
thread_comments,
103104
file_annotations: args.get_flag("file-annotations"),
105+
not_ignored: args
106+
.get_many::<String>("files")
107+
.map(|files| Vec::from_iter(files.map(|v| v.to_owned()))),
104108
}
105109
}
106110
}
@@ -196,3 +200,21 @@ impl Default for FeedbackInput {
196200
}
197201
}
198202
}
203+
204+
#[cfg(test)]
205+
mod test {
206+
use crate::cli::get_arg_parser;
207+
208+
use super::Cli;
209+
210+
#[test]
211+
fn parse_positional() {
212+
let parser = get_arg_parser();
213+
let args = parser.get_matches_from(["cpp-linter", "file1.c", "file2.h"]);
214+
let cli = Cli::from(&args);
215+
let not_ignored = cli.not_ignored.expect("failed to parse positional args");
216+
assert!(!not_ignored.is_empty());
217+
assert!(not_ignored.contains(&String::from("file1.c")));
218+
assert!(not_ignored.contains(&String::from("file2.h")));
219+
}
220+
}

cpp-linter-lib/src/run.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ pub async fn run_main(args: Vec<String>) -> i32 {
6565
}
6666

6767
if cli.repo_root != "." {
68-
env::set_current_dir(Path::new(&cli.repo_root)).unwrap();
68+
env::set_current_dir(Path::new(&cli.repo_root))
69+
.unwrap_or_else(|_| panic!("'{}' is inaccessible or does not exist", cli.repo_root));
6970
}
7071

7172
let rest_api_client = GithubApiClient::new();
@@ -78,6 +79,9 @@ pub async fn run_main(args: Vec<String>) -> i32 {
7879

7980
let mut file_filter = FileFilter::new(&cli.ignore, cli.extensions.clone());
8081
file_filter.parse_submodules();
82+
if let Some(files) = &cli.not_ignored {
83+
file_filter.not_ignored.extend(files.clone());
84+
}
8185

8286
if !file_filter.ignored.is_empty() {
8387
log::info!("Ignored:");
@@ -92,14 +96,6 @@ pub async fn run_main(args: Vec<String>) -> i32 {
9296
}
9397
}
9498

95-
if let Some(files) = args.get_many::<Vec<String>>("files") {
96-
for list in files {
97-
file_filter
98-
.not_ignored
99-
.extend(list.iter().map(|v| v.to_string()).collect::<Vec<String>>());
100-
}
101-
}
102-
10399
start_log_group(String::from("Get list of specified source files"));
104100
let files = if cli.lines_changed_only != LinesChangedOnly::Off || cli.files_changed_only {
105101
// parse_diff(github_rest_api_payload)
@@ -140,7 +136,10 @@ mod test {
140136
run_main(vec![
141137
"cpp-linter".to_string(),
142138
"-l".to_string(),
143-
"false".to_string()
139+
"false".to_string(),
140+
"--repo-root".to_string(),
141+
"tests".to_string(),
142+
"demo/demo.cpp".to_string(),
144143
])
145144
.await,
146145
0

0 commit comments

Comments
 (0)