-
-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathCommandLineTest.java
More file actions
685 lines (606 loc) · 24.4 KB
/
Copy pathCommandLineTest.java
File metadata and controls
685 lines (606 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
* Copyright 2016-2026 Qameta Software Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure;
import io.qameta.allure.command.MainCommand;
import io.qameta.allure.option.ConfigOptions;
import io.qameta.allure.option.ReportLanguageOptions;
import io.qameta.allure.option.ReportNameOptions;
import io.qameta.allure.option.VerboseOptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static io.qameta.allure.CommandLine.GENERATE_COMMAND;
import static io.qameta.allure.CommandLine.OPEN_COMMAND;
import static io.qameta.allure.CommandLine.PLUGIN_COMMAND;
import static io.qameta.allure.CommandLine.SERVE_COMMAND;
import static io.qameta.allure.ExitCode.ARGUMENT_PARSING_ERROR;
import static io.qameta.allure.ExitCode.NO_ERROR;
import static io.qameta.allure.testdata.TestData.randomPort;
import static io.qameta.allure.testdata.TestData.randomString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author charlie (Dmitry Baev).
*/
class CommandLineTest {
private Commands commands;
private CommandLine commandLine;
@BeforeEach
void setUp() {
this.commands = mock(Commands.class);
this.commandLine = new CommandLine(commands);
}
/**
* Verifies the explicit application home is used as the commandline home.
* The test checks APP_HOME has priority over the commandline location.
*/
@Description
@Test
void shouldResolveAllureHomeFromAppHome(@TempDir final Path temp) {
final Path appHome = temp.resolve("custom-home");
final URI commandlineLocation = temp.resolve("allure").resolve("lib").resolve("allure-commandline.jar")
.toUri();
final Optional<Path> result = resolveAllureHome(appHome.toString(), commandlineLocation);
assertThat(result)
.hasValue(appHome);
}
/**
* Verifies the commandline can infer an installed distribution home.
* The test checks a commandline jar under lib resolves to the parent distribution directory.
*/
@Description
@Test
void shouldInferAllureHomeFromCommandlineJar(@TempDir final Path temp) throws IOException {
final Path allureHome = createAllureHome(temp);
final Path commandlineJar = createCommandlineJar(allureHome);
final Optional<Path> result = resolveAllureHome(null, commandlineJar.toUri());
assertThat(result)
.hasValue(allureHome);
}
/**
* Verifies invalid commandline locations are ignored.
* The test checks a candidate without distribution config and plugins does not resolve.
*/
@Description
@Test
void shouldIgnoreInvalidInferredAllureHome(@TempDir final Path temp) throws IOException {
final Path commandlineJar = createCommandlineJar(temp.resolve("invalid-allure"));
final Optional<Path> result = resolveAllureHome(null, commandlineJar.toUri());
assertThat(result)
.isEmpty();
}
/**
* Verifies that invoking the parser without arguments is rejected.
* The test checks the command line reports an argument parsing error.
*/
@Description
@Test
void shouldParseEmptyArguments() {
final Optional<ExitCode> parse = parse();
assertThat(parse)
.hasValue(ExitCode.ARGUMENT_PARSING_ERROR);
}
/**
* Verifies parsing the verbose flag with help output.
* The test checks verbose options are populated and the command exits successfully.
*/
@Description
@Test
void shouldParseVerboseFlag() {
final Optional<ExitCode> parse = parse("-v", "--help");
assertThat(parse)
.isEmpty();
assertThat(commandLine.getMainCommand().getVerboseOptions())
.isNotNull()
.hasFieldOrPropertyWithValue("verbose", true)
.hasFieldOrPropertyWithValue("quiet", false);
final ExitCode exitCode = runCommand();
assertThat(exitCode)
.isEqualTo(NO_ERROR);
}
/**
* Verifies parsing the quiet flag with help output.
* The test checks quiet options are populated and the command exits successfully.
*/
@Description
@Test
void shouldParseQuietFlag() {
final Optional<ExitCode> parse = parse("-q", "--help");
assertThat(parse)
.isEmpty();
assertThat(commandLine.getMainCommand().getVerboseOptions())
.isNotNull()
.hasFieldOrPropertyWithValue("verbose", false)
.hasFieldOrPropertyWithValue("quiet", true);
final ExitCode exitCode = runCommand();
assertThat(exitCode)
.isEqualTo(NO_ERROR);
}
/**
* Verifies the generate command accepts result directories that do not exist yet.
* The test checks parsing succeeds for arbitrary result path arguments.
*/
@Description
@Test
void shouldAllowResultsDirectoriesThatNotExists() {
final Optional<ExitCode> exitCode = parse(GENERATE_COMMAND, randomString(), randomString());
assertThat(exitCode)
.isEmpty();
}
/**
* Verifies running the generate command delegates to the command service.
* The test checks parsed result directories, output directory, and default flags are passed through.
*/
@Description
@Test
void shouldRunGenerate(@TempDir final Path temp) throws IOException {
final Path report = Files.createDirectories(temp.resolve("report"));
final Path firstResult = Files.createDirectories(temp.resolve("first"));
final Path secondResult = Files.createDirectories(temp.resolve("second"));
final List<Path> results = Arrays.asList(firstResult, secondResult);
when(
commands.generate(
eq(report), eq(results),
eq(false), eq(false),
any(ConfigOptions.class),
any(ReportNameOptions.class), any(ReportLanguageOptions.class)
)
).thenReturn(NO_ERROR);
final Optional<ExitCode> exitCode = parse(
GENERATE_COMMAND, firstResult.toString(), secondResult.toString(),
"--output", report.toString()
);
assertThat(exitCode)
.isEmpty();
final ExitCode code = runCommand();
verify(commands, times(1))
.generate(
eq(report), eq(results),
eq(false), eq(false),
any(ConfigOptions.class),
any(ReportNameOptions.class), any(ReportLanguageOptions.class)
);
assertThat(code)
.isEqualTo(NO_ERROR);
}
/**
* Verifies the generate command captures a custom report name.
* The test checks the delegated report-name options contain the parsed name.
*/
@Description
@Test
void shouldRunGenerateWithReportName(@TempDir final Path temp) throws IOException {
final Path report = Files.createDirectories(temp.resolve("report"));
final Path firstResult = Files.createDirectories(temp.resolve("first"));
final Path secondResult = Files.createDirectories(temp.resolve("second"));
final List<Path> results = Arrays.asList(firstResult, secondResult);
final String reportName = randomString();
when(
commands.generate(
eq(report), eq(results),
eq(false), eq(false),
any(ConfigOptions.class),
any(ReportNameOptions.class), any(ReportLanguageOptions.class)
)
).thenReturn(NO_ERROR);
final Optional<ExitCode> exitCode = parse(
GENERATE_COMMAND, firstResult.toString(), secondResult.toString(),
"--output", report.toString(),
"--name", reportName
);
assertThat(exitCode)
.isEmpty();
final ArgumentCaptor<ReportNameOptions> captor = ArgumentCaptor.captor();
final ExitCode code = runCommand();
verify(commands, times(1))
.generate(
eq(report), eq(results), eq(false), eq(false),
any(ConfigOptions.class), captor.capture(), any(ReportLanguageOptions.class)
);
assertThat(code)
.isEqualTo(NO_ERROR);
assertThat(captor.getValue())
.extracting(ReportNameOptions::getReportName)
.isEqualTo(reportName);
}
/**
* Verifies the generate command captures a custom report language.
* The test checks the delegated language options contain the parsed language code.
*/
@Description
@Test
void shouldRunGenerateWithReportLanguage(@TempDir final Path temp) throws IOException {
final Path report = Files.createDirectories(temp.resolve("report"));
final Path firstResult = Files.createDirectories(temp.resolve("first"));
final Path secondResult = Files.createDirectories(temp.resolve("second"));
final List<Path> results = Arrays.asList(firstResult, secondResult);
final String lang = "nl";
when(
commands.generate(
eq(report), eq(results),
eq(false), eq(false),
any(ConfigOptions.class),
any(ReportNameOptions.class), any(ReportLanguageOptions.class)
)
).thenReturn(NO_ERROR);
final Optional<ExitCode> exitCode = parse(
GENERATE_COMMAND, firstResult.toString(), secondResult.toString(),
"--output", report.toString(),
"--lang", lang
);
assertThat(exitCode)
.isEmpty();
final ArgumentCaptor<ReportLanguageOptions> captor = ArgumentCaptor.captor();
final ExitCode code = runCommand();
verify(commands, times(1))
.generate(
eq(report), eq(results), eq(false), eq(false),
any(ConfigOptions.class), any(ReportNameOptions.class), captor.capture()
);
assertThat(code)
.isEqualTo(NO_ERROR);
assertThat(captor.getValue())
.extracting(ReportLanguageOptions::getReportLanguage)
.isEqualTo(lang);
}
/**
* Verifies running the open command delegates to the command service.
* The test checks the parsed report directory and port are passed through.
*/
@Description
@Test
void shouldRunOpen(@TempDir final Path report) {
final int port = randomPort();
when(commands.open(report, null, port))
.thenReturn(NO_ERROR);
final Optional<ExitCode> exitCode = parse(
OPEN_COMMAND, "--port", String.valueOf(port), report.toString()
);
assertThat(exitCode)
.isEmpty();
final ExitCode code = runCommand();
verify(commands, times(1)).open(report, null, port);
assertThat(code)
.isEqualTo(NO_ERROR);
}
/**
* Verifies the open command rejects multiple report directories.
* The test checks parsing returns an argument parsing error for ambiguous input.
*/
@Description
@Test
void shouldNotLetToSpecifyFewReportDirectories(@TempDir final Path temp) throws IOException {
final Path first = Files.createDirectories(temp.resolve("first"));
final Path second = Files.createDirectories(temp.resolve("second"));
final Optional<ExitCode> exitCode = parse(
OPEN_COMMAND, first.toString(), second.toString()
);
assertThat(exitCode)
.isPresent()
.hasValue(ExitCode.ARGUMENT_PARSING_ERROR);
}
/**
* Verifies command-specific help can be parsed and run.
* The test checks help for the open command exits successfully.
*/
@Description
@Test
void shouldRunHelpForCommand() {
final Optional<ExitCode> exitCode = parse(
"--help", OPEN_COMMAND
);
assertThat(exitCode)
.isEmpty();
final ExitCode run = runCommand();
assertThat(run)
.isEqualTo(NO_ERROR);
}
/**
* Verifies the version flag can be parsed and run.
* The test checks version output exits successfully without a subcommand.
*/
@Description
@Test
void shouldPrintVersion() {
final Optional<ExitCode> exitCode = parse(
"--version"
);
assertThat(exitCode)
.isEmpty();
final ExitCode run = runCommand();
assertThat(run)
.isEqualTo(NO_ERROR);
}
/**
* Verifies the serve command parses host, port, profile, and result directories.
* The test checks the delegated config options include the requested profile.
*/
@Description
@Test
void shouldParseServeCommand(@TempDir final Path temp) throws IOException {
final int port = randomPort();
final String host = randomString();
final String profile = randomString();
final Path first = Files.createDirectories(temp.resolve("first"));
final Path second = Files.createDirectories(temp.resolve("second"));
final Optional<ExitCode> code = parse(
SERVE_COMMAND,
"--port", String.valueOf(port),
"--host", host,
"--profile", profile,
first.toString(), second.toString()
);
assertThat(code)
.isEmpty();
final ArgumentCaptor<ConfigOptions> captor = ArgumentCaptor.captor();
when(
commands.serve(
eq(Arrays.asList(first, second)), eq(host), eq(port),
captor.capture(), any(ReportNameOptions.class), any(ReportLanguageOptions.class)
)
)
.thenReturn(NO_ERROR);
final ExitCode run = runCommand();
assertThat(run)
.isEqualTo(NO_ERROR);
assertThat(captor.getAllValues())
.hasSize(1)
.extracting(ConfigOptions::getProfile)
.containsExactly(profile);
}
/**
* Verifies the serve command parses a custom report name.
* The test checks the delegated report-name options contain the requested name.
*/
@Description
@Test
void shouldParseServeCommandWithReportName(@TempDir final Path temp) throws IOException {
final int port = randomPort();
final String host = randomString();
final String profile = randomString();
final String reportName = randomString();
final Path first = Files.createDirectories(temp.resolve("first"));
final Path second = Files.createDirectories(temp.resolve("second"));
final Optional<ExitCode> code = parse(
SERVE_COMMAND,
"--port", String.valueOf(port),
"--host", host,
"--profile", profile,
"--report-name", reportName,
first.toString(), second.toString()
);
assertThat(code)
.isEmpty();
final ArgumentCaptor<ConfigOptions> captorConfig = ArgumentCaptor.captor();
final ArgumentCaptor<ReportNameOptions> captorReportName = ArgumentCaptor.captor();
when(
commands.serve(
eq(Arrays.asList(first, second)), eq(host), eq(port),
captorConfig.capture(), captorReportName.capture(), any(ReportLanguageOptions.class)
)
)
.thenReturn(NO_ERROR);
final ExitCode run = runCommand();
assertThat(run)
.isEqualTo(NO_ERROR);
assertThat(captorConfig.getAllValues())
.hasSize(1)
.extracting(ConfigOptions::getProfile)
.containsExactly(profile);
assertThat(captorReportName.getValue())
.extracting(ReportNameOptions::getReportName)
.isEqualTo(reportName);
}
/**
* Verifies the serve command parses a custom report language.
* The test checks the delegated language options contain the requested language code.
*/
@Description
@Test
void shouldParseServeCommandWithReportLanguage(@TempDir final Path temp) throws IOException {
final int port = randomPort();
final String host = randomString();
final String profile = randomString();
final String lang = "de";
final Path first = Files.createDirectories(temp.resolve("first"));
final Path second = Files.createDirectories(temp.resolve("second"));
final Optional<ExitCode> code = parse(
SERVE_COMMAND,
"--port", String.valueOf(port),
"--host", host,
"--profile", profile,
"--lang", lang,
first.toString(), second.toString()
);
assertThat(code)
.isEmpty();
final ArgumentCaptor<ConfigOptions> captorConfig = ArgumentCaptor.captor();
final ArgumentCaptor<ReportLanguageOptions> captorReportLang = ArgumentCaptor.captor();
when(
commands.serve(
eq(Arrays.asList(first, second)), eq(host), eq(port),
captorConfig.capture(), any(ReportNameOptions.class), captorReportLang.capture()
)
)
.thenReturn(NO_ERROR);
final ExitCode run = runCommand();
assertThat(run)
.isEqualTo(NO_ERROR);
assertThat(captorConfig.getAllValues())
.hasSize(1)
.extracting(ConfigOptions::getProfile)
.containsExactly(profile);
assertThat(captorReportLang.getValue())
.extracting(ReportLanguageOptions::getReportLanguage)
.isEqualTo(lang);
}
/**
* Verifies serve command language validation.
* The test checks an unsupported language value is rejected during parsing.
*/
@Description
@Test
void shouldValidateLanguageValue() {
final Optional<ExitCode> exitCode = parse(SERVE_COMMAND, "--lang", "invalid");
assertThat(exitCode)
.isPresent()
.hasValue(ARGUMENT_PARSING_ERROR);
}
/**
* Verifies serve command port validation.
* The test checks an out-of-range port value is rejected during parsing.
*/
@Description
@Test
void shouldValidatePortValue() {
final Optional<ExitCode> exitCode = parse(SERVE_COMMAND, "--port", "213123");
assertThat(exitCode)
.isPresent()
.hasValue(ARGUMENT_PARSING_ERROR);
}
/**
* Verifies the plugin command lists configured plugins.
* The test checks plugin listing is delegated and exits successfully.
*/
@Description
@Test
void shouldPrintPluginList() {
final Optional<ExitCode> exitCode = parse(PLUGIN_COMMAND);
assertThat(exitCode)
.isEmpty();
when(commands.listPlugins(any(ConfigOptions.class))).thenReturn(NO_ERROR);
final ExitCode run = runCommand();
assertThat(run)
.isEqualTo(NO_ERROR);
}
/**
* Verifies verbose options can be parsed without a command while execution still fails.
* The test checks parsing accepts the option and running returns an argument parsing error.
*/
@Description
@Test
void shouldHandleVerboseOptionsWithoutArgs() {
final String verboseOption = "-q";
final Optional<ExitCode> exitCode = parse(verboseOption);
assertThat(exitCode)
.isEmpty();
final ExitCode run = runCommand();
assertThat(run)
.isEqualTo(ARGUMENT_PARSING_ERROR);
}
@Step("Parse command line arguments")
private Optional<ExitCode> parse(final String... arguments) {
final Optional<ExitCode> result = commandLine.parse(arguments);
attachCommandParse(arguments, result);
return result;
}
@Step("Run parsed command")
private ExitCode runCommand() {
final ExitCode result = commandLine.run();
Allure.addAttachment(
"command-run.txt",
"text/plain",
String.format("runResult=%s%n%s", result, describeMainCommand()),
".txt"
);
return result;
}
private void attachCommandParse(final String[] arguments, final Optional<ExitCode> result) {
Allure.addAttachment(
"command-parse.txt",
"text/plain",
String.format(
"arguments=%s%nparseResult=%s%n%s",
Arrays.toString(arguments),
result.map(ExitCode::name).orElse("<empty>"),
describeMainCommand()
),
".txt"
);
}
private String describeMainCommand() {
final MainCommand mainCommand = commandLine.getMainCommand();
final VerboseOptions verboseOptions = mainCommand.getVerboseOptions();
return String.format(
"help=%s%nversion=%s%nverbose=%s%nquiet=%s%n",
mainCommand.isHelp(),
mainCommand.isVersion(),
verboseOptions.isVerbose(),
verboseOptions.isQuiet()
);
}
@Step("Create installed Allure home")
private Path createAllureHome(final Path temp) throws IOException {
final Path allureHome = temp.resolve("allure");
Files.createDirectories(allureHome.resolve("config"));
Files.write(allureHome.resolve("config").resolve("allure.yml"), Arrays.asList("plugins: []"));
Files.createDirectories(allureHome.resolve("plugins"));
Allure.addAttachment(
"allure-home-layout.txt",
"text/plain",
String.format(
"home=%s%nconfig=%s%nplugins=%s%n",
allureHome,
allureHome.resolve("config").resolve("allure.yml"),
allureHome.resolve("plugins")
),
".txt"
);
return allureHome;
}
@Step("Create commandline jar")
private Path createCommandlineJar(final Path allureHome) throws IOException {
final Path lib = Files.createDirectories(allureHome.resolve("lib"));
final Path commandlineJar = lib.resolve("allure-commandline-test.jar");
Files.write(commandlineJar, Arrays.asList("commandline jar marker"));
Allure.addAttachment(
"commandline-location.txt",
"text/plain",
String.format("commandlineJar=%s%n", commandlineJar),
".txt"
);
return commandlineJar;
}
@Step("Resolve Allure home")
private Optional<Path> resolveAllureHome(final String appHome, final URI commandlineLocation) {
final Optional<Path> result = CommandLine.resolveAllureHome(appHome, commandlineLocation);
Allure.addAttachment(
"allure-home-resolution.txt",
"text/plain",
String.format(
"appHome=%s%ncommandlineLocation=%s%nresolvedHome=%s%n",
appHome,
commandlineLocation,
result.map(Path::toString).orElse("<empty>")
),
".txt"
);
return result;
}
}