ruby_raider 4.0.1 → 4.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.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/e2e_tests.yml +3 -0
  3. data/README.md +58 -1
  4. data/RELEASE.md +25 -0
  5. data/lib/commands/schedule_commands.rb +84 -0
  6. data/lib/generators/templates/common/git_ignore.tt +1 -0
  7. data/lib/ruby_raider/runtime/retry_reset.rb +32 -0
  8. data/lib/ruby_raider/runtime/rspec.rb +1 -0
  9. data/lib/ruby_raider/runtime/show_browser.rb +53 -0
  10. data/lib/ruby_raider/runtime.rb +2 -0
  11. data/lib/ruby_raider.rb +31 -0
  12. data/lib/utilities/schedules/adapter.rb +22 -0
  13. data/lib/utilities/schedules/base.rb +27 -0
  14. data/lib/utilities/schedules/kind.rb +74 -0
  15. data/lib/utilities/schedules/legacy.rb +196 -0
  16. data/lib/utilities/schedules/legacy_stub.rb +62 -0
  17. data/lib/utilities/schedules/next_run.rb +61 -0
  18. data/lib/utilities/schedules/notifier.rb +76 -0
  19. data/lib/utilities/schedules/os/command_runner.rb +73 -0
  20. data/lib/utilities/schedules/os/cron.rb +147 -0
  21. data/lib/utilities/schedules/os/launchd.rb +182 -0
  22. data/lib/utilities/schedules/os/linux.rb +81 -0
  23. data/lib/utilities/schedules/os/systemd.rb +194 -0
  24. data/lib/utilities/schedules/os/task_scheduler.rb +195 -0
  25. data/lib/utilities/schedules/os_enumerator.rb +30 -0
  26. data/lib/utilities/schedules/project.rb +70 -0
  27. data/lib/utilities/schedules/rule.rb +162 -0
  28. data/lib/utilities/schedules/run_lock.rb +160 -0
  29. data/lib/utilities/schedules/run_log.rb +122 -0
  30. data/lib/utilities/schedules/run_report.rb +139 -0
  31. data/lib/utilities/schedules/run_wrapper.rb +216 -0
  32. data/lib/utilities/schedules/schedule.rb +89 -0
  33. data/lib/utilities/schedules/schedule_store.rb +53 -0
  34. data/lib/utilities/schedules/scheduler.rb +178 -0
  35. data/lib/utilities/schedules/scheduler_steps.rb +69 -0
  36. data/lib/utilities/schedules/selection.rb +133 -0
  37. data/lib/utilities/schedules/spawn.rb +68 -0
  38. data/lib/utilities/schedules/store.rb +139 -0
  39. data/lib/version +1 -1
  40. data/ruby_raider.gemspec +1 -0
  41. data/sig/commands/schedule_commands.rbs +12 -0
  42. data/sig/ruby_raider/runtime/retry_reset.rbs +7 -0
  43. data/sig/ruby_raider/runtime/show_browser.rbs +27 -0
  44. data/sig/ruby_raider.rbs +6 -0
  45. data/sig/utilities/schedules/adapter.rbs +5 -0
  46. data/sig/utilities/schedules/base.rbs +15 -0
  47. data/sig/utilities/schedules/kind.rbs +32 -0
  48. data/sig/utilities/schedules/legacy.rbs +45 -0
  49. data/sig/utilities/schedules/legacy_stub.rbs +15 -0
  50. data/sig/utilities/schedules/next_run.rbs +15 -0
  51. data/sig/utilities/schedules/notifier.rbs +29 -0
  52. data/sig/utilities/schedules/os/command_runner.rbs +27 -0
  53. data/sig/utilities/schedules/os/cron.rbs +34 -0
  54. data/sig/utilities/schedules/os/launchd.rbs +46 -0
  55. data/sig/utilities/schedules/os/linux.rbs +33 -0
  56. data/sig/utilities/schedules/os/systemd.rbs +44 -0
  57. data/sig/utilities/schedules/os/task_scheduler.rbs +56 -0
  58. data/sig/utilities/schedules/os_enumerator.rbs +12 -0
  59. data/sig/utilities/schedules/project.rbs +22 -0
  60. data/sig/utilities/schedules/rule.rbs +72 -0
  61. data/sig/utilities/schedules/run_lock.rbs +29 -0
  62. data/sig/utilities/schedules/run_log.rbs +33 -0
  63. data/sig/utilities/schedules/run_report.rbs +34 -0
  64. data/sig/utilities/schedules/run_wrapper.rbs +45 -0
  65. data/sig/utilities/schedules/schedule.rbs +27 -0
  66. data/sig/utilities/schedules/schedule_store.rbs +21 -0
  67. data/sig/utilities/schedules/scheduler.rbs +49 -0
  68. data/sig/utilities/schedules/scheduler_steps.rbs +22 -0
  69. data/sig/utilities/schedules/selection.rbs +36 -0
  70. data/sig/utilities/schedules/spawn.rbs +10 -0
  71. data/sig/utilities/schedules/store.rbs +39 -0
  72. data/spec/commands/schedule_commands_spec.rb +284 -0
  73. data/spec/integration/content/common_content_spec.rb +8 -2
  74. data/spec/integration/content/gemfile_content_spec.rb +2 -2
  75. data/spec/runtime/retry_reset_spec.rb +112 -0
  76. data/spec/runtime/show_browser_project_spec.rb +70 -0
  77. data/spec/runtime/show_browser_spec.rb +101 -0
  78. data/spec/utilities/schedules/kind_spec.rb +81 -0
  79. data/spec/utilities/schedules/legacy_spec.rb +433 -0
  80. data/spec/utilities/schedules/legacy_stub_spec.rb +144 -0
  81. data/spec/utilities/schedules/next_run_spec.rb +119 -0
  82. data/spec/utilities/schedules/notifier_spec.rb +120 -0
  83. data/spec/utilities/schedules/os/command_runner_spec.rb +55 -0
  84. data/spec/utilities/schedules/os/cron_spec.rb +292 -0
  85. data/spec/utilities/schedules/os/launchd_spec.rb +306 -0
  86. data/spec/utilities/schedules/os/linux_spec.rb +163 -0
  87. data/spec/utilities/schedules/os/list_ids_spec.rb +202 -0
  88. data/spec/utilities/schedules/os/systemd_spec.rb +333 -0
  89. data/spec/utilities/schedules/os/task_scheduler_spec.rb +511 -0
  90. data/spec/utilities/schedules/os_entry_spec.rb +54 -0
  91. data/spec/utilities/schedules/os_enumerator_spec.rb +57 -0
  92. data/spec/utilities/schedules/project_spec.rb +125 -0
  93. data/spec/utilities/schedules/run_lock_spec.rb +143 -0
  94. data/spec/utilities/schedules/run_log_spec.rb +83 -0
  95. data/spec/utilities/schedules/run_report_spec.rb +150 -0
  96. data/spec/utilities/schedules/run_wrapper_integration_spec.rb +166 -0
  97. data/spec/utilities/schedules/run_wrapper_spec.rb +430 -0
  98. data/spec/utilities/schedules/schedule_spec.rb +120 -0
  99. data/spec/utilities/schedules/schedule_store_spec.rb +151 -0
  100. data/spec/utilities/schedules/scheduler_launchd_spec.rb +77 -0
  101. data/spec/utilities/schedules/scheduler_spec.rb +445 -0
  102. data/spec/utilities/schedules/selection_spec.rb +176 -0
  103. data/spec/utilities/schedules/spawn_spec.rb +104 -0
  104. metadata +106 -2
