diff --git a/lib/src/command/cache.dart b/lib/src/command/cache.dart index 4a370f728..afdb3d15f 100644 --- a/lib/src/command/cache.dart +++ b/lib/src/command/cache.dart @@ -6,6 +6,7 @@ import '../command.dart'; import 'cache_add.dart'; import 'cache_clean.dart'; import 'cache_list.dart'; +import 'cache_path.dart'; import 'cache_preload.dart'; import 'cache_repair.dart'; @@ -22,6 +23,7 @@ class CacheCommand extends PubCommand { addSubcommand(CacheAddCommand()); addSubcommand(CacheListCommand()); addSubcommand(CacheCleanCommand()); + addSubcommand(CachePathCommand()); addSubcommand(CacheRepairCommand()); addSubcommand(CachePreloadCommand()); } diff --git a/lib/src/command/cache_path.dart b/lib/src/command/cache_path.dart new file mode 100644 index 000000000..026ac9d2b --- /dev/null +++ b/lib/src/command/cache_path.dart @@ -0,0 +1,22 @@ +// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file +// 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 '../command.dart'; +import '../log.dart' as log; + +class CachePathCommand extends PubCommand { + @override + String get name => 'path'; + @override + String get description => 'Prints the path to the global PUB_CACHE.'; + @override + bool get takesArguments => false; + + CachePathCommand(); + + @override + Future runProtected() async { + log.message(cache.rootDir); + } +} diff --git a/test/cache/path_test.dart b/test/cache/path_test.dart new file mode 100644 index 000000000..dd8fef098 --- /dev/null +++ b/test/cache/path_test.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// 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 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../descriptor.dart' as d; +import '../test_pub.dart'; + +void main() { + test('running pub cache path', () async { + final cache = p.join(d.sandbox, cachePath); + await runPub(args: ['cache', 'path'], output: cache); + }); + test( + 'running pub cache path with PUB_CACHE set prints the right path', + () async { + final envCache = p.join(d.sandbox, 'otherCache'); + await runPub( + args: ['cache', 'path'], + environment: {'PUB_CACHE': envCache}, + output: envCache, + ); + }, + ); +} diff --git a/test/testdata/goldens/help_test/pub cache --help.txt b/test/testdata/goldens/help_test/pub cache --help.txt index fdb0b2c09..966e6919c 100644 --- a/test/testdata/goldens/help_test/pub cache --help.txt +++ b/test/testdata/goldens/help_test/pub cache --help.txt @@ -10,6 +10,7 @@ Usage: pub cache [arguments...] Available subcommands: add Install a package. clean Clears the global PUB_CACHE. + path Prints the path to the global PUB_CACHE. repair Reinstall cached packages. Run "pub help" to see global options. diff --git a/test/testdata/goldens/help_test/pub cache path --help.txt b/test/testdata/goldens/help_test/pub cache path --help.txt new file mode 100644 index 000000000..23cbd902d --- /dev/null +++ b/test/testdata/goldens/help_test/pub cache path --help.txt @@ -0,0 +1,11 @@ +# GENERATED BY: test/help_test.dart + +## Section 0 +$ pub cache path --help +Prints the path to the global PUB_CACHE. + +Usage: pub cache path [arguments...] +-h, --help Print this usage information. + +Run "pub help" to see global options. +