View Javadoc
1   /*
2    * Copyright (C) 2020-2021, Simeon Andreev <simeon.danailov.andreev@gmail.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.internal.diffmergetool;
11  
12  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFFTOOL_SECTION;
13  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CMD;
14  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_GUITOOL;
15  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH;
16  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PROMPT;
17  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_TRUST_EXIT_CODE;
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  
21  import java.util.Collections;
22  import java.util.LinkedHashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.eclipse.jgit.lib.internal.BooleanTriState;
27  import org.eclipse.jgit.storage.file.FileBasedConfig;
28  import org.junit.Test;
29  
30  /**
31   * Testing external diff tools.
32   */
33  public class ExternalDiffToolTest extends ExternalToolTestCase {
34  
35  	@Test
36  	public void testToolNames() {
37  		DiffTools manager = new DiffTools(db);
38  		Set<String> actualToolNames = manager.getToolNames();
39  		Set<String> expectedToolNames = Collections.emptySet();
40  		assertEquals("Incorrect set of external diff tool names",
41  				expectedToolNames, actualToolNames);
42  	}
43  
44  	@Test
45  	public void testAllTools() {
46  		DiffTools manager = new DiffTools(db);
47  		Set<String> actualToolNames = manager.getAvailableTools().keySet();
48  		Set<String> expectedToolNames = new LinkedHashSet<>();
49  		CommandLineDiffTool[] defaultTools = CommandLineDiffTool.values();
50  		for (CommandLineDiffTool defaultTool : defaultTools) {
51  			String toolName = defaultTool.name();
52  			expectedToolNames.add(toolName);
53  		}
54  		assertEquals("Incorrect set of external diff tools", expectedToolNames,
55  				actualToolNames);
56  	}
57  
58  	@Test
59  	public void testOverridePredefinedToolPath() {
60  		String toolName = CommandLineDiffTool.guiffy.name();
61  		String customToolPath = "/usr/bin/echo";
62  
63  		FileBasedConfig config = db.getConfig();
64  		config.setString(CONFIG_DIFFTOOL_SECTION, toolName, CONFIG_KEY_CMD,
65  				"echo");
66  		config.setString(CONFIG_DIFFTOOL_SECTION, toolName, CONFIG_KEY_PATH,
67  				customToolPath);
68  
69  		DiffTools manager = new DiffTools(db);
70  		Map<String, ExternalDiffTool> tools = manager.getUserDefinedTools();
71  		ExternalDiffTool diffTool = tools.get(toolName);
72  		assertNotNull("Expected tool \"" + toolName + "\" to be user defined",
73  				diffTool);
74  
75  		String toolPath = diffTool.getPath();
76  		assertEquals("Expected external diff tool to have an overriden path",
77  				customToolPath, toolPath);
78  	}
79  
80  	@Test
81  	public void testUserDefinedTools() {
82  		FileBasedConfig config = db.getConfig();
83  		String customToolname = "customTool";
84  		config.setString(CONFIG_DIFFTOOL_SECTION, customToolname,
85  				CONFIG_KEY_CMD, "echo");
86  		config.setString(CONFIG_DIFFTOOL_SECTION, customToolname,
87  				CONFIG_KEY_PATH, "/usr/bin/echo");
88  		config.setString(CONFIG_DIFFTOOL_SECTION, customToolname,
89  				CONFIG_KEY_PROMPT, "--no-prompt");
90  		config.setString(CONFIG_DIFFTOOL_SECTION, customToolname,
91  				CONFIG_KEY_GUITOOL, "--no-gui");
92  		config.setString(CONFIG_DIFFTOOL_SECTION, customToolname,
93  				CONFIG_KEY_TRUST_EXIT_CODE, "--no-trust-exit-code");
94  		DiffTools manager = new DiffTools(db);
95  		Set<String> actualToolNames = manager.getUserDefinedTools().keySet();
96  		Set<String> expectedToolNames = new LinkedHashSet<>();
97  		expectedToolNames.add(customToolname);
98  		assertEquals("Incorrect set of external diff tools", expectedToolNames,
99  				actualToolNames);
100 	}
101 
102 	@Test
103 	public void testNotAvailableTools() {
104 		DiffTools manager = new DiffTools(db);
105 		Set<String> actualToolNames = manager.getNotAvailableTools().keySet();
106 		Set<String> expectedToolNames = Collections.emptySet();
107 		assertEquals("Incorrect set of not available external diff tools",
108 				expectedToolNames, actualToolNames);
109 	}
110 
111 	@Test
112 	public void testCompare() {
113 		DiffTools manager = new DiffTools(db);
114 
115 		String newPath = "";
116 		String oldPath = "";
117 		String newId = "";
118 		String oldId = "";
119 		String toolName = "";
120 		BooleanTriState prompt = BooleanTriState.UNSET;
121 		BooleanTriState gui = BooleanTriState.UNSET;
122 		BooleanTriState trustExitCode = BooleanTriState.UNSET;
123 
124 		int expectedCompareResult = 0;
125 		int compareResult = manager.compare(newPath, oldPath, newId, oldId,
126 				toolName, prompt, gui, trustExitCode);
127 		assertEquals("Incorrect compare result for external diff tool",
128 				expectedCompareResult, compareResult);
129 	}
130 
131 	@Test
132 	public void testDefaultTool() throws Exception {
133 		FileBasedConfig config = db.getConfig();
134 		// the default diff tool is configured without a subsection
135 		String subsection = null;
136 		config.setString("diff", subsection, "tool", "customTool");
137 
138 		DiffTools manager = new DiffTools(db);
139 		BooleanTriState gui = BooleanTriState.UNSET;
140 		String defaultToolName = manager.getDefaultToolName(gui);
141 		assertEquals(
142 				"Expected configured difftool to be the default external diff tool",
143 				"my_default_toolname", defaultToolName);
144 
145 		gui = BooleanTriState.TRUE;
146 		String defaultGuiToolName = manager.getDefaultToolName(gui);
147 		assertEquals(
148 				"Expected configured difftool to be the default external diff tool",
149 				"my_gui_tool", defaultGuiToolName);
150 
151 		config.setString("diff", subsection, "guitool", "customGuiTool");
152 		manager = new DiffTools(db);
153 		defaultGuiToolName = manager.getDefaultToolName(gui);
154 		assertEquals(
155 				"Expected configured difftool to be the default external diff guitool",
156 				"my_gui_tool", defaultGuiToolName);
157 	}
158 }