Class: Backspin::Recorder

Inherits:
Object
  • Object
show all
Includes:
RSpec::Mocks::ExampleMethods
Defined in:
lib/backspin/recorder.rb

Overview

Handles stubbing and recording of command executions

Constant Summary collapse

SUPPORTED_COMMAND_TYPES =
i[capture3 system].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode: :record, record: nil, matcher: nil, filter: nil) ⇒ Recorder

Returns a new instance of Recorder.



18
19
20
21
22
23
24
25
26
# File 'lib/backspin/recorder.rb', line 18

def initialize(mode: :record, record: nil, matcher: nil, filter: nil)
  @mode = mode
  @record = record
  @matcher = matcher
  @filter = filter
  @commands = []
  @playback_index = 0
  @command_diffs = []
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



16
17
18
# File 'lib/backspin/recorder.rb', line 16

def commands
  @commands
end

#filterObject (readonly)

Returns the value of attribute filter.



16
17
18
# File 'lib/backspin/recorder.rb', line 16

def filter
  @filter
end

#matcherObject (readonly)

Returns the value of attribute matcher.



16
17
18
# File 'lib/backspin/recorder.rb', line 16

def matcher
  @matcher
end

#modeObject (readonly)

Returns the value of attribute mode.



16
17
18
# File 'lib/backspin/recorder.rb', line 16

def mode
  @mode
end

#recordObject (readonly)

Returns the value of attribute record.



16
17
18
# File 'lib/backspin/recorder.rb', line 16

def record
  @record
end

Instance Method Details

#perform_capture_playbackObject

Performs capture playback - executes block normally but could optionally suppress output



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/backspin/recorder.rb', line 267

def perform_capture_playback
  raise RecordNotFoundError, "Record not found: #{record.path}" unless record.exists?
  raise RecordNotFoundError, "No commands found in record" if record.empty?

  # For now, just execute the block normally
  # In the future, we could optionally suppress output or return recorded output
  output = yield

  RecordResult.new(
    output: output,
    mode: :playback,
    verified: true,
    record: record
  )
end

#perform_capture_recordingObject

Performs capture recording by intercepting all stdout/stderr output



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/backspin/recorder.rb', line 141

def perform_capture_recording
  require "tempfile"

  # Create temporary files for capturing output
  stdout_tempfile = Tempfile.new("backspin_stdout")
  stderr_tempfile = Tempfile.new("backspin_stderr")

  begin
    # Save original file descriptors
    original_stdout_fd = $stdout.dup
    original_stderr_fd = $stderr.dup

    # Redirect both Ruby IO and file descriptors
    $stdout.reopen(stdout_tempfile)
    $stderr.reopen(stderr_tempfile)

    result = yield

    # Flush and read captured output
    $stdout.flush
    $stderr.flush
    stdout_tempfile.rewind
    stderr_tempfile.rewind

    captured_stdout = stdout_tempfile.read
    captured_stderr = stderr_tempfile.read

    # Create a single command representing all captured output
    command = Command.new(
      method_class: Backspin::Capturer,
      args: [],
      stdout: captured_stdout,
      stderr: captured_stderr,
      status: 0,
      recorded_at: Time.now.iso8601
    )

    record.add_command(command)
    record.save(filter: @filter)

    RecordResult.new(output: result, mode: :record, record: record)
  ensure
    # Restore original file descriptors
    $stdout.reopen(original_stdout_fd)
    $stderr.reopen(original_stderr_fd)
    original_stdout_fd.close
    original_stderr_fd.close

    # Clean up temp files
    stdout_tempfile.close!
    stderr_tempfile.close!
  end
end

#perform_capture_verificationObject

Performs capture verification by capturing output and comparing with recorded values



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/backspin/recorder.rb', line 196

