Repository navigation
Expand file tree
/
Copy pathsemantic_ruby_gem_spec.rb
More file actions
150 lines (136 loc) · 7.1 KB
/
Copy pathsemantic_ruby_gem_spec.rb
File metadata and controls
150 lines (136 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
require 'spec_helper'
require 'ruby-gem-release/workflow/semantic_ruby_gem'
RSpec.describe RubyGemRelease::Workflow::SemanticRubyGem do
let(:values) do
{ 'INPUT_VERSION' => '', 'SOURCE_COMMIT' => 'a' * 40, 'GEMSPEC_VERSION' => '9.9.9',
'RELEASE_VERSION' => '1.3.0', 'RELEASE_DATE' => '2026-11-09' }
end
let(:provider) { RubyGemRelease::ExecutionProvider.new(environment: values) }
let(:components) do
{
version: instance_double(RubyGemRelease::Version::RubyConstant, read: '1.3.0', set!: nil, increment!: '1.3.1', next_version: '1.3.1'),
git: instance_double(RubyGemRelease::SourceControl::Git, commit!: nil, tag!: nil, push!: nil, commit_next!: nil),
package: instance_double(RubyGemRelease::Package::RubyGem, build!: nil, built?: false),
destination: instance_double(RubyGemRelease::Destination, authenticate!: nil, publish!: nil),
changelog: instance_double(RubyGemRelease::Changelog::Generated, release!: nil, prepare_next!: nil)
}
end
let(:semantic) { instance_double(RubyGemRelease::SemanticRelease, analyze: decision) }
let(:decision) do
RubyGemRelease::SemanticRelease::Decision.new(head: 'a' * 40, last_release: { 'tag' => 'v1.2.3', 'version' => '1.2.3', 'commit' => 'b' * 40 },
commits: [], release_type: 'minor', version: '1.3.0', notes: 'Generated notes.')
end
around do |example|
original = Rake.application
Rake.application = Rake::Application.new
example.run
ensure
Rake.application = original
end
before do
allow(components.fetch(:git)).to receive(:stage_changes!).and_yield
end
def install(**options)
workflow = described_class.new(**components, semantic_release: semantic, provider: provider, hosting: nil, **options)
yield workflow if block_given?
workflow.install_tasks
end
def invoke(*tasks)
tasks.each { |task| Rake::Task["release:#{task}"].invoke }
end
it 'analyzes once, sets the selected version rather than the development version, and defaults to no bump' do
received = nil
install(success: ->(ctx) { received = ctx })
invoke('validate')
Rake::Task['release:validate'].reenable
invoke('validate', 'prepare', 'publish', 'bump', 'success')
expect(semantic).to have_received(:analyze).once
expect(components.fetch(:version)).to have_received(:set!).with('1.3.0')
expect(components.fetch(:changelog)).to have_received(:release!).with(have_attributes(version: '1.3.0', gemspec_version: '9.9.9', notes: decision.notes))
expect(components.fetch(:version)).not_to have_received(:increment!)
expect(components.fetch(:git)).not_to have_received(:commit_next!)
expect(received).to have_attributes(analysis: have_attributes(version: '1.3.0'), release: have_attributes(version: '1.3.0', gemspec_version: '9.9.9', next_version: nil))
end
it 'bumps the configured next version part when bumping is enabled' do
install(bump: true, next_version: :minor)
invoke('validate', 'prepare', 'publish', 'bump')
expect(components.fetch(:version)).to have_received(:increment!).with(from: '1.3.0', part: :minor)
end
it 'optionally bumps and records the next development version without replacing analysis' do
received = nil
prepared = nil
install(bump: true, success: ->(ctx) { received = ctx }) do |workflow|
workflow.after(:prepare_next) { |ctx| prepared = ctx }
end
invoke('validate', 'prepare', 'publish', 'bump', 'success')
expect(components.fetch(:version)).to have_received(:increment!)
expect(received.release.next_version).to eq('1.3.1')
expect(received.analysis.to_h).to eq(decision.to_h)
expect(prepared).to have_attributes(analysis: decision, release: have_attributes(next_version: '1.3.1'))
end
it 'rejects a no-release decision without consulting handwritten notes or invoking downstream operations' do
skipped = RubyGemRelease::SemanticRelease::Decision.new(head: 'a' * 40, last_release: nil, commits: [], release_type: nil, version: nil, notes: nil)
allow(semantic).to receive(:analyze).and_return(skipped)
install
expect { invoke('validate') }.to raise_error(ArgumentError, /Nothing to release/)
Rake::Task['release:validate'].reenable
expect { invoke('validate') }.to raise_error(ArgumentError, /Nothing to release/)
expect(semantic).to have_received(:analyze).once
%w[prepare publish bump success].each do |task|
expect { invoke(task) }.to raise_error(ArgumentError, /semantic/)
end
expect(components.fetch(:destination)).not_to have_received(:authenticate!)
expect(components.fetch(:version)).not_to have_received(:set!)
expect(components.fetch(:package)).not_to have_received(:build!)
expect(components.fetch(:version)).not_to have_received(:increment!)
end
it 'rejects selected-version, initial-commit, and explicit-input changes before publication or authentication' do
install
invoke('validate')
[
['RELEASE_VERSION', '1.3.1', /differs/],
['SOURCE_COMMIT', 'c' * 40, /another commit/],
['INPUT_VERSION', '2.0.0', /explicit version changed/]
].each do |key, changed, message|
original = values[key]
values[key] = changed
Rake::Task['release:publish'].reenable
expect { invoke('publish') }.to raise_error(ArgumentError, message)
values[key] = original
end
expect(components.fetch(:destination)).not_to have_received(:authenticate!)
end
it 'rejects preparation before analysis and preserves failure callbacks when state is corrupted' do
received = nil
install(failure: ->(failure) { received = failure })
expect { invoke('prepare') }.to raise_error(ArgumentError, /persisted release decision/)
Dir.mktmpdir do |directory|
path = File.join(directory, 'state.json')
File.write(path, '{')
values['RELEASE_STATE_FILE'] = path
invoke('failure')
expect(received.context.analysis).to be_nil
expect(received.context.release).to have_attributes(version: '1.3.0')
end
end
it 'supports disabling changelog updates and rejects invalid bump policies' do
install(changelog: nil)
invoke('validate', 'prepare')
expect(components.fetch(:version)).to have_received(:set!).with('1.3.0')
expect(components.fetch(:changelog)).not_to have_received(:release!)
expect { described_class.new(version: components.fetch(:version), bump: :patch) }.to raise_error(ArgumentError, /bump/)
end
it 'exposes the selected decision after validation and shares preparation hooks without enabling development bumps' do
received = []
workflow = described_class.new(**components, semantic_release: semantic, provider: provider, hosting: nil)
%i[validate prepare_release prepare_next commit_next push_next].each do |stage|
workflow.after(stage) { |ctx| received << ctx }
end
workflow.install_tasks
invoke('validate', 'prepare', 'publish', 'bump')
expect(received.map(&:stage)).to eq(%i[validate prepare_release])
expect(received).to all(have_attributes(analysis: decision, release: have_attributes(version: '1.3.0', notes: 'Generated notes.')))
expect(semantic).to have_received(:analyze).once
end
end