4
4
# Assertions about when books are rebuilt based on changes in source
5
5
# repositories or the book's configuration.
6
6
RSpec . describe 'building all books' do
7
+ class Config
8
+ attr_accessor :target_branch
9
+
10
+ def initialize
11
+ @target_branch = nil
12
+ end
13
+ end
7
14
describe 'change detection' do
8
15
def self . build_twice (
9
16
before_first_build :,
10
- before_second_build : -> ( src ) { }
17
+ before_second_build :
11
18
)
12
19
convert_before do |src , dest |
20
+ config = Config . new
13
21
# Allow the caller to customize the source.
14
- before_first_build . call ( src )
22
+ before_first_build . call ( src , config )
15
23
16
24
# Convert the first time. This should build the docs.
17
- dest . convert_all src . conf
25
+ dest . convert_all src . conf , target_branch : config . target_branch
18
26
19
27
# Take some action between the builds.
20
- before_second_build . call ( src )
28
+ before_second_build . call ( src , config )
21
29
22
30
# Convert the second time.
23
- dest . convert_all src . conf
31
+ dest . convert_all src . conf , target_branch : config . target_branch
24
32
25
33
# Checkout the files so we can assert about them.
26
- dest . checkout_conversion
34
+ dest . checkout_conversion branch : config . target_branch
27
35
end
28
36
include_context 'build one book twice'
29
37
end
30
38
31
39
def self . build_one_book_out_of_one_repo_twice (
32
- before_first_build : -> ( src ) { } ,
33
- before_second_build : -> ( src ) { }
40
+ before_first_build : -> ( src , config ) { } ,
41
+ before_second_build : -> ( src , config ) { }
34
42
)
35
43
build_twice (
36
- before_first_build : lambda do |src |
44
+ before_first_build : lambda do |src , config |
37
45
repo = src . repo_with_index 'repo' , 'Some text.'
38
46
book = src . book 'Test'
39
47
book . source repo , 'index.asciidoc'
40
48
41
49
# Allow the caller to customize the source
42
- before_first_build . call ( src )
50
+ before_first_build . call src , config
43
51
end ,
44
52
before_second_build : before_second_build
45
53
)
@@ -50,13 +58,15 @@ def self.build_one_book_out_of_two_repos_twice(
50
58
before_second_build : -> ( src ) { }
51
59
)
52
60
build_twice (
53
- before_first_build : lambda do |src |
61
+ before_first_build : lambda do |src , _config |
54
62
src . simple_include
55
63
56
64
# Allow the caller to customize the source
57
- before_first_build . call ( src )
65
+ before_first_build . call src
58
66
end ,
59
- before_second_build : before_second_build
67
+ before_second_build : lambda do |src , _config |
68
+ before_second_build . call src
69
+ end
60
70
)
61
71
end
62
72
@@ -100,25 +110,51 @@ def self.build_one_book_out_of_two_repos_twice(
100
110
build_one_book_out_of_one_repo_twice
101
111
include_examples 'second build is noop'
102
112
end
103
-
104
- context 'because there are unrelated changes source repo' do
113
+ context 'even when there are unrelated changes source repo' do
105
114
build_one_book_out_of_one_repo_twice (
106
- before_second_build : lambda do |src |
115
+ before_second_build : lambda do |src , _config |
107
116
repo = src . repo 'repo'
108
117
repo . write 'garbage' , 'junk'
109
118
repo . commit 'adding junk'
110
119
end
111
120
)
112
121
include_examples 'second build is noop'
113
122
end
123
+ context 'even when there is a target_branch' do
124
+ build_one_book_out_of_one_repo_twice (
125
+ before_first_build : lambda do |_src , config |
126
+ config . target_branch = 'new_target'
127
+ end
128
+ )
129
+ include_examples 'second build is noop'
130
+ end
131
+ context 'even when there is a new target branch' do
132
+ # Adding a new target branch will cause us to fork it from the
133
+ # master branch which so we won't have to rebuild the book *but*
134
+ # we push anyway so the new target branch is available.
135
+ build_one_book_out_of_one_repo_twice (
136
+ before_second_build : lambda do |_src , config |
137
+ config . target_branch = 'new_target'
138
+ end
139
+ )
140
+ context 'the second build' do
141
+ let ( :out ) { outputs [ 1 ] }
142
+ it "doesn't print that it is building any books" do
143
+ expect ( out ) . not_to include ( ': Building ' )
144
+ end
145
+ it "doesn't print that it is commiting changes" do
146
+ expect ( out ) . not_to include ( 'Commiting changes' )
147
+ end
148
+ it 'prints that it is pushing changes' do
149
+ expect ( out ) . to include ( 'Pushing changes' )
150
+ end
151
+ end
152
+ end
114
153
end
115
154
context "when the second build isn't a noop" do
116
- let ( :latest_revision ) { 'changed text' }
117
- let ( :new_text ) { 'New text.' }
118
-
119
155
context 'because the source repo changes' do
120
156
build_one_book_out_of_one_repo_twice (
121
- before_second_build : lambda do |src |
157
+ before_second_build : lambda do |src , _config |
122
158
repo = src . repo 'repo'
123
159
repo . write 'index.asciidoc' , <<~ASCIIDOC
124
160
= Title
@@ -130,35 +166,49 @@ def self.build_one_book_out_of_two_repos_twice(
130
166
repo . commit 'changed text'
131
167
end
132
168
)
169
+ let ( :latest_revision ) { 'changed text' }
170
+ let ( :new_text ) { 'New text.' }
133
171
include_examples 'second build is not a noop'
134
172
end
135
173
context 'because the book changes from asciidoc to asciidoctor' do
136
174
build_one_book_out_of_one_repo_twice (
137
- before_first_build : lambda do |src |
175
+ before_first_build : lambda do |src , _config |
138
176
book = src . book 'Test'
139
177
book . asciidoctor = false
140
178
end ,
141
- before_second_build : lambda do |src |
179
+ before_second_build : lambda do |src , _config |
142
180
book = src . book 'Test'
143
181
book . asciidoctor = true
144
182
end
145
183
)
146
- # We didn't change the revision....
147
184
let ( :latest_revision ) { 'init' }
148
- # And the text hasn't changed
149
185
let ( :new_text ) { 'Some text.' }
150
186
include_examples 'second build is not a noop'
151
187
end
152
188
context 'because the book changes from asciidoctor to asciidoc' do
153
189
build_one_book_out_of_one_repo_twice (
154
- before_second_build : lambda do |src |
190
+ before_second_build : lambda do |src , _config |
155
191
book = src . book 'Test'
156
192
book . asciidoctor = false
157
193
end
158
194
)
159
- # We didn't change the revision....
160
195
let ( :latest_revision ) { 'init' }
161
- # And the text hasn't changed
196
+ let ( :new_text ) { 'Some text.' }
197
+ include_examples 'second build is not a noop'
198
+ end
199
+ context 'because we remove the target_branch' do
200
+ # Removing the target branch causes us to build into the *empty*
201
+ # master branch. Being empty, there aren't any books in it to
202
+ # consider "already built".
203
+ build_one_book_out_of_one_repo_twice (
204
+ before_first_build : lambda do |_src , config |
205
+ config . target_branch = 'new_target'
206
+ end ,
207
+ before_second_build : lambda do |_src , config |
208
+ config . target_branch = nil # nil means don't override
209
+ end
210
+ )
211
+ let ( :latest_revision ) { 'init' }
162
212
let ( :new_text ) { 'Some text.' }
163
213
include_examples 'second build is not a noop'
164
214
end
0 commit comments