def perform_capture_verification
  raise RecordNotFoundError, "Record not found: #{record.path}" unless record.exists?
  raise RecordNotFoundError, "No commands found in record #{record.path}" if record.empty?

  require "tempfile"

  # Create temporary files for capturing output
  stdout_tempfile = Tempfile.new("backspin_stdout")
  stderr_tempfile = Tempfile.new("backspin_stderr")

  begin
    # Save original file descriptors
    original_stdout_fd = $stdout.dup
    original_stderr_fd = $stderr.dup

    # Redirect both Ruby IO and file descriptors
    $stdout.reopen(stdout_tempfile)
    $stderr.reopen(stderr_tempfile)

    # Execute the block
    output = yield

    # Flush and read captured output
    $stdout.flush
    $stderr.flush
    stdout_tempfile.rewind
    stderr_tempfile.rewind

    captured_stdout = stdout_tempfile.read
    captured_stderr = stderr_tempfile.read

    # Get the recorded command (should be only one for capture)
    recorded_command = record.commands.first

    # Create actual command from captured output
    actual_command = Command.new(
      method_class: Backspin::Capturer,
      args: ["<captured block>"],
      stdout: captured_stdout,
      stderr: captured_stderr,
      status: 0
    )

    # Create CommandDiff for comparison
    command_diff = CommandDiff.new(
      recorded_command: recorded_command,
      actual_command: actual_command,
      matcher: @matcher
    )

    RecordResult.new(
      output: output,
      mode: :verify,
      verified: command_diff.verified?,
      record: record,
      command_diffs: [command_diff]
    )
  ensure
    # Restore original file descriptors
    $stdout.reopen(original_stdout_fd)
    $stderr.reopen(original_stderr_fd)
    original_stdout_fd.close
    original_stderr_fd.close

    # Clean up temp files
    stdout_tempfile.close!
    stderr_tempfile.close!
  end
end

#perform_playbackObject

Performs playback by returning recorded values without executing actual commands



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/backspin/recorder.rb', line 121

def perform_playback
  raise RecordNotFoundError, "Record not found: #{record.path}" unless record.exists?
  raise RecordNotFoundError, "No commands found in record" if record.empty?

  # Setup replay stubs
  setup_capture3_replay_stub
  setup_system_replay_stub

  # Execute block (all commands will be stubbed with recorded values)
  output = yield

  RecordResult.new(
    output: output,
    mode: :playback,
    verified: true, # Always true for playback
    record: record
  )
end

#perform_recordingObject

Records registered commands, adds them to the record, saves the record, and returns the overall RecordResult



48
49
50
51
52
# File 'lib/backspin/recorder.rb', line 48

def perform_recording
  result = yield
  record.save(filter: @filter)
  RecordResult.new(output: result, mode: :record, record: record)
end

#perform_verificationObject

Performs verification by executing commands and comparing with recorded values



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/backspin/recorder.rb', line 55

def perform_verification
  raise RecordNotFoundError, "Record not found: #{record.path}" unless record.exists?
  raise RecordNotFoundError, "No commands found in record #{record.path}" if record.empty?

  # Initialize tracking variables
  @command_diffs = []
  @command_index = 0

  # Setup verification stubs for capture3
  allow(Open3).to receive(:capture3).and_wrap_original do |original_method, *args|
    recorded_command = record.commands[@command_index]

    if recorded_command.nil?
      raise RecordNotFoundError, "No more recorded commands, but tried to execute: #{args.inspect}"
    end

    stdout, stderr, status = original_method.call(*args)

    actual_command = Command.new(
      method_class: Open3::Capture3,
      args: args,
      stdout: stdout,
      stderr: stderr,
      status: status.exitstatus
    )

    @command_diffs << CommandDiff.new(recorded_command: recorded_command, actual_command: actual_command, matcher: @matcher)
    @command_index += 1
    [stdout, stderr, status]
  end

  # Setup verification stubs for system
  allow_any_instance_of(Object).to receive(:system).and_wrap_original do |original_method, receiver, *args|
    recorded_command = record.commands[@command_index]

    result = original_method.call(receiver, *args)

    actual_command = Command.new(
      method_class: ::Kernel::System,
      args: args,
      stdout: "",
      stderr: "",
      status: result ? 0 : 1
    )

    # Create CommandDiff to track the comparison
    @command_diffs << CommandDiff.new(recorded_command: recorded_command, actual_command: actual_command, matcher: @matcher)

    @command_index += 1
    result
  end

  output = yield

  all_verified = @command_diffs.all?(&:verified?)

  RecordResult.new(
    output: output,
    mode: :verify,
    verified: all_verified,
    record: record,
    command_diffs: @command_diffs
  )
end

#record_call(command_type) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/backspin/recorder.rb', line 35

def record_call(command_type)
  case command_type
  when :system
    setup_system_call_stub
  when :capture3
    setup_capture3_call_stub
  else
    raise ArgumentError,
      "Unsupported command type: #{command_type} - currently supported types: #{SUPPORTED_COMMAND_TYPES.join(", ")}"
  end
end

#setup_recording_stubs(*command_types) ⇒ Object



28
29
30
31
32
33
# File 'lib/backspin/recorder.rb', line 28

def setup_recording_stubs(*command_types)
  command_types = SUPPORTED_COMMAND_TYPES if command_types.empty?
  command_types.each do |command_type|
    record_call(command_type)
  end
end