Class: Homebrew::DevCmd::Test

Inherits:
AbstractCommand show all
Defined in:
dev-cmd/test.rb

Instance Attribute Summary

Attributes inherited from AbstractCommand

#args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, #initialize, parser

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#runvoid

This method returns an undefined value.



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
# File 'dev-cmd/test.rb', line 33

def run
  Homebrew.install_bundler_gems!(groups: ["formula_test"], setup_path: false)

  require "formula_assertions"
  require "formula_free_port"

  args.named.to_resolved_formulae.each do |f|
    # Cannot test uninstalled formulae
    unless f.latest_version_installed?
      ofail "Testing requires the latest version of #{f.full_name}"
      next
    end

    # Cannot test formulae without a test method
    unless f.test_defined?
      ofail "#{f.full_name} defines no test"
      next
    end

    # Don't test unlinked formulae
    if !args.force? && !f.keg_only? && !f.linked?
      ofail "#{f.full_name} is not linked"
      next
    end

    # Don't test formulae missing test dependencies
    missing_test_deps = f.recursive_dependencies do |dependent, dependency|
      Dependency.prune if dependency.installed?
      next if dependency.test? && dependent == f

      Dependency.prune unless dependency.required?
    end.map(&:to_s)
    unless missing_test_deps.empty?
      ofail "#{f.full_name} is missing test dependencies: #{missing_test_deps.join(" ")}"
      next
    end

    oh1 "Testing #{f.full_name}"

    env = ENV.to_hash

    begin
      exec_args = HOMEBREW_RUBY_EXEC_ARGS + %W[
        --
        #{HOMEBREW_LIBRARY_PATH}/test.rb
        #{f.path}
      ].concat(args.options_only)

      exec_args << "--HEAD" if f.head?

      Utils.safe_fork do
        if Sandbox.available?
          sandbox = Sandbox.new
          f.logs.mkpath
          sandbox.record_log(f.logs/"test.sandbox.log")
          sandbox.allow_write_temp_and_cache
          sandbox.allow_write_log(f)
          sandbox.allow_write_xcode
          sandbox.allow_write_path(HOMEBREW_PREFIX/"var/cache")
          sandbox.allow_write_path(HOMEBREW_PREFIX/"var/homebrew/locks")
          sandbox.allow_write_path(HOMEBREW_PREFIX/"var/log")
          sandbox.allow_write_path(HOMEBREW_PREFIX/"var/run")
          sandbox.exec(*exec_args)
        else
          exec(*exec_args)
        end
      end
    rescue Exception => e # rubocop:disable Lint/RescueException
      retry if retry_test?(f)
      ofail "#{f.full_name}: failed"
      $stderr.puts e, Utils::Backtrace.clean(e)
    ensure
      ENV.replace(env)
    end
  end
end