|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe 'building all books' do |
| 4 | + describe '--sub_dir' do |
| 5 | + ## |
| 6 | + # Setups up a repo that looks like: |
| 7 | + # master sub_me |
| 8 | + # original master-------->subbed |
| 9 | + # | |
| 10 | + # new master |
| 11 | + # | |
| 12 | + # too new master |
| 13 | + # |
| 14 | + # Optionally runs the build once on the commit "new master". Then it always |
| 15 | + # runs the build, substituting sub_me for the master branch. If: |
| 16 | + # * --keep_hash isn't specified or |
| 17 | + # * the sub_me branch has outstanding changes or |
| 18 | + # * there was a merge conflict or |
| 19 | + # * the build wasn't run against "new master" |
| 20 | + # then --sub_dir will pick up the contents of the directory and the build |
| 21 | + # won't have "new maser" because it was forked from "original master". |
| 22 | + # Otherwise, --keep_hash and --sub_dir will cause the build to merge |
| 23 | + # "new master" and "subbed" and build against *that*. |
| 24 | + def self.convert_with_sub(keep_hash: true, commit_sub: true, |
| 25 | + build_with_init: true, |
| 26 | + cause_merge_conflict: false, premerge: false) |
| 27 | + convert_before do |src, dest| |
| 28 | + repo = setup_repo src |
| 29 | + setup_book src, repo |
| 30 | + dest.prepare_convert_all(src.conf).convert if build_with_init |
| 31 | + modify_master_after_build repo |
| 32 | + setup_sub repo, commit_sub, cause_merge_conflict, premerge |
| 33 | + convert src, repo, dest, keep_hash |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def self.setup_repo(src) |
| 38 | + repo = src.repo 'repo' |
| 39 | + repo.write 'docs/index.adoc', index |
| 40 | + repo.write 'docs/from_master.adoc', 'original master' |
| 41 | + repo.write 'docs/from_subbed.adoc', 'unsubbed' |
| 42 | + repo.commit 'original master' |
| 43 | + repo.write 'docs/from_master.adoc', 'new master' |
| 44 | + repo.write 'docs/conflict', 'from master' |
| 45 | + repo.commit 'new master' |
| 46 | + repo |
| 47 | + end |
| 48 | + |
| 49 | + def self.setup_book(src, repo) |
| 50 | + book = src.book 'Test' |
| 51 | + book.index = 'docs/index.adoc' |
| 52 | + book.source repo, 'docs' |
| 53 | + end |
| 54 | + |
| 55 | + def self.modify_master_after_build(repo) |
| 56 | + repo.write 'docs/from_master.adoc', 'too new master' |
| 57 | + repo.commit 'too new master' |
| 58 | + end |
| 59 | + |
| 60 | + def self.setup_sub(repo, commit_sub, cause_merge_conflict, premerge) |
| 61 | + repo.switch_to_branch 'HEAD~2' |
| 62 | + repo.switch_to_new_branch 'sub_me' |
| 63 | + repo.write 'docs/from_subbed.adoc', 'now subbed' |
| 64 | + repo.write 'docs/conflict', 'from subbed' if cause_merge_conflict |
| 65 | + repo.commit 'subbed' if commit_sub |
| 66 | + repo.merge 'master' if premerge |
| 67 | + end |
| 68 | + |
| 69 | + def self.convert(src, repo, dest, keep_hash) |
| 70 | + builder = dest.prepare_convert_all src.conf |
| 71 | + builder.sub_dir repo, 'master' |
| 72 | + builder.keep_hash if keep_hash |
| 73 | + builder.convert |
| 74 | + dest.checkout_conversion |
| 75 | + end |
| 76 | + |
| 77 | + def self.index |
| 78 | + <<~ASCIIDOC |
| 79 | + = Title |
| 80 | +
|
| 81 | + [[chapter]] |
| 82 | + == Chapter |
| 83 | +
|
| 84 | + include::from_master.adoc[] |
| 85 | + include::from_subbed.adoc[] |
| 86 | + ASCIIDOC |
| 87 | + end |
| 88 | + |
| 89 | + let(:logs) { outputs[-1] } |
| 90 | + |
| 91 | + shared_examples 'examples' do |master| |
| 92 | + file_context 'raw/test/master/chapter.html' do |
| 93 | + it "contains the #{master} master changes" do |
| 94 | + expect(contents).to include("<p>#{master} master</p>") |
| 95 | + end |
| 96 | + it 'contains the subbed changes' do |
| 97 | + expect(contents).to include('<p>now subbed</p>') |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + shared_examples 'contains the original master and subbed changes' do |
| 102 | + include_examples 'examples', 'original' |
| 103 | + end |
| 104 | + shared_examples 'contains the new master and subbed changes' do |
| 105 | + include_examples 'examples', 'new' |
| 106 | + end |
| 107 | + shared_examples 'contains the too new master and subbed changes' do |
| 108 | + include_examples 'examples', 'too new' |
| 109 | + end |
| 110 | + shared_examples 'log merge' do |path| |
| 111 | + it "log that it started merging [#{path}]" do |
| 112 | + expect(logs).to include(<<~LOGS) |
| 113 | + Test: Merging the subbed dir for [repo][master][#{path}] into the last successful build. |
| 114 | + LOGS |
| 115 | + end |
| 116 | + it "log that it merged [#{path}]" do |
| 117 | + expect(logs).to include(<<~LOGS) |
| 118 | + Test: Merged the subbed dir for [repo][master][#{path}] into the last successful build. |
| 119 | + LOGS |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + describe 'without --keep_hash' do |
| 124 | + convert_with_sub keep_hash: false |
| 125 | + it "doesn't log that it won't merge because of uncommitted changes" do |
| 126 | + expect(logs).not_to include(<<~LOGS) |
| 127 | + Test: Not merging the subbed dir for [repo][master][docs] because it has uncommitted changes. |
| 128 | + LOGS |
| 129 | + end |
| 130 | + include_examples 'contains the original master and subbed changes' |
| 131 | + end |
| 132 | + describe 'with --keep_hash' do |
| 133 | + describe 'when there are uncommitted changes' do |
| 134 | + convert_with_sub commit_sub: false |
| 135 | + it "logs that it won't merge because of uncommitted changes" do |
| 136 | + expect(logs).to include(<<~LOGS) |
| 137 | + Test: Not merging the subbed dir for [repo][master][docs] because it has uncommitted changes. |
| 138 | + LOGS |
| 139 | + end |
| 140 | + include_examples 'contains the original master and subbed changes' |
| 141 | + end |
| 142 | + describe 'when the source is new' do |
| 143 | + convert_with_sub build_with_init: false |
| 144 | + it "log that it won't merge because the source is new" do |
| 145 | + expect(logs).to include(<<~LOGS) |
| 146 | + Test: Not merging the subbed dir for [repo][master][docs] because it is new. |
| 147 | + LOGS |
| 148 | + end |
| 149 | + include_examples 'contains the original master and subbed changes' |
| 150 | + end |
| 151 | + describe 'when the subbed dir can be merged' do |
| 152 | + convert_with_sub |
| 153 | + include_examples 'log merge', 'docs' |
| 154 | + include_examples 'contains the new master and subbed changes' |
| 155 | + end |
| 156 | + describe 'when the subbed dir has already been merged' do |
| 157 | + # This simulates what github will do if you ask it to build the "sha" |
| 158 | + # of the merged PR instead of the "head" of the branch. |
| 159 | + convert_with_sub premerge: true |
| 160 | + include_examples 'log merge', 'docs' |
| 161 | + include_examples 'contains the too new master and subbed changes' |
| 162 | + end |
| 163 | + describe 'when there is a conflict merging the subbed dir' do |
| 164 | + convert_with_sub cause_merge_conflict: true |
| 165 | + it 'logs that it failed to merge' do |
| 166 | + expect(logs).to include(<<~LOGS) |
| 167 | + Test: Failed to merge the subbed dir for [repo][master][docs] into the last successful build: |
| 168 | + LOGS |
| 169 | + end |
| 170 | + it 'logs the conflict' do |
| 171 | + expect(logs).to include(<<~LOGS) |
| 172 | + CONFLICT (add/add): Merge conflict in docs/conflict |
| 173 | + LOGS |
| 174 | + end |
| 175 | + include_examples 'contains the original master and subbed changes' |
| 176 | + end |
| 177 | + describe 'when there is more than one source using the same repo' do |
| 178 | + def self.setup_book(src, repo) |
| 179 | + book = src.book 'Test' |
| 180 | + book.index = 'docs/index.adoc' |
| 181 | + book.source repo, 'docs/index.adoc' |
| 182 | + book.source repo, 'docs/from_master.adoc' |
| 183 | + book.source repo, 'docs/from_subbed.adoc' |
| 184 | + end |
| 185 | + convert_with_sub |
| 186 | + include_examples 'log merge', 'docs/index.adoc' |
| 187 | + include_examples 'log merge', 'docs/from_master.adoc' |
| 188 | + include_examples 'log merge', 'docs/from_subbed.adoc' |
| 189 | + include_examples 'contains the new master and subbed changes' |
| 190 | + end |
| 191 | + describe 'when more than one book uses the same source' do |
| 192 | + def self.setup_book(src, repo) |
| 193 | + %w[Test Test2].each do |name| |
| 194 | + book = src.book name |
| 195 | + book.index = 'docs/index.adoc' |
| 196 | + book.source repo, 'docs' |
| 197 | + end |
| 198 | + end |
| 199 | + convert_with_sub |
| 200 | + include_examples 'log merge', 'docs' |
| 201 | + it 'logs only one merge' do |
| 202 | + # This asserts that we log a single merge. We *should* be using the |
| 203 | + # cache instead. |
| 204 | + expect(logs).not_to match(/Merged.+Merged/m) |
| 205 | + end |
| 206 | + include_examples 'contains the new master and subbed changes' |
| 207 | + end |
| 208 | + end |
| 209 | + end |
| 210 | +end |
0 commit comments