Skip to content

Commit

Permalink
Make dir and file names optional when using PackageContextForTest
Browse files Browse the repository at this point in the history
  • Loading branch information
evanweible-wf committed Nov 10, 2023
1 parent 6633735 commit 5330958
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,15 @@ import 'package:test/test.dart';
void main() {
group('AlwaysThrowsFixer', () {
test('returns Never instead', () async {
final pkg = await PackageContextForTest.fromPubspec('pkg', '''
final pkg = await PackageContextForTest.fromPubspec('''
name: pkg
publish_to: none
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
meta: ^1.0.0
''');
final context = await pkg.addFile('test.dart', '''
final context = await pkg.addFile('''
import 'package:meta/meta.dart';
@alwaysThrows toss() { throw 'Thrown'; }
''');
Expand Down
24 changes: 17 additions & 7 deletions lib/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ Future<FileContext> fileContextForTest(String name, String sourceText) async {
/// will enable the analyzer to resolve imports and symbols from other source
/// files and dependencies.
/// test('MySuggestor', () async {
/// var pkg = await PackageContextForTest.fromPubspec('pkg', '''
/// var pkg = await PackageContextForTest.fromPubspec('''
/// name: pkg
/// version: 0.0.0
/// environment:
/// sdk: '>=3.0.0 <4.0.0'
/// dependencies:
/// meta: ^1.0.0
/// ''');
/// var context = await pkg.addFile('foo.dart', '''
/// var context = await pkg.addFile('''
/// import 'package:meta/meta.dart';
/// @visibleForTesting var foo = true;
/// ''');
Expand All @@ -94,17 +94,23 @@ class PackageContextForTest {
final AnalysisContextCollection _collection;
final String _name;
final String _root;
static int _fileCounter = 0;
static int _packageCounter = 0;

/// Creates a temporary directory named [dirName] using the `test_descriptor`
/// package, installs dependencies with `dart pub get`, sets up an analysis
/// context for the package, and returns a [PackageContextForTest] wrapper
/// that allows you to add source files to the package and use them in tests.
///
/// If [dirName] is null, a unique name will be generated.
///
/// Throws an [ArgumentError] if it fails to install dependencies.
static Future<PackageContextForTest> fromPubspec(
String dirName,
String pubspecContents,
) async {
String pubspecContents, [
String? dirName,
]) async {
dirName ??= 'package_${_packageCounter++}';

await d.dir(dirName, [
d.file('pubspec.yaml', pubspecContents),
]).create();
Expand Down Expand Up @@ -133,12 +139,16 @@ ${pubGet.stderr}
/// package) with the given [sourceText] using the `test_descriptor` package
/// and returns a [FileContext] wrapper around it.
///
/// If [path] is null, a unique filename will be generated.
///
/// The returned [FileContext] will use the analysis context for this whole
/// package rather than just this file, which enables testing of [Suggestor]s
/// that require the resolved AST.
///
/// See [PackageContextForTest] for an example.
Future<FileContext> addFile(String path, String sourceText) async {
Future<FileContext> addFile(String sourceText, [String? path]) async {
path ??= 'test_${_fileCounter++}.dart';

// Use test_descriptor to create the file in a temporary directory
d.Descriptor descriptor;
final segments = p.split(path);
Expand All @@ -150,8 +160,8 @@ ${pubGet.stderr}
}
// Add the root directory.
descriptor = d.dir(_name, [descriptor]);
await descriptor.create();

await descriptor.create();
final canonicalizedPath = p.canonicalize(p.join(d.sandbox, _name, path));
return FileContext(canonicalizedPath, _collection, root: _root);
}
Expand Down
4 changes: 2 additions & 2 deletions test/ast_visiting_suggestor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ void main() {
});

test('should resolve AST and work with imports', () async {
final pkg = await PackageContextForTest.fromPubspec('pkg', '''
final pkg = await PackageContextForTest.fromPubspec('''
name: pkg
publish_to: none
environment:
sdk: '>=2.19.0 <4.0.0'
dependencies:
meta: ^1.0.0
''');
final context = await pkg.addFile('test.dart', '''
final context = await pkg.addFile('''
import 'package:meta/meta.dart';
@alwaysThrows toss() { throw 'Thrown'; }
''');
Expand Down

0 comments on commit 5330958

Please sign in to comment.