tests: Use Parser to parse args

This commit is contained in:
Gautham Goli 2018-03-25 13:18:23 +05:30
parent 63da230425
commit 191e49511b
3 changed files with 21 additions and 10 deletions

View File

@ -1,4 +1,4 @@
#: * `tests` [`--verbose`] [`--coverage`] [`--generic`] [`--no-compat`] [`--only=`<test_script>[`:`<line_number>]] [`--seed` <seed>] [`--online`] [`--official-cmd-taps`]: #: * `tests` [`--verbose`] [`--coverage`] [`--generic`] [`--no-compat`] [`--only=`<test_script>[`:`<line_number>]] [`--seed=`<seed>] [`--online`] [`--official-cmd-taps`]:
#: Run Homebrew's unit and integration tests. If provided, #: Run Homebrew's unit and integration tests. If provided,
#: `--only=`<test_script> runs only <test_script>_spec.rb, and `--seed` #: `--only=`<test_script> runs only <test_script>_spec.rb, and `--seed`
#: randomizes tests with the provided value instead of a random seed. #: randomizes tests with the provided value instead of a random seed.
@ -15,6 +15,7 @@
#: If `--online` is passed, include tests that use the GitHub API and tests #: If `--online` is passed, include tests that use the GitHub API and tests
#: that use any of the taps for official external commands. #: that use any of the taps for official external commands.
require "cli_parser"
require "fileutils" require "fileutils"
require "tap" require "tap"
@ -22,6 +23,16 @@ module Homebrew
module_function module_function
def tests def tests
args = Homebrew::CLI::Parser.new do
switch "--no-compat"
switch "--generic"
switch "-v", "--verbose"
switch "--coverage"
switch "--online"
flag "--only", required: true
flag "--seed", required: true
end.parse
HOMEBREW_LIBRARY_PATH.cd do HOMEBREW_LIBRARY_PATH.cd do
ENV.delete("HOMEBREW_VERBOSE") ENV.delete("HOMEBREW_VERBOSE")
ENV.delete("VERBOSE") ENV.delete("VERBOSE")
@ -29,16 +40,16 @@ module Homebrew
ENV.delete("HOMEBREW_TEMP") ENV.delete("HOMEBREW_TEMP")
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1" ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
ENV["HOMEBREW_DEVELOPER"] = "1" ENV["HOMEBREW_DEVELOPER"] = "1"
ENV["HOMEBREW_NO_COMPAT"] = "1" if ARGV.include? "--no-compat" ENV["HOMEBREW_NO_COMPAT"] = "1" if args.no_compat?
ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if ARGV.include? "--generic" ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if args.generic?
if ARGV.include? "--online" if args.online?
ENV["HOMEBREW_TEST_ONLINE"] = "1" ENV["HOMEBREW_TEST_ONLINE"] = "1"
else else
ENV["HOMEBREW_NO_GITHUB_API"] = "1" ENV["HOMEBREW_NO_GITHUB_API"] = "1"
end end
if ARGV.include? "--coverage" if args.coverage?
ENV["HOMEBREW_TESTS_COVERAGE"] = "1" ENV["HOMEBREW_TESTS_COVERAGE"] = "1"
FileUtils.rm_f "test/coverage/.resultset.json" FileUtils.rm_f "test/coverage/.resultset.json"
end end
@ -58,8 +69,8 @@ module Homebrew
parallel = true parallel = true
files = if ARGV.value("only") files = if args.only
test_name, line = ARGV.value("only").split(":", 2) test_name, line = args.only.split(":", 2)
if line.nil? if line.nil?
Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb") Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb")
@ -84,7 +95,7 @@ module Homebrew
# Generate seed ourselves and output later to avoid multiple different # Generate seed ourselves and output later to avoid multiple different
# seeds being output when running parallel tests. # seeds being output when running parallel tests.
seed = ARGV.include?("--seed") ? ARGV.next : rand(0xFFFF).to_i seed = args.seed ? args.seed : rand(0xFFFF).to_i
args = ["-I", HOMEBREW_LIBRARY_PATH/"test"] args = ["-I", HOMEBREW_LIBRARY_PATH/"test"]
args += %W[ args += %W[

View File

@ -880,7 +880,7 @@ With `--verbose` or `-v`, many commands print extra debugging information. Note
Example: `brew install jruby && brew test jruby` Example: `brew install jruby && brew test jruby`
* `tests` [`--verbose`] [`--coverage`] [`--generic`] [`--no-compat`] [`--only=``test_script`[`:``line_number`]] [`--seed` `seed`] [`--online`] [`--official-cmd-taps`]: * `tests` [`--verbose`] [`--coverage`] [`--generic`] [`--no-compat`] [`--only=``test_script`[`:``line_number`]] [`--seed=``seed`] [`--online`] [`--official-cmd-taps`]:
Run Homebrew's unit and integration tests. If provided, Run Homebrew's unit and integration tests. If provided,
`--only=``test_script` runs only `test_script`_spec.rb, and `--seed` `--only=``test_script` runs only `test_script`_spec.rb, and `--seed`
randomizes tests with the provided value instead of a random seed. randomizes tests with the provided value instead of a random seed.

View File

@ -896,7 +896,7 @@ If \fB\-\-keep\-tmp\fR is passed, the temporary files created for the test are n
Example: \fBbrew install jruby && brew test jruby\fR Example: \fBbrew install jruby && brew test jruby\fR
. .
.TP .TP
\fBtests\fR [\fB\-\-verbose\fR] [\fB\-\-coverage\fR] [\fB\-\-generic\fR] [\fB\-\-no\-compat\fR] [\fB\-\-only=\fR\fItest_script\fR[\fB:\fR\fIline_number\fR]] [\fB\-\-seed\fR \fIseed\fR] [\fB\-\-online\fR] [\fB\-\-official\-cmd\-taps\fR] \fBtests\fR [\fB\-\-verbose\fR] [\fB\-\-coverage\fR] [\fB\-\-generic\fR] [\fB\-\-no\-compat\fR] [\fB\-\-only=\fR\fItest_script\fR[\fB:\fR\fIline_number\fR]] [\fB\-\-seed=\fR\fIseed\fR] [\fB\-\-online\fR] [\fB\-\-official\-cmd\-taps\fR]
Run Homebrew\'s unit and integration tests\. If provided, \fB\-\-only=\fR\fItest_script\fR runs only \fItest_script\fR_spec\.rb, and \fB\-\-seed\fR randomizes tests with the provided value instead of a random seed\. Run Homebrew\'s unit and integration tests\. If provided, \fB\-\-only=\fR\fItest_script\fR runs only \fItest_script\fR_spec\.rb, and \fB\-\-seed\fR randomizes tests with the provided value instead of a random seed\.
. .
.IP .IP