Class: Homebrew::DevCmd::Tests Private

Inherits:
AbstractCommand show all
Includes:
OS::Linux::DevCmd::Tests, OS::Mac::DevCmd::Tests, SystemCommand::Mixin
Defined in:
dev-cmd/tests.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/tests.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args

Instance Method Summary collapse

Methods included from SystemCommand::Mixin

#system_command, #system_command!

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::DevCmd::Tests::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/tests.rbi', line 10

def args; end

#runvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'dev-cmd/tests.rb', line 56

def run
  # Given we might be testing various commands, we probably want everything (except sorbet-static)
  groups = Homebrew.valid_gem_groups - ["sorbet"]
  groups << "prof" if args.stackprof? || args.vernier? || args.ruby_prof?
  Homebrew.install_bundler_gems!(groups:)

  HOMEBREW_LIBRARY_PATH.cd do
    setup_environment!

    # Needs required here, after `setup_environment!`, so that
    # `HOMEBREW_TEST_GENERIC_OS` is set and `OS.linux?` and `OS.mac?` both
    # `return false`.
    require "extend/os/dev-cmd/tests"

    check_test_environment!

    parallel = !args.no_parallel?

    only = args.only
    files = if only
      only.split(",").flat_map do |test|
        test_name, line = test.split(":", 2)
        tests = if line.present?
          parallel = false
          ["test/#{test_name}_spec.rb:#{line}"]
        else
          Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb")
        end
        raise UsageError, "Invalid `--only` argument: #{test}" if tests.blank?

        tests
      end
    elsif args.changed?
      changed_test_files
    else
      Dir.glob("test/**/*_spec.rb")
    end

    if files.blank?
      raise UsageError, "The `--only` argument requires a valid file or folder name!" if only

      if args.changed?
        opoo "No tests are directly associated with the changed files!"
        return
      end
    end

    # We use `ParallelTests.last_process?` in `test/spec_helper.rb` to
    # handle SimpleCov output but, due to how the method is implemented,
    # it doesn't work as expected if the number of processes is greater
    # than one but lower than the number of CPU cores in the execution
    # environment. Coverage information isn't saved in that scenario,
    # so we disable parallel testing as a workaround in this case.
    parallel = false if args.coverage? && files.length < Hardware::CPU.cores

    parallel_rspec_log_name = "parallel_runtime_rspec"
    parallel_rspec_log_name = "#{parallel_rspec_log_name}.generic" if args.generic?
    parallel_rspec_log_name = "#{parallel_rspec_log_name}.online" if args.online?
    parallel_rspec_log_name = "#{parallel_rspec_log_name}.log"

    parallel_rspec_log_path = if ENV["CI"]
      "tests/#{parallel_rspec_log_name}"
    else
      "#{HOMEBREW_CACHE}/#{parallel_rspec_log_name}"
    end
    ENV["PARALLEL_RSPEC_LOG_PATH"] = parallel_rspec_log_path

    parallel_args = if ENV["CI"]
      %W[
        --combine-stderr
        --serialize-stdout
        --runtime-log #{parallel_rspec_log_path}
      ]
    else
      %w[
        --nice
      ]
    end

    # Generate seed ourselves and output later to avoid multiple different
    # seeds being output when running parallel tests.
    seed = args.seed || rand(0xFFFF).to_i

    bundle_args = ["-I", (HOMEBREW_LIBRARY_PATH/"test").to_s]
    bundle_args += %W[
      --seed #{seed}
      --color
      --require spec_helper
    ]
    bundle_args << "--fail-fast" if args.fail_fast?
    bundle_args << "--profile" << args.profile if args.profile
    bundle_args << "--tag" << "~needs_arm" unless Hardware::CPU.arm?
    bundle_args << "--tag" << "~needs_intel" unless Hardware::CPU.intel?
    bundle_args << "--tag" << "~needs_network" unless args.online?
    bundle_args << "--tag" << "~needs_ci" unless ENV["CI"]

    bundle_args = os_bundle_args(bundle_args)
    files = os_files(files)

    puts "Randomized with seed #{seed}"

    ENV["HOMEBREW_DEBUG"] = "1" if args.debug? # Used in spec_helper.rb to require the "debug" gem.

    # Workaround for:
    #
    # ```
    # ruby: no -r allowed while running setuid (SecurityError)
    # ```
    Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid

    test_prof = "#{HOMEBREW_LIBRARY_PATH}/tmp/test_prof"
    if args.stackprof?
      ENV["TEST_STACK_PROF"] = "1"
      prof_input_filename = "#{test_prof}/stack-prof-report-wall-raw-total.dump"
      prof_filename = "#{test_prof}/stack-prof-report-wall-raw-total.html"
    elsif args.vernier?
      ENV["TEST_VERNIER"] = "1"
    elsif args.ruby_prof?
      ENV["TEST_RUBY_PROF"] = "call_stack"
      prof_filename = "#{test_prof}/ruby-prof-report-call_stack-wall-total.html"
    end

    if parallel
      system("bundle", "exec", "parallel_rspec", *parallel_args,
             "--", *bundle_args, "--", *files)
    else
      system("bundle", "exec", "rspec", *bundle_args, "--", *files)
    end
    success = $CHILD_STATUS.success?

    safe_system "stackprof --d3-flamegraph #{prof_input_filename} > #{prof_filename}" if args.stackprof?

    exec_browser prof_filename if prof_filename

    return if success

    Homebrew.failed = true
  end
