Skip to content

Commit 28a008a

Browse files
authored
Reduce cli tests calling main directly (#85855)
The main method of Command is not normally called by cli tests. Instead they call the execute helper which calls mainWithoutErrorHandling. Sometimes, though, it is desirable to test the top level behavior. This commit cleans up the base class tests to not call main directly. This will help with future refactorings to change the signature of main, so that less uses need to be changed.
1 parent eb031ff commit 28a008a

File tree

5 files changed

+113
-177
lines changed

5 files changed

+113
-177
lines changed

distribution/tools/geoip-cli/src/test/java/org/elasticsearch/geoip/GeoIpCliTests.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
package org.elasticsearch.geoip;
1010

11+
import joptsimple.OptionException;
12+
1113
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
1214
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
1315
import org.apache.lucene.tests.util.LuceneTestCase;
14-
import org.elasticsearch.cli.MockTerminal;
16+
import org.elasticsearch.cli.Command;
17+
import org.elasticsearch.cli.CommandTestCase;
1518
import org.elasticsearch.xcontent.XContentParser;
1619
import org.elasticsearch.xcontent.XContentParserConfiguration;
1720
import org.elasticsearch.xcontent.XContentType;
@@ -34,7 +37,7 @@
3437
import static org.hamcrest.Matchers.hasKey;
3538

3639
@LuceneTestCase.SuppressFileSystems(value = "ExtrasFS") // Don't randomly add 'extra' files to directory.
37-
public class GeoIpCliTests extends LuceneTestCase {
40+
public class GeoIpCliTests extends CommandTestCase {
3841

3942
private Path source;
4043
private Path target;
@@ -46,16 +49,14 @@ public void setUp() throws Exception {
4649
}
4750

4851
public void testNoSource() throws Exception {
49-
MockTerminal terminal = new MockTerminal();
50-
new GeoIpCli().main(new String[] {}, terminal);
51-
assertThat(terminal.getErrorOutput(), containsString("Missing required option(s) [s/source]"));
52+
var e = expectThrows(OptionException.class, () -> execute());
53+
assertThat(e.getMessage(), containsString("Missing required option(s) [s/source]"));
5254
}
5355

5456
public void testDifferentDirectories() throws Exception {
5557
Map<String, byte[]> data = createTestFiles(source);
5658

57-
GeoIpCli cli = new GeoIpCli();
58-
cli.main(new String[] { "-t", target.toAbsolutePath().toString(), "-s", source.toAbsolutePath().toString() }, new MockTerminal());
59+
execute("-t", target.toAbsolutePath().toString(), "-s", source.toAbsolutePath().toString());
5960

6061
try (Stream<Path> list = Files.list(source)) {
6162
List<String> files = list.map(p -> p.getFileName().toString()).collect(Collectors.toList());
@@ -73,9 +74,7 @@ public void testDifferentDirectories() throws Exception {
7374

7475
public void testSameDirectory() throws Exception {
7576
Map<String, byte[]> data = createTestFiles(target);
76-
77-
GeoIpCli cli = new GeoIpCli();
78-
cli.main(new String[] { "-s", target.toAbsolutePath().toString() }, new MockTerminal());
77+
execute("-s", target.toAbsolutePath().toString());
7978

8079
try (Stream<Path> list = Files.list(target)) {
8180
List<String> files = list.map(p -> p.getFileName().toString()).collect(Collectors.toList());
@@ -143,4 +142,9 @@ private Map<String, byte[]> createTestFiles(Path dir) throws IOException {
143142

144143
return data;
145144
}
145+
146+
@Override
147+
protected Command newCommand() {
148+
return new GeoIpCli();
149+
}
146150
}

distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/cli/ListPluginsCommandTests.java

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010

1111
import org.apache.lucene.tests.util.LuceneTestCase;
1212
import org.elasticsearch.Version;
13-
import org.elasticsearch.cli.ExitCodes;
14-
import org.elasticsearch.cli.MockTerminal;
15-
import org.elasticsearch.cli.UserException;
13+
import org.elasticsearch.cli.Command;
14+
import org.elasticsearch.cli.CommandTestCase;
1615
import org.elasticsearch.common.settings.Settings;
1716
import org.elasticsearch.env.Environment;
1817
import org.elasticsearch.env.TestEnvironment;
1918
import org.elasticsearch.plugins.PluginInfo;
2019
import org.elasticsearch.plugins.PluginTestUtil;
21-
import org.elasticsearch.test.ESTestCase;
2220
import org.junit.Before;
2321

2422
import java.io.IOException;
@@ -30,47 +28,19 @@
3028
import java.util.stream.Collectors;
3129

3230
@LuceneTestCase.SuppressFileSystems("*")
33-
public class ListPluginsCommandTests extends ESTestCase {
31+
public class ListPluginsCommandTests extends CommandTestCase {
3432

3533
private Path home;
3634
private Environment env;
3735

3836
@Before
39-
public void setUp() throws Exception {
40-
super.setUp();
37+
public void initEnv() throws Exception {
4138
home = createTempDir();
4239
Files.createDirectories(home.resolve("plugins"));
4340
Settings settings = Settings.builder().put("path.home", home).build();
4441
env = TestEnvironment.newEnvironment(settings);
4542
}
4643

47-
static MockTerminal listPlugins(Path home) throws Exception {
48-
return listPlugins(home, new String[0]);
49-
}
50-
51-
static MockTerminal listPlugins(Path home, String[] args) throws Exception {
52-
String[] argsAndHome = new String[args.length + 1];
53-
System.arraycopy(args, 0, argsAndHome, 0, args.length);
54-
argsAndHome[args.length] = "-Epath.home=" + home;
55-
MockTerminal terminal = new MockTerminal();
56-
int status = new ListPluginsCommand() {
57-
@Override
58-
protected Environment createEnv(Map<String, String> settings) throws UserException {
59-
Settings.Builder builder = Settings.builder().put("path.home", home);
60-
settings.forEach((k, v) -> builder.put(k, v));
61-
final Settings realSettings = builder.build();
62-
return new Environment(realSettings, home.resolve("config"));
63-
}
64-
65-
@Override
66-
protected boolean addShutdownHook() {
67-
return false;
68-
}
69-
}.main(argsAndHome, terminal);
70-
assertEquals(ExitCodes.OK, status);
71-
return terminal;
72-
}
73-
7444
private static String buildMultiline(String... args) {
7545
return Arrays.stream(args).collect(Collectors.joining("\n", "", "\n"));
7646
}
@@ -108,32 +78,31 @@ private static void buildFakePlugin(
10878

10979
public void testPluginsDirMissing() throws Exception {
11080
Files.delete(env.pluginsFile());
111-
IOException e = expectThrows(IOException.class, () -> listPlugins(home));
81+
IOException e = expectThrows(IOException.class, () -> execute());
11282
assertEquals("Plugins directory missing: " + env.pluginsFile(), e.getMessage());
11383
}
11484

11585
public void testNoPlugins() throws Exception {
116-
MockTerminal terminal = listPlugins(home);
86+
execute();
11787
assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty());
11888
}
11989

12090
public void testOnePlugin() throws Exception {
12191
buildFakePlugin(env, "fake desc", "fake", "org.fake");
122-
MockTerminal terminal = listPlugins(home);
92+
execute();
12393
assertEquals(buildMultiline("fake"), terminal.getOutput());
12494
}
12595

12696
public void testTwoPlugins() throws Exception {
12797
buildFakePlugin(env, "fake desc", "fake1", "org.fake");
12898
buildFakePlugin(env, "fake desc 2", "fake2", "org.fake");
129-
MockTerminal terminal = listPlugins(home);
99+
execute();
130100
assertEquals(buildMultiline("fake1", "fake2"), terminal.getOutput());
131101
}
132102

133103
public void testPluginWithVerbose() throws Exception {
134104
buildFakePlugin(env, "fake desc", "fake_plugin", "org.fake");
135-
String[] params = { "-v" };
136-
MockTerminal terminal = listPlugins(home, params);
105+
execute("-v");
137106
assertEquals(
138107
buildMultiline(
139108
"Plugins directory: " + env.pluginsFile(),
@@ -156,8 +125,7 @@ public void testPluginWithVerbose() throws Exception {
156125

157126
public void testPluginWithNativeController() throws Exception {
158127
buildFakePlugin(env, "fake desc 1", "fake_plugin1", "org.fake", true);
159-
String[] params = { "-v" };
160-
MockTerminal terminal = listPlugins(home, params);
128+
execute("-v");
161129
assertEquals(
162130
buildMultiline(
163131
"Plugins directory: " + env.pluginsFile(),
@@ -181,8 +149,7 @@ public void testPluginWithNativeController() throws Exception {
181149
public void testPluginWithVerboseMultiplePlugins() throws Exception {
182150
buildFakePlugin(env, "fake desc 1", "fake_plugin1", "org.fake");
183151
buildFakePlugin(env, "fake desc 2", "fake_plugin2", "org.fake2");
184-
String[] params = { "-v" };
185-
MockTerminal terminal = listPlugins(home, params);
152+
execute("-v");
186153
assertEquals(
187154
buildMultiline(
188155
"Plugins directory: " + env.pluginsFile(),
@@ -218,22 +185,21 @@ public void testPluginWithVerboseMultiplePlugins() throws Exception {
218185
public void testPluginWithoutVerboseMultiplePlugins() throws Exception {
219186
buildFakePlugin(env, "fake desc 1", "fake_plugin1", "org.fake");
220187
buildFakePlugin(env, "fake desc 2", "fake_plugin2", "org.fake2");
221-
MockTerminal terminal = listPlugins(home, new String[0]);
222-
String output = terminal.getOutput();
223-
assertEquals(buildMultiline("fake_plugin1", "fake_plugin2"), output);
188+
execute();
189+
assertEquals(buildMultiline("fake_plugin1", "fake_plugin2"), terminal.getOutput());
224190
}
225191

226192
public void testPluginWithoutDescriptorFile() throws Exception {
227193
final Path pluginDir = env.pluginsFile().resolve("fake1");
228194
Files.createDirectories(pluginDir);
229-
NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> listPlugins(home));
195+
NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> execute());
230196
assertEquals(pluginDir.resolve(PluginInfo.ES_PLUGIN_PROPERTIES).toString(), e.getFile());
231197
}
232198

233199
public void testPluginWithWrongDescriptorFile() throws Exception {
234200
final Path pluginDir = env.pluginsFile().resolve("fake1");
235201
PluginTestUtil.writePluginProperties(pluginDir, "description", "fake desc");
236-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> listPlugins(home));
202+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> execute());
237203
final Path descriptorPath = pluginDir.resolve(PluginInfo.ES_PLUGIN_PROPERTIES);
238204
assertEquals("property [name] is missing in [" + descriptorPath.toString() + "]", e.getMessage());
239205
}
@@ -256,19 +222,34 @@ public void testExistingIncompatiblePlugin() throws Exception {
256222
);
257223
buildFakePlugin(env, "fake desc 2", "fake_plugin2", "org.fake2");
258224

259-
MockTerminal terminal = listPlugins(home);
225+
execute();
260226
String message = "plugin [fake_plugin1] was built for Elasticsearch version 1.0.0 but version " + Version.CURRENT + " is required";
261227
assertEquals("""
262228
fake_plugin1
263229
fake_plugin2
264230
""", terminal.getOutput());
265231
assertEquals("WARNING: " + message + "\n", terminal.getErrorOutput());
266232

267-
String[] params = { "-s" };
268-
terminal = listPlugins(home, params);
233+
terminal.reset();
234+
execute("-s");
269235
assertEquals("""
270236
fake_plugin1
271237
fake_plugin2
272238
""", terminal.getOutput());
273239
}
240+
241+
@Override
242+
protected Command newCommand() {
243+
return new ListPluginsCommand() {
244+
@Override
245+
protected Environment createEnv(Map<String, String> settings) {
246+
return env;
247+
}
248+
249+
@Override
250+
protected boolean addShutdownHook() {
251+
return false;
252+
}
253+
};
254+
}
274255
}

0 commit comments

Comments
 (0)