diff --git a/CHANGELOG.md b/CHANGELOG.md index 99ade249ffecf0325d0367d51b2f8fcc1c0ae411..087276f636ba9723bade94d0da49d55dce4a2acf 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 aad1fcd6310a86e7be05ea0110b388ae116df0c7..e774dc43f545816de81ac4ec01c8989ab3d575bb 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 6b38a4d3be3e65d395d3d7fa4ad139d77e7e155d..59e536dd1bc83c31a21c4f242a02d75737ff5848 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.