Skip to content

Commit b181d7d

Browse files
committed
add strictness to tests
Exclude clang v7 and v8 on windows for PR review tests. Those versions do not have a consistent cross-platform output from clang-tidy.
1 parent 638ceed commit b181d7d

17 files changed

+1001
-2289
lines changed

.github/workflows/run-dev-tests.yml

+5-15
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,25 @@ jobs:
7575
run: sudo apt-get update
7676

7777
- name: Install clang v7
78+
if: matrix.os == 'Linux'
7879
uses: cpp-linter/cpp_linter_rs/install-clang-action@pr-reviews
7980
with:
8081
version: '7'
8182

8283
- name: Collect Coverage for clang v7
83-
working-directory: cpp-linter-lib
84+
if: matrix.os == 'Linux'
8485
env:
8586
CLANG_VERSION: '7'
8687
run: just test
8788

8889
- name: Install clang v8
90+
if: matrix.os == 'Linux'
8991
uses: cpp-linter/cpp_linter_rs/install-clang-action@pr-reviews
9092
with:
9193
version: '8'
9294

9395
- name: Collect Coverage for clang v8
94-
working-directory: cpp-linter-lib
96+
if: matrix.os == 'Linux'
9597
env:
9698
CLANG_VERSION: '8'
9799
run: just test
@@ -102,7 +104,6 @@ jobs:
102104
version: '9'
103105

104106
- name: Collect Coverage for clang v9
105-
working-directory: cpp-linter-lib
106107
env:
107108
CLANG_VERSION: '9'
108109
run: just test
@@ -113,7 +114,6 @@ jobs:
113114
version: '10'
114115

115116
- name: Collect Coverage for clang v10
116-
working-directory: cpp-linter-lib
117117
env:
118118
CLANG_VERSION: '10'
119119
run: just test
@@ -124,7 +124,6 @@ jobs:
124124
version: '11'
125125

126126
- name: Collect Coverage for clang v11
127-
working-directory: cpp-linter-lib
128127
env:
129128
CLANG_VERSION: '11'
130129
run: just test
@@ -135,7 +134,6 @@ jobs:
135134
version: '12'
136135

137136
- name: Collect Coverage for clang v12
138-
working-directory: cpp-linter-lib
139137
env:
140138
CLANG_VERSION: '12'
141139
run: just test
@@ -146,7 +144,6 @@ jobs:
146144
version: '13'
147145

148146
- name: Collect Coverage for clang v13
149-
working-directory: cpp-linter-lib
150147
env:
151148
CLANG_VERSION: '13'
152149
run: just test
@@ -157,7 +154,6 @@ jobs:
157154
version: '14'
158155

159156
- name: Collect Coverage for clang v14
160-
working-directory: cpp-linter-lib
161157
env:
162158
CLANG_VERSION: '14'
163159
run: just test
@@ -168,7 +164,6 @@ jobs:
168164
version: '15'
169165

170166
- name: Collect Coverage for clang v15
171-
working-directory: cpp-linter-lib
172167
env:
173168
CLANG_VERSION: '15'
174169
run: just test
@@ -179,7 +174,6 @@ jobs:
179174
version: '16'
180175

181176
- name: Collect Coverage for clang v16
182-
working-directory: cpp-linter-lib
183177
env:
184178
CLANG_VERSION: '16'
185179
run: just test
@@ -190,7 +184,6 @@ jobs:
190184
version: '17'
191185

192186
- name: Collect Coverage for clang v17
193-
working-directory: cpp-linter-lib
194187
env:
195188
CLANG_VERSION: '17'
196189
run: just test
@@ -201,13 +194,11 @@ jobs:
201194
version: '18'
202195

203196
- name: Collect Coverage for clang v18
204-
working-directory: cpp-linter-lib
205197
env:
206198
CLANG_VERSION: '18'
207199
run: just test --run-ignored=all
208200

209201
- name: Generate Coverage HTML report
210-
working-directory: cpp-linter-lib
211202
run: just pretty-cov
212203

213204
- name: Upload coverage data
@@ -218,12 +209,11 @@ jobs:
218209

219210
- name: Generate Coverage lcov report
220211
if: runner.os == 'Linux'
221-
working-directory: cpp-linter-lib
222212
run: just lcov
223213

224214
- uses: codecov/codecov-action@v4
225215
if: runner.os == 'Linux'
226216
with:
227217
token: ${{secrets.CODECOV_TOKEN}}
228-
files: cpp-linter-lib/lcov.info
218+
files: lcov.info
229219
fail_ci_if_error: true # optional (default = false)

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
}

0 commit comments

Comments
 (0)