|
| 1 | +import static org.hamcrest.Matchers.containsString |
| 2 | +import static org.hamcrest.Matchers.endsWith |
1 | 3 | import static org.hamcrest.Matchers.equalTo |
2 | 4 | import static org.hamcrest.Matchers.hasItem |
3 | 5 | import static org.hamcrest.Matchers.instanceOf |
| 6 | +import static org.hamcrest.Matchers.not |
| 7 | +import static org.hamcrest.Matchers.startsWith |
4 | 8 | import static org.hamcrest.MatcherAssert.assertThat |
5 | 9 | import static org.mockito.Mockito.any |
6 | | -import static org.mockito.Mockito.doReturn; |
| 10 | +import static org.mockito.Mockito.doReturn |
7 | 11 | import static org.mockito.Mockito.eq |
8 | | -import static org.mockito.Mockito.mock; |
9 | | -import static org.mockito.Mockito.spy; |
10 | | -import static org.mockito.Mockito.verify; |
| 12 | +import static org.mockito.Mockito.mock |
| 13 | +import static org.mockito.Mockito.spy |
| 14 | +import static org.mockito.Mockito.times |
| 15 | +import static org.mockito.Mockito.verify |
11 | 16 |
|
12 | 17 | import org.junit.jupiter.api.Nested |
13 | 18 | import org.junit.jupiter.api.Test |
@@ -131,6 +136,81 @@ class FlywayMigrationPluginTest { |
131 | 136 |
|
132 | 137 | verify(mockWorkflowScript).withEnv(eq(expectedList), any(Closure.class)) |
133 | 138 | } |
| 139 | + |
| 140 | + @Test |
| 141 | + void runsConfirmMigrationIfConfirmBeforeApplyAndHasPendingMigration() { |
| 142 | + def plugin = spy(new FlywayMigrationPlugin()) |
| 143 | + doReturn(true).when(plugin).hasPendingMigration(any(Object.class)) |
| 144 | + FlywayMigrationPlugin.confirmBeforeApplyingMigration(true) |
| 145 | + |
| 146 | + def flywayClosure = plugin.flywayMigrateClosure() |
| 147 | + flywayClosure.delegate = new MockWorkflowScript() |
| 148 | + flywayClosure { -> } |
| 149 | + |
| 150 | + verify(plugin).confirmMigration(any(Object.class)) |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void doesNotRunConfirmMigrationIfNotConfirmBeforeApplyAndHasPendingMigration() { |
| 155 | + def plugin = spy(new FlywayMigrationPlugin()) |
| 156 | + doReturn(true).when(plugin).hasPendingMigration(any(Object.class)) |
| 157 | + FlywayMigrationPlugin.confirmBeforeApplyingMigration(false) |
| 158 | + |
| 159 | + def flywayClosure = plugin.flywayMigrateClosure() |
| 160 | + flywayClosure.delegate = new MockWorkflowScript() |
| 161 | + flywayClosure { -> } |
| 162 | + |
| 163 | + verify(plugin, times(0)).confirmMigration(any(Object.class)) |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + void doesNotRunConfirmMigrationIfConfirmBeforeApplyAndDoesNotHavePendingMigration() { |
| 168 | + def plugin = spy(new FlywayMigrationPlugin()) |
| 169 | + doReturn(false).when(plugin).hasPendingMigration(any(Object.class)) |
| 170 | + FlywayMigrationPlugin.confirmBeforeApplyingMigration(true) |
| 171 | + |
| 172 | + def flywayClosure = plugin.flywayMigrateClosure() |
| 173 | + flywayClosure.delegate = new MockWorkflowScript() |
| 174 | + flywayClosure { -> } |
| 175 | + |
| 176 | + verify(plugin, times(0)).confirmMigration(any(Object.class)) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Nested |
| 181 | + public class HasPendingMigration { |
| 182 | + @Test |
| 183 | + void returnsTrueWhenShellReturnsTrueString() { |
| 184 | + def plugin = new FlywayMigrationPlugin() |
| 185 | + def workflowScript = spy(new MockWorkflowScript()) |
| 186 | + doReturn('true').when(workflowScript).sh(any(Map.class)) |
| 187 | + |
| 188 | + def result = plugin.hasPendingMigration(workflowScript) |
| 189 | + |
| 190 | + assertThat(result, equalTo(true)) |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + void returnsFalseWhenShellReturnsFalseString() { |
| 195 | + def plugin = new FlywayMigrationPlugin() |
| 196 | + def workflowScript = spy(new MockWorkflowScript()) |
| 197 | + doReturn('false').when(workflowScript).sh(any(Map.class)) |
| 198 | + |
| 199 | + def result = plugin.hasPendingMigration(workflowScript) |
| 200 | + |
| 201 | + assertThat(result, equalTo(false)) |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + void returnsFalseWhenShellReturnsAnyOtherString() { |
| 206 | + def plugin = new FlywayMigrationPlugin() |
| 207 | + def workflowScript = spy(new MockWorkflowScript()) |
| 208 | + doReturn('blahblah').when(workflowScript).sh(any(Map.class)) |
| 209 | + |
| 210 | + def result = plugin.hasPendingMigration(workflowScript) |
| 211 | + |
| 212 | + assertThat(result, equalTo(false)) |
| 213 | + } |
134 | 214 | } |
135 | 215 |
|
136 | 216 | @Nested |
@@ -162,28 +242,119 @@ class FlywayMigrationPluginTest { |
162 | 242 | @Nested |
163 | 243 | public class BuildFlywayCommand { |
164 | 244 | @Test |
165 | | - void disablesEchoBeforeFlywayAndEnablesEchoAfterByDefault() { |
| 245 | + void constructsTheFlywayCommand() { |
| 246 | + def flywayCommand = 'flyway foo' |
| 247 | + def command = mock(FlywayCommand.class) |
| 248 | + doReturn(flywayCommand).when(command).toString() |
| 249 | + def plugin = new FlywayMigrationPlugin() |
| 250 | + |
| 251 | + def result = plugin.buildFlywayCommand(command) |
| 252 | + |
| 253 | + assertThat(result, containsString(flywayCommand)) |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + void disablesEchoBeforeFlywayByDefault() { |
166 | 258 | def flywayCommand = 'flyway foo' |
167 | 259 | def command = mock(FlywayCommand.class) |
168 | 260 | doReturn(flywayCommand).when(command).toString() |
169 | 261 | def plugin = new FlywayMigrationPlugin() |
170 | 262 |
|
171 | 263 | def result = plugin.buildFlywayCommand(command) |
172 | 264 |
|
173 | | - assertThat(result, equalTo("set +x\n${flywayCommand}\nset -x".toString())) |
| 265 | + assertThat(result, startsWith("set +x")) |
174 | 266 | } |
175 | 267 |
|
176 | 268 | @Test |
177 | | - void returnsTheCommandIfEchoEnabled() { |
| 269 | + void disablesEchoAfterFlywayByDefault() { |
178 | 270 | def flywayCommand = 'flyway foo' |
179 | 271 | def command = mock(FlywayCommand.class) |
180 | 272 | doReturn(flywayCommand).when(command).toString() |
181 | 273 | def plugin = new FlywayMigrationPlugin() |
182 | | - FlywayMigrationPlugin.withEchoEnabled() |
183 | 274 |
|
184 | 275 | def result = plugin.buildFlywayCommand(command) |
185 | 276 |
|
186 | | - assertThat(result, equalTo(flywayCommand)) |
| 277 | + assertThat(result, endsWith("set -x")) |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + void prefixesFlywayCommandWithPipelineFail() { |
| 282 | + def plugin = spy(new FlywayMigrationPlugin()) |
| 283 | + |
| 284 | + def result = plugin.buildFlywayCommand(mock(FlywayCommand.class)) |
| 285 | + |
| 286 | + assertThat(result, containsString("set -o pipefail")) |
| 287 | + } |
| 288 | + |
| 289 | + @Test |
| 290 | + void pipesFlywayCommandToFileWithTee() { |
| 291 | + def plugin = spy(new FlywayMigrationPlugin()) |
| 292 | + |
| 293 | + def result = plugin.buildFlywayCommand(mock(FlywayCommand.class)) |
| 294 | + |
| 295 | + assertThat(result, containsString("| tee flyway_output.txt")) |
| 296 | + } |
| 297 | + |
| 298 | + @Nested |
| 299 | + public class WithEchoEnabled { |
| 300 | + @Test |
| 301 | + void doesNotDisbleEchoBeforeFlyway() { |
| 302 | + def flywayCommand = 'flyway foo' |
| 303 | + def command = mock(FlywayCommand.class) |
| 304 | + doReturn(flywayCommand).when(command).toString() |
| 305 | + def plugin = new FlywayMigrationPlugin() |
| 306 | + |
| 307 | + FlywayMigrationPlugin.withEchoEnabled() |
| 308 | + def result = plugin.buildFlywayCommand(command) |
| 309 | + |
| 310 | + assertThat(result, not(startsWith("set +x"))) |
| 311 | + } |
| 312 | + |
| 313 | + @Test |
| 314 | + void doesNotReenbleEchoAfterFlyway() { |
| 315 | + def flywayCommand = 'flyway foo' |
| 316 | + def command = mock(FlywayCommand.class) |
| 317 | + doReturn(flywayCommand).when(command).toString() |
| 318 | + def plugin = new FlywayMigrationPlugin() |
| 319 | + |
| 320 | + FlywayMigrationPlugin.withEchoEnabled() |
| 321 | + def result = plugin.buildFlywayCommand(command) |
| 322 | + |
| 323 | + assertThat(result, not(endsWith("set -x"))) |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + @Nested |
| 328 | + public class WithConfirmBeforeApplyingMigrationDisabled { |
| 329 | + @Test |
| 330 | + void doesNotPrefixFlywayCommandWithPipelineFail() { |
| 331 | + def plugin = spy(new FlywayMigrationPlugin()) |
| 332 | + FlywayMigrationPlugin.confirmBeforeApplyingMigration(false) |
| 333 | + |
| 334 | + def result = plugin.buildFlywayCommand(mock(FlywayCommand.class)) |
| 335 | + |
| 336 | + assertThat(result, not(containsString("set -o pipefail"))) |
| 337 | + } |
| 338 | + |
| 339 | + @Test |
| 340 | + void doesNotPipeFlywayCommandToFileWthTee() { |
| 341 | + def plugin = spy(new FlywayMigrationPlugin()) |
| 342 | + FlywayMigrationPlugin.confirmBeforeApplyingMigration(false) |
| 343 | + |
| 344 | + def result = plugin.buildFlywayCommand(mock(FlywayCommand.class)) |
| 345 | + |
| 346 | + assertThat(result, not(containsString("| tee flyway_output.txt"))) |
| 347 | + } |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + @Nested |
| 352 | + public class ConfirmBeforeApplyingMigration { |
| 353 | + @Test |
| 354 | + void isFluent() { |
| 355 | + def result = FlywayMigrationPlugin.confirmBeforeApplyingMigration() |
| 356 | + |
| 357 | + assertThat(result, equalTo(FlywayMigrationPlugin.class)) |
187 | 358 | } |
188 | 359 | } |
189 | 360 | } |
|
0 commit comments