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,430 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'stringio'
5
+ require 'tmpdir'
6
+ require_relative '../../../lib/utilities/schedules/run_wrapper'
7
+
8
+ # The fake spawner needs its calls, outcome and project next to the wrapper
9
+ # rubocop:disable RSpec/MultipleMemoizedHelpers
10
+ RSpec.describe RubyRaider::Schedules::RunWrapper do # rubocop:disable RSpec/FilePath
11
+ let(:dir) { File.realpath(Dir.mktmpdir('schedules-wrapper')) }
12
+ let(:project) { RubyRaider::Schedules::Project.new(dir) }
13
+ let(:store) { RubyRaider::Schedules::ScheduleStore.new(project) }
14
+ let(:out) { StringIO.new }
15
+ let(:err) { StringIO.new }
16
+ let(:now) { Time.utc(2026, 11, 11, 8, 0, 0) }
17
+ let(:calls) { [] }
18
+ let(:notices) { [] }
19
+ let(:notifier) do
20
+ sent = notices
21
+ Class.new { define_method(:notify) { |title, message| sent << [title, message] && false } }.new
22
+ end
23
+ # What the fake rspec run does: exit code, stopped, and the statuses of the run.json it writes (nil: none)
24
+ let(:outcome) { { code: 0, stopped: false, statuses: %w[passed passed] } }
25
+ let(:spawner) do
26
+ lambda do |env, argv, chdir|
27
+ calls << { env: env, argv: argv, chdir: chdir, running: File.exist?(File.join(dir, '.raider/schedules/running.json')) }
28
+ write_run(outcome[:statuses]) if outcome[:statuses]
29
+ [outcome[:code], outcome[:stopped]]
30
+ end
31
+ end
32
+ let(:wrapper) { described_class.new(project, out: out, err: err, spawner: spawner, clock: -> { now }, notifier: notifier) }
33
+ let(:folder) { File.join(dir, '.raider/schedules/nightly') }
34
+
35
+ def write_run(statuses, started_at: '2026-11-11T08:00:01.000Z')
36
+ results = File.join(dir, 'raider-results')
37
+ FileUtils.mkdir_p(File.join(results, 'attachments'))
38
+ File.write(File.join(results, 'run.json'), JSON.generate('run' => { 'started_at' => started_at }, 'results' => statuses.map { |status| { 'status' => status } }))
39
+ File.write(File.join(results, 'index.html'), '<html>')
40
+ end
41
+
42
+ def schedule(name: 'nightly', **options)
43
+ store.save(RubyRaider::Schedules::Schedule.build(project: project, name: name, every: 'day', at: '08:00', host_os: 'darwin', **options))
44
+ end
45
+
46
+ def lines = File.readlines(File.join(folder, 'runs.jsonl')).map { |line| JSON.parse(line) }
47
+
48
+ before do
49
+ FileUtils.mkdir_p(File.join(dir, 'spec'))
50
+ File.write(File.join(dir, 'spec/login_spec.rb'), '')
51
+ File.write(File.join(dir, 'spec/cart_spec.rb'), '')
52
+ schedule
53
+ end
54
+
55
+ after { FileUtils.rm_rf(dir) }
56
+
57
+ describe 'a passing run' do
58
+ it 'runs bundle exec rspec with the scheduled ruby and bundle, from the project, with no path for all tests' do
59
+ entry = store.find('nightly')
60
+
61
+ expect(wrapper.run('nightly')).to eq(0)
62
+ expect(calls.first[:argv]).to eq([entry.ruby, entry.bundle, 'exec', 'rspec'])
63
+ expect([calls.first[:chdir], calls.first[:env]['BUNDLE_GEMFILE']]).to eq([dir, File.join(dir, 'Gemfile')])
64
+ end
65
+
66
+ it 'records the run line with counts, trigger and the report copy, and no messages' do
67
+ wrapper.run('nightly')
68
+
69
+ line = lines.last
70
+ expect(line.keys).to eq(%w[run_id started_at finished_at status counts trigger results_dir report_copy])
71
+ expect(line).to include('status' => 'passed', 'counts' => { 'total' => 2, 'passed' => 2, 'failed' => 0, 'skipped' => 0 },
72
+ 'trigger' => 'manual', 'results_dir' => 'raider-results', 'report_copy' => '.raider/schedules/nightly/last',
73
+ 'started_at' => '2026-11-11T08:00:00.000Z', 'finished_at' => '2026-11-11T08:00:00.000Z')
74
+ expect(line['run_id']).to match(/\A[0-9a-f]{16}\z/)
75
+ expect(Dir.children(File.join(folder, 'last')).sort).to eq(%w[attachments index.html run.json])
76
+ end
77
+
78
+ it 'prints one result line and does not notify' do
79
+ schedule(notify: true)
80
+ wrapper.run('nightly')
81
+
82
+ expect([out.string, notices]).to eq(["Schedule nightly passed: 2 tests\n", []])
83
+ end
84
+
85
+ it 'says test for a total of one' do
86
+ outcome.merge!(statuses: %w[passed])
87
+ wrapper.run('nightly')
88
+
89
+ expect(out.string).to eq("Schedule nightly passed: 1 test\n")
90
+ end
91
+
92
+ it 'holds running.json while rspec runs and removes it after' do
93
+ wrapper.run('nightly')
94
+
95
+ expect([calls.first[:running], File.exist?(File.join(dir, '.raider/schedules/running.json'))]).to eq([true, false])
96
+ end
97
+
98
+ it 'records a schedule trigger' do
99
+ wrapper.run('nightly', trigger: 'schedule')
100
+
101
+ expect(lines.last['trigger']).to eq('schedule')
102
+ end
103
+ end
104
+
105
+ describe 'selection' do
106
+ it 'passes one tag' do
107
+ schedule(selection: RubyRaider::Schedules::Selection.new(kind: 'tag', tag: 'smoke'))
108
+ wrapper.run('nightly')
109
+
110
+ expect(calls.first[:argv].drop(2)).to eq(%w[exec rspec --tag smoke])
111
+ end
112
+
113
+ it 'passes the listed files' do
114
+ schedule(selection: RubyRaider::Schedules::Selection.new(kind: 'files', files: %w[spec/login_spec.rb spec/cart_spec.rb]))
115
+ wrapper.run('nightly')
116
+
117
+ expect(calls.first[:argv].drop(2)).to eq(%w[exec rspec spec/login_spec.rb spec/cart_spec.rb])
118
+ end
119
+ end
120
+
121
+ describe 'env' do
122
+ around do |example|
123
+ saved = %w[HEADLESS RAIDER_SHOW_BROWSER RETRY_COUNT RSPEC_RETRY_RETRY_COUNT].to_h { |key| [key, ENV.fetch(key, nil)] }
124
+ example.run
125
+ ensure
126
+ saved.each { |key, value| ENV[key] = value }
127
+ end
128
+
129
+ def env_for(show_browser:, inherited:)
130
+ ENV['HEADLESS'] = inherited
131
+ ENV['RAIDER_SHOW_BROWSER'] = '1'
132
+ ENV['RSPEC_RETRY_RETRY_COUNT'] = '9'
133
+ schedule(show_browser: show_browser, retries: 2)
134
+ wrapper.run('nightly')
135
+ calls.last[:env].slice('HEADLESS', 'RAIDER_SHOW_BROWSER', 'RETRY_COUNT', 'RSPEC_RETRY_RETRY_COUNT')
136
+ end
137
+
138
+ hidden = { 'HEADLESS' => 'true', 'RAIDER_SHOW_BROWSER' => nil, 'RETRY_COUNT' => '3', 'RSPEC_RETRY_RETRY_COUNT' => nil }
139
+ shown = { 'HEADLESS' => nil, 'RAIDER_SHOW_BROWSER' => '1', 'RETRY_COUNT' => '3', 'RSPEC_RETRY_RETRY_COUNT' => nil }
140
+
141
+ { [false, nil] => hidden, [false, 'false'] => hidden, [true, nil] => shown, [true, 'true'] => shown }.each do |(show, inherited), env|
142
+ it "sets #{env.compact} for show_browser #{show} with HEADLESS #{inherited.inspect} inherited" do
143
+ expect(env_for(show_browser: show, inherited: inherited)).to eq(env)
144
+ end
145
+ end
146
+
147
+ it 'sets the GEM_HOME and GEM_PATH captured at add, unsetting one not captured' do
148
+ allow(RubyRaider::Schedules::Schedule).to receive(:captured_gem_env).and_return('GEM_HOME' => '/gems/home')
149
+ schedule
150
+ wrapper.run('nightly')
151
+
152
+ expect(calls.last[:env].slice('GEM_HOME', 'GEM_PATH')).to eq('GEM_HOME' => '/gems/home', 'GEM_PATH' => nil)
153
+ end
154
+
155
+ it 'leaves the caller\'s GEM_HOME and GEM_PATH when none were captured' do
156
+ allow(RubyRaider::Schedules::Schedule).to receive(:captured_gem_env).and_return({})
157
+ schedule
158
+ wrapper.run('nightly')
159
+
160
+ expect(calls.last[:env].keys & %w[GEM_HOME GEM_PATH]).to eq([])
161
+ end
162
+
163
+ it 'counts tries: one with no retries' do
164
+ wrapper.run('nightly')
165
+
166
+ expect(calls.first[:env]['RETRY_COUNT']).to eq('1')
167
+ end
168
+
169
+ it 'never writes config/config.yml' do
170
+ config = File.join(dir, 'config/config.yml')
171
+ FileUtils.mkdir_p(File.dirname(config))
172
+ File.write(config, "browser: chrome\nheadless: true\n")
173
+ schedule(show_browser: true)
174
+ wrapper.run('nightly')
175
+
176
+ expect(File.binread(config)).to eq("browser: chrome\nheadless: true\n")
177
+ end
178
+ end
179
+
180
+ describe 'a failing run' do
181
+ let(:outcome) { { code: 1, stopped: false, statuses: %w[passed failed quarantined skipped] } }
182
+
183
+ it 'exits 1, prints the failures and notifies when asked' do
184
+ schedule(notify: true)
185
+
186
+ expect(wrapper.run('nightly')).to eq(1)
187
+ expect(out.string).to eq("Schedule nightly failed: 2 of 4 tests failed\n")
188
+ expect(notices).to eq([['Ruby Raider: nightly failed', '2 of 4 tests failed']])
189
+ expect(lines.last['counts']).to eq('total' => 4, 'passed' => 1, 'failed' => 2, 'skipped' => 1)
190
+ end
191
+
192
+ it 'says test for a total of one' do
193
+ outcome.merge!(statuses: %w[failed])
194
+ schedule(notify: true)
195
+ wrapper.run('nightly')
196
+
197
+ expect([out.string, notices.last.last]).to eq(["Schedule nightly failed: 1 of 1 test failed\n", '1 of 1 test failed'])
198
+ end
199
+
200
+ it 'does not notify without notify' do
201
+ wrapper.run('nightly')
202
+
203
+ expect(notices).to eq([])
204
+ end
205
+ end
206
+
207
+ describe 'setup errors (exit 2)' do
208
+ before { schedule(notify: true) }
209
+
210
+ it 'is a setup error when rspec exits 1 without a report' do
211
+ outcome.merge!(code: 1, statuses: nil)
212
+
213
+ expect(wrapper.run('nightly')).to eq(2)
214
+ expect(lines.last).to include('status' => 'setup_error', 'counts' => { 'total' => 0, 'passed' => 0, 'failed' => 0, 'skipped' => 0 })
215
+ expect(lines.last).not_to have_key('report_copy')
216
+ expect(notices).to eq([['Ruby Raider: nightly failed', 'Could not start: see the run output']])
217
+ expect(out.string).to eq("Schedule nightly could not start: see the run output\n")
218
+ end
219
+
220
+ it 'points a scheduled run at its scheduler.log', skip: Gem.win_platform? do
221
+ outcome.merge!(code: 1, statuses: nil)
222
+ wrapper.run('nightly', trigger: 'schedule')
223
+
224
+ expect(notices.last.last).to eq('Could not start: see .raider/schedules/nightly/scheduler.log')
225
+ end
226
+
227
+ it 'is a setup error when no example ran' do
228
+ outcome.merge!(code: 0, statuses: [])
229
+
230
+ expect(wrapper.run('nightly')).to eq(2)
231
+ end
232
+
233
+ it 'is a setup error when the run.json is an earlier run, which it keeps' do
234
+ outcome.merge!(code: 1, statuses: nil)
235
+ write_run(%w[failed], started_at: '2026-11-10T08:00:00.000Z')
236
+
237
+ expect(wrapper.run('nightly')).to eq(2)
238
+ expect([lines.last['status'], File.exist?(File.join(folder, 'last')), File.exist?(File.join(dir, 'raider-results/run.json'))]).to eq(['setup_error', false, true])
239
+ end
240
+
241
+ it 'is a setup error when rspec did not start' do
242
+ outcome.merge!(code: nil, statuses: nil)
243
+
244
+ expect(wrapper.run('nightly')).to eq(2)
245
+ end
246
+
247
+ it 'records a missing spec file before anything runs' do
248
+ schedule(notify: true, selection: RubyRaider::Schedules::Selection.new(kind: 'files', files: %w[spec/login_spec.rb]))
249
+ File.delete(File.join(dir, 'spec/login_spec.rb'))
250
+
251
+ expect(wrapper.run('nightly')).to eq(2)
252
+ expect([calls, lines.last['status']]).to eq([[], 'setup_error'])
253
+ expect(out.string).to eq("Schedule nightly could not start: spec/login_spec.rb not found: remove the schedule or add it again\n")
254
+ expect(notices).to eq([['Ruby Raider: nightly failed', 'Could not start: spec/login_spec.rb not found']])
255
+ expect(File.exist?(File.join(dir, '.raider/schedules/running.json'))).to be(false)
256
+ end
257
+
258
+ it 'never changes the exit code for a notification that could not be shown' do
259
+ outcome.merge!(code: 1, statuses: nil)
260
+
261
+ expect(wrapper.run('nightly')).to eq(2)
262
+ expect(notices.size).to eq(1)
263
+ end
264
+ end
265
+
266
+ describe 'a stopped run' do
267
+ let(:outcome) { { code: 1, stopped: true, statuses: %w[passed failed] } }
268
+
269
+ it 'is a setup error even with failures, never notified' do
270
+ schedule(notify: true)
271
+
272
+ expect(wrapper.run('nightly')).to eq(2)
273
+ expect([lines.last['status'], notices]).to eq(['setup_error', []])
274
+ expect(out.string).to eq("Schedule nightly was stopped\n")
275
+ end
276
+ end
277
+
278
+ # INT or TERM outside the spawn's own traps arrives as Interrupt or SignalException
279
+ describe 'a stop around the spawn' do
280
+ let(:running) { File.join(dir, '.raider/schedules/running.json') }
281
+ # Each stop is raised once: what comes after it runs normally
282
+ let(:stops) { [] }
283
+
284
+ def stop_after_acquire(signal)
285
+ stops << signal
286
+ allow(RubyRaider::Schedules::RunLock).to receive(:new).and_wrap_original do |original, *args|
287
+ original.call(*args).tap do |lock|
288
+ allow(lock).to receive(:acquire).and_wrap_original do |acquire, **options|
289
+ acquire.call(**options).tap { |held| raise(stops.shift) if held && stops.any? }
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ it 'records a stop while taking the lock: exit 2, one stopped line, lock and running.json freed, never notified' do
296
+ schedule(notify: true)
297
+ stop_after_acquire(Interrupt.new)
298
+
299
+ expect(wrapper.run('nightly')).to eq(2)
300
+ expect([calls, notices, lines.map { |line| line['status'] }, File.exist?(running)]).to eq([[], [], ['setup_error'], false])
301
+ expect(out.string).to eq("Schedule nightly was stopped\n")
302
+ expect(described_class.new(project, out: out, err: err, spawner: spawner, clock: -> { now }, notifier: notifier).run('nightly')).to eq(0)
303
+ end
304
+
305
+ it 'records a TERM while the report is copied after rspec ended, with the counts, as stopped' do
306
+ allow(RubyRaider::Schedules::RunReport).to receive(:load).and_wrap_original do |original, *args, **options|
307
+ original.call(*args, **options).tap { |report| allow(report).to receive(:copy_to).and_raise(SignalException.new('TERM')) }
308
+ end
309
+
310
+ expect(wrapper.run('nightly')).to eq(2)
311
+ expect(lines.map { |line| line.values_at('status', 'counts') }).to eq([['setup_error', { 'total' => 2, 'passed' => 2, 'failed' => 0, 'skipped' => 0 }]])
312
+ expect([out.string, File.exist?(running)]).to eq(["Schedule nightly was stopped\n", false])
313
+ end
314
+
315
+ it 'keeps the recorded outcome when the stop comes after the run line' do
316
+ stops << Interrupt.new
317
+ allow(RubyRaider::Schedules::RunLog).to receive(:new).and_wrap_original do |original, *args, **options|
318
+ original.call(*args, **options).tap do |log|
319
+ allow(log).to receive(:append).and_wrap_original do |append, line|
320
+ append.call(line).tap { raise(stops.shift) if stops.any? }
321
+ end
322
+ end
323
+ end
324
+
325
+ expect(wrapper.run('nightly')).to eq(0)
326
+ expect(lines.map { |line| line['status'] }).to eq(['passed'])
327
+ end
328
+ end
329
+
330
+ describe 'a run line that cannot be written' do
331
+ before do
332
+ FileUtils.mkdir_p(folder)
333
+ File.symlink(File.join(dir, 'elsewhere.jsonl'), File.join(folder, 'runs.jsonl'))
334
+ schedule(notify: true)
335
+ end
336
+
337
+ it 'keeps a stop: not notified, still stopped' do
338
+ outcome.merge!(code: 1, stopped: true)
339
+
340
+ expect(wrapper.run('nightly')).to eq(2)
341
+ expect([notices, out.string, File.exist?(File.join(dir, 'elsewhere.jsonl'))]).to eq([[], "Schedule nightly was stopped\n", false])
342
+ expect(err.string).to include('Could not record the run')
343
+ end
344
+
345
+ it 'keeps the outcome of a run that passed' do
346
+ expect(wrapper.run('nightly')).to eq(0)
347
+ end
348
+ end
349
+
350
+ describe 'busy (exit 3, nothing spawned, raider-results untouched)' do
351
+ before do
352
+ schedule(notify: true)
353
+ write_run(%w[failed])
354
+ end
355
+
356
+ def results_snapshot = Dir.glob('**/*', base: File.join(dir, 'raider-results')).sort.map { |path| [path, File.mtime(File.join(dir, 'raider-results', path))] }
357
+
358
+ it 'is busy while another run holds the lock' do
359
+ before = results_snapshot
360
+ File.open(File.join(dir, '.raider/schedules/run.lock'), File::RDWR | File::CREAT, 0o600) do |held|
361
+ held.flock(File::LOCK_EX)
362
+
363
+ expect(wrapper.run('nightly')).to eq(3)
364
+ end
365
+ expect([calls, notices, lines.last['status'], results_snapshot]).to eq([[], [], 'busy', before])
366
+ expect(out.string).to eq("Schedule nightly is already running\n")
367
+ end
368
+
369
+ it 'is busy while running.json names a live process' do
370
+ File.write(File.join(dir, '.raider/schedules/running.json'), JSON.generate('pid' => Process.ppid))
371
+
372
+ expect(wrapper.run('nightly')).to eq(3)
373
+ expect(File.exist?(File.join(dir, '.raider/schedules/running.json'))).to be(true)
374
+ end
375
+ end
376
+
377
+ describe 'refusals (Error, exit 2, no line)' do
378
+ it 'refuses an unknown schedule' do
379
+ expect { wrapper.run('nope') }.to raise_error(RubyRaider::Schedules::Error, /nope/) { |error| expect(error.exit_code).to eq(2) }
380
+ expect(File.exist?(File.join(dir, '.raider/schedules/nope'))).to be(false)
381
+ end
382
+
383
+ it 'refuses an entry that no longer validates' do
384
+ yml = File.join(dir, '.raider/schedules/schedules.yml')
385
+ File.write(yml, File.read(yml).sub(/os_id: .*/, 'os_id: dev.rubyraider.schedule.0000000000.nightly'))
386
+
387
+ expect { wrapper.run('nightly') }.to raise_error(RubyRaider::Schedules::Error)
388
+ expect(calls).to eq([])
389
+ end
390
+
391
+ it 'refuses an unknown trigger' do
392
+ expect { wrapper.run('nightly', trigger: 'desktop') }.to raise_error(RubyRaider::Schedules::Error, /trigger/)
393
+ end
394
+ end
395
+
396
+ describe 'scheduler.log' do
397
+ let(:log) { File.join(folder, 'scheduler.log') }
398
+
399
+ before do
400
+ FileUtils.mkdir_p(folder)
401
+ File.write("#{log}.1", 'older')
402
+ end
403
+
404
+ it 'rotates a scheduled run log over 1 MB to scheduler.log.1' do
405
+ File.write(log, 'x' * ((1024 * 1024) + 1))
406
+ wrapper.run('nightly', trigger: 'schedule')
407
+
408
+ expect([File.exist?(log), File.size("#{log}.1")]).to eq([false, (1024 * 1024) + 1])
409
+ end
410
+
411
+ it 'keeps a log of 1 MB or less, and any log on a manual run' do
412
+ File.write(log, 'x' * (1024 * 1024))
413
+ wrapper.run('nightly', trigger: 'schedule')
414
+ File.write(log, 'x' * ((1024 * 1024) + 1))
415
+ wrapper.run('nightly')
416
+
417
+ expect([File.size(log), File.read("#{log}.1")]).to eq([(1024 * 1024) + 1, 'older'])
418
+ end
419
+
420
+ it 'does not rotate a linked log' do
421
+ target = File.join(dir, 'big.log')
422
+ File.write(target, 'x' * ((1024 * 1024) + 1))
423
+ File.symlink(target, log)
424
+ wrapper.run('nightly', trigger: 'schedule')
425
+
426
+ expect([File.symlink?(log), File.read("#{log}.1")]).to eq([true, 'older'])
427
+ end
428
+ end
429
+ end
430
+ # rubocop:enable RSpec/MultipleMemoizedHelpers
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tmpdir'
4
+ require 'digest'
5
+ require_relative '../../../lib/utilities/schedules/schedule'
6
+
7
+ RSpec.describe RubyRaider::Schedules::Schedule do # rubocop:disable RSpec/FilePath
8
+ let(:dir) { File.realpath(Dir.mktmpdir('schedules-schedule')) }
9
+ let(:project) { RubyRaider::Schedules::Project.new(dir) }
10
+ let(:now) { Time.utc(2026, 11, 9, 10, 0, 0) }
11
+ let(:mac) { 'darwin24' }
12
+
13
+ before do
14
+ FileUtils.mkdir_p(File.join(dir, 'spec'))
15
+ File.write(File.join(dir, 'spec/a_spec.rb'), '')
16
+ end
17
+
18
+ after { FileUtils.rm_rf(dir) }
19
+
20
+ def build(**options)
21
+ described_class.build(project: project, name: 'nightly', every: 'day', at: '08:00', now: now, host_os: mac, **options)
22
+ end
23
+
24
+ def error_for(**options)
25
+ build(**options)
26
+ nil
27
+ rescue RubyRaider::Schedules::Error => e
28
+ e.message
29
+ end
30
+
31
+ def files(*names) = RubyRaider::Schedules::Selection.parse(files: names, tag: nil, root: dir, host_os: mac)
32
+
33
+ it 'keeps every field of the entry, with all tests by default' do
34
+ schedule = build(every: 'week', on: 'tue', retries: 2, notify: true, show_browser: true)
35
+ hash = schedule.to_h
36
+
37
+ expect(hash.keys).to eq(%w[name every at on retries notify show_browser selection os_id ruby bundle gem_env created_at])
38
+ expect(hash.slice('name', 'every', 'at', 'on', 'retries', 'notify', 'show_browser', 'selection', 'created_at'))
39
+ .to eq({ 'name' => 'nightly', 'every' => 'week', 'at' => '08:00', 'on' => 'tue', 'retries' => 2, 'notify' => true,
40
+ 'show_browser' => true, 'selection' => { 'kind' => 'all' }, 'created_at' => '2026-11-09T10:00:00Z' })
41
+ end
42
+
43
+ it 'names the os_id after the project and the name' do
44
+ expect(build.os_id).to eq("dev.rubyraider.schedule.#{Digest::SHA256.hexdigest(dir)[0, 10]}.nightly")
45
+ end
46
+
47
+ it 'runs raider schedule run NAME and nothing else from the OS entry' do
48
+ schedule = build(retries: 3, notify: true, show_browser: true, selection: files('spec/a_spec.rb'))
49
+
50
+ expect(schedule.argv).to eq([schedule.ruby, schedule.bundle, 'exec', 'raider', 'schedule', 'run', 'nightly', '--project', dir, '--trigger', 'schedule'])
51
+ end
52
+
53
+ it 'logs the scheduler output under .raider/schedules and keeps the 4.1 env' do
54
+ schedule = build
55
+
56
+ expect([schedule.log_path, schedule.env.keys.first(2), schedule.working_directory])
57
+ .to eq([File.join(dir, '.raider/schedules/nightly/scheduler.log'), %w[PATH LANG], dir])
58
+ end
59
+
60
+ it 'refuses show_browser on Linux and nowhere else' do
61
+ expect([error_for(show_browser: true, host_os: 'x86_64-linux-gnu'), error_for(show_browser: false, host_os: 'x86_64-linux-gnu'),
62
+ error_for(show_browser: true, host_os: 'mingw32')])
63
+ .to eq(["Show the browser isn't available for scheduled runs on Linux", nil, nil])
64
+ end
65
+
66
+ it 'refuses a bad name, show_browser or selection' do
67
+ errors = [error_for(name: 'Nightly'), error_for(name: 'a' * 61), error_for(show_browser: 'yes'), error_for(selection: { 'kind' => 'all' })]
68
+
69
+ expect(errors).to eq(['Invalid schedule name "Nightly": use 1 to 60 of a-z, 0-9 and _', "Invalid schedule name \"#{'a' * 61}\": use 1 to 60 of a-z, 0-9 and _",
70
+ 'Invalid show_browser "yes": use true or false', 'Invalid selection: use a Selection'])
71
+ end
72
+
73
+ it 'shares the rule checks with flow schedules' do
74
+ expect([error_for(every: 'month'), error_for(at: '8:00'), error_for(retries: 4), error_for(notify: 'yes')])
75
+ .to eq(['Invalid every "month": use hour, day, weekday or week', 'Invalid at "8:00": use HH:MM (24h)',
76
+ 'Invalid retries 4: use 0 to 3', 'Invalid notify "yes": use true or false'])
77
+ end
78
+
79
+ describe '.from_h' do
80
+ it 'reads back what to_h wrote' do
81
+ schedule = build(selection: files('spec/a_spec.rb'), show_browser: true)
82
+
83
+ expect(described_class.from_h(schedule.to_h, dir, host_os: mac).to_h).to eq(schedule.to_h)
84
+ end
85
+
86
+ it 'loads an entry whose spec file is gone' do
87
+ hash = build(selection: files('spec/a_spec.rb')).to_h
88
+ File.delete(File.join(dir, 'spec/a_spec.rb'))
89
+
90
+ expect(described_class.from_h(hash, dir, host_os: mac).selection.missing_file(dir)).to eq('spec/a_spec.rb')
91
+ end
92
+
93
+ it 'treats an entry without show_browser as off' do
94
+ expect(described_class.from_h(build.to_h.except('show_browser'), dir, host_os: mac).show_browser).to be(false)
95
+ end
96
+
97
+ it 'refuses an entry from another project, a flow entry, a broken selection or a non-mapping' do
98
+ other = File.realpath(Dir.mktmpdir('schedules-other'))
99
+ flow = build.to_h.merge('os_id' => build.os_id.sub('.schedule.', '.flow.'))
100
+ entries = [[build.to_h, other], [flow, dir], [build.to_h.merge('selection' => { 'kind' => 'tag', 'tag' => 'a;b' }), dir],
101
+ [build.to_h.except('selection'), dir], ['nightly', dir]]
102
+ errors = entries.map do |hash, root|
103
+ described_class.from_h(hash, root, host_os: mac)
104
+ nil
105
+ rescue RubyRaider::Schedules::Error => e
106
+ e.message
107
+ end
108
+
109
+ expect(errors).to all(be_a(String))
110
+ ensure
111
+ FileUtils.rm_rf(other)
112
+ end
113
+ end
114
+
115
+ it 'is frozen' do
116
+ schedule = build(selection: files('spec/a_spec.rb'))
117
+
118
+ expect([schedule, schedule.selection, schedule.name]).to all(be_frozen)
119
+ end
120
+ end