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
MINITEST_FAIL_FAST_PATH =
File.expand_path('minitest_fail_fast.rb', __dir__).freeze

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, stop_on_first_failure: false) ⇒ TestCommand

Returns a new instance of TestCommand.



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

def initialize(spec_file, use_bundle_exec:, framework: nil, runner: :spawn, example_filters: [], worker_env_var: nil,
               stop_on_first_failure: false)
  @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
  @stop_on_first_failure = stop_on_first_failure
end

Instance Attribute Details

#example_filtersObject (readonly)

Returns the value of attribute example_filters.



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

def example_filters
  @example_filters
end

#frameworkObject (readonly)

Returns the value of attribute framework.



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

def framework
  @framework
end

#spec_fileObject (readonly)

Returns the value of attribute spec_file.



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

def spec_file
  @spec_file
end

#use_bundle_execObject (readonly)

Returns the value of attribute use_bundle_exec.



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

def use_bundle_exec
  @use_bundle_exec
end

Class Method Details

.detect_framework(spec_file) ⇒ Object



73
74
75
# File 'lib/mutation_tester/test_command.rb', line 73

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

.use_bundle_exec?(path) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mutation_tester/test_command.rb', line 77

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



26
27
28
29
# File 'lib/mutation_tester/test_command.rb', line 26

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

#commandObject



31
32
33
34
# File 'lib/mutation_tester/test_command.rb', line 31

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)


67
68
69
70
71
# File 'lib/mutation_tester/test_command.rb', line 67

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

  ForkRunner.available?
end

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mutation_tester/test_command.rb', line 43

def run(timeout: nil, chdir: nil, capture: false)
  if fork_execution?
    fork_runner = ForkRunner.acquire(use_bundle_exec: @use_bundle_exec, framework: @framework)
    if fork_runner
      return fork_runner.execute(
        spec_file,
        timeout: timeout,
        chdir: chdir || Dir.pwd,
        capture: capture,
        args: filter_args,
        stop_on_first_failure: @stop_on_first_failure
      )
    end
  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



36
37
38
39
40
41
# File 'lib/mutation_tester/test_command.rb', line 36

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