Skip to content

Commit 4bd31c6

Browse files
committed
add strictness to tests
1 parent 638ceed commit 4bd31c6

16 files changed

+973
-2265
lines changed

cpp-linter-lib/src/clang_tools/clang_format.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ impl Clone for Replacement {
7777
}
7878
}
7979

80+
/// Get a string that summarizes the given `--style`
81+
pub fn summarize_style(style: &str) -> String {
82+
if ["google", "chromium", "microsoft", "mozilla", "webkit"].contains(&style) {
83+
// capitalize the first letter
84+
let mut char_iter = style.chars();
85+
let first_char = char_iter.next().unwrap();
86+
first_char.to_uppercase().collect::<String>() + char_iter.as_str()
87+
} else if style == "llvm" || style == "gnu" {
88+
style.to_ascii_uppercase()
89+
} else {
90+
String::from("Custom")
91+
}
92+
}
93+
8094
/// Get a total count of clang-format advice from the given list of [FileObj]s.
8195
pub fn tally_format_advice(files: &[Arc<Mutex<FileObj>>]) -> u64 {
8296
let mut total = 0;
@@ -199,7 +213,7 @@ pub fn run_clang_format(
199213

200214
#[cfg(test)]
201215
mod tests {
202-
use super::{FormatAdvice, Replacement};
216+
use super::{summarize_style, FormatAdvice, Replacement};
203217
use serde::Deserialize;
204218

205219
#[test]
@@ -257,4 +271,23 @@ mod tests {
257271
.unwrap();
258272
assert_eq!(expected, document);
259273
}
274+
275+
fn formalize_style(style: &str, expected: &str) {
276+
assert_eq!(summarize_style(style), expected);
277+
}
278+
279+
#[test]
280+
fn formalize_llvm_style() {
281+
formalize_style("llvm", "LLVM");
282+
}
283+
284+
#[test]
285+
fn formalize_google_style() {
286+
formalize_style("google", "Google");
287+
}
288+
289+
#[test]
290+
fn formalize_custom_style() {
291+
formalize_style("file", "Custom");
292+
}
260293
}

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

+45-33
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl ReviewComments {
258258
.as_str(),
259259
);
260260
}
261-
if total > 0 {
261+
if !self.full_patch[t as usize].is_empty() {
262262
body.push_str(
263263
format!(
264264
"\n<details><summary>Click here for the full {tool_name} patch</summary>\n\n```diff\n{}```\n\n</details>\n",
@@ -267,7 +267,11 @@ impl ReviewComments {
267267
);
268268
} else {
269269
body.push_str(
270-
format!("\nNo concerns reported by {}. Great job! :tada:", tool_name).as_str(),
270+
format!(
271+
"\nNo concerns reported by {}. Great job! :tada:\n",
272+
tool_name
273+
)
274+
.as_str(),
271275
)
272276
}
273277
}
@@ -350,6 +354,9 @@ pub trait MakeSuggestions {
350354
.expect("Failed to convert patch buffer to string")
351355
.as_str(),
352356
);
357+
if summary_only {
358+
return;
359+
}
353360
for hunk_id in 0..hunks_total {
354361
let (hunk, line_count) = patch.hunk(hunk_id).expect("Failed to get hunk from patch");
355362
hunks_in_patch += 1;
@@ -359,51 +366,43 @@ pub trait MakeSuggestions {
359366
}
360367
let (start_line, end_line) = hunk_range.unwrap();
361368
let mut suggestion = String::new();
362-
let suggestion_help = if !summary_only {
363-
Some(self.get_suggestion_help(start_line, end_line))
364-
} else {
365-
None
366-
};
369+
let suggestion_help = self.get_suggestion_help(start_line, end_line);
367370
let mut removed = vec![];
368371
for line_index in 0..line_count {
369372
let diff_line = patch
370373
.line_in_hunk(hunk_id, line_index)
371374
.expect("Failed to get line in a hunk");
372375
let line = String::from_utf8(diff_line.content().to_owned())
373376
.expect("Failed to convert line buffer to string");
374-
if !summary_only {
375-
if ['+', ' '].contains(&diff_line.origin()) {
376-
suggestion.push_str(line.as_str());
377-
} else {
378-
removed.push(
379-
diff_line
380-
.old_lineno()
381-
.expect("Removed line has no line number?!"),
382-
);
383-
}
377+
if ['+', ' '].contains(&diff_line.origin()) {
378+
suggestion.push_str(line.as_str());
379+
} else {
380+
removed.push(
381+
diff_line
382+
.old_lineno()
383+
.expect("Removed line has no line number?!"),
384+
);
384385
}
385386
}
386-
if !summary_only {
387-
if suggestion.is_empty() && !removed.is_empty() {
388-
suggestion.push_str(
389-
format!(
390-
"Please remove the line(s)\n- {}",
391-
removed
392-
.iter()
393-
.map(|l| l.to_string())
394-
.collect::<Vec<String>>()
395-
.join("\n- ")
396-
)
397-
.as_str(),
387+
if suggestion.is_empty() && !removed.is_empty() {
388+
suggestion.push_str(
389+
format!(
390+
"Please remove the line(s)\n- {}",
391+
removed
392+
.iter()
393+
.map(|l| l.to_string())
394+
.collect::<Vec<String>>()
395+
.join("\n- ")
398396
)
399-
} else {
400-
suggestion = format!("```suggestion\n{suggestion}```",);
401-
}
397+
.as_str(),
398+
)
399+
} else {
400+
suggestion = format!("```suggestion\n{suggestion}```",);
402401
}
403402
let comment = Suggestion {
404403
line_start: start_line,
405404
line_end: end_line,
406-
suggestion: format!("{}\n{suggestion}", suggestion_help.unwrap_or_default()),
405+
suggestion: format!("{suggestion_help}\n{suggestion}"),
407406
path: file_name.clone(),
408407
};
409408
if !review_comments.is_comment_in_suggestions(&comment) {
@@ -463,4 +462,17 @@ mod tests {
463462
.to_string()
464463
.contains(TOOL_NAME)));
465464
}
465+
466+
#[test]
467+
fn get_exe_by_invalid_path() {
468+
let tool_exe = get_clang_tool_exe(TOOL_NAME, "non-existent-path");
469+
assert!(tool_exe.is_err());
470+
}
471+
472+
#[test]
473+
fn get_exe_by_invalid_name() {
474+
let clang_version = env::var("CLANG_VERSION").unwrap_or("16".to_string());
475+
let tool_exe = get_clang_tool_exe("not-a-clang-tool", &clang_version);
476+
assert!(tool_exe.is_err());
477+
}
466478
}

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

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::{fmt::Display, path::PathBuf};
22

33
use clap::ArgMatches;
44

@@ -27,6 +27,16 @@ impl LinesChangedOnly {
2727
}
2828
}
2929

30+
impl Display for LinesChangedOnly {
31+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32+
match self {
33+
LinesChangedOnly::Off => write!(f, "false"),
34+
LinesChangedOnly::Diff => write!(f, "diff"),
35+
LinesChangedOnly::On => write!(f, "true"),
36+
}
37+
}
38+
}
39+
3040
/// A structure to contain parsed CLI options.
3141
pub struct Cli {
3242
pub version: String,
@@ -134,6 +144,16 @@ impl ThreadComments {
134144
}
135145
}
136146

147+
impl Display for ThreadComments {
148+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149+
match self {
150+
ThreadComments::On => write!(f, "true"),
151+
ThreadComments::Off => write!(f, "false"),
152+
ThreadComments::Update => write!(f, "update"),
153+
}
154+
}
155+
}
156+
137157
/// A data structure to contain CLI options that relate to
138158
/// clang-tidy or clang-format arguments.
139159
#[derive(Debug, Clone, Default)]
@@ -227,7 +247,7 @@ impl Default for FeedbackInput {
227247
mod test {
228248
use crate::cli::get_arg_parser;
229249

230-
use super::Cli;
250+
use super::{Cli, LinesChangedOnly, ThreadComments};
231251

232252
#[test]
233253
fn parse_positional() {
@@ -239,4 +259,21 @@ mod test {
239259
assert!(not_ignored.contains(&String::from("file1.c")));
240260
assert!(not_ignored.contains(&String::from("file2.h")));
241261
}
262+
263+
#[test]
264+
fn display_lines_changed_only_enum() {
265+
let input = "diff".to_string();
266+
assert_eq!(
267+
LinesChangedOnly::from_string(&input),
268+
LinesChangedOnly::Diff
269+
);
270+
assert_eq!(format!("{}", LinesChangedOnly::Diff), input);
271+
}
272+
273+
#[test]
274+
fn display_thread_comments_enum() {
275+
let input = "false".to_string();
276+
assert_eq!(ThreadComments::from_string(&input), ThreadComments::Off);
277+
assert_eq!(format!("{}", ThreadComments::Off), input);
278+
}
242279
}

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,30 @@ impl FileObj {
130130
) {
131131
let original_content =
132132
fs::read(&self.name).expect("Failed to read original contents of file");
133-
133+
let file_name = self
134+
.name
135+
.to_str()
136+
.expect("Failed to convert file extension to string")
137+
.replace("\\", "/");
138+
let file_path = Path::new(&file_name);
134139
if let Some(advice) = &self.format_advice {
135140
if let Some(patched) = &advice.patched {
136-
let mut patch = make_patch(&self.name, patched, &original_content);
141+
let mut patch = make_patch(file_path, patched, &original_content);
137142
advice.get_suggestions(review_comments, self, &mut patch, summary_only);
138143
}
139144
}
140145

141146
if let Some(advice) = &self.tidy_advice {
142147
if let Some(patched) = &advice.patched {
143-
let mut patch = make_patch(&self.name, patched, &original_content);
148+
let mut patch = make_patch(file_path, patched, &original_content);
144149
advice.get_suggestions(review_comments, self, &mut patch, summary_only);
145150
}
146151

152+
if summary_only {
153+
return;
154+
}
155+
147156
// now check for clang-tidy warnings with no fixes applied
148-
let file_name = self
149-
.name
150-
.to_str()
151-
.expect("Failed to convert file extension to string")
152-
.replace("\\", "/");
153157
let file_ext = self
154158
.name
155159
.extension()

0 commit comments

Comments
 (0)