1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.api;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
48
49 import java.util.Set;
50 import java.util.TreeSet;
51
52 import org.eclipse.jgit.api.errors.GitAPIException;
53 import org.eclipse.jgit.errors.NoWorkTreeException;
54 import org.eclipse.jgit.junit.RepositoryTestCase;
55 import org.junit.Before;
56 import org.junit.Test;
57
58
59
60
61 public class CleanCommandTest extends RepositoryTestCase {
62 private Git git;
63
64 @Before
65 public void setUp() throws Exception {
66 super.setUp();
67 git = new Git(db);
68
69
70 writeTrashFile("File1.txt", "Hello world");
71 writeTrashFile("File2.txt", "Delete Me");
72 writeTrashFile("File3.txt", "Delete Me");
73
74
75 writeTrashFile("sub-noclean/File1.txt", "Hello world");
76 writeTrashFile("sub-noclean/File2.txt", "Delete Me");
77 writeTrashFile("sub-clean/File4.txt", "Delete Me");
78 writeTrashFile("sub-noclean/Ignored.txt", "Ignored");
79 writeTrashFile(".gitignore", "/ignored-dir\n/sub-noclean/Ignored.txt");
80 writeTrashFile("ignored-dir/Ignored2.txt", "Ignored");
81
82
83 git.add().addFilepattern("File1.txt").call();
84 git.add().addFilepattern("sub-noclean/File1.txt").call();
85 git.add().addFilepattern(".gitignore").call();
86 git.commit().setMessage("Initial commit").call();
87 }
88
89 @Test
90 public void testClean() throws NoWorkTreeException, GitAPIException {
91
92 StatusCommand command = git.status();
93 Status status = command.call();
94 Set<String> files = status.getUntracked();
95 assertTrue(files.size() > 0);
96
97
98 Set<String> cleanedFiles = git.clean().call();
99
100 status = git.status().call();
101 files = status.getUntracked();
102
103 assertTrue(files.size() == 1);
104 assertTrue(cleanedFiles.contains("File2.txt"));
105 assertTrue(cleanedFiles.contains("File3.txt"));
106 assertTrue(!cleanedFiles.contains("sub-noclean/File1.txt"));
107 assertTrue(cleanedFiles.contains("sub-noclean/File2.txt"));
108 assertTrue(!cleanedFiles.contains("sub-clean/File4.txt"));
109 }
110
111 @Test
112 public void testCleanDirs() throws NoWorkTreeException, GitAPIException {
113
114 StatusCommand command = git.status();
115 Status status = command.call();
116 Set<String> files = status.getUntracked();
117 assertTrue(files.size() > 0);
118
119
120 Set<String> cleanedFiles = git.clean().setCleanDirectories(true).call();
121
122 status = git.status().call();
123 files = status.getUntracked();
124
125 assertTrue(files.size() == 0);
126 assertTrue(cleanedFiles.contains("File2.txt"));
127 assertTrue(cleanedFiles.contains("File3.txt"));
128 assertTrue(!cleanedFiles.contains("sub-noclean/File1.txt"));
129 assertTrue(cleanedFiles.contains("sub-noclean/File2.txt"));
130 assertTrue(cleanedFiles.contains("sub-clean/"));
131 }
132
133 @Test
134 public void testCleanWithPaths() throws NoWorkTreeException,
135 GitAPIException {
136
137 StatusCommand command = git.status();
138 Status status = command.call();
139 Set<String> files = status.getUntracked();
140 assertTrue(files.size() > 0);
141
142
143 Set<String> paths = new TreeSet<String>();
144 paths.add("File3.txt");
145 Set<String> cleanedFiles = git.clean().setPaths(paths).call();
146
147 status = git.status().call();
148 files = status.getUntracked();
149 assertTrue(files.size() == 3);
150 assertTrue(cleanedFiles.contains("File3.txt"));
151 assertFalse(cleanedFiles.contains("File2.txt"));
152 }
153
154 @Test
155 public void testCleanWithDryRun() throws NoWorkTreeException,
156 GitAPIException {
157
158 StatusCommand command = git.status();
159 Status status = command.call();
160 Set<String> files = status.getUntracked();
161 assertTrue(files.size() > 0);
162
163
164 Set<String> cleanedFiles = git.clean().setDryRun(true).call();
165
166 status = git.status().call();
167 files = status.getUntracked();
168
169 assertEquals(4, files.size());
170 assertTrue(cleanedFiles.contains("File2.txt"));
171 assertTrue(cleanedFiles.contains("File3.txt"));
172 assertTrue(!cleanedFiles.contains("sub-noclean/File1.txt"));
173 assertTrue(cleanedFiles.contains("sub-noclean/File2.txt"));
174 }
175
176 @Test
177 public void testCleanDirsWithDryRun() throws NoWorkTreeException,
178 GitAPIException {
179
180 StatusCommand command = git.status();
181 Status status = command.call();
182 Set<String> files = status.getUntracked();
183 assertTrue(files.size() > 0);
184
185
186 Set<String> cleanedFiles = git.clean().setDryRun(true)
187 .setCleanDirectories(true).call();
188
189 status = git.status().call();
190 files = status.getUntracked();
191
192 assertTrue(files.size() == 4);
193 assertTrue(cleanedFiles.contains("File2.txt"));
194 assertTrue(cleanedFiles.contains("File3.txt"));
195 assertTrue(!cleanedFiles.contains("sub-noclean/File1.txt"));
196 assertTrue(cleanedFiles.contains("sub-noclean/File2.txt"));
197 assertTrue(cleanedFiles.contains("sub-clean/"));
198 }
199
200 @Test
201 public void testCleanWithDryRunAndNoIgnore() throws NoWorkTreeException,
202 GitAPIException {
203
204 Set<String> cleanedFiles = git.clean().setDryRun(true).setIgnore(false)
205 .call();
206
207 Status status = git.status().call();
208 Set<String> files = status.getIgnoredNotInIndex();
209
210 assertTrue(files.size() == 2);
211 assertTrue(cleanedFiles.contains("sub-noclean/Ignored.txt"));
212 assertTrue(!cleanedFiles.contains("ignored-dir/"));
213 }
214
215 @Test
216 public void testCleanDirsWithDryRunAndNoIgnore()
217 throws NoWorkTreeException, GitAPIException {
218
219 Set<String> cleanedFiles = git.clean().setDryRun(true).setIgnore(false)
220 .setCleanDirectories(true).call();
221
222 Status status = git.status().call();
223 Set<String> files = status.getIgnoredNotInIndex();
224
225 assertTrue(files.size() == 2);
226 assertTrue(cleanedFiles.contains("sub-noclean/Ignored.txt"));
227 assertTrue(cleanedFiles.contains("ignored-dir/"));
228 }
229
230 }