From 8f87b9ec2e85b6a12b80b7b0be7a06b0f0066011 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum <nweiz@google.com> Date: Wed, 15 Apr 2015 15:57:13 -0700 Subject: [PATCH] Support a --version flag. Closes #72 R=kevmoo@google.com Review URL: https://codereview.chromium.org//1053113004 --- CHANGELOG.md | 4 +++ lib/src/executable.dart | 68 ++++++++++++++++++++++++++++++++++++ test/runner/runner_test.dart | 1 + 3 files changed, 73 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99ade249..087276f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 0.12.0-beta.8 + +* Add a `--version` flag. + ### 0.12.0-beta.7 * Browser tests can now load assets by making HTTP requests to the corresponding diff --git a/lib/src/executable.dart b/lib/src/executable.dart index aad1fcd6..e774dc43 100644 --- a/lib/src/executable.dart +++ b/lib/src/executable.dart @@ -74,6 +74,8 @@ bool get _usesTransformer { void main(List<String> args) { _parser.addFlag("help", abbr: "h", negatable: false, help: "Shows this usage information."); + _parser.addFlag("version", negatable: false, + help: "Shows the package's version."); _parser.addOption("package-root", hide: true); _parser.addOption("name", abbr: 'n', @@ -114,6 +116,14 @@ void main(List<String> args) { return; } + if (options["version"]) { + if (!_printVersion()) { + stderr.writeln("Couldn't find version number."); + exitCode = exit_codes.data; + } + return; + } + var color = options["color"]; if (color == null) color = canUseSpecialChars; @@ -292,3 +302,61 @@ Usage: pub run test:test [files or directories...] ${_parser.usage} """); } + +/// Prints the version number of the test package. +/// +/// This loads the version number from the current package's lockfile. It +/// returns true if it successfully printed the version number and false if it +/// couldn't be loaded. +bool _printVersion() { + var lockfile; + try { + lockfile = loadYaml(new File("pubspec.lock").readAsStringSync()); + } on FormatException catch (_) { + return false; + } on IOException catch (_) { + return false; + } + + if (lockfile is! Map) return false; + var packages = lockfile["packages"]; + if (packages is! Map) return false; + var package = packages["test"]; + if (package is! Map) return false; + + var source = package["source"]; + if (source is! String) return false; + + switch (source) { + case "hosted": + var version = package["version"]; + if (version is! String) return false; + + print(version); + return true; + + case "git": + var version = package["version"]; + if (version is! String) return false; + var description = package["description"]; + if (description is! Map) return false; + var ref = description["resolved-ref"]; + if (ref is! String) return false; + + print("$version (${ref.substring(0, 7)})"); + return true; + + case "path": + var version = package["version"]; + if (version is! String) return false; + var description = package["description"]; + if (description is! Map) return false; + var path = description["path"]; + if (path is! String) return false; + + print("$version (from $path)"); + return true; + + default: return false; + } +} diff --git a/test/runner/runner_test.dart b/test/runner/runner_test.dart index 6b38a4d3..59e536dd 100644 --- a/test/runner/runner_test.dart +++ b/test/runner/runner_test.dart @@ -42,6 +42,7 @@ final _usage = """ Usage: pub run test:test [files or directories...] -h, --help Shows this usage information. + --version Shows the package's version. -n, --name A substring of the name of the test to run. Regular expression syntax is supported. -- GitLab