Don't attempt to fetch advisories when --offline (#4697)
diff --git a/lib/src/source/hosted.dart b/lib/src/source/hosted.dart index 269c7a0..f0be1f9 100644 --- a/lib/src/source/hosted.dart +++ b/lib/src/source/hosted.dart
@@ -581,6 +581,10 @@ PackageRef ref, SystemCache cache, ) async { + if (cache.isOffline) { + // Don't attempt to fetch advisories in `--offline` mode. + return null; + } final description = ref.description; if (description is! HostedDescription) { throw ArgumentError('Wrong source');
diff --git a/test/get/hosted/advisory_test.dart b/test/get/hosted/advisory_test.dart index ded0377..3381235 100644 --- a/test/get/hosted/advisory_test.dart +++ b/test/get/hosted/advisory_test.dart
@@ -2,7 +2,11 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:io'; + +import 'package:path/path.dart' as p; import 'package:shelf/shelf.dart'; +import 'package:test/test.dart'; import '../../descriptor.dart' as d; import '../../golden_file.dart'; @@ -426,4 +430,34 @@ await ctx.run(['get']); }); + + test('do not fetch advisories when.`--offline`', () async { + final server = await servePackages(); + server.serve('foo', '1.2.3'); + + await d.dir(appPath, [ + d.pubspec({ + 'name': 'app', + 'dependencies': {'foo': '^1.0.0'}, + }), + ]).create(); + + server.addAdvisory( + advisoryId: '123', + displayUrl: 'https://github.com/advisories/123', + aliases: ['abc', 'def'], + affectedPackages: [ + AffectedPackage(name: 'foo', versions: ['1.2.3']), + ], + ); + + await pubGet(); + server.serveErrors(); // Ensures that the server gets no requests + + File( + p.join(d.sandbox, d.hostedCachePath(), '.cache', 'foo-advisories.json'), + ).deleteSync(); + File(p.join(d.sandbox, appPath, 'pubspec.lock')).deleteSync(); + await pubGet(args: ['--offline']); + }); }