Skip to content

Commit

Permalink
add pubspec caching to PubWorkspacePackages
Browse files Browse the repository at this point in the history
See: https://github.com/dart-lang/linter/issues/29

Change-Id: Ifcd4ad359a43c9183ab42ed6bb0ebb3586c73afd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134861
Commit-Queue: Phil Quitslund <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
pq authored and [email protected] committed Feb 7, 2020
1 parent 4d10cd9 commit 15198eb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
32 changes: 29 additions & 3 deletions pkg/analyzer/lib/src/workspace/pub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/context/packages.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/lint/pub.dart';
import 'package:analyzer/src/workspace/simple.dart';
import 'package:analyzer/src/workspace/workspace.dart';
import 'package:analyzer/src/context/packages.dart';

/// Information about a Pub workspace.
class PubWorkspace extends SimpleWorkspace {
Expand All @@ -18,10 +19,14 @@ class PubWorkspace extends SimpleWorkspace {
/// Each Pub workspace is itself one package.
PubWorkspacePackage _theOnlyPackage;

/// The associated pubspec file.
final File _pubspecFile;

PubWorkspace._(
ResourceProvider provider,
Map<String, List<Folder>> packageMap,
String root,
this._pubspecFile,
) : super(provider, packageMap, root);

@override
Expand Down Expand Up @@ -52,10 +57,11 @@ class PubWorkspace extends SimpleWorkspace {
return null;
}

if (folder.getChildAssumingFile(_pubspecName).exists) {
var pubspec = folder.getChildAssumingFile(_pubspecName);
if (pubspec.exists) {
// Found the pubspec.yaml file; this is our root.
String root = folder.path;
return PubWorkspace._(provider, packageMap, root);
return PubWorkspace._(provider, packageMap, root, pubspec);
}

// Go up a folder.
Expand All @@ -73,11 +79,31 @@ class PubWorkspacePackage extends WorkspacePackage {
@override
final String root;

Pubspec _pubspec;

/// A flag to indicate if we've tried to parse the pubspec.
bool _parsedPubspec = false;

@override
final PubWorkspace workspace;

PubWorkspacePackage(this.root, this.workspace);

/// Get the associated parsed [Pubspec], or `null` if there was an error in
/// reading or parsing.
Pubspec get pubspec {
if (!_parsedPubspec) {
_parsedPubspec = true;
try {
final content = workspace._pubspecFile.readAsStringSync();
_pubspec = Pubspec.parse(content);
} catch (_) {
// Pubspec will be null.
}
}
return _pubspec;
}

@override
bool contains(Source source) {
String filePath = filePathFromSource(source);
Expand Down
31 changes: 31 additions & 0 deletions pkg/analyzer/test/src/lint/linter/linter_context_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:async';
import 'package:analyzer/src/context/builder.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/lint/linter.dart';
import 'package:analyzer/src/workspace/pub.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

Expand All @@ -17,6 +18,7 @@ main() {
defineReflectiveTests(CanBeConstConstructorTest);
defineReflectiveTests(CanBeConstTest);
defineReflectiveTests(EvaluateExpressionTest);
defineReflectiveTests(PubDependencyTest);
});
}

Expand Down Expand Up @@ -289,3 +291,32 @@ var x = 42;
return context.evaluateConstant(node);
}
}

@reflectiveTest
class PubDependencyTest extends AbstractLinterContextTest {
test_dependencies() async {
newFile('/test/pubspec.yaml', content: '''
name: test
dependencies:
args: '>=0.12.1 <2.0.0'
charcode: ^1.1.0
''');
await resolve(r'''
/// Dummy class.
class C { }
''');

expect(context.package, TypeMatcher<PubWorkspacePackage>());
final pubPackage = context.package as PubWorkspacePackage;
final pubspec = pubPackage.pubspec;

final argsDep = pubspec.dependencies
.singleWhere((element) => element.name.text == 'args');
expect(argsDep.version.value.text, '>=0.12.1 <2.0.0');

final charCodeDep = pubspec.dependencies
.singleWhere((element) => element.name.text == 'charcode');
expect(charCodeDep.version.value.text, '^1.1.0');
}
}

0 comments on commit 15198eb

Please sign in to comment.