end

#setup_environment!Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'dev-cmd/tests.rb', line 197

def setup_environment!
  # Cleanup any unwanted user configuration.
  allowed_test_env = %w[
    HOMEBREW_GITHUB_API_TOKEN
    HOMEBREW_CACHE
    HOMEBREW_LOGS
    HOMEBREW_TEMP
  ]
  allowed_test_env << "HOMEBREW_USE_RUBY_FROM_PATH" if Homebrew::EnvConfig.developer?
  Homebrew::EnvConfig::ENVS.keys.map(&:to_s).each do |env|
    next if allowed_test_env.include?(env)

    ENV.delete(env)
  end

  # Fetch JSON API files if needed.
  require "api"
  Homebrew::API.fetch_api_files!

  # Codespaces HOMEBREW_PREFIX and /tmp are mounted 755 which makes Ruby warn constantly.
  if (ENV["HOMEBREW_CODESPACES"] == "true") && (HOMEBREW_TEMP.to_s == "/tmp")
    # Need to keep this fairly short to avoid socket paths being too long in tests.
    homebrew_prefix_tmp = "/home/linuxbrew/tmp"
    ENV["HOMEBREW_TEMP"] = homebrew_prefix_tmp
    FileUtils.mkdir_p homebrew_prefix_tmp
    system "chmod", "-R", "g-w,o-w", HOMEBREW_PREFIX, homebrew_prefix_tmp
  end

  ENV["HOMEBREW_TESTS"] = "1"
  ENV.delete("HOMEBREW_ASK")
  ENV["HOMEBREW_NO_AUTO_UPDATE"] = "1"
  ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
  ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if args.generic?
  ENV["HOMEBREW_TEST_ONLINE"] = "1" if args.online?
  # Keep in sync with `Library/Homebrew/brew.sh`.
  if ENV["HOMEBREW_TESTS_NO_SORBET_RUNTIME"]
    ENV.delete("HOMEBREW_SORBET_RUNTIME")
    ENV.delete("HOMEBREW_SORBET_RECURSIVE")
  else
    ENV["HOMEBREW_SORBET_RUNTIME"] = "1"
    ENV["HOMEBREW_SORBET_RECURSIVE"] = "1"
  end

  ENV["USER"] ||= system_command!("id", args: ["-nu"]).stdout.chomp

  # Avoid local configuration messing with tests, e.g. git being configured
  # to use GPG to sign by default
  ENV["HOME"] = "#{HOMEBREW_LIBRARY_PATH}/test"
  # Keep generic tool caches (e.g. RuboCop) out of the sandboxed test home.
  ENV["XDG_CACHE_HOME"] = "#{HOMEBREW_CACHE}/tests"
  # Sandbox the config home too, so the spec teardown can't delete the real `trust.json`.
  ENV["HOMEBREW_USER_CONFIG_HOME"] = "#{Dir.home}/.homebrew"

  # Print verbose output when requesting debug or verbose output.
  ENV["HOMEBREW_VERBOSE_TESTS"] = "1" if args.debug? || args.verbose?

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

  # 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
end