betamax 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5b5fbb5686ea27e8f9fd4f21eb6a086c4949a7461f848e385f2fb64a23fd333
4
- data.tar.gz: 24c0341506d19c85757c88d408343390d76810c92c13717eda21735e2de32f18
3
+ metadata.gz: b13224e3fa5f820802a68dc000dbabab4a023eff18250e3b0af5e9fb36f98092
4
+ data.tar.gz: 70fb0889db8f8b4df8b7b60527895f27d806bda46164990670cec559e5e0b29c
5
5
  SHA512:
6
- metadata.gz: df2ce112b5ca2b706212e9963208757726e00ba7df005306feae2d58bd8a40347927a7f6e3cdd71df2b717b537fd9f2df855af836dfdba28af5d4f8723dd475b
7
- data.tar.gz: bf29734cdb839478aa0e555292cd850c91baae0adb7d628e620bbab011ad1379e7143d47b837f858845c32430c3ce8313959b462d1151afb99739fcde412e15e
6
+ metadata.gz: '0698af118ee8b4a7900e8fa490548b9967e4c91c5fa1cb7d7543b5f55aaf71ab1993a8b6efdfa80467c830e86f24227be5f42e3b0509bb628657914c49f01003'
7
+ data.tar.gz: ea48915b0269d20c9810d7a65a8519c0b9a6b84cdab6dd918822ba92da084fbd427b0d2145ce7f27757a5bacf39d9476a7646e0fcf2fbbbcbff2a26d64db51e6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## 0.2.0
4
+
5
+ - Add Minitest support via `Betamax::Minitest#with_betamax`.
6
+ - Clear the active player when a test raises, so a later test cannot record onto a stale player.
7
+
3
8
  ## 0.1.1 - 2026-06-15
4
9
 
5
10
  - Add UNLICENSE
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Betamax
2
2
 
