Module: Pod::UnitExecutable

Defined in:
lib/cocoapods-unit-test/executable.rb

Defined Under Namespace

Classes: Indenter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.capture_command(executable, command, capture: :merge, env: {}, **kwargs) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cocoapods-unit-test/executable.rb', line 66

def self.capture_command(executable, command, capture: :merge, env: {}, **kwargs)
  bin = which!(executable)
  command = command.map(&:to_s)
  case capture
  when :merge then Open3.capture2e(env, [bin, bin], *command, **kwargs)
  when :both then Open3.capture3(env, [bin, bin], *command, **kwargs)
  when :out then Open3.capture3(env, [bin, bin], *command, **kwargs).values_at(0, -1)
  when :err then Open3.capture3(env, [bin, bin], *command, **kwargs).drop(1)
  when :none then Open3.capture3(env, [bin, bin], *command, **kwargs).last
  end
end

.capture_command!(executable, command, **kwargs) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/cocoapods-unit-test/executable.rb', line 78

def self.capture_command!(executable, command, **kwargs)
  capture_command(executable, command, **kwargs).tap do |result|
    result = Array(result)
    status = result.last
    unless status.success?
      output = result[0..-2].join
      raise Informative, "#{bin} #{command.join(' ')}\n\n#{output}".strip
    end
  end
end

.execute_command(executable, command, raise_on_failure = true) ⇒ Object



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
# File 'lib/cocoapods-unit-test/executable.rb', line 14

def self.execute_command(executable, command, raise_on_failure = true)
  bin = which!(executable)

  command = command.map(&:to_s)
  if File.basename(bin) == 'tar.exe'
    # Tar on Windows needs --force-local
    command.push('--force-local')
  end
  full_command = "#{bin} #{command.join(' ')}"

  stdout = Indenter.new(STDOUT)
  stderr = Indenter.new(STDERR)

  status = popen3(bin, command, stdout, stderr)
  stdout = stdout.join
  stderr = stderr.join
  output = stdout + stderr
  unless status.success?
    UI.puts("$ #{full_command}")
    if raise_on_failure
      raise Informative, "#{full_command}\n\n#{output}"
    else
      UI.puts("[!] Failed: #{full_command}".red)
    end
  end

  output
end

.which(program) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cocoapods-unit-test/executable.rb', line 43

def self.which(program)
  program = program.to_s
  paths = ENV.fetch('PATH') { '' }.split(File::PATH_SEPARATOR)
  paths.unshift('./')
  paths.uniq!
  paths.each do |path|
    bin = File.expand_path(program, path)
    if Gem.win_platform?
      bin += '.exe'
    end
    if File.file?(bin) && File.executable?(bin)
      return bin
    end
  end
  nil
end

.which!(program) ⇒ Object



60
61
62
63
64
# File 'lib/cocoapods-unit-test/executable.rb', line 60

def self.which!(program)
  which(program).tap do |bin|
    raise Informative, "Unable to locate the executable `#{program}`" unless bin
  end
end

Instance Method Details

#executable(name) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/cocoapods-unit-test/executable.rb', line 5

def executable(name)
  unless respond_to?("#{name.to_s}!")
    define_method(name.to_s + '!') do |*command|
      UnitExecutable.execute_command(name, Array(command).flatten, true)
    end
  end
  
end