View Javadoc
1   /*
2    * Copyright (C) 2017, 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.assertFalse;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.eclipse.jgit.api.Git;
20  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
21  import org.eclipse.jgit.pgm.opt.CmdLineParser;
22  import org.eclipse.jgit.pgm.opt.SubcommandHandler;
23  import org.junit.Test;
24  import org.kohsuke.args4j.Argument;
25  
26  public class TextBuiltinTest extends CLIRepositoryTestCase {
27  	public static class GitCliJGitWrapperParser {
28  		@Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class)
29  		TextBuiltin subcommand;
30  
31  		@Argument(index = 1, metaVar = "metaVar_arg")
32  		List<String> arguments = new ArrayList<>();
33  	}
34  
35  	private String[] runAndCaptureUsingInitRaw(String... args)
36  			throws Exception {
37  		CLIGitCommand.Result result = new CLIGitCommand.Result();
38  
39  		GitCliJGitWrapperParser bean = new GitCliJGitWrapperParser();
40  		final CmdLineParser clp = new CmdLineParser(bean);
41  		clp.parseArgument(args);
42  
43  		final TextBuiltin cmd = bean.subcommand;
44  		cmd.initRaw(db, null, null, result.out, result.err);
45  		cmd.execute(bean.arguments.toArray(new String[bean.arguments.size()]));
46  		if (cmd.getOutputWriter() != null) {
47  			cmd.getOutputWriter().flush();
48  		}
49  		if (cmd.getErrorWriter() != null) {
50  			cmd.getErrorWriter().flush();
51  		}
52  		return result.outLines().toArray(new String[0]);
53  	}
54  
55  	@Test
56  	public void testCleanDeleteDirs() throws Exception {
57  		try (Git git = new Git(db)) {
58  			git.commit().setMessage("initial commit").call();
59  
60  			writeTrashFile("dir/file", "someData");
61  			writeTrashFile("a", "someData");
62  			writeTrashFile("b", "someData");
63  
64  			// all these files should be there
65  			assertTrue(check(db, "a"));
66  			assertTrue(check(db, "b"));
67  			assertTrue(check(db, "dir/file"));
68  
69  			assertArrayOfLinesEquals(new String[] { "Removing a", "Removing b",
70  					"Removing dir/" },
71  					runAndCaptureUsingInitRaw("clean", "-d", "-f"));
72  			assertFalse(check(db, "a"));
73  			assertFalse(check(db, "b"));
74  			assertFalse(check(db, "dir/file"));
75  		}
76  	}
77  }