|
| 1 | +/* |
| 2 | + * Copyright 2016 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 | + * http://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.gradle.spotless; |
| 17 | + |
| 18 | +import org.eclipse.jgit.api.Git; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +public class RatchetFromTest extends GradleIntegrationTest { |
| 22 | + @Test |
| 23 | + public void singleProjectExhaustive() throws Exception { |
| 24 | + Git git = Git.init().setDirectory(rootFolder()).call(); |
| 25 | + setFile("build.gradle").toLines( |
| 26 | + "plugins {", |
| 27 | + " id 'com.diffplug.gradle.spotless'", |
| 28 | + "}", |
| 29 | + "spotless {", |
| 30 | + " ratchetFrom 'baseline'", |
| 31 | + " format 'misc', {", |
| 32 | + " target '*.md'", |
| 33 | + " custom 'lowercase', { str -> str.toLowerCase() }", |
| 34 | + " bumpThisNumberIfACustomStepChanges(1)", |
| 35 | + " }", |
| 36 | + "}"); |
| 37 | + setFile("test.md").toContent("HELLO"); |
| 38 | + git.add().addFilepattern("test.md").call(); |
| 39 | + git.commit().setMessage("Initial state").call(); |
| 40 | + // tag this initial state as the baseline for spotless to ratchet from |
| 41 | + git.tag().setName("baseline").call(); |
| 42 | + |
| 43 | + // so at this point we have test.md, and it would normally be dirty, |
| 44 | + // but because it is unchanged, spotless says it is clean |
| 45 | + assertClean(); |
| 46 | + |
| 47 | + // but if we change it so that it is not clean, spotless will now say it is dirty |
| 48 | + setFile("test.md").toContent("HELLO WORLD"); |
| 49 | + assertDirty(); |
| 50 | + gradleRunner().withArguments("spotlessApply").build(); |
| 51 | + assertFile("test.md").hasContent("hello world"); |
| 52 | + |
| 53 | + // but if we make it unchanged again, it goes back to being clean |
| 54 | + setFile("test.md").toContent("HELLO"); |
| 55 | + assertClean(); |
| 56 | + |
| 57 | + // and if we make the index dirty |
| 58 | + setFile("test.md").toContent("HELLO WORLD"); |
| 59 | + git.add().addFilepattern("test.md").call(); |
| 60 | + { |
| 61 | + // and the content dirty in the same way, then it's dirty |
| 62 | + assertDirty(); |
| 63 | + // if we make the content something else dirty, then it's dirty |
| 64 | + setFile("test.md").toContent("HELLO MOM"); |
| 65 | + assertDirty(); |
| 66 | + // if we make the content unchanged, even though index it and index are dirty, then it's clean |
| 67 | + setFile("test.md").toContent("HELLO"); |
| 68 | + assertClean(); |
| 69 | + // if we delete the file, but it's still in the index, then it's clean |
| 70 | + setFile("test.md").deleted(); |
| 71 | + assertClean(); |
| 72 | + } |
| 73 | + // if we remove the file from the index |
| 74 | + git.rm().addFilepattern("test.md").setCached(true).call(); |
| 75 | + { |
| 76 | + // and it's gone in real life too, then it's clean |
| 77 | + assertClean(); |
| 78 | + // if the content is there and unchanged, then it's clean |
| 79 | + setFile("test.md").toContent("HELLO"); |
| 80 | + assertClean(); |
| 81 | + // if the content is dirty, then it's dirty |
| 82 | + setFile("test.md").toContent("HELLO WORLD"); |
| 83 | + assertDirty(); |
| 84 | + } |
| 85 | + |
| 86 | + // new files always get checked |
| 87 | + setFile("new.md").toContent("HELLO"); |
| 88 | + { |
| 89 | + assertDirty(); |
| 90 | + // even if they are added |
| 91 | + git.add().addFilepattern("new.md").call(); |
| 92 | + assertDirty(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void assertClean() throws Exception { |
| 97 | + gradleRunner().withArguments("spotlessCheck").build(); |
| 98 | + } |
| 99 | + |
| 100 | + private void assertDirty() throws Exception { |
| 101 | + gradleRunner().withArguments("spotlessCheck").buildAndFail(); |
| 102 | + } |
| 103 | +} |
0 commit comments