|
19 | 19 | import static com.google.common.collect.ImmutableList.toImmutableList; |
20 | 20 | import static com.google.common.collect.ImmutableMap.toImmutableMap; |
21 | 21 | import static com.google.common.truth.Truth.assertThat; |
| 22 | +import static java.nio.charset.StandardCharsets.UTF_8; |
22 | 23 | import static org.junit.Assert.assertThrows; |
23 | 24 |
|
24 | 25 | import com.google.common.collect.Collections2; |
25 | 26 | import com.google.common.collect.ImmutableList; |
26 | 27 | import com.google.common.collect.ImmutableMap; |
27 | 28 | import com.google.errorprone.ErrorProneOptions.Severity; |
28 | 29 | import com.google.errorprone.apply.ImportOrganizer; |
| 30 | +import java.io.File; |
| 31 | +import java.io.IOException; |
| 32 | +import java.nio.file.Files; |
29 | 33 | import java.util.Arrays; |
30 | 34 | import java.util.Collection; |
31 | 35 | import java.util.List; |
@@ -325,4 +329,120 @@ public void severityOrder() { |
325 | 329 | assertThat(options.getSeverityMap()).containsExactlyEntriesIn(severityMap).inOrder(); |
326 | 330 | } |
327 | 331 | } |
| 332 | + |
| 333 | + @Test |
| 334 | + public void processArgumentsFileParsing() throws IOException { |
| 335 | + File source = File.createTempFile("ep_argfile_", ".cfg"); |
| 336 | + source.deleteOnExit(); |
| 337 | + Files.write( |
| 338 | + source.toPath(), |
| 339 | + ImmutableList.of( |
| 340 | + "# Line comment", |
| 341 | + "-Xep:UnicodeEscape:OFF", |
| 342 | + "-Xep:InvalidBlockTag:OFF # inline comment", |
| 343 | + "-Xep:JavaUtilDate:OFF", |
| 344 | + "# Several flags on a single line are acceptable:", |
| 345 | + "-Xep:LabelledBreakTarget:OFF \t -Xep:JUnit4TestNotRun:OFF" |
| 346 | + + " -Xep:ComparableType:OFF", |
| 347 | + "-Xep:EqualsHashCode:WARN", |
| 348 | + "-Xep:ReturnValueIgnored:WARN", |
| 349 | + " # Indents and trailing, comment also indented:", |
| 350 | + "\t\t \t-Xep:ArrayToString:WARN", |
| 351 | + " -Xep:MisusedDayOfYear:WARN\t ", |
| 352 | + " -Xep:SelfComparison:WARN ", |
| 353 | + "-Xep:MisusedWeekYear:WARN"), |
| 354 | + UTF_8); |
| 355 | + |
| 356 | + String[] args = {"@" + source.getAbsolutePath()}; |
| 357 | + ImmutableMap<String, Severity> expectedSeverityMap = |
| 358 | + ImmutableMap.<String, Severity>builder() |
| 359 | + .put("UnicodeEscape", Severity.OFF) |
| 360 | + .put("InvalidBlockTag", Severity.OFF) |
| 361 | + .put("JavaUtilDate", Severity.OFF) |
| 362 | + .put("LabelledBreakTarget", Severity.OFF) |
| 363 | + .put("JUnit4TestNotRun", Severity.OFF) |
| 364 | + .put("ComparableType", Severity.OFF) |
| 365 | + .put("EqualsHashCode", Severity.WARN) |
| 366 | + .put("ReturnValueIgnored", Severity.WARN) |
| 367 | + .put("ArrayToString", Severity.WARN) |
| 368 | + .put("MisusedDayOfYear", Severity.WARN) |
| 369 | + .put("SelfComparison", Severity.WARN) |
| 370 | + .put("MisusedWeekYear", Severity.WARN) |
| 371 | + .buildOrThrow(); |
| 372 | + |
| 373 | + ErrorProneOptions options = ErrorProneOptions.processArgs(args); |
| 374 | + assertThat(options.getSeverityMap()).isEqualTo(expectedSeverityMap); |
| 375 | + } |
| 376 | + |
| 377 | + @Test |
| 378 | + public void processArgumentsFileOverrides() throws IOException { |
| 379 | + File source1 = File.createTempFile("ep_argfile_", ".cfg"); |
| 380 | + source1.deleteOnExit(); |
| 381 | + File source2 = File.createTempFile("ep_argfile_", ".cfg"); |
| 382 | + source2.deleteOnExit(); |
| 383 | + |
| 384 | + Files.write( |
| 385 | + source1.toPath(), |
| 386 | + ImmutableList.of( |
| 387 | + "-Xep:InvalidParam:WARN", "-Xep:JUnit4TestNotRun:WARN", "-Xep:EmptyBlockTag:WARN"), |
| 388 | + UTF_8); |
| 389 | + Files.write( |
| 390 | + source2.toPath(), |
| 391 | + ImmutableList.of( |
| 392 | + "-Xep:EffectivelyPrivate:OFF", |
| 393 | + "-Xep:ReturnValueIgnored:OFF", |
| 394 | + "-Xep:EmptyBlockTag:OFF", |
| 395 | + "-Xep:IntLongMath:OFF"), |
| 396 | + UTF_8); |
| 397 | + |
| 398 | + String[] args = { |
| 399 | + "-Xep:InvalidParam:ERROR", |
| 400 | + "-Xep:EffectivelyPrivate:ERROR", |
| 401 | + "-Xep:UnicodeEscape:ERROR", |
| 402 | + "-Xep:JavaUtilDate:ERROR", |
| 403 | + "@" + source1.getAbsolutePath(), |
| 404 | + "-Xep:UnicodeEscape:ERROR", |
| 405 | + "-Xep:JUnit4TestNotRun:ERROR", |
| 406 | + "@" + source2.getAbsolutePath(), |
| 407 | + "-Xep:JavaUtilDate:ERROR", |
| 408 | + "-Xep:IntLongMath:ERROR" |
| 409 | + }; |
| 410 | + |
| 411 | + // To make the result easier to follow, the `args` set flags to ERROR, |
| 412 | + // the first config file uses WARN, and the second config file uses OFF. |
| 413 | + ImmutableMap<String, Severity> expectedSeverityMap = |
| 414 | + ImmutableMap.<String, Severity>builder() |
| 415 | + .put("InvalidParam", Severity.WARN) |
| 416 | + .put("EffectivelyPrivate", Severity.OFF) |
| 417 | + .put("UnicodeEscape", Severity.ERROR) |
| 418 | + .put("JavaUtilDate", Severity.ERROR) |
| 419 | + .put("JUnit4TestNotRun", Severity.ERROR) |
| 420 | + .put("EmptyBlockTag", Severity.OFF) |
| 421 | + .put("ReturnValueIgnored", Severity.OFF) |
| 422 | + .put("IntLongMath", Severity.ERROR) |
| 423 | + .buildOrThrow(); |
| 424 | + |
| 425 | + ErrorProneOptions options = ErrorProneOptions.processArgs(args); |
| 426 | + assertThat(options.getSeverityMap()).isEqualTo(expectedSeverityMap); |
| 427 | + } |
| 428 | + |
| 429 | + @Test |
| 430 | + public void processArgumentsFileRefOtherConfig() throws IOException { |
| 431 | + File source = File.createTempFile("ep_argfile_", ".cfg"); |
| 432 | + source.deleteOnExit(); |
| 433 | + Files.write( |
| 434 | + source.toPath(), |
| 435 | + ImmutableList.of("-Xep:InvalidParam:WARN", "@other.cfg", "-Xep:EmptyBlockTag:WARN"), |
| 436 | + UTF_8); |
| 437 | + assertThrows( |
| 438 | + InvalidCommandLineOptionException.class, |
| 439 | + () -> ErrorProneOptions.processArgs(new String[] {"@" + source.getAbsolutePath()})); |
| 440 | + } |
| 441 | + |
| 442 | + @Test |
| 443 | + public void processArgumentsFileMissing() { |
| 444 | + assertThrows( |
| 445 | + InvalidCommandLineOptionException.class, |
| 446 | + () -> ErrorProneOptions.processArgs(new String[] {"@test_cfg_is_missing.cfg"})); |
| 447 | + } |
328 | 448 | } |
0 commit comments