Class: MutationTester::TestCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/mutation_tester/test_command.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

POLL_INTERVAL =
0.05

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_file, use_bundle_exec:, framework: nil, runner: :spawn, example_filters: [], worker_env_var: nil) ⇒ TestCommand

Returns a new instance of TestCommand.



14
15
16
17
18
19
20
21
# File 'lib/mutation_tester/test_command.rb', line 14

def initialize(spec_file, use_bundle_exec:, framework: nil, runner: :spawn, example_filters: [], worker_env_var: nil)
  @spec_file = spec_file
  @framework = framework || self.class.detect_framework(spec_file)
  @use_bundle_exec = use_bundle_exec
  @runner = runner
  @example_filters = @framework == :rspec ? Array(example_filters) : []
  @worker_env_var = worker_env_var
end

Instance Attribute Details

#example_filtersObject (readonly)

Returns the value of attribute example_filters.



12
13
14
# File 'lib/mutation_tester/test_command.rb', line 12

def example_filters
  @example_filters
end

#frameworkObject (readonly)

Returns the value of attribute framework.



12
13
14
# File 'lib/mutation_tester/test_command.rb', line 12

def framework
  @framework
end

#spec_fileObject (readonly)

Returns the value of attribute spec_file.



12
13
14
# File 'lib/mutation_tester/test_command.rb', line 12

def spec_file
  @spec_file
end

#use_bundle_execObject (readonly)

Returns the value of attribute use_bundle_exec.



12
13
14
# File 'lib/mutation_tester/test_command.rb', line 12

def use_bundle_exec
  @use_bundle_exec
end

Class Method Details

.detect_framework(spec_file) ⇒ Object



62
63
64
# File 'lib/mutation_tester/test_command.rb', line 62

def self.detect_framework(spec_file)
  FrameworkDetector.detect(spec_file)
end

.use_bundle_exec?(path) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mutation_tester/test_command.rb', line 66

def self.use_bundle_exec?(path)
  dir = File.dirname(File.expand_path(path))

  loop do
    return true if File.exist?(File.join(dir, 'Gemfile'))

    parent = File.dirname(dir)
    break if parent == dir

    dir = parent
  end

  false
end

Instance Method Details

#argvObject



23
24
25
26
# File 'lib/mutation_tester/test_command.rb', line 23

def argv
  parts = [runner, spec_file, *filter_args]
  @use_bundle_exec ? ['bundle', 'exec', *parts] : parts
end

#commandObject



28
29
30
31
# File 'lib/mutation_tester/test_command.rb', line 28

def command
  cmd = ([runner, spec_file] + example_filters.flat_map { |f| ['-e', "'#{f}'"] }).join(' ')
  @use_bundle_exec ? "bundle exec #{cmd}" : cmd
end

#fork_execution?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/mutation_tester/test_command.rb', line 55

def fork_execution?
  return false if @framework == :minitest
  return false if @runner == :spawn

  ForkRunner.available?
end

#run(timeout: nil, chdir: nil, capture: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mutation_tester/test_command.rb', line 40

def run(timeout: nil, chdir: nil, capture: false)
  if fork_execution?
    fork_runner = ForkRunner.acquire(use_bundle_exec: @use_bundle_exec)
    return fork_runner.execute(spec_file, timeout: timeout, chdir: chdir || Dir.pwd, capture: capture, args: filter_args) if fork_runner
  end

  return run_captured(timeout: timeout, chdir: chdir) if capture

  spawn_options = { pgroup: true, %i[out err] => File::NULL }
  spawn_options[:chdir] = chdir if chdir

  pid = Process.spawn(*spawn_argv, spawn_options)
  wait_with_deadline(pid, timeout)
end

#shell_command(timeout: nil, quiet: false) ⇒ Object



33
34
35
36
37
38
# File 'lib/mutation_tester/test_command.rb', line 33

def shell_command(timeout: nil, quiet: false)
  cmd = command
  cmd = "timeout #{timeout}s #{cmd}" if timeout
  cmd = "#{cmd} > /dev/null 2>&1" if quiet
  cmd
end