Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't trigger provider scoped lints in tests #3056

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved

## 2.3.3 - 2023-10-28

- `riverpod` upgraded to `2.4.5`
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);
}
}
4 changes: 3 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,9 @@ dev_dependencies:
riverpod_lint:
path: ../riverpod_lint
riverpod_generator: ^2.0.0
test: ^1.15.0
test: ^1.24.9
flutter_test:
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
sdk: flutter

dependency_overrides:
riverpod_generator:
Expand Down
1 change: 1 addition & 0 deletions packages/riverpod_lint_flutter_test/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ dependency_overrides:
path: ../riverpod_annotation
riverpod_lint:
path: ../riverpod_lint
test_api: ^0.6.1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flutter_test has a pinned dependency on test_api: 0.6.0 but the test package needs test_api: ^0.61. Needed to do this to get the packages all playing nicely.

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(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test where ProviderScope is not prevent in pumpWidget

And one where ProviderScope is prevent within pumpWidget, but is not the very first widget

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wil do, slick!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more tests covering these cases.

overrides: [
scopedProvider.overrideWith((ref) => 0),
unimplementedScopedProvider.overrideWith((ref) => 0),
rootProvider.overrideWith((ref) => 0),
],
child: Container(),
),
);
});
}

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