-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: disable avoid_hardcoded_color lint if it's a test file (#83)
* feat: disable avoid_hardcoded_color lint if it's a test file * test: add file for tests
- Loading branch information
Showing
8 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
packages/altive_lints/example/lints/avoid_hardcoded_color_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,28 @@ | ||
// Check the `avoid_hardcoded_color` rule. | ||
// | ||
// This file ends with the name `_test.dart`, | ||
// so it should be exempt from the warning. | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
class MyWidget extends StatelessWidget { | ||
const MyWidget({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
children: [ | ||
const ColoredBox(color: Color(0xFF00FF00)), | ||
const ColoredBox(color: Color.fromRGBO(0, 255, 0, 1)), | ||
const ColoredBox(color: Colors.green), | ||
ColoredBox(color: Theme.of(context).colorScheme.primary), | ||
ColoredBox(color: _colorScheme.primary), | ||
const ColoredBox(color: Colors.transparent), | ||
], | ||
); | ||
} | ||
} | ||
|
||
ColorScheme get _colorScheme => const ColorScheme.dark( | ||
primary: Color.fromRGBO(0, 255, 0, 1), | ||
); |
27 changes: 27 additions & 0 deletions
27
packages/altive_lints/example/test/avoid_hardcoded_color.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,27 @@ | ||
// Check the `avoid_hardcoded_color` rule. | ||
// | ||
// It should exclude warnings for the entire `test` directory. | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
class MyWidget extends StatelessWidget { | ||
const MyWidget({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
children: [ | ||
const ColoredBox(color: Color(0xFF00FF00)), | ||
const ColoredBox(color: Color.fromRGBO(0, 255, 0, 1)), | ||
const ColoredBox(color: Colors.green), | ||
ColoredBox(color: Theme.of(context).colorScheme.primary), | ||
ColoredBox(color: _colorScheme.primary), | ||
const ColoredBox(color: Colors.transparent), | ||
], | ||
); | ||
} | ||
} | ||
|
||
ColorScheme get _colorScheme => const ColorScheme.dark( | ||
primary: Color.fromRGBO(0, 255, 0, 1), | ||
); |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import 'package:analyzer/source/source.dart'; | ||
|
||
/// Whether the given [source] is a test file. | ||
bool isTestFile(Source source) { | ||
return source.uri.pathSegments.contains('test') || | ||
source.shortName.endsWith('_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 |
---|---|---|
|
@@ -18,3 +18,6 @@ dependencies: | |
analyzer: ^6.7.0 | ||
collection: ^1.18.0 | ||
custom_lint_builder: ^0.7.0 | ||
|
||
dev_dependencies: | ||
test: ^1.25.14 |
35 changes: 35 additions & 0 deletions
35
packages/altive_lints/test/src/utils/files_utils_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,35 @@ | ||
import 'package:altive_lints/src/utils/files_utils.dart'; | ||
import 'package:analyzer/file_system/physical_file_system.dart'; | ||
import 'package:analyzer/source/file_source.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('isTestFile', () { | ||
test('False if it is a not test file', () { | ||
final resourceProvider = PhysicalResourceProvider.INSTANCE; | ||
|
||
const filePath = '/project/lib/src/implementation.dart'; | ||
final file = resourceProvider.getFile(filePath); | ||
final source = FileSource(file); | ||
expect(isTestFile(source), isFalse); | ||
}); | ||
|
||
test('True if it is a test file', () { | ||
final resourceProvider = PhysicalResourceProvider.INSTANCE; | ||
|
||
const filePath = '/project/lib/src/implementation_test.dart'; | ||
final file = resourceProvider.getFile(filePath); | ||
final source = FileSource(file); | ||
expect(isTestFile(source), isTrue); | ||
}); | ||
|
||
test('True if it is a test directory', () { | ||
final resourceProvider = PhysicalResourceProvider.INSTANCE; | ||
|
||
const filePath = '/project/test/src/util.dart'; | ||
final file = resourceProvider.getFile(filePath); | ||
final source = FileSource(file); | ||
expect(isTestFile(source), isTrue); | ||
}); | ||
}); | ||
} |