|
| 1 | +/* |
| 2 | + * Copyright (C) 2019-2021 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.changelog.gradle; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Paths; |
| 24 | +import org.eclipse.jgit.api.Git; |
| 25 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 26 | +import org.eclipse.jgit.revwalk.RevWalk; |
| 27 | +import org.junit.Rule; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.rules.TemporaryFolder; |
| 30 | +import org.junit.rules.TestWatcher; |
| 31 | +import org.junit.runner.Description; |
| 32 | + |
| 33 | +public class ChangelogPluginPushTest extends GradleHarness { |
| 34 | + |
| 35 | + @Rule |
| 36 | + public DeleteOnSuccessTemporaryFolder temporaryFolder = new DeleteOnSuccessTemporaryFolder(new File("build")); |
| 37 | + @Rule |
| 38 | + public KeepTempFolderOnFailure keepTempFolderOnFailure = new KeepTempFolderOnFailure(temporaryFolder); |
| 39 | + |
| 40 | + @Test |
| 41 | + public void verifyAnnotatedTagMessage() throws IOException, GitAPIException { |
| 42 | + |
| 43 | + final String testCaseFolder = "1.0.3-annotatedTag"; |
| 44 | + File origin = initUpstreamRepo(testCaseFolder); |
| 45 | + |
| 46 | + final File working = temporaryFolder.newFolder("working"); |
| 47 | + Git git = Git.cloneRepository().setURI(origin.getAbsolutePath()) |
| 48 | + .setDirectory(working).call(); |
| 49 | + |
| 50 | + copyResourceFile(working, testCaseFolder, "CHANGELOG.md"); |
| 51 | + |
| 52 | + gradleRunner().withProjectDir(working)/*.withDebug(true)*/ |
| 53 | + .withArguments("changelogPush").build(); |
| 54 | + |
| 55 | + assertEquals("Version is 1.0.3, here are the changes:" |
| 56 | + + "\n\n### Fixed\n" |
| 57 | + + "- this should be in tag message\n", |
| 58 | + annotatedTagMessage(git, "release/1.0.3")); |
| 59 | + } |
| 60 | + |
| 61 | + private String annotatedTagMessage(Git localGit, final String tagName) throws IOException { |
| 62 | + try (RevWalk walk = new RevWalk(localGit.getRepository())) { |
| 63 | + return walk.parseTag( |
| 64 | + localGit.getRepository().findRef(tagName).getObjectId()) |
| 65 | + .getFullMessage(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private File initUpstreamRepo(String testCaseFolder) throws IOException, GitAPIException { |
| 70 | + File origin = temporaryFolder.newFolder("origin"); |
| 71 | + Git git = Git.init().setDirectory(origin).call(); |
| 72 | + copyResourceFile(origin, "settings.gradle"); |
| 73 | + copyResourceFile(origin, testCaseFolder, "build.gradle"); |
| 74 | + git.add().addFilepattern(".").call(); |
| 75 | + git.commit() |
| 76 | + .setMessage("Commit all changes including additions") |
| 77 | + .call(); |
| 78 | + return origin; |
| 79 | + } |
| 80 | + |
| 81 | + private void copyResourceFile(File working, String testCaseFolder, String fileName) throws IOException { |
| 82 | + Files.copy(Paths.get("src/test/resources", testCaseFolder, fileName), working.toPath().resolve(fileName)); |
| 83 | + } |
| 84 | + |
| 85 | + private void copyResourceFile(File working, String fileName) throws IOException { |
| 86 | + Files.copy(Paths.get("src/test/resources", fileName), working.toPath().resolve(fileName)); |
| 87 | + } |
| 88 | + |
| 89 | + static class KeepTempFolderOnFailure extends TestWatcher { |
| 90 | + private final DeleteOnSuccessTemporaryFolder folder; |
| 91 | + |
| 92 | + KeepTempFolderOnFailure(DeleteOnSuccessTemporaryFolder folder) { |
| 93 | + this.folder = folder; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected void failed(Throwable e, Description description) { |
| 98 | + folder.disableDeletion(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + static class DeleteOnSuccessTemporaryFolder extends TemporaryFolder { |
| 103 | + |
| 104 | + boolean canDelete = true; |
| 105 | + |
| 106 | + public DeleteOnSuccessTemporaryFolder(File file) { |
| 107 | + super(file); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected void after() { |
| 112 | + if (canDelete) |
| 113 | + super.after(); |
| 114 | + } |
| 115 | + |
| 116 | + public void disableDeletion() { |
| 117 | + canDelete = false; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments