Skip to content

Commit a207a07

Browse files
authored
Add integration tests for require_braces #106 (#127)
Also introduce an option `-Dtest_focus_on_rule=<rule_name>` to only run integration tests for a specific rule, which is useful when working on individual rules as the entire suite takes a bit longer to run.
1 parent f5b952f commit a207a07

File tree

13 files changed

+219
-16
lines changed

13 files changed

+219
-16
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
# Can't use 24 - see https://github.com/SimonKagstrom/kcov/issues/473
2222
runs-on: ubuntu-22.04
2323
name: Test coverage
24-
timeout-minutes: 4
24+
timeout-minutes: 5
2525
steps:
2626
- name: "Checkout repo"
2727
uses: actions/checkout@v4

.github/workflows/linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
test:
2020
runs-on: ubuntu-latest
2121
name: Test Linux
22-
timeout-minutes: 5
22+
timeout-minutes: 10
2323
steps:
2424
- name: "Checkout repo"
2525
uses: actions/checkout@v4

.github/workflows/windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
test:
2020
runs-on: windows-latest
2121
name: Test Windows
22-
timeout-minutes: 6
22+
timeout-minutes: 10
2323
steps:
2424
- name: "Checkout repo"
2525
uses: actions/checkout@v4

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ All tests:
410410
zig build test
411411
```
412412
413+
To focus on a single rule when running integration tests:
414+
415+
```shell
416+
zig build integration-test -Dtest_focus_on_rule=require_braces
417+
```
418+
413419
### Run lint on self
414420
415421
```shell

