Skip to content

Commit 4bfcb5b

Browse files
committed
Refactor the maven test for git ratchet to facilitate the next commit.
1 parent f073304 commit 4bfcb5b

File tree

1 file changed

+82
-74
lines changed

1 file changed

+82
-74
lines changed

plugin-maven/src/test/java/com/diffplug/spotless/maven/GitRatchetMavenTest.java

+82-74
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.eclipse.jgit.lib.RefDatabase;
2525
import org.junit.Test;
2626

27+
import com.diffplug.common.base.StringPrinter;
28+
2729
public class GitRatchetMavenTest extends MavenIntegrationHarness {
2830
private static final String TEST_PATH = "src/markdown/test.md";
2931

@@ -36,90 +38,96 @@ private Git initRepo() throws IllegalStateException, GitAPIException, IOExceptio
3638
return git;
3739
}
3840

41+
private static final String RATCHET_FROM_POM = StringPrinter.buildStringFromLines(
42+
"<formats>",
43+
" <format>",
44+
" <ratchetFrom>baseline</ratchetFrom>",
45+
" <includes>",
46+
" <include>src/markdown/*.md</include>",
47+
" </includes>",
48+
" <replace>",
49+
" <name>Lowercase hello</name>",
50+
" <search>HELLO</search>",
51+
" <replacement>hello</replacement>",
52+
" </replace>",
53+
" <replace>",
54+
" <name>Lowercase world</name>",
55+
" <search>WORLD</search>",
56+
" <replacement>world</replacement>",
57+
" </replace>",
58+
" <replace>",
59+
" <name>Lowercase world</name>",
60+
" <search>MOM</search>",
61+
" <replacement>mom</replacement>",
62+
" </replace>",
63+
" </format>",
64+
"</formats>");
65+
3966
@Test
4067
public void singleProjectExhaustive() throws Exception {
4168
try (Git git = initRepo()) {
42-
writePom(
43-
"<formats>",
44-
" <format>",
45-
" <ratchetFrom>baseline</ratchetFrom>",
46-
" <includes>",
47-
" <include>src/markdown/*.md</include>",
48-
" </includes>",
49-
" <replace>",
50-
" <name>Lowercase hello</name>",
51-
" <search>HELLO</search>",
52-
" <replacement>hello</replacement>",
53-
" </replace>",
54-
" <replace>",
55-
" <name>Lowercase world</name>",
56-
" <search>WORLD</search>",
57-
" <replacement>world</replacement>",
58-
" </replace>",
59-
" <replace>",
60-
" <name>Lowercase world</name>",
61-
" <search>MOM</search>",
62-
" <replacement>mom</replacement>",
63-
" </replace>",
64-
" </format>",
65-
"</formats>");
66-
setFile(TEST_PATH).toContent("HELLO");
67-
git.add().addFilepattern(TEST_PATH).call();
68-
git.commit().setMessage("Initial state").call();
69-
// tag this initial state as the baseline for spotless to ratchet from
70-
git.tag().setName("baseline").call();
69+
writePom(RATCHET_FROM_POM);
70+
checkBehavior(git);
71+
}
72+
}
7173

72-
// so at this point we have test.md, and it would normally be dirty,
73-
// but because it is unchanged, spotless says it is clean
74-
assertClean();
74+
private void checkBehavior(Git git) throws Exception {
75+
setFile(TEST_PATH).toContent("HELLO");
76+
git.add().addFilepattern(TEST_PATH).call();
77+
git.commit().setMessage("Initial state").call();
78+
// tag this initial state as the baseline for spotless to ratchet from
79+
git.tag().setName("baseline").call();
7580

76-
// but if we change it so that it is not clean, spotless will now say it is dirty
77-
setFile(TEST_PATH).toContent("HELLO WORLD");
78-
assertDirty();
79-
mavenRunner().withArguments("spotless:apply").runNoError();
80-
assertFile(TEST_PATH).hasContent("hello world");
81+
// so at this point we have test.md, and it would normally be dirty,
82+
// but because it is unchanged, spotless says it is clean
83+
assertClean();
84+
85+
// but if we change it so that it is not clean, spotless will now say it is dirty
86+
setFile(TEST_PATH).toContent("HELLO WORLD");
87+
assertDirty();
88+
mavenRunner().withArguments("spotless:apply").runNoError();
89+
assertFile(TEST_PATH).hasContent("hello world");
8190

82-
// but if we make it unchanged again, it goes back to being clean
91+
// but if we make it unchanged again, it goes back to being clean
92+
setFile(TEST_PATH).toContent("HELLO");
93+
assertClean();
94+
95+
// and if we make the index dirty
96+
setFile(TEST_PATH).toContent("HELLO WORLD");
97+
git.add().addFilepattern(TEST_PATH).call();
98+
{
99+
// and the content dirty in the same way, then it's dirty
100+
assertDirty();
101+
// if we make the content something else dirty, then it's dirty
102+
setFile(TEST_PATH).toContent("HELLO MOM");
103+
assertDirty();
104+
// if we make the content unchanged, even though index it and index are dirty, then it's clean
83105
setFile(TEST_PATH).toContent("HELLO");
84106
assertClean();
85-
86-
// and if we make the index dirty
107+
// if we delete the file, but it's still in the index, then it's clean
108+
setFile(TEST_PATH).deleted();
109+
assertClean();
110+
}
111+
// if we remove the file from the index
112+
git.rm().addFilepattern(TEST_PATH).setCached(true).call();
113+
{
114+
// and it's gone in real life too, then it's clean
115+
assertClean();
116+
// if the content is there and unchanged, then it's clean
117+
setFile(TEST_PATH).toContent("HELLO");
118+
assertClean();
119+
// if the content is dirty, then it's dirty
87120
setFile(TEST_PATH).toContent("HELLO WORLD");
88-
git.add().addFilepattern(TEST_PATH).call();
89-
{
90-
// and the content dirty in the same way, then it's dirty
91-
assertDirty();
92-
// if we make the content something else dirty, then it's dirty
93-
setFile(TEST_PATH).toContent("HELLO MOM");
94-
assertDirty();
95-
// if we make the content unchanged, even though index it and index are dirty, then it's clean
96-
setFile(TEST_PATH).toContent("HELLO");
97-
assertClean();
98-
// if we delete the file, but it's still in the index, then it's clean
99-
setFile(TEST_PATH).deleted();
100-
assertClean();
101-
}
102-
// if we remove the file from the index
103-
git.rm().addFilepattern(TEST_PATH).setCached(true).call();
104-
{
105-
// and it's gone in real life too, then it's clean
106-
assertClean();
107-
// if the content is there and unchanged, then it's clean
108-
setFile(TEST_PATH).toContent("HELLO");
109-
assertClean();
110-
// if the content is dirty, then it's dirty
111-
setFile(TEST_PATH).toContent("HELLO WORLD");
112-
assertDirty();
113-
}
121+
assertDirty();
122+
}
114123

115-
// new files always get checked
116-
setFile("new.md").toContent("HELLO");
117-
{
118-
assertDirty();
119-
// even if they are added
120-
git.add().addFilepattern("new.md").call();
121-
assertDirty();
122-
}
124+
// new files always get checked
125+
setFile("new.md").toContent("HELLO");
126+
{
127+
assertDirty();
128+
// even if they are added
129+
git.add().addFilepattern("new.md").call();
130+
assertDirty();
123131
}
124132
}
125133

0 commit comments

Comments
 (0)