Skip to content

Commit

Permalink
Add hook/ to strict dependencies check (#4364)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored Sep 3, 2024
1 parent d86e3c9 commit 9f93040
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
12 changes: 7 additions & 5 deletions lib/src/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,17 @@ abstract class Validator {
});
}

/// Returns the [files] that are inside [dir] (relative to the package
/// entrypoint).
/// Returns the [files] that are [path] or inside [path] (relative to the
/// package entrypoint).
// TODO(sigurdm): Consider moving this to a more central location.
List<String> filesBeneath(String dir, {required bool recursive}) {
final base = p.canonicalize(p.join(package.dir, dir));
List<String> filesBeneath(String path, {required bool recursive}) {
final base = p.canonicalize(p.join(package.dir, path));
return files
.where(
recursive
? (file) => p.isWithin(base, p.canonicalize(file))
? (file) =>
p.isWithin(base, p.canonicalize(file)) ||
p.canonicalize(file) == base
: (file) => p.canonicalize(p.dirname(file)) == base,
)
.toList();
Expand Down
15 changes: 10 additions & 5 deletions lib/src/validator/strict_dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,22 @@ class StrictDependenciesValidator extends Validator {
Future validate() async {
final dependencies = package.dependencies.keys.toSet()..add(package.name);
final devDependencies = MapKeySet(package.devDependencies);
_validateLibBin(dependencies, devDependencies);
_validateLibBinHook(dependencies, devDependencies);
_validateBenchmarkTestTool(dependencies, devDependencies);
}

/// Validates that no Dart files in `lib/` or `bin/` have dependencies that
/// aren't in [deps].
/// Validates that no Dart files in `lib/`, `bin/`, `hook/build.dart`, or
/// `hook/link.dart` have dependencies that aren't in [deps].
///
/// The [devDeps] are used to generate special warnings for files that import
/// dev dependencies.
void _validateLibBin(Set<String> deps, Set<String> devDeps) {
for (var usage in _usagesBeneath(['lib', 'bin'])) {
void _validateLibBinHook(Set<String> deps, Set<String> devDeps) {
for (var usage in _usagesBeneath([
'bin',
'hook/build.dart',
'hook/link.dart',
'lib',
])) {
if (!deps.contains(usage.package)) {
if (devDeps.contains(usage.package)) {
errors.add(usage.dependencyMisplaceMessage());
Expand Down
50 changes: 50 additions & 0 deletions test/validator/strict_dependencies_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,56 @@ linter:
);
});

test('hook does not declare an "import" as a dependency', () async {
await d.dir(
p.join(appPath, 'hook'),
[
d.file('build.dart', r'''
import 'package:silly_monkey/silly_monkey.dart';
'''),
],
).create();

await expectValidationDeprecated(
strictDeps,
errors: [
matches('does not have silly_monkey in the `dependencies` section'),
],
);
});

test('hook declares an import as a devDependency for', () async {
await d.dir(
appPath,
[
d.libPubspec(
'test_pkg',
'1.0.0',
devDeps: {'silly_monkey': '^1.2.3'},
sdk: '>=1.8.0 <2.0.0',
),
d.dir(
'hook',
[
d.file('build.dart', r'''
import 'package:silly_monkey/silly_monkey.dart';
'''),
],
),
],
).create();

await expectValidationDeprecated(
strictDeps,
errors: [
matches(
'silly_monkey is in the `dev_dependencies` section of '
'`pubspec.yaml`',
),
],
);
});

test('does not declare an "export" as a dependency', () async {
await d.file(p.join(appPath, 'lib', 'library.dart'), r'''
export 'package:silly_monkey/silly_monkey.dart';
Expand Down

0 comments on commit 9f93040

Please sign in to comment.