Skip to content

Commit

Permalink
Don't trigger provider scoped lints in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lockie Richter committed Oct 30, 2023
1 parent e23f769 commit 005009a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/riverpod_lint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Unreleased patch
- Updated `scoped_providers_should_specify_dependencies` to ignore instances of using pumpWidget in tests

## 2.3.2 - 2023-10-21

- `riverpod_analyzer_utils` upgraded to `0.4.2`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ extension SimpleIdentifierX on SimpleIdentifier {
return libraryUri.scheme == 'package' &&
libraryUri.pathSegments.first == 'flutter';
}

bool get isPumpWidget {
if (name != 'pumpWidget') return false;

final library = staticElement?.library;
if (library == null) return false;
final libraryUri = Uri.tryParse(library.identifier);
if (libraryUri == null) return false;

return libraryUri.scheme == 'package' &&
libraryUri.pathSegments.first == 'flutter_test';
}
}

class ScopedProvidersShouldSpecifyDependencies extends RiverpodLintRule {
Expand Down Expand Up @@ -90,6 +102,7 @@ class ScopedProvidersShouldSpecifyDependencies extends RiverpodLintRule {

// If the ProviderScope isn't directly as a child of runApp, it is scoped
return enclosingExpression is! MethodInvocation ||
!enclosingExpression.methodName.isFlutterRunApp;
(!enclosingExpression.methodName.isFlutterRunApp &&
!enclosingExpression.methodName.isPumpWidget);
}
}
3 changes: 2 additions & 1 deletion packages/riverpod_lint_flutter_test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ dev_dependencies:
riverpod_lint:
path: ../riverpod_lint
riverpod_generator: ^2.0.0
test: ^1.15.0
flutter_test:
sdk: flutter

dependency_overrides:
riverpod_generator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'package:flutter/material.dart' as flutter;
import 'package:flutter/material.dart' hide runApp;
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

Expand Down Expand Up @@ -121,6 +122,21 @@ void definitelyNotAMain() {
);
}

void someTestFunction() {
testWidgets('override repositoryProvider in test', (tester) async {
await tester.pumpWidget(
ProviderScope(
overrides: [
scopedProvider.overrideWith((ref) => 0),
unimplementedScopedProvider.overrideWith((ref) => 0),
rootProvider.overrideWith((ref) => 0),
],
child: Container(),
),
);
});
}

Widget fn() {
return ProviderScope(
overrides: [
Expand Down

0 comments on commit 005009a

Please sign in to comment.