aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/dev-cmd/tests.rb
blob: 72d6143fcf7c0b5a7cca29a34f9bad6bfea36ab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#:  * `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,
#:    `--only=`<test_script> runs only <test_script>_spec.rb, and `--seed`
#:    randomizes tests with the provided value instead of a random seed.
#:
#:    If `--verbose` (or `-v`) is passed, print the command that runs the tests.
#:
#:    If `--coverage` is passed, also generate code coverage reports.
#:
#:    If `--generic` is passed, only run OS-agnostic tests.
#:
#:    If `--no-compat` is passed, do not load the compatibility layer when
#:    running tests.
#:
#:    If `--online` is passed, include tests that use the GitHub API.
#:
#:    If `--official-cmd-taps` is passed, include tests that use any of the
#:    taps for official external commands.

require "fileutils"
require "tap"

module Homebrew
  module_function

  def tests
    HOMEBREW_LIBRARY_PATH.cd do
      ENV.delete("HOMEBREW_VERBOSE")
      ENV.delete("VERBOSE")
      ENV.delete("HOMEBREW_CASK_OPTS")
      ENV.delete("HOMEBREW_TEMP")
      ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
      ENV["HOMEBREW_DEVELOPER"] = "1"
      ENV["HOMEBREW_NO_COMPAT"] = "1" if ARGV.include? "--no-compat"
      ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if ARGV.include? "--generic"

      if ARGV.include? "--online"
        ENV["HOMEBREW_TEST_ONLINE"] = "1"
      else
        ENV["HOMEBREW_NO_GITHUB_API"] = "1"
      end

      if ARGV.include? "--official-cmd-taps"
        ENV["HOMEBREW_TEST_OFFICIAL_CMD_TAPS"] = "1"
      end

      if ARGV.include? "--coverage"
        ENV["HOMEBREW_TESTS_COVERAGE"] = "1"
        FileUtils.rm_f "test/coverage/.resultset.json"
      end

      ENV["BUNDLE_GEMFILE"] = "#{HOMEBREW_LIBRARY_PATH}/test/Gemfile"

      # Override author/committer as global settings might be invalid and thus
      # will cause silent failure during the setup of dummy Git repositories.
      %w[AUTHOR COMMITTER].each do |role|
        ENV["GIT_#{role}_NAME"] = "brew tests"
        ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost"
        ENV["GIT_#{role}_DATE"]  = "Sun Jan 22 19:59:13 2017 +0000"
      end

      Homebrew.install_gem_setup_path! "bundler"
      unless quiet_system("bundle", "check")
        system "bundle", "install"
      end

      parallel = true

      files = if ARGV.value("only")
        test_name, line = ARGV.value("only").split(":", 2)

        if line.nil?
          Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb")
        else
          parallel = false
          ["test/#{test_name}_spec.rb:#{line}"]
        end
      else
        Dir.glob("test/**/*_spec.rb").reject { |p| p =~ %r{^test/vendor/bundle/} }
      end

      opts = []

      if ENV["CI"]
        opts << "--combine-stderr"
        opts << "--serialize-stdout"
      end

      args = [
        "--color",
        "-I", HOMEBREW_LIBRARY_PATH/"test",
        "--require", "spec_helper",
        "--format", "progress",
        "--format", "ParallelTests::RSpec::RuntimeLogger",
        "--out", "tmp/parallel_runtime_rspec.log"
      ]

      args << "--seed" << ARGV.next if ARGV.include? "--seed"

      unless OS.mac?
        args << "--tag" << "~needs_macos"
        files = files.reject { |p| p =~ %r{^test/(os/mac|cask)(/.*|_spec\.rb)$} }
      end

      unless OS.linux?
        files = files.reject { |p| p =~ %r{^test/os/linux(/.*|_spec\.rb)$} }
      end

      if parallel
        system "bundle", "exec", "parallel_rspec", *opts, "--", *args, "--", *files
      else
        system "bundle", "exec", "rspec", *args, "--", *files
      end

      return if $?.success?
      Homebrew.failed = true
    end
  end
end