This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add static code diagnostic prefer-correct-test-file-name (#1000)
* feat: add static code diagnostic prefer-correct-test-file-name * chore: fix admotion * chore: replace usage of name2 with name Co-authored-by: Dmitry Krutskikh <dmitry.krutskikh@gmail.com>
- Loading branch information
1 parent
a254d6e
commit 1d8e535
Showing
14 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...analyzers/lint_analyzer/rules/rules_list/prefer_correct_test_file_name/config_parser.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
part of 'prefer_correct_test_file_name_rule.dart'; | ||
|
||
class _ConfigParser { | ||
static const _namePatternConfig = 'name-pattern'; | ||
|
||
static String parseNamePattern(Map<String, Object> config) { | ||
final raw = config[_namePatternConfig]; | ||
|
||
return raw is String ? raw : '_test.dart'; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...er/rules/rules_list/prefer_correct_test_file_name/prefer_correct_test_file_name_rule.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// ignore_for_file: public_member_api_docs | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
|
||
import '../../../../../utils/node_utils.dart'; | ||
import '../../../lint_utils.dart'; | ||
import '../../../models/internal_resolved_unit_result.dart'; | ||
import '../../../models/issue.dart'; | ||
import '../../../models/severity.dart'; | ||
import '../../models/common_rule.dart'; | ||
import '../../rule_utils.dart'; | ||
|
||
part 'config_parser.dart'; | ||
part 'visitor.dart'; | ||
|
||
class PreferCorrectTestFileNameRule extends CommonRule { | ||
static const String ruleId = 'prefer-correct-test-file-name'; | ||
|
||
static const _warningMessage = 'Test file name should end with '; | ||
|
||
final String _fileNamePattern; | ||
|
||
PreferCorrectTestFileNameRule([Map<String, Object> config = const {}]) | ||
: _fileNamePattern = _ConfigParser.parseNamePattern(config), | ||
super( | ||
id: ruleId, | ||
severity: readSeverity(config, Severity.warning), | ||
excludes: | ||
hasExcludes(config) ? readExcludes(config) : ['/**', '!test/**'], | ||
); | ||
|
||
@override | ||
Iterable<Issue> check(InternalResolvedUnitResult source) { | ||
final visitor = _Visitor(source.path, _fileNamePattern); | ||
|
||
source.unit.visitChildren(visitor); | ||
|
||
return visitor.declaration | ||
.map((declaration) => createIssue( | ||
rule: this, | ||
location: nodeLocation(node: declaration, source: source), | ||
message: '$_warningMessage$_fileNamePattern', | ||
)) | ||
.toList(growable: false); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_correct_test_file_name/visitor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
part of 'prefer_correct_test_file_name_rule.dart'; | ||
|
||
class _Visitor extends GeneralizingAstVisitor<void> { | ||
final String path; | ||
final String pattern; | ||
|
||
final _declarations = <FunctionDeclaration>[]; | ||
|
||
Iterable<FunctionDeclaration> get declaration => _declarations; | ||
|
||
_Visitor(this.path, this.pattern); | ||
|
||
@override | ||
void visitFunctionDeclaration(FunctionDeclaration node) { | ||
// ignore: deprecated_member_use | ||
if (node.name.name != 'main' || _matchesTestName(path)) { | ||
return; | ||
} | ||
|
||
_declarations.add(node); | ||
} | ||
|
||
bool _matchesTestName(String path) => path.endsWith(pattern); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...int_analyzer/rules/rules_list/prefer_correct_test_file_name/examples/correct_example.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void main() {} |
4 changes: 4 additions & 0 deletions
4
...lyzers/lint_analyzer/rules/rules_list/prefer_correct_test_file_name/examples/example.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// LINT | ||
void main() { | ||
print('Hello'); | ||
} |
54 changes: 54 additions & 0 deletions
54
...les/rules_list/prefer_correct_test_file_name/prefer_correct_test_file_name_rule_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/severity.dart'; | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/rules/rules_list/prefer_correct_test_file_name/prefer_correct_test_file_name_rule.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../../../helpers/rule_test_helper.dart'; | ||
|
||
const _examplePath = 'prefer_correct_test_file_name/examples/example.dart'; | ||
const _correctExamplePath = | ||
'prefer_correct_test_file_name/examples/correct_example.dart'; | ||
|
||
void main() { | ||
group('PreferCorrectTestFileNameRule', () { | ||
test('initialization', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = PreferCorrectTestFileNameRule().check(unit); | ||
|
||
RuleTestHelper.verifyInitialization( | ||
issues: issues, | ||
ruleId: 'prefer-correct-test-file-name', | ||
severity: Severity.warning, | ||
); | ||
}); | ||
|
||
test('reports about found issues', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final config = {'name-pattern': 'correct_example.dart'}; | ||
|
||
final issues = PreferCorrectTestFileNameRule(config).check(unit); | ||
|
||
RuleTestHelper.verifyIssues( | ||
issues: issues, | ||
startLines: [2], | ||
startColumns: [1], | ||
locationTexts: [ | ||
'void main() {\n' | ||
" print('Hello');\n" | ||
'}', | ||
], | ||
messages: [ | ||
'Test file name should end with correct_example.dart', | ||
], | ||
); | ||
}); | ||
|
||
test('reports no found issues', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_correctExamplePath); | ||
final config = {'name-pattern': 'correct_example.dart'}; | ||
|
||
final issues = PreferCorrectTestFileNameRule(config).check(unit); | ||
|
||
RuleTestHelper.verifyNoIssues(issues); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
website/docs/rules/common/prefer-correct-test-file-name.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import RuleDetails from '@site/src/components/RuleDetails'; | ||
|
||
<RuleDetails version="4.19.0" severity="warning" /> | ||
|
||
Warns if the file within `/test` contains a `main`, but the file name doesn't end with `_test.dart`. | ||
|
||
:::note | ||
|
||
If you want to set `exclude` config for this rule, the default `['/**', '!test/**']` will be overriden. | ||
|
||
::: | ||
|
||
### Example {#example} | ||
|
||
**❌ Bad:** | ||
|
||
File name: **some_file.dart** | ||
|
||
```dart | ||
void main() { | ||
... | ||
} | ||
``` | ||
|
||
**✅ Good:** | ||
|
||
File name: **some_file_test.dart** | ||
|
||
```dart | ||
void main() { | ||
... | ||
} | ||
``` | ||
|
||
File name: **some_other_file.dart** | ||
|
||
```dart | ||
void helperFunction() { | ||
... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters