Skip to content

Commit 7a144b5

Browse files
committed
add strictness to tests
1 parent 638ceed commit 7a144b5

17 files changed

+998
-2289
lines changed

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

+1-15
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ jobs:
8080
version: '7'
8181

8282
- name: Collect Coverage for clang v7
83-
working-directory: cpp-linter-lib
8483
env:
8584
CLANG_VERSION: '7'
8685
run: just test
@@ -91,7 +90,6 @@ jobs:
9190
version: '8'
9291

9392
- name: Collect Coverage for clang v8
94-
working-directory: cpp-linter-lib
9593
env:
9694
CLANG_VERSION: '8'
9795
run: just test
@@ -102,7 +100,6 @@ jobs:
102100
version: '9'
103101

104102
- name: Collect Coverage for clang v9
105-
working-directory: cpp-linter-lib
106103
env:
107104
CLANG_VERSION: '9'
108105
run: just test
@@ -113,7 +110,6 @@ jobs:
113110
version: '10'
114111

115112
- name: Collect Coverage for clang v10
116-
working-directory: cpp-linter-lib
117113
env:
118114
CLANG_VERSION: '10'
119115
run: just test
@@ -124,7 +120,6 @@ jobs:
124120
version: '11'
125121

126122
- name: Collect Coverage for clang v11
127-
working-directory: cpp-linter-lib
128123
env:
129124
CLANG_VERSION: '11'
130125
run: just test
@@ -135,7 +130,6 @@ jobs:
135130
version: '12'
136131

137132
- name: Collect Coverage for clang v12
138-
working-directory: cpp-linter-lib
139133
env:
140134
CLANG_VERSION: '12'
141135
run: just test
@@ -146,7 +140,6 @@ jobs:
146140
version: '13'
147141

148142
- name: Collect Coverage for clang v13
149-
working-directory: cpp-linter-lib
150143
env:
151144
CLANG_VERSION: '13'
152145
run: just test
@@ -157,7 +150,6 @@ jobs:
157150
version: '14'
158151

159152
- name: Collect Coverage for clang v14
160-
working-directory: cpp-linter-lib
161153
env:
162154
CLANG_VERSION: '14'
163155
run: just test
@@ -168,7 +160,6 @@ jobs:
168160
version: '15'
169161

170162
- name: Collect Coverage for clang v15
171-
working-directory: cpp-linter-lib
172163
env:
173164
CLANG_VERSION: '15'
174165
run: just test
@@ -179,7 +170,6 @@ jobs:
179170
version: '16'
180171

181172
- name: Collect Coverage for clang v16
182-
working-directory: cpp-linter-lib
183173
env:
184174
CLANG_VERSION: '16'
185175
run: just test
@@ -190,7 +180,6 @@ jobs:
190180
version: '17'
191181

192182
- name: Collect Coverage for clang v17
193-
working-directory: cpp-linter-lib
194183
env:
195184
CLANG_VERSION: '17'
196185
run: just test
@@ -201,13 +190,11 @@ jobs:
201190
version: '18'
202191

203192
- name: Collect Coverage for clang v18
204-
working-directory: cpp-linter-lib
205193
env:
206194
CLANG_VERSION: '18'
207195
run: just test --run-ignored=all
208196

209197
- name: Generate Coverage HTML report
210-
working-directory: cpp-linter-lib
211198
run: just pretty-cov
212199

213200
- name: Upload coverage data
@@ -218,12 +205,11 @@ jobs:
218205

219206
- name: Generate Coverage lcov report
220207
if: runner.os == 'Linux'
221-
working-directory: cpp-linter-lib
222208
run: just lcov
223209

224210
- uses: codecov/codecov-action@v4
225211
if: runner.os == 'Linux'
226212
with:
227213
token: ${{secrets.CODECOV_TOKEN}}
228-
files: cpp-linter-lib/lcov.info
214+
files: lcov.info
229215
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)