pipe/test/package_target_test.dart
trvon 31607b5bcb
Some checks failed
Lint Commits / commitlint (pull_request) Successful in 16s
Tests / Static Analysis (pull_request) Has been cancelled
Tests / Tests (pull_request) Has been cancelled
fix(queries): resolve a subdirectory path to its enclosing package
Found by dogfooding on nuwayze: `pipe queries lib` from the repo root
failed with

  no resolved package graph at .dart_tool/package_config.json
    remedy: run `flutter pub get` or `dart pub get` in .../nuwayze-w3/lib

It failed even with the package resolved, because the path was taken
as the project root: pipe looked for lib/.dart_tool/package_config.json,
which never exists, and the remedy sent the user to run pub get in a
directory that has no pubspec.

enclosingPackage() (project.dart) splits a path into the package that
contains it (the path itself when it holds a pubspec.yaml, else the
nearest ancestor that does) and the directory it names under that
root. `pipe queries` loads the package root and scans only that
directory, so `pipe queries lib/services` is `--dir lib/services` on
the package; an explicit --dir with such a path is relative to it.
Without a resolved graph the error now names the package root.

loadProject's own remedy uses the same lookup, so `pipe check lib`
(and the other commands, which still take the path as the root) now
says the path is inside the package at <root> and suggests passing
that with `--dir lib`, instead of pub get in lib.

Tests: enclosingPackage on a root, a nested directory, a nested
package and a missing path; loadProject's remedy for a directory
inside a package; and `pipe queries` on the fixture's lib/data
(root reported as the package, one file discovered), with
`lib --dir data`, and on an unresolved package where the remedy names
the root, not lib.

On nuwayze-w3: `pipe queries lib` exits 0 with 313 queries over 847
files, identical to `pipe queries .`; `pipe queries lib/services` scans
161 files and every one of its 212 queries sits under lib/services.
233 tests pass.
2026-09-25 20:16:23 -06:00

64 lines
2 KiB
Dart

import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:pipe/pipe.dart';
import 'package:test/test.dart';
void main() {
late String root;
setUp(() {
root = Directory.systemTemp
.createTempSync('pipe-package-target')
.resolveSymbolicLinksSync();
File(p.join(root, 'pubspec.yaml')).writeAsStringSync('name: outer\n');
Directory(p.join(root, 'lib', 'data')).createSync(recursive: true);
File(p.join(root, 'lib', 'data', 'a.dart')).writeAsStringSync('');
File(p.join(root, 'nested', 'pubspec.yaml'))
..createSync(recursive: true)
..writeAsStringSync('name: nested\n');
Directory(p.join(root, 'nested', 'lib')).createSync();
});
tearDown(() => Directory(root).deleteSync(recursive: true));
group('enclosingPackage', () {
test('a package root is its own package, with no scope', () {
final target = enclosingPackage(root);
expect(target.root, root);
expect(target.scope, isNull);
});
test('a directory inside a package resolves to the package root', () {
final target = enclosingPackage(p.join(root, 'lib', 'data'));
expect(target.root, root);
expect(target.scope, p.join('lib', 'data'));
});
test('the nearest pubspec wins', () {
final target = enclosingPackage(p.join(root, 'nested', 'lib'));
expect(target.root, p.join(root, 'nested'));
expect(target.scope, 'lib');
});
test('a missing path is returned unchanged for loadProject to reject', () {
final missing = p.join(root, 'nope');
expect(enclosingPackage(missing).root, missing);
expect(enclosingPackage(missing).scope, isNull);
});
});
test('loadProject on a directory inside a package names the package', () {
// `pipe check lib` used to say "run `dart pub get` in .../lib".
expect(
() => loadProject(p.join(root, 'lib')),
throwsA(
isA<PipeException>().having(
(e) => e.remedy,
'remedy',
allOf(contains('inside the package at $root'), contains('--dir lib')),
),
),
);
});
}