View Javadoc
1   /*
2    * Copyright (C) 2022, Matthias Sohn <matthias.sohn@sap.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  
11  package org.eclipse.jgit.pgm;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
16  import org.junit.Before;
17  import org.junit.Test;
18  
19  public class DiffTest extends CLIRepositoryTestCase {
20  
21  	private static final String NO_NEWLINE = "\\ No newline at end of file";
22  
23  	@Before
24  	public void setup() throws Exception {
25  		writeTrashFile("a", "a");
26  		execute("git add a");
27  		execute("git commit -m added");
28  	}
29  
30  	@Test
31  	public void testDiffCommitNewFile() throws Exception {
32  		writeTrashFile("a1", "a");
33  		String result = toString(execute("git diff"));
34  		assertEquals(
35  				toString("diff --git a/a1 b/a1", "new file mode 100644",
36  						"index 0000000..2e65efe", "--- /dev/null", "+++ b/a1",
37  						"@@ -0,0 +1 @@", "+a", NO_NEWLINE),
38  				result);
39  	}
40  
41  	@Test
42  	public void testDiffCommitModifiedFile() throws Exception {
43  		writeTrashFile("a", "a1");
44  		String result = toString(execute("git diff"));
45  		assertEquals(
46  				toString("diff --git a/a b/a", "index 2e65efe..59ef8d1 100644",
47  						"--- a/a", "+++ b/a", "@@ -1 +1 @@",
48  						"-a", NO_NEWLINE, "+a1", NO_NEWLINE),
49  				result);
50  	}
51  
52  	@Test
53  	public void testDiffCommitModifiedFileNameOnly() throws Exception {
54  		writeTrashFile("a", "a1");
55  		writeTrashFile("b", "b");
56  		String result = toString(execute("git diff --name-only"));
57  		assertEquals(toString("a", "b"), result);
58  	}
59  
60  	@Test
61  	public void testDiffCommitModifiedFileNameStatus() throws Exception {
62  		writeTrashFile("a", "a1");
63  		writeTrashFile("b", "b");
64  		String result = toString(execute("git diff --name-status"));
65  		assertEquals(toString("M\ta", "A\tb"), result);
66  	}
67  }