Add `hook/` to strict dependencies check (#4364)
diff --git a/lib/src/validator.dart b/lib/src/validator.dart index 112b90b..fa204ec 100644 --- a/lib/src/validator.dart +++ b/lib/src/validator.dart
@@ -216,15 +216,17 @@ }); } - /// 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();
diff --git a/lib/src/validator/strict_dependencies.dart b/lib/src/validator/strict_dependencies.dart index c0a203a..362fcb9 100644 --- a/lib/src/validator/strict_dependencies.dart +++ b/lib/src/validator/strict_dependencies.dart
@@ -65,17 +65,22 @@ 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());
diff --git a/test/validator/strict_dependencies_test.dart b/test/validator/strict_dependencies_test.dart index 155f326..9c0a5d7 100644 --- a/test/validator/strict_dependencies_test.dart +++ b/test/validator/strict_dependencies_test.dart
@@ -282,6 +282,56 @@ ); }); + 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';