Skip to content

Commit

Permalink
Add test coverage for deprecation warnings on boolean options specifi…
Browse files Browse the repository at this point in the history
…ed with the old option name when [special boolean syntax](https://bazel.build/reference/command-line-reference#option-syntax) is used.

PiperOrigin-RevId: 575291893
Change-Id: Ia86cf72937a96d2014f772094dd661e546714cdd
  • Loading branch information
Googler authored and copybara-github committed Oct 20, 2023
1 parent ea76dd2 commit a17256e
Showing 1 changed file with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,14 @@ public static class OldNameExample extends OptionsBase {
defaultValue = "defaultValue"
)
public String flag;

@Option(
name = "new_boolean_name",
oldName = "old_boolean_name",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.NO_OP},
defaultValue = "false")
public boolean booleanFlag;
}

@Test
Expand Down Expand Up @@ -2045,6 +2053,107 @@ public void testOldNameCanonicalization() throws Exception {
.containsExactly("--new_name=foo");
}

@Test
public void testOldName_booleanTrue() throws OptionsParsingException {
OptionsParser parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--old_boolean_name=true");
OldNameExample result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isTrue();
// Using old option name should cause a warning.
assertThat(parser.getWarnings())
.contains("Option 'old_boolean_name' is deprecated: Use --new_boolean_name instead");
assertThat(parser.getWarnings()).containsNoDuplicates();

parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--new_boolean_name=true");
result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isTrue();
// Should be no warnings if the new name is used.
assertThat(parser.getWarnings()).isEmpty();
}

@Test
public void testOldName_booleanFalse() throws OptionsParsingException {
OptionsParser parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--old_boolean_name=false");
OldNameExample result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isFalse();
// Using old option name should cause a warning.
assertThat(parser.getWarnings())
.contains("Option 'old_boolean_name' is deprecated: Use --new_boolean_name instead");
assertThat(parser.getWarnings()).containsNoDuplicates();

parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--new_boolean_name=false");
result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isFalse();
// Should be no warnings if the new name is used.
assertThat(parser.getWarnings()).isEmpty();
}

@Test
public void testOldName_specialBooleanSyntax() throws OptionsParsingException {
OptionsParser parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--old_boolean_name");
OldNameExample result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isTrue();
// Using old option name should cause a warning.
assertThat(parser.getWarnings())
.contains("Option 'old_boolean_name' is deprecated: Use --new_boolean_name instead");
assertThat(parser.getWarnings()).containsNoDuplicates();

parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--new_boolean_name");
result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isTrue();
// Should be no warnings if the new name is used.
assertThat(parser.getWarnings()).isEmpty();
}

@Test
public void testOldName_negatedSpecialBooleanSyntax() throws OptionsParsingException {
OptionsParser parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--noold_boolean_name");
OldNameExample result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isFalse();
// Using old option name should cause a warning.
assertThat(parser.getWarnings())
.contains("Option 'old_boolean_name' is deprecated: Use --new_boolean_name instead");
assertThat(parser.getWarnings()).containsNoDuplicates();

parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--nonew_boolean_name");
result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isFalse();
// Should be no warnings if the new name is used.
assertThat(parser.getWarnings()).isEmpty();
}

@Test
public void testOldName_repeatedBooleanFlag() throws OptionsParsingException {
OptionsParser parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--old_boolean_name=false", "--old_boolean_name");
OldNameExample result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isTrue();
// Using old option name should cause a single warning even if the old name was specified
// multiple times.
assertThat(parser.getWarnings())
.contains("Option 'old_boolean_name' is deprecated: Use --new_boolean_name instead");
assertThat(parser.getWarnings()).containsNoDuplicates();
}

@Test
public void testOldName_overriddenByNewName() throws OptionsParsingException {
OptionsParser parser = OptionsParser.builder().optionsClasses(OldNameExample.class).build();
parser.parse("--old_boolean_name=false", "--new_boolean_name");
OldNameExample result = parser.getOptions(OldNameExample.class);
assertThat(result.booleanFlag).isTrue();
// Using old option name should cause a warning even when overridden by new name.
assertThat(parser.getWarnings())
.contains("Option 'old_boolean_name' is deprecated: Use --new_boolean_name instead");
assertThat(parser.getWarnings()).containsNoDuplicates();
}

public static class OldNameNoWarningExample extends OptionsBase {
@Option(
name = "new_name",
Expand Down

0 comments on commit a17256e

Please sign in to comment.