Skip to content

Commit

Permalink
added catch block into isInstalled so that it would throw if an inval…
Browse files Browse the repository at this point in the history
…id package name was installed. instead it just returns false.
  • Loading branch information
bsutton committed Apr 13, 2024
1 parent 90f00c1 commit 9f1a9dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
11 changes: 8 additions & 3 deletions dcli/lib/src/util/pub_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,13 @@ class PubCache {
join(pathToDartLang, '$packageName-$version');

/// Returns true if the package is installed in pub-cache
bool isInstalled(String packageName) =>
findPrimaryVersion(packageName) != null;
bool isInstalled(String packageName) {
try {
return findPrimaryVersion(packageName) != null;
} on core.FindException {
return false;
}
}

/// Finds and returns the latest (non-pre-release) version installed into pub
/// cache for the given package.
Expand Down Expand Up @@ -286,7 +291,7 @@ class PubCache {

/// run pub global list to see if dcli is run from a local path.
final line = DartSdk()
.runPub(args: ['global', 'list'], progress: Progress.capture())
. runPub(args: ['global', 'list'], progress: Progress.capture())
.lines
.firstWhere((line) => line.startsWith(packageName),
orElse: () => notFound);
Expand Down
2 changes: 1 addition & 1 deletion dcli_sdk/lib/src/commands/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ compile [--nowarmup] [--install] [--overwrite] [<script path.dart>, <script path
if (!PubCache().isInstalled(packageName) &&
!PubCache().isGloballyActivated(packageName)) {
throw InvalidCommandArgumentException('''
To compile the package $packageName it must first be installed.
To compile the package $packageName it must first be installed.
Run:
dart pub global activate $packageName
''');
Expand Down
24 changes: 24 additions & 0 deletions dcli_sdk/test/src/commands/compile_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:dcli_sdk/src/commands/compile.dart';
import 'package:dcli_sdk/src/util/exceptions.dart';
import 'package:test/test.dart';

void main() {
test('compile invalid package name', () async {
// expect(() => range(5, 3),
// throwsA(predicate((e) => e is ArgumentError && e.message == 'start must be less than stop')));
expect(
() => CompileCommand().compilePackage('git/dcli_scripts'),
throwsA(predicate((e) =>
e is InvalidCommandArgumentException &&
e.message
.contains('To compile the package git/dcli_scripts it must')

// ==
// '''
// To compile the package git/dcli_scripts it must first be installed.
// Run:
// dart pub global activate git/dcli_scripts
// '''
)));
});
}

0 comments on commit 9f1a9dc

Please sign in to comment.