View Javadoc
1   /*
2    * Copyright (C) 2011, Abhishek Bhatnagar <abhatnag@redhat.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Tests for CleanCommand
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  		// create test files
70  		writeTrashFile("File1.txt", "Hello world");
71  		writeTrashFile("File2.txt", "Delete Me");
72  		writeTrashFile("File3.txt", "Delete Me");
73  
74  		// create files in sub-directories.
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  		// add and commit first file
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  		// create status
92  		StatusCommand command = git.status();
93  		Status status = command.call();
94  		Set<String> files = status.getUntracked();
95  		assertTrue(files.size() > 0);
96  
97  		// run clean
98  		Set<String> cleanedFiles = git.clean().call();
99  
100 		status = git.status().call();
101 		files = status.getUntracked();
102 
103 		assertTrue(files.size() == 1); // one remains (directories not cleaned)
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 		// create status
114 		StatusCommand command = git.status();
115 		Status status = command.call();
116 		Set<String> files = status.getUntracked();
117 		assertTrue(files.size() > 0);
118 
119 		// run clean
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 		// create status
137 		StatusCommand command = git.status();
138 		Status status = command.call();
139 		Set<String> files = status.getUntracked();
140 		assertTrue(files.size() > 0);
141 
142 		// run clean with setPaths
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 		// create status
158 		StatusCommand command = git.status();
159 		Status status = command.call();
160 		Set<String> files = status.getUntracked();
161 		assertTrue(files.size() > 0);
162 
163 		// run clean
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 		// create status
180 		StatusCommand command = git.status();
181 		Status status = command.call();
182 		Set<String> files = status.getUntracked();
183 		assertTrue(files.size() > 0);
184 
185 		// run clean
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 		// run clean
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 		// run clean
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 }