@@ -0,0 +1,284 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+ require 'tmpdir'
5
+ require_relative '../../lib/ruby_raider'
6
+ require_relative '../../lib/commands/schedule_commands'
7
+
8
+ # Runs in this process through RubyRaider::Raider.start, so the `raider schedule` subcommand path is the real
9
+ # one (thor 1.2.2). Every example stubs Scheduler.new, RunWrapper.new and Legacy.new (or gives Legacy a
10
+ # recording adapter): nothing reaches the OS scheduler or spawns rspec.
11
+ RSpec.describe ScheduleCommands do
12
+ let(:scheduler) { instance_double(RubyRaider::Schedules::Scheduler, add: nil, list: nil, remove: nil) }
13
+ let(:wrapper) { instance_double(RubyRaider::Schedules::RunWrapper, run: 0) }
14
+ let(:legacy) { instance_double(RubyRaider::Schedules::Legacy, cleanup: nil) }
15
+ let(:dir) { File.realpath(Dir.mktmpdir('schedule-commands')) }
16
+
17
+ before do
18
+ allow(RubyRaider::Schedules::Scheduler).to receive(:new).and_return(scheduler)
19
+ allow(RubyRaider::Schedules::RunWrapper).to receive(:new).and_return(wrapper)
20
+ allow(RubyRaider::Schedules::Legacy).to receive(:new).and_return(legacy)
21
+ end
22
+
23
+ after { FileUtils.rm_rf(dir) }
24
+
25
+ # [stdout, stderr, exit code or nil when the command did not exit]
26
+ def raider(*args)
27
+ out = StringIO.new
28
+ err = StringIO.new
29
+ stdout = $stdout
30
+ stderr = $stderr
31
+ $stdout = out
32
+ $stderr = err
33
+ code = begin
34
+ RubyRaider::Raider.start(args)
35
+ nil
36
+ rescue SystemExit => e
37
+ e.status
38
+ ensure
39
+ $stdout = stdout
40
+ $stderr = stderr
41
+ end
42
+ [out.string, err.string, code]
43
+ end
44
+
45
+ def project_root
46
+ satisfy { |project| project.root == dir }
47
+ end
48
+
49
+ describe 'add' do
50
+ it 'takes the spec files and the options in any order' do
51
+ raider('schedule', 'add', 'nightly', '--every', 'day', 'spec/a_spec.rb', '--show-browser', 'spec/b_spec.rb',
52
+ '--project', dir)
53
+
54
+ expect(scheduler).to have_received(:add).with('nightly', every: 'day', files: %w[spec/a_spec.rb spec/b_spec.rb],
55
+ tag: nil, at: nil, on: nil, retries: 0, notify: false,
56
+ show_browser: true)
57
+ expect(RubyRaider::Schedules::Scheduler).to have_received(:new).with(project_root)
58
+ end
59
+
60
+ it 'passes --at, --on, --tag, --retries and --notify' do
61
+ raider('schedule', 'add', 'smoke', '--tag', 'smoke', '--every', 'week', '--on', 'mon', '--at', '08:30',
62
+ '--retries', '2', '--notify', '--project', dir)
63
+
64
+ expect(scheduler).to have_received(:add).with('smoke', every: 'week', files: [], tag: 'smoke', at: '08:30',
65
+ on: 'mon', retries: 2, notify: true, show_browser: false)
66
+ end
67
+
68
+ it 'is reached through raider sc' do
69
+ raider('sc', 'add', 'nightly', '--every', 'day', '--project', dir)
70
+
71
+ expect(scheduler).to have_received(:add).with('nightly', hash_including(every: 'day', files: []))
72
+ end
73
+
74
+ it 'exits 2 with one stderr line when --every is left out' do
75
+ # The real Scheduler refuses; its adapter is a double that fails on any call
76
+ allow(RubyRaider::Schedules::Scheduler).to receive(:new).and_wrap_original do |original, project|
77
+ original.call(project, adapter: instance_double(RubyRaider::Schedules::Os::Launchd), out: $stdout, err: $stderr)
78
+ end
79
+
80
+ out, err, code = raider('schedule', 'add', 'nightly', '--at', '08:00', '--project', dir)
81
+
82
+ expect([out, err, code]).to eq(['', "Invalid every nil: use hour, day, weekday or week\n", 2])
83
+ end
84
+
85
+ it 'exits 2 with one stderr line when the project has no Gemfile, before the OS scheduler is touched' do
86
+ allow(RubyRaider::Schedules::Scheduler).to receive(:new).and_wrap_original do |original, project|
87
+ original.call(project, adapter: instance_double(RubyRaider::Schedules::Os::Launchd), out: $stdout, err: $stderr)
88
+ end
89
+ version = File.read(File.expand_path('../../lib/version', __dir__)).strip
90
+
91
+ out, err, code = raider('schedule', 'add', 'nightly', '--every', 'day', '--at', '08:00', '--project', dir)
92
+
93
+ expect([out, err, code]).to eq(['', "Scheduling needs ruby_raider #{version} or later in this project: no Gemfile found\n", 2])
94
+ expect(File.exist?(File.join(dir, '.raider'))).to be(false)
95
+ end
96
+ end
97
+
98
+ describe 'refusals' do
99
+ it 'exits 2 with the error on one stderr line' do
100
+ allow(scheduler).to receive(:remove).and_raise(RubyRaider::Schedules::Error, 'No schedule gone')
101
+
102
+ out, err, code = raider('schedule', 'remove', 'gone', '--project', dir)
103
+
104
+ expect([out, err, code]).to eq(['', "No schedule gone\n", 2])
105
+ end
106
+
107
+ it 'keeps a --project with a newline on one stderr line' do
108
+ out, err, code = raider('schedule', 'list', '--project', "/nonexistent/a\nb")
109
+
110
+ expect([out, err, code]).to eq(['', "Project folder not found: /nonexistent/a?b\n", 2])
111
+ expect(RubyRaider::Schedules::Scheduler).not_to have_received(:new)
112
+ expect(RubyRaider::Schedules::Legacy).not_to have_received(:new)
113
+ end
114
+
115
+ it 'exits 2 for an argument that is not valid UTF-8, through schedule and sc' do
116
+ results = [raider('schedule', 'add', "a\xFF", '--every', 'day'), raider('sc', 'remove', "a\xFF")]
117
+
118
+ expect(results).to all(eq(['', "Invalid argument \"a\\xFF\": use valid UTF-8\n", 2]))
119
+ expect(RubyRaider::Schedules::Scheduler).not_to have_received(:new)
120
+ end
121
+ end
122
+
123
+ describe 'list and remove' do
124
+ it 'lists as text, or as JSON with --json' do
125
+ raider('schedule', 'list', '--project', dir)
126
+ raider('schedule', 'list', '--json', '--project', dir)
127
+
128
+ expect(scheduler).to have_received(:list).with(json: false, notice: nil).ordered
129
+ expect(scheduler).to have_received(:list).with(json: true, notice: nil).ordered
130
+ end
131
+
132
+ it 'removes by name' do
133
+ raider('schedule', 'remove', 'nightly', '--project', dir)
134
+
135
+ expect(scheduler).to have_received(:remove).with('nightly')
136
+ end
137
+ end
138
+
139
+ describe 'run' do
140
+ it 'maps run to the wrapper with the manual trigger by default' do
141
+ _out, _err, code = raider('schedule', 'run', 'nightly', '--project', dir)
142
+
143
+ expect(code).to eq(0)
144
+ expect(wrapper).to have_received(:run).with('nightly', trigger: 'manual')
145
+ expect(RubyRaider::Schedules::RunWrapper).to have_received(:new).with(project_root)
146
+ end
147
+
148
+ it "exits with the wrapper's code and passes --trigger" do
149
+ codes = [1, 2, 3].map do |status|
150
+ allow(wrapper).to receive(:run).and_return(status)
151
+ raider('sc', 'run', 'nightly', '--trigger', 'schedule', '--project', dir).last
152
+ end
153
+
154
+ expect(codes).to eq([1, 2, 3])
155
+ expect(wrapper).to have_received(:run).with('nightly', trigger: 'schedule').exactly(3).times
156
+ end
157
+
158
+ it 'exits 2 with one stderr line when the wrapper refuses' do
159
+ allow(wrapper).to receive(:run).and_raise(RubyRaider::Schedules::Error, 'Invalid trigger "x": use manual or schedule')
160
+
161
+ out, err, code = raider('schedule', 'run', 'nightly', '--trigger', 'x', '--project', dir)
162
+
163
+ expect([out, err, code]).to eq(['', "Invalid trigger \"x\": use manual or schedule\n", 2])
164
+ end
165
+ end
166
+
167
+ describe 'the 4.1 cleanup' do # rubocop:disable RSpec/MultipleMemoizedHelpers
168
+ # Plays the OS scheduler for Legacy: one 4.1 entry of this project, removed or refused
169
+ let(:fake_adapter_class) do
170
+ Class.new do
171
+ attr_reader :ids, :removed
172
+ attr_accessor :failure
173
+
174
+ def initialize(ids)
175
+ @ids = ids
176
+ @removed = []
177
+ end
178
+
179
+ def list_ids(prefix) = ids.select { |id| id.start_with?(prefix) }
180
+
181
+ def remove(os_id)
182
+ raise failure if failure
183
+
184
+ @removed << os_id
185
+ true
186
+ end
187
+ end
188
+ end
189
+ let(:adapter) { fake_adapter_class.new([RubyRaider::Schedules::Legacy::OldRule.os_id_for(dir, 'tredssd')]) }
190
+ let(:notice) { "Removed 1 schedule made with Ruby Raider 4.1 (tredssd); it can't run in 4.2. Add it again with raider schedule add." }
191
+
192
+ before do
193
+ FileUtils.mkdir_p(File.join(dir, '.raider', 'flows'))
194
+ allow(RubyRaider::Schedules::Legacy).to receive(:new).and_wrap_original do |original, project|
195
+ original.call(project, adapter: adapter)
196
+ end
197
+ end
198
+
199
+ it 'runs before every command, with the project' do
200
+ allow(RubyRaider::Schedules::Legacy).to receive(:new).and_return(legacy)
201
+ [%w[add nightly --every day], %w[list], %w[remove nightly], %w[run nightly]].each do |args|
202
+ raider('schedule', *args, '--project', dir)
203
+ end
204
+
205
+ expect(RubyRaider::Schedules::Legacy).to have_received(:new).with(project_root).exactly(4).times
206
+ expect(legacy).to have_received(:cleanup).with(no_args).exactly(4).times
207
+ end
208
+
209
+ it 'cleans up before the command runs' do
210
+ allow(scheduler).to receive(:remove) { expect(adapter.removed.size).to eq(1) }
211
+
212
+ raider('schedule', 'remove', 'nightly', '--project', dir)
213
+
214
+ expect(scheduler).to have_received(:remove).with('nightly')
215
+ end
216
+
217
+ # The fake keeps listing the entry, so the second list finds it again
218
+ it 'writes the notice to stderr in text mode, and passes it to the JSON' do
219
+ _out, err, code = raider('schedule', 'list', '--project', dir)
220
+ _json_out, json_err, = raider('schedule', 'list', '--json', '--project', dir)
221
+
222
+ expect([err, code, json_err]).to eq(["#{notice}\n", nil, "#{notice}\n"])
223
+ expect(scheduler).to have_received(:list).with(json: false, notice: notice).ordered
224
+ expect(scheduler).to have_received(:list).with(json: true, notice: notice).ordered
225
+ end
226
+
227
+ it "keeps run's exit code when the cleanup fails" do
228
+ adapter.failure = RubyRaider::Schedules::Error.new('denied')
229
+ allow(wrapper).to receive(:run).and_return(1)
230
+
231
+ out, err, code = raider('schedule', 'run', 'nightly', '--project', dir)
232
+
233
+ expect([out, code]).to eq(['', 1])
234
+ expect(err).to eq("Could not remove 1 schedule made with Ruby Raider 4.1 (tredssd: denied); it is tried again next time.\n")
235
+ end
236
+
237
+ it 'is skipped, with no adapter, when .raider/flows is not a folder' do
238
+ allow(RubyRaider::Schedules).to receive(:adapter)
239
+ FileUtils.rm_rf(File.join(dir, '.raider', 'flows'))
240
+ outside = File.realpath(Dir.mktmpdir('schedule-commands-outside'))
241
+ File.symlink(outside, File.join(dir, '.raider', 'flows'))
242
+ results = [raider('schedule', 'run', 'nightly', '--project', dir)]
243
+ FileUtils.rm_rf(File.join(dir, '.raider'))
244
+ results << raider('schedule', 'list', '--project', dir)
245
+
246
+ expect(results).to eq([['', '', 0], ['', '', nil]])
247
+ expect(RubyRaider::Schedules::Legacy).not_to have_received(:new)
248
+ expect(RubyRaider::Schedules).not_to have_received(:adapter)
249
+ ensure
250
+ FileUtils.rm_rf(outside) if outside
251
+ end
252
+
253
+ it 'prints nothing when there is nothing to clean' do
254
+ adapter.ids.clear
255
+
256
+ out, err, code = raider('schedule', 'run', 'nightly', '--project', dir)
257
+
258
+ expect([out, err, code]).to eq(['', '', 0])
259
+ end
260
+ end
261
+
262
+ describe 'help' do
263
+ it 'lists the four commands in raider help schedule' do
264
+ out, = raider('help', 'schedule')
265
+
266
+ expect(out).to include('schedule add NAME [SPEC_FILE ...]', 'schedule list', 'schedule remove NAME',
267
+ 'schedule run NAME')
268
+ end
269
+
270
+ # Thor would also reach run_schedule from `run` by prefix; the map is what keeps `run` working if that changes
271
+ it 'maps run to run_schedule explicitly and shows it as run NAME' do
272
+ out, = raider('help', 'schedule')
273
+
274
+ expect(described_class.map).to include('run' => :run_schedule)
275
+ expect(out).to match(/^ +\S+ schedule run NAME +# Runs the schedule NAME now/)
276
+ end
277
+
278
+ it 'lists schedule in raider help' do
279
+ out, = raider('help')
280
+
281
+ expect(out).to include('schedule', 'Runs the tests on a schedule')
282
+ end
283
+ end
284
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'content_helper'
4
+ require_relative '../../../lib/utilities/schedules/schedule_store'
4
5
 
5
6
  describe 'Common file content' do
6
7
  # --- Rakefile ---
@@ -77,6 +78,10 @@ describe 'Common file content' do
77
78
  it 'includes raider-results' do
78
79
  expect(gitignore).to include('raider-results')
79
80
  end
81
+
82
+ it 'ignores .raider/schedules/ with the exact line raider schedule add looks for' do
83
+ expect(gitignore.lines.map(&:chomp)).to include(RubyRaider::Schedules::ScheduleStore::GITIGNORE_LINE)
84
+ end
80
85
  end
81
86
 
82
87
  shared_examples 'web gitignore for saved logins' do |project_name|
@@ -89,8 +94,9 @@ describe 'Common file content' do
89
94
  end
90
95
 
91
96
  shared_examples 'mobile gitignore without saved logins' do |project_name|
92
- it 'has no .raider line' do
93
- expect(read_generated(project_name, '.gitignore')).not_to include('.raider')
97
+ it 'has no .raider line but .raider/schedules/' do
98
+ lines = read_generated(project_name, '.gitignore').lines.map(&:strip)
99
+ expect(lines.grep(/\A\.raider/)).to eq(['.raider/schedules/'])
94
100
  end
95
101
  end
96
102
 
@@ -85,8 +85,8 @@ describe 'Gemfile content' do
85
85
  expect(gemfile).to include("gem 'ruby_raider', '~> #{major_minor}'")
86
86
  end
87
87
 
88
- it 'pins ruby_raider to the 4.0 release line' do
89
- expect(gemfile).to include("gem 'ruby_raider', '~> 4.0'")
88
+ it 'pins ruby_raider to the 4.2 release line' do
89
+ expect(gemfile).to include("gem 'ruby_raider', '~> 4.2'")
90
90
  end
91
91
  end
92
92
  end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'open3'
5
+ require 'rbconfig'
6
+ require 'tmpdir'
7
+
8
+ # Real rspec-retry runs in a subprocess, no browser. The suite mimics a generated project: driver_helper
9
+ # memoises @driver, the after hook quits it, and a quit driver refuses every call like a dead session.
10
+ RSpec.describe 'Retrying a test with a fresh driver' do
11
+ lib = File.expand_path('../../lib', __dir__)
12
+
13
+ suite = <<~'RUBY'
14
+ require 'json'
15
+ require 'rspec'
16
+ require 'rspec/retry' if ENV['RETRY'] == 'before'
17
+ require 'ruby_raider/runtime/rspec'
18
+ require 'rspec/retry' if ENV['RETRY'] == 'after'
19
+
20
+ LOG = ENV.fetch('LOG')
21
+ def log(entry) = File.open(LOG, 'a') { |file| file.puts(JSON.generate(entry)) }
22
+
23
+ class FakeDriver
24
+ def quit = @quit = true
25
+
26
+ def title
27
+ raise Errno::ECONNREFUSED, '127.0.0.1:9515' if @quit
28
+
29
+ 'ok'
30
+ end
31
+ end
32
+
33
+ module DriverHelper
34
+ def driver = @driver ||= FakeDriver.new
35
+ end
36
+
37
+ RSpec.configure do |config|
38
+ config.include(DriverHelper)
39
+ config.default_retry_count = ENV.fetch('RETRY_COUNT', 0).to_i if config.respond_to?(:default_retry_count=)
40
+ # A project's own callback: one that resets the driver itself, or one that does not
41
+ if ENV['OWN_CALLBACK']
42
+ config.retry_callback = proc do
43
+ log('callback' => 'project')
44
+ @driver = nil if ENV['OWN_CALLBACK'] == 'resets'
45
+ end
46
+ end
47
+ config.before(:each) { driver.title }
48
+ config.after(:each) { driver.quit }
49
+ config.after(:suite) do
50
+ callback = config.retry_callback if config.respond_to?(:retry_callback)
51
+ owner = { Raider::RetryReset::CALLBACK => 'ours', nil => 'none' }.fetch(callback, 'project')
52
+ log('installed' => config.respond_to?(:retry_callback) ? owner : 'no rspec-retry')
53
+ end
54
+ end
55
+
56
+ RSpec.describe 'Flaky' do
57
+ it 'fails on its first try and passes on the next' do
58
+ tries = File.readlines(LOG).count { |line| line.include?('"try"') } + 1
59
+ log('try' => tries, 'driver' => driver.object_id)
60
+ expect(tries).to eq(ENV.fetch('PASS_ON', '2').to_i)
61
+ end
62
+ end
63
+ RUBY
64
+
65
+ # The subprocess is a plain ruby, not bundle exec: it finds rspec and rspec-retry through the bundle env
66
+ # (BUNDLE_GEMFILE, RUBYOPT=-rbundler/setup) it inherits from this rspec run.
67
+ define_method(:run_suite) do |env|
68
+ Dir.mktmpdir('raider-retry') do |dir|
69
+ spec = File.join(dir, 'flaky_spec.rb')
70
+ log = File.join(dir, 'log.jsonl')
71
+ File.write(spec, suite)
72
+ File.write(log, '')
73
+ out, err, status = Open3.capture3(env.merge('LOG' => log, 'RSPEC_RETRY_RETRY_COUNT' => nil), RbConfig.ruby, '-I', lib,
74
+ '-e', 'require "rspec/core"; exit RSpec::Core::Runner.run(ARGV)', spec, chdir: dir)
75
+ { status: status.exitstatus, output: out + err, log: File.readlines(log).map { |line| JSON.parse(line) } }
76
+ end
77
+ end
78
+
79
+ define_method(:tries) { |result| result[:log].select { |entry| entry.key?('try') } }
80
+ define_method(:installed) { |result| result[:log].find { |entry| entry.key?('installed') }&.fetch('installed') }
81
+
82
+ %w[before after].each do |order|
83
+ it "passes on the second try with a fresh driver when rspec/retry is required #{order} the runtime" do
84
+ result = run_suite('RETRY' => order, 'RETRY_COUNT' => '2')
85
+ expect([result[:status], tries(result).map { |entry| entry['try'] }]).to eq([0, [1, 2]]), result[:output]
86
+ expect(tries(result).map { |entry| entry['driver'] }.uniq.size).to eq(2)
87
+ expect(installed(result)).to eq('ours')
88
+ end
89
+ end
90
+
91
+ it 'keeps a retry_callback the project set' do
92
+ result = run_suite('RETRY' => 'before', 'RETRY_COUNT' => '2', 'OWN_CALLBACK' => 'resets')
93
+ expect([result[:status], installed(result)]).to eq([0, 'project']), result[:output]
94
+ expect(result[:log]).to include({ 'callback' => 'project' })
95
+ end
96
+
97
+ it 'a project callback that keeps the driver still fails on the quit session' do
98
+ result = run_suite('RETRY' => 'before', 'RETRY_COUNT' => '2', 'OWN_CALLBACK' => 'keeps')
99
+ expect([result[:status], installed(result), tries(result).size]).to eq([1, 'project', 1])
100
+ expect(result[:output]).to include('Errno::ECONNREFUSED')
101
+ end
102
+
103
+ it 'changes nothing when no retry happens' do
104
+ result = run_suite('RETRY' => 'before', 'RETRY_COUNT' => '2', 'PASS_ON' => '1')
105
+ expect([result[:status], tries(result).size, installed(result)]).to eq([0, 1, 'ours']), result[:output]
106
+ end
107
+
108
+ it 'does nothing without rspec-retry' do
109
+ result = run_suite('PASS_ON' => '1')
110
+ expect([result[:status], installed(result)]).to eq([0, 'no rspec-retry']), result[:output]
111
+ end
112
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'json'
5
+ require 'open3'
6
+ require 'rbconfig'
7
+ require 'rspec'
8
+ require 'tmpdir'
9
+
10
+ # A Selenium project generated by this branch (its helper templates are byte-identical to 4.0.1's) with
11
+ # `headless: true` in config/config.yml. Its own helpers/driver_helper.rb builds the browser options, no
12
+ # browser is started. RAIDER_SHOW_BROWSER=1 drops the --headless it adds; config.yml is never written.
13
+ RSpec.describe 'Show the browser in a generated project', :slow do
14
+ lib = File.expand_path('../../lib', __dir__)
15
+
16
+ generate = <<~RUBY
17
+ require 'generators/invoke_generators'
18
+ InvokeGenerators.generate_framework(automation: 'selenium', framework: 'rspec', name: 'shown', examples: false)
19
+ RUBY
20
+
21
+ # Loads the project's real driver_helper and prints the args of the options it builds
22
+ options = <<~RUBY
23
+ require 'json'
24
+ require './helpers/driver_helper'
25
+ helper = Object.new.extend(DriverHelper)
26
+ helper.instance_variable_set(:@config, YAML.load_file('config/config.yml'))
27
+ built = helper.send(:create_webdriver_options)
28
+ puts JSON.generate(built.as_json.fetch(built.class::KEY).fetch('args'))
29
+ RUBY
30
+
31
+ define_method(:ruby) do |env, dir, script|
32
+ out, err, status = Open3.capture3(env, RbConfig.ruby, '-I', lib, '-e', script, chdir: dir)
33
+ raise "ruby failed in #{dir}: #{err}" unless status.success?
34
+
35
+ out
36
+ end
37
+
38
+ # rubocop:disable RSpec/InstanceVariable
39
+ before(:all) do # rubocop:disable RSpec/BeforeAfterAll
40
+ @tmp = Dir.mktmpdir('raider-show-browser')
41
+ ruby({}, @tmp, generate)
42
+ @project = File.join(@tmp, 'shown')
43
+ config = File.join(@project, 'config', 'config.yml')
44
+ File.write(config, "#{File.read(config)}headless: true\n")
45
+ @before = File.binread(config)
46
+ @args = { off: { 'RAIDER_SHOW_BROWSER' => nil, 'HEADLESS' => nil }, shown: { 'RAIDER_SHOW_BROWSER' => '1', 'HEADLESS' => nil },
47
+ shown_hidden_env: { 'RAIDER_SHOW_BROWSER' => '1', 'HEADLESS' => 'true' }, other: { 'RAIDER_SHOW_BROWSER' => 'true', 'HEADLESS' => nil } }
48
+ .transform_values { |env| JSON.parse(ruby(env, @project, options)) }
49
+ end
50
+
51
+ after(:all) { FileUtils.rm_rf(@tmp) } # rubocop:disable RSpec/BeforeAfterAll
52
+
53
+ it 'requires the runtime from the generated driver_helper' do
54
+ expect(File.read(File.join(@project, 'helpers', 'driver_helper.rb'))).to include("require 'ruby_raider/runtime'")
55
+ end
56
+
57
+ it 'keeps --headless without RAIDER_SHOW_BROWSER=1' do
58
+ expect(@args.values_at(:off, :other)).to all(include('--headless'))
59
+ end
60
+
61
+ it 'drops --headless with RAIDER_SHOW_BROWSER=1, even with HEADLESS set, and keeps the browser arguments' do
62
+ expected = @args[:off] - ['--headless']
63
+ expect(@args.values_at(:shown, :shown_hidden_env)).to eq([expected, expected])
64
+ end
65
+
66
+ it 'never writes config.yml' do
67
+ expect(File.binread(File.join(@project, 'config', 'config.yml'))).to eq(@before)
68
+ end
69
+ # rubocop:enable RSpec/InstanceVariable
70
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'open3'
5
+ require 'rbconfig'
6
+ require 'rspec'
7
+ require_relative '../../lib/ruby_raider/runtime/show_browser'
8
+
9
+ # The prepend is process-wide, so every case that loads the runtime and looks at the Options classes runs
10
+ # in its own ruby with an explicit RAIDER_SHOW_BROWSER. This process only checks the pure helpers.
11
+ RSpec.describe Raider::ShowBrowser do # rubocop:disable RSpec/FilePath
12
+ lib = File.expand_path('../../lib', __dir__)
13
+
14
+ # Loads the runtime the way a project's driver_helper does, then reports the real Options classes
15
+ probe = <<~RUBY
16
+ require 'json'
17
+ require 'ruby_raider/runtime'
18
+ filter = Raider::ShowBrowser::Filter
19
+ Raider::ShowBrowser.install(ENV) if ARGV.include?('again')
20
+ report = %w[Chrome Edge Firefox].to_h do |browser|
21
+ klass = Selenium::WebDriver.const_get(browser)::Options
22
+ built = klass.new(args: ['--headless', 'start-maximized', '--headless=new', '-headless'])
23
+ %w[--headless --headless=old -headless window-size=800,600 --headlessly headless].each { |arg| built.add_argument(arg) }
24
+ set = klass.new
25
+ set.args = ['--headless', 'set']
26
+ keyed = klass.new('args' => ['-headless', 'keyed'])
27
+ args = ->(options) { options.as_json.fetch(klass::KEY).fetch('args') }
28
+ added = klass.new
29
+ returned = [added.add_argument('--headless'), added.add_argument('kept')].map { |value| value.equal?(added.options[:args]) }
30
+ [browser, { 'prepended' => klass.ancestors.count(filter), 'built' => args.call(built), 'set' => args.call(set),
31
+ 'keyed' => keyed.options['args'], 'returned' => returned }]
32
+ end
33
+ puts JSON.generate(report)
34
+ RUBY
35
+
36
+ define_method(:probe) do |value, *argv|
37
+ out, err, status = Open3.capture3({ 'RAIDER_SHOW_BROWSER' => value, 'HEADLESS' => nil }, RbConfig.ruby, '-I', lib, '-e', probe, *argv)
38
+ raise "probe failed: #{err}" unless status.success?
39
+
40
+ JSON.parse(out)
41
+ end
42
+
43
+ context 'with RAIDER_SHOW_BROWSER=1' do
44
+ let(:report) { probe('1') }
45
+
46
+ it 'prepends the filter once to the Chrome, Edge and Firefox options' do
47
+ expect(report.transform_values { |browser| browser['prepended'] }).to eq('Chrome' => 1, 'Edge' => 1, 'Firefox' => 1)
48
+ end
49
+
50
+ it 'prepends it once when installed again' do
51
+ expect(probe('1', 'again').transform_values { |browser| browser['prepended'] }).to eq('Chrome' => 1, 'Edge' => 1, 'Firefox' => 1)
52
+ end
53
+
54
+ it 'drops --headless, --headless=* and -headless from args: and add_argument and keeps every other argument' do
55
+ expected = %w[start-maximized window-size=800,600 --headlessly headless]
56
+ expect(report.transform_values { |browser| browser['built'] }).to eq('Chrome' => expected, 'Edge' => expected, 'Firefox' => expected)
57
+ end
58
+
59
+ it 'returns the args from add_argument as Selenium does, also for a dropped switch' do
60
+ expect(report.transform_values { |browser| browser['returned'] }).to eq('Chrome' => [true, true], 'Edge' => [true, true], 'Firefox' => [true, true])
61
+ end
62
+
63
+ it 'drops them from args= and from a string args key' do
64
+ expect(report.transform_values { |browser| browser.values_at('set', 'keyed') })
65
+ .to eq('Chrome' => [%w[set], %w[keyed]], 'Edge' => [%w[set], %w[keyed]], 'Firefox' => [%w[set], %w[keyed]])
66
+ end
67
+ end
68
+
69
+ [nil, '', '0', 'true', '1 ', 'yes'].each do |value|
70
+ it "prepends nothing and keeps --headless when RAIDER_SHOW_BROWSER is #{value.inspect}" do
71
+ report = probe(value)
72
+ expect(report.transform_values { |browser| [browser['prepended'], browser['built'].first, browser['set']] })
73
+ .to eq('Chrome' => [0, '--headless', %w[--headless set]], 'Edge' => [0, '--headless', %w[--headless set]],
74
+ 'Firefox' => [0, '--headless', %w[--headless set]])
75
+ end
76
+ end
77
+
78
+ describe '.headless?' do
79
+ it 'matches only the headless switches' do
80
+ args = ['--headless', '--headless=new', '--headless=', '-headless', 'headless', '--headlessly', '-headless=new', ' --headless', :'--headless', nil]
81
+ expect(args.select { |arg| described_class.headless?(arg) }).to eq(['--headless', '--headless=new', '--headless=', '-headless'])
82
+ end
83
+ end
84
+
85
+ describe '.without_headless' do
86
+ it 'returns a new array and leaves the given one untouched' do
87
+ given = %w[--headless keep].freeze
88
+ expect(described_class.without_headless(given)).to eq(%w[keep])
89
+ end
90
+
91
+ it 'passes anything that is not an array through' do
92
+ expect([nil, '--headless'].map { |value| described_class.without_headless(value) }).to eq([nil, '--headless'])
93
+ end
94
+ end
95
+
96
+ describe '.install' do
97
+ it 'installs nothing unless the value is exactly 1' do
98
+ expect([{}, { 'RAIDER_SHOW_BROWSER' => 'true' }].map { |env| described_class.install(env) }).to eq([[], []])
99
+ end
100
+ end
101
+ end