Class: Testprune::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/testprune/runner.rb

Overview

Boots the target project’s suite in a subprocess, instrumented so the adapters capture per-test coverage. Reuses the gem’s lib via RUBYOPT (-I) so the target project does not need testprune in its Gemfile.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Runner

Returns a new instance of Runner.



12
13
14
# File 'lib/testprune/runner.rb', line 12

def initialize(config)
  @config = config
end

Instance Method Details

#call(explicit_command = nil) ⇒ Object

explicit_command: array form of a user-supplied test command (after ‘–`), or nil to autodetect.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/testprune/runner.rb', line 18

def call(explicit_command = nil)
  framework, command = resolve(explicit_command)

  FileUtils.mkdir_p(@config.output_dir)
  File.delete(@config.run_file) if File.exist?(@config.run_file)

  warn("testprune: framework=#{framework}  running: #{command.join(' ')}")
  ok = system(env, *command, chdir: @config.root)

  unless File.exist?(@config.run_file)
    raise Error, 'suite finished but no run.json was captured — the adapter may ' \
                 'not have loaded. Check the framework/command and source paths.'
  end

  count = begin
    JSON.parse(File.read(@config.run_file)).fetch('tests', []).size
  rescue JSON::ParserError
    '(unreadable — suite may have been interrupted mid-write)'
  end
  warn("testprune: captured #{count} test(s) -> #{@config.run_file}")
  warn('testprune: suite exited non-zero; coverage was still captured.') unless ok
  ok
end

#command_for_paths(paths) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/testprune/runner.rb', line 42

def command_for_paths(paths)
  bundler = File.exist?(File.join(@config.root, 'Gemfile'))
  prefix  = bundler ? %w[bundle exec] : []
  if File.directory?(File.join(@config.root, 'spec'))
    prefix + %w[rspec] + paths
  elsif File.exist?(File.join(@config.root, 'bin', 'rails'))
    prefix + %w[rails test] + paths
  else
    prefix + %w[rake test] + paths
  end
end