3
- Betamax allows for the recording and playback of arbitrary Ruby objects to simplify testing external dependencies.
3
+ Betamax allows for the recording and playback of arbitrary Ruby objects to simplify testing external dependencies. Think of it like the [VCR](https://github.com/vcr/vcr) gem, but for any complicated object, not just HTTP interactions. This gem was originally developed to write a test suite for a MiniDisc NetMD Walkman library, where the recording and playback of real USB traffic was desired.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,11 +18,59 @@ gem install betamax
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Currently this gem supports testing with RSpec. First, require the gem in your spec helper:
22
+
23
+ ```ruby
24
+ # File: spec/spec_helper.rb
25
+
26
+ require 'betamax'
27
+ ```
28
+
29
+ Then, record an object. The first time you run your test, a recording will be created in `spec/betamax_tapes/`. Subsequent test runs will playback from your recording. Make sure to tag your example as `:betamax` to enable recording and playback.
30
+
31
+ ```ruby
32
+ RSpec.describe 'a MiniDisc device' do
33
+ let(:walkman) do
34
+ usb_device = LibUSB::Context.devices(idProduct: 0x0084).first
35
+
36
+ MiniDisc::Device.new usb_device: Betamax.record(usb_device)
37
+ end
38
+
39
+ it 'has a tracklist', :betamax do
40
+ walkman.open do |disc|
41
+ disc.tracks.first.title.should == 'Song of the Highwire Shrimper'
42
+ end
43
+ end
44
+ end
45
+
46
+ ```
47
+
48
+ In the above example, the test will continue to work even if the USB device is no longer present. If for some reason changes to your code change the interaction with your recorded object, your test will fail. This could be because methods were called in a different order, block arguments changed, method calls were expected but did not take place, etc.
49
+
50
+ ### Minitest
51
+
52
+ Include `Betamax::Minitest` in your test class and surround the recorded interaction with `with_betamax`. Tapes are named after the test class and method and stored in `test/betamax_tapes/` (override with `tapes_folder:`).
53
+
54
+ ```ruby
55
+ class WalkmanTest < Minitest::Test
56
+ include Betamax::Minitest
57
+
58
+ def test_tracklist
59
+ with_betamax do
60
+ usb_device = LibUSB::Context.devices(idProduct: 0x0084).first
61
+ walkman = MiniDisc::Device.new usb_device: Betamax.record(usb_device)
62
+
63
+ walkman.open do |disc|
64
+ assert_equal 'Song of the Highwire Shrimper', disc.tracks.first.title
65
+ end
66
+ end
67
+ end
68
+ end
69
+ ```
22
70
 
23
71
  ## Development
24
72
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the RSpec and Minitest suites and RuboCop. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
74
 
27
75
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
76
 
data/Rakefile CHANGED
@@ -1,10 +1,15 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rspec/core/rake_task'
4
+ require 'rubocop/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new :spec
5
7
 
6
- require "rubocop/rake_task"
8
+ Rake::TestTask.new :test do |task|
9
+ task.libs << 'lib'
10
+ task.pattern = 'test/**/*_test.rb'
11
+ end
7
12
 
8
13
  RuboCop::RakeTask.new
9
14
 
10
- task default: %i[spec rubocop]
15
+ task default: %i[spec test rubocop]
@@ -3,7 +3,7 @@ module Betamax
3
3
  class Error < StandardError; end
4
4
 
5
5
  class NoTapeInserted < Error
6
- def initialize message = "No tape inserted"
6
+ def initialize message = 'No tape inserted'
7
7
  super
8
8
  end
9
9
  end
@@ -51,7 +51,7 @@ module Betamax
51
51
 
52
52
  class UnusedRecordings < PlaybackError
53
53
  def initialize unused_methods
54
- method_names = unused_methods.map(&:method_name).join(", ")
54
+ method_names = unused_methods.map(&:method_name).join(', ')
55
55
  super "Recording has #{unused_methods.size} unused method(s): #{method_names}"
56
56
  end
57
57
  end
@@ -0,0 +1,27 @@
1
+ module Betamax
2
+ module Minitest
3
+ DEFAULT_TAPES_FOLDER = Pathname.new 'test/betamax_tapes/'
4
+
5
+ # Adapts a Minitest test and block to the example interface Player expects.
6
+ class Example
7
+ attr_reader :metadata
8
+
9
+ def initialize test, &block
10
+ @metadata = { description: test.name, example_group: { description: test.class.name } }
11
+ @block = block
12
+ end
13
+
14
+ def run
15
+ @block.call
16
+ end
17
+
18
+ def exception
19
+ nil
20
+ end
21
+ end
22
+
23
+ def with_betamax(tapes_folder: DEFAULT_TAPES_FOLDER, &)
24
+ Player.new(Example.new(self, &), tapes_folder: Pathname.new(tapes_folder)).play
25
+ end
26
+ end
27
+ end
@@ -13,6 +13,8 @@ module Betamax
13
13
  insert_tape
14
14
  @example.run
15
15
  eject_tape unless @example.exception
16
+ ensure
17
+ Fiber[:betamax_player] = nil
16
18
  end
17
19
 
18
20
  def record object
data/lib/betamax/rspec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Betamax
2
2
  module RSpec
3
- DEFAULT_TAPES_FOLDER = Pathname.new "spec/betamax_tapes/"
3
+ DEFAULT_TAPES_FOLDER = Pathname.new 'spec/betamax_tapes/'
4
4
 
5
5
  def play_rspec example, tapes_folder: DEFAULT_TAPES_FOLDER
6
6
  Player.new(example, tapes_folder:).play
data/lib/betamax/tape.rb CHANGED
@@ -1,5 +1,5 @@
1
- require "fileutils"
2
- require "yaml"
1
+ require 'fileutils'
2
+ require 'yaml'
3
3
 
4
4
  module Betamax
5
5
  class Tape
@@ -20,11 +20,11 @@ module Betamax
20
20
 
21
21
  def self.from_rspec_example example, tapes_folder:
22
22
  name = cassette_name_for example.metadata
23
- name.gsub! "::", "_"
24
- name.gsub!(/[\s-]+/, "_")
25
- name.gsub! %r{[^a-zA-Z0-9_/]}, ""
23
+ name.gsub! '::', '_'
24
+ name.gsub!(/[\s-]+/, '_')
25
+ name.gsub! %r{[^a-zA-Z0-9_/]}, ''
26
26
 
27
- filename = Pathname.new(name).sub_ext ".yaml"
27
+ filename = Pathname.new(name).sub_ext '.yaml'
28
28
  full_path = tapes_folder / filename
29
29
 
30
30
  new full_path
@@ -44,7 +44,7 @@ module Betamax
44
44
  current = current[:parent_example_group]
45
45
  end
46
46
 
47
- description_parts.join "/"
47
+ description_parts.join '/'
48
48
  end
49
49
 
50
50
  def load
@@ -66,7 +66,7 @@ module Betamax
66
66
  version: Recording::VERSION,
67
67
  objects: { default: proxy }
68
68
 
69
- File.open @path, "w" do |file|
69
+ File.open @path, 'w' do |file|
70
70
  file.puts recording.to_yaml
71
71
  end
72
72
  end
@@ -1,3 +1,3 @@
1
1
  module Betamax
2
- VERSION = "0.1.1".freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/betamax.rb CHANGED
@@ -1,16 +1,17 @@
1
- require "pathname"
2
-
3
- require_relative "betamax/errors"
4
- require_relative "betamax/method_player"
5
- require_relative "betamax/method_recorder"
6
- require_relative "betamax/player"
7
- require_relative "betamax/recorded_method"
8
- require_relative "betamax/recorded_object"
9
- require_relative "betamax/recorded_yielding"
10
- require_relative "betamax/recording"
11
- require_relative "betamax/rspec"
12
- require_relative "betamax/tape"
13
- require_relative "betamax/version"
1
+ require 'pathname'
2
+
3
+ require_relative 'betamax/errors'
4
+ require_relative 'betamax/method_player'
5
+ require_relative 'betamax/method_recorder'
6
+ require_relative 'betamax/minitest'
7
+ require_relative 'betamax/player'
8
+ require_relative 'betamax/recorded_method'
9
+ require_relative 'betamax/recorded_object'
10
+ require_relative 'betamax/recorded_yielding'
11
+ require_relative 'betamax/recording'
12
+ require_relative 'betamax/rspec'
13
+ require_relative 'betamax/tape'
14
+ require_relative 'betamax/version'
14
15
 
15
16
  module Betamax
16
17
  extend RSpec
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: betamax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John DeSilva
@@ -25,6 +25,7 @@ files:
25
25
  - lib/betamax/errors.rb
26
26
  - lib/betamax/method_player.rb
27
27
  - lib/betamax/method_recorder.rb
28
+ - lib/betamax/minitest.rb
28
29
  - lib/betamax/player.rb
29
30
  - lib/betamax/recorded_method.rb
30
31
  - lib/betamax/recorded_object.rb
@@ -48,14 +49,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
49
  requirements:
49
50
  - - ">="
50
51
  - !ruby/object:Gem::Version
51
- version: 3.2.0
52
+ version: '3.3'
52
53
  required_rubygems_version: !ruby/object:Gem::Requirement
53
54
  requirements:
54
55
  - - ">="
55
56
  - !ruby/object:Gem::Version
56
57
  version: '0'
57
58
  requirements: []
58
- rubygems_version: 4.0.10
59
+ rubygems_version: 4.0.16
59
60
  specification_version: 4
60
61
  summary: Record and playback of arbitrary Ruby objects
61
62
  test_files: []