Search code examples
flutterdartflutter-test

How to disable a specific analysis option rule in a folder


I have a project which can be simplified to:

lib/
 |- main.dart
test/
test_environment/
 |- test_test.dart

Where test_environment contains a set of "special" tests that cannot simply be run with

flutter test

They need some specific setting (for example: flutter test --dart-define=myVar=myValue).


In my lib/main.dart, I have something like:

class MyClass {
  @visibleForTesting
  MyClass.testConstructor();
}

I would like to use MyClass.testConstructor() from test/test_helper.dart. But when I do, I have a warning:

The member 'MyClass.testConstructor' can only be used within 'package:my_project/lib/main.dart' or a test. dart(invalid_use_of_visible_for_testing_member)`

I would like to disable this rule for the test_environment/ folder, but not for the lib/ folder. How can I do that?


Solution

  • The solution I found was to create a custom analysis_options.yaml for the folder test_environment/ that extends the root analysis_options.yaml:

    lib/
     |- main.dart
    test/
    test_environment/
     |- test_test.dart
     |- analysis_options.yaml
    analysis_options.yaml
    

    And in test_environment/analysis_options.yaml:

    include: ../analysis_options.yaml
    
    analyzer:
      errors:
        invalid_use_of_visible_for_testing_member: ignore # <- Disable the analyzer rule.
    
    linter:
      rules:
        only_throw_errors: false # <- An example to show how to disable a linter rule.
    

    This takes the analyzer/linter rules set by the root analysis_options.yaml and overrides the invalid_use_of_visible_for_testing_member to disable it in test_environment.