build.zig

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ const StepBuilder = struct {
190190
pub fn build(b: *std.Build) void {
191191
const target = b.standardTargetOptions(.{});
192192
const optimize = b.standardOptimizeOption(.{});
193-
const coverage = b.option(bool, "coverage", "Generate a coverage report with kcov");
193+
const test_coverage = b.option(bool, "coverage", "Generate a coverage report with kcov");
194+
const test_focus_on_rule = b.option([]const u8, "test_focus_on_rule", "Only run integration tests for this rule");
194195

195196
const zlinter_lib_module = b.addModule("zlinter", .{
196197
.root_source_file = b.path("src/lib/zlinter.zig"),
@@ -216,7 +217,7 @@ pub fn build(b: *std.Build) void {
216217

217218
const unit_tests_exe = b.addTest(.{
218219
.root_module = zlinter_lib_module,
219-
.use_llvm = coverage,
220+
.use_llvm = test_coverage,
220221
});
221222

222223
// --------------------------------------------------------------------
@@ -263,6 +264,9 @@ pub fn build(b: *std.Build) void {
263264
install_coverage.step.dependOn(&merge_coverage.step);
264265

265266
const run_integration_tests = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "test" });
267+
if (test_focus_on_rule) |r| {
268+
run_integration_tests.addArg(b.fmt("-Dtest_focus_on_rule={s}", .{r}));
269+
}
266270
run_integration_tests.setCwd(b.path("./integration_tests"));
267271
run_integration_tests.has_side_effects = true;
268272

@@ -297,7 +301,7 @@ pub fn build(b: *std.Build) void {
297301
integration_check_step.dependOn(&run_integration_check.step);
298302

299303
const unit_test_step = b.step("unit-test", "Run unit tests");
300-
if (coverage orelse false) {
304+
if (test_coverage orelse false) {
301305
const cover_run = std.Build.Step.Run.create(b, "Unit test coverage");
302306
cover_run.addArgs(&.{ kcov_bin, "--clean", "--collect-only" });
303307
cover_run.addPrefixedDirectoryArg("--include-pattern=", b.path("src"));
@@ -314,10 +318,10 @@ pub fn build(b: *std.Build) void {
314318
const test_rule_exe = b.addTest(.{
315319
.name = b.fmt("{s}_unit_test_coverage", .{rule_import.name}),
316320
.root_module = rule_import.module,
317-
.use_llvm = coverage,
321+
.use_llvm = test_coverage,
318322
});
319323

320-
if (coverage orelse false) {
324+
if (test_coverage orelse false) {
321325
const cover_run = std.Build.Step.Run.create(b, "Unit test coverage");
322326
cover_run.addArgs(&.{ kcov_bin, "--clean", "--collect-only" });
323327
cover_run.addPrefixedDirectoryArg("--include-pattern=", b.path("src"));

integration_tests/build.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub fn build(b: *std.Build) !void {
44
const target = b.standardTargetOptions(.{});
55
const optimize = b.standardOptimizeOption(.{});
66

7+
const test_focus_on_rule = b.option([]const u8, "test_focus_on_rule", "Only run tests for this rule");
78
const test_step = b.step("test", "Run tests");
89

910
const test_cases_path = b.path("test_cases/").getPath3(b, null).sub_path;
@@ -39,6 +40,12 @@ pub fn build(b: *std.Build) !void {
3940
std.log.err("Test case file skipped as its invalid: {s}", .{item.path});
4041
continue;
4142
}];
43+
if (test_focus_on_rule) |r| {
44+
if (!std.mem.eql(u8, rule_name, r)) {
45+
std.log.warn("Skipping {s}", .{rule_name});
46+
continue;
47+
}
48+
}
4249
run_integration_test.addArg(rule_name);
4350

4451
const test_name = item.basename[0..(item.basename.len - input_suffix.len)];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Situations that are normally errors but under certain situations are ignored.
2+
pub fn ignored() void {
3+
const x = 10;
4+
5+
const label = if (x > 10) "over 10" else "under 10";
6+
_ = label;
7+
8+
return if (x > 20)
9+
"over 20"
10+
else
11+
"under 20";
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.{
2+
.if_statement = .{
3+
.severity = .@"error",
4+
.requirement = .all,
5+
},
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No issues!
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
pub fn main() void {
2+
var val: u32 = 0;
3+
4+
// If statements:
5+
if (val < 10) val += 1;
6+
// zig fmt: off
7+
if (val < 10) { val += 1; }
8+
// zig fmt: on
9+
if (val < 10) {
10+
val += 1;
11+
}
12+
if (val < 10)
13+
val += 1
14+
else if (val < 10) {
15+
val += 1;
16+
val += 1;
17+
} else {
18+
val += 1;
19+
}
20+
21+
// While statements:
22+
while (val < 10) val += 1;
23+
// zig fmt: off
24+
while (val < 10) { val += 1; }
25+
// zig fmt: on
26+
while (val < 10) {
27+
val += 1;
28+
}
29+
while (val < 10)
30+
val += 1;
31+
while (val < 10) {
32+
val += 1;
33+
val += 1;
34+
}
35+
36+
// For statements:
37+
for (0..1) |i| val += i;
38+
// zig fmt: off
39+
for (0..1) |i| { val += i; }
40+
// zig fmt: on
41+
for (0..1) |i| {
42+
val += i;
43+
}
44+
for (0..1) |i|
45+
val += i;
46+
for (0..1) |i| {
47+
val += i;
48+
val += i;
49+
}
50+
51+
// Defer
52+
defer {
53+
val = 0;
54+
}
55+
defer val = 1;
56+
defer {
57+
val = 1;
58+
val = 2;
59+
}
60+
// zig fmt: off
61+
defer { val = 1; }
62+
// zig fmt: on
63+
64+
// Switch
65+
switch (val) {
66+
0 => val = 1,
67+
1 => {
68+
val = 2;
69+
},
70+
2 => {
71+
val = 3;
72+
val = 4;
73+
},
74+
// zig fmt: off
75+
3 => { val = 5; },
76+
// zig fmt: on
77+
4 => {},
78+
else => {},
79+
}
80+
}

0 commit comments

Comments
 (0)