Class: Specbandit::CliAdapter

Inherits:
Object
  • Object
show all
Includes:
Adapter
Defined in:
lib/specbandit/cli_adapter.rb

Overview

CLI adapter: spawns a shell command for each batch.

Works with any test runner. The command string is split on whitespace, and file paths are appended as arguments:

<executable> [...command_args] [...command_opts] [...file_paths]

Example: command=“bundle exec rspec”, command_opts=[“–format”, “documentation”]

-> system("bundle", "exec", "rspec", "--format", "documentation", "file1.rb", "file2.rb")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, command_opts: [], verbose: false, output: $stdout) ⇒ CliAdapter

Returns a new instance of CliAdapter.



20
21
22
23
24
25
# File 'lib/specbandit/cli_adapter.rb', line 20

def initialize(command:, command_opts: [], verbose: false, output: $stdout)
  @command = command
  @command_opts = Array(command_opts)
  @verbose = verbose
  @output = output
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



18
19
20
# File 'lib/specbandit/cli_adapter.rb', line 18

def command
  @command
end

#command_optsObject (readonly)

Returns the value of attribute command_opts.



18
19
20
# File 'lib/specbandit/cli_adapter.rb', line 18

def command_opts
  @command_opts
end

#outputObject (readonly)

Returns the value of attribute output.



18
19
20
# File 'lib/specbandit/cli_adapter.rb', line 18

def output
  @output
end

#verboseObject (readonly)

Returns the value of attribute verbose.



18
19
20
# File 'lib/specbandit/cli_adapter.rb', line 18

def verbose
  @verbose
end

Instance Method Details

#run_batch(files, batch_num) ⇒ Object

Spawn the command with file paths appended as arguments. Returns a BatchResult with the exit code and timing.



32
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
# File 'lib/specbandit/cli_adapter.rb', line 32

def run_batch(files, batch_num)
  command_parts = command.split(/\s+/)
  args = command_parts + command_opts + files

  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  if verbose
    # Inherit stdio so user sees output in real-time
    system(*args)
    exit_code = $?.exitstatus || 1
  else
    stdout, stderr, status = Open3.capture3(*args)
    exit_code = status.exitstatus || 1

    # Print stderr on failure
    output.puts stderr if exit_code != 0 && stderr && !stderr.strip.empty?

    # Print stdout if any
    output.print(stdout) if stdout && !stdout.empty?
  end

  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time

  BatchResult.new(
    batch_num: batch_num,
    files: files,
    exit_code: exit_code,
    duration: duration
  )
end

#setupObject

No-op for CLI adapter.



28
# File 'lib/specbandit/cli_adapter.rb', line 28

def setup; end

#teardownObject

No-op for CLI adapter.



64
# File 'lib/specbandit/cli_adapter.rb', line 64

def teardown; end