Skip to content

Commit ce4c367

Browse files
Add separate analysis options file for tests (#5)
* Add separate analysis options file for tests * Bump version, update changelog * Update usage doc * Remove no-magic-number ignore * Explain `late` keyword disabled in test rules * Add a more elaborate rationale to disable `late` lint in tests
1 parent a4ece68 commit ce4c367

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.6
2+
3+
- Add a separate anayisis options file for tests
4+
15
## 0.0.5
26

37
- Remove the `sort_constructors_first` rule

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ And then include `solid_lints` into your project top-level `analysis_options.yam
1818
include: package:solid_lints/analysis_options.yaml
1919
```
2020

21+
Also you can use a specialized rule set designed for Dart tests.
22+
Add an `analysis_options.yaml` file under the `test/` directory, and include the ruleset:
23+
24+
```yaml
25+
include: package:solid_lints/analysis_options_test.yaml
26+
```
27+
2128
Then you can see suggestions in your IDE or you can run checks manually:
2229

2330
```bash

lib/analysis_options_test.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
include: package:solid_lints/analysis_options.yaml
2+
3+
dart_code_metrics:
4+
rules:
5+
6+
# Late keyword is allowed in tests in order to enable the use of custom mocks and
7+
# fakes.
8+
9+
# It has several benefits over its alternatives (see below):
10+
11+
# - Allows initialising the mocks inside the `setUp` method.
12+
# - Allows resetting mocks by re-initialising them inside `setUp`
13+
# - Allows passing the mock inside functions that require non-null params,
14+
# without any extra force-unwraps or null checks.
15+
# - Uninitialized test mocks are clearly traceable.
16+
# - Prodices minimal visual clutter.
17+
18+
# Alternatives considered:
19+
20+
# 1. Making the mocks `final` and non-nullable, and adding a `reset()` method
21+
# to them, which would return the mock/fake to its initial state.
22+
# - Works well with generated code from testing libraries like `mockito`.
23+
# - It's less practical with hand-written mocks, where it's possible to add a
24+
# piece of state and forget to reset it, which might lead to hard-to-trace
25+
# errors. Usually re-instantiating a test mock simplifies its code and
26+
# prevents such errors altogether, but that requires making the field
27+
# non-final, as well as delegating its initialisation to the `setUp` method.
28+
# 2. Making the mocks nullable, and using `.?` access syntax.
29+
# - In many cases will cause harder-to-trace testcase failures, if a mock was
30+
# not initialised.
31+
# - Does not allow passing the nullable mock into constructors/methods that
32+
# require a non-nullable input.
33+
# 3. Making the mocks nullable, and adding null-checks in test code.
34+
# - Will lead to significant code bloat, without much benefit, as a null mock
35+
# will still usually lead to a failed test.
36+
# 4. Making the mocks nullable and using the "bang" operator (`!`):
37+
# - In terms of behavior similar to `late`, but requires using the operator in
38+
# many places inside the test code, adding uninformative visual noise.
39+
# - The use of this operator is also discouraged by the main ruleset.
40+
avoid-late-keyword: false

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: solid_lints
22
description: Lints for Dart and Flutter based on software industry standards and best practices.
3-
version: 0.0.5
3+
version: 0.0.6
44
homepage: https://github.com/solid-software/solid_lints/
55

66
environment:

0 commit comments

Comments
 (0)