View Javadoc
1   /*
2    * Copyright (C) 2016, Ned Twigg <ned.twigg@diffplug.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.pgm;
11  
12  import static org.eclipse.jgit.junit.JGitTestUtil.check;
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.eclipse.jgit.api.Git;
18  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
19  import org.junit.Test;
20  
21  public class CleanTest extends CLIRepositoryTestCase {
22  	@Test
23  	public void testCleanRequiresForce() throws Exception {
24  		try (Git git = new Git(db)) {
25  			assertArrayOfLinesEquals(
26  					new String[] { "Removing a", "Removing b" },
27  					execute("git clean"));
28  		} catch (Die e) {
29  			// TODO: should be "fatal: clean.requireForce defaults to true and
30  			// neither -i, -n, nor -f given; refusing to clean" but we don't
31  			// support -i yet. Fix this when/if we add support for -i.
32  			assertEquals(
33  					"fatal: clean.requireForce defaults to true and neither -n nor -f given; refusing to clean",
34  					e.getMessage());
35  		}
36  	}
37  
38  	@Test
39  	public void testCleanRequiresForceConfig() throws Exception {
40  		try (Git git = new Git(db)) {
41  			git.getRepository().getConfig().setBoolean("clean", null,
42  					"requireForce", false);
43  			assertArrayOfLinesEquals(
44  					new String[] { "" },
45  					execute("git clean"));
46  		}
47  	}
48  
49  	@Test
50  	public void testCleanLeaveDirs() throws Exception {
51  		try (Git git = new Git(db)) {
52  			git.commit().setMessage("initial commit").call();
53  
54  			writeTrashFile("dir/file", "someData");
55  			writeTrashFile("a", "someData");
56  			writeTrashFile("b", "someData");
57  
58  			// all these files should be there
59  			assertTrue(check(db, "a"));
60  			assertTrue(check(db, "b"));
61  			assertTrue(check(db, "dir/file"));
62  
63  			// dry run should make no change
64  			assertArrayOfLinesEquals(
65  					new String[] { "Removing a", "Removing b" },
66  					execute("git clean -n"));
67  			assertTrue(check(db, "a"));
68  			assertTrue(check(db, "b"));
69  			assertTrue(check(db, "dir/file"));
70  
71  			// force should make a change
72  			assertArrayOfLinesEquals(
73  					new String[] { "Removing a", "Removing b" },
74  					execute("git clean -f"));
75  			assertFalse(check(db, "a"));
76  			assertFalse(check(db, "b"));
77  			assertTrue(check(db, "dir/file"));
78  		}
79  	}
80  
81  	@Test
82  	public void testCleanDeleteDirs() throws Exception {
83  		try (Git git = new Git(db)) {
84  			git.commit().setMessage("initial commit").call();
85  
86  			writeTrashFile("dir/file", "someData");
87  			writeTrashFile("a", "someData");
88  			writeTrashFile("b", "someData");
89  
90  			// all these files should be there
91  			assertTrue(check(db, "a"));
92  			assertTrue(check(db, "b"));
93  			assertTrue(check(db, "dir/file"));
94  
95  			assertArrayOfLinesEquals(
96  					new String[] { "Removing a", "Removing b",
97  							"Removing dir/" },
98  					execute("git clean -d -f"));
99  			assertFalse(check(db, "a"));
100 			assertFalse(check(db, "b"));
101 			assertFalse(check(db, "dir/file"));
102 		}
103 	}
104 }