fix: Non-ASCII test names being escaped when creating run configurations#8838
fix: Non-ASCII test names being escaped when creating run configurations#8838chika3742 wants to merge 1 commit intoflutter:mainfrom
Conversation
StringUtil.escapeProperty() was incorrectly applied to test names in CommonTestConfigUtils.findTestName(), converting non-ASCII characters (e.g. Japanese "テスト") to Unicode escape sequences. This caused `flutter test --plain-name` to receive an escaped string that did not match the actual test name, preventing the test from running. Fix by removing the escapeProperty() call, which is intended for .properties file encoding and is unnecessary here.
There was a problem hiding this comment.
Code Review
This pull request correctly resolves an issue where non-ASCII test names were improperly escaped, preventing tests from running. The refactoring of test name extraction logic into a separate, testable method is a positive change for code quality. My review includes suggestions to further improve the new code by adhering to the repository's style guide, specifically by using java.util.Optional to avoid returning null values, which enhances null safety.
| @Nullable | ||
| public String extractTestName(@NotNull DartCallExpression call) { | ||
| final DartStringLiteralExpression lit = DartSyntax.getArgument(call, 0, DartStringLiteralExpression.class); | ||
| if (lit == null) return null; | ||
|
|
||
| final String name = DartSyntax.unquote(lit); | ||
| if (name == null) return null; | ||
|
|
||
| return StringUtil.escapeProperty(name, false); | ||
| return DartSyntax.unquote(lit); | ||
| } |
There was a problem hiding this comment.
According to the repository style guide (line 48), java.util.Optional should be used for return types that may be empty to avoid returning null. This new method is a good place to apply this pattern. Note that this will require changes at the call sites (which I've commented on separately) and an import for java.util.Optional.
| @Nullable | |
| public String extractTestName(@NotNull DartCallExpression call) { | |
| final DartStringLiteralExpression lit = DartSyntax.getArgument(call, 0, DartStringLiteralExpression.class); | |
| if (lit == null) return null; | |
| final String name = DartSyntax.unquote(lit); | |
| if (name == null) return null; | |
| return StringUtil.escapeProperty(name, false); | |
| return DartSyntax.unquote(lit); | |
| } | |
| @NotNull | |
| public java.util.Optional<String> extractTestName(@NotNull DartCallExpression call) { | |
| final DartStringLiteralExpression lit = DartSyntax.getArgument(call, 0, DartStringLiteralExpression.class); | |
| if (lit == null) { | |
| return java.util.Optional.empty(); | |
| } | |
| return java.util.Optional.ofNullable(DartSyntax.unquote(lit)); | |
| } |
References
- The style guide recommends using
java.util.Optionalfor return types that may be empty instead of returningnull. (link)
There was a problem hiding this comment.
Since findTestName() returns @Nullable String, wrapping the result in Optional only here is inconsistent.
| final DartCallExpression call = findEnclosingTestCall(elt, getTestsFromOutline(elt.getContainingFile())); | ||
| if (call == null) return null; | ||
|
|
||
| return extractTestName(call); |
There was a problem hiding this comment.
To accommodate the change in extractTestName to return an Optional<String> (as suggested in another comment), this call site needs to be updated to unwrap the optional and maintain the method's contract of returning a nullable String.
| return extractTestName(call); | |
| return extractTestName(call).orElse(null); |
References
- This change is a consequence of applying the style guide rule (line 48) to use
java.util.Optionalfor nullable return types. (link)
| final String name = TestConfigUtils.getInstance().extractTestName(call); | ||
| assertEquals("テスト", name); |
There was a problem hiding this comment.
If extractTestName is updated to return Optional<String> as suggested in another comment, this test will need to be updated to handle the Optional return type. You may also need to add a static import for org.junit.Assert.assertTrue.
final java.util.Optional<String> name = TestConfigUtils.getInstance().extractTestName(call);
org.junit.Assert.assertTrue(name.isPresent());
assertEquals("テスト", name.get());References
- This test update is required to correctly test the
extractTestNamemethod after applying the style guide rule (line 48) to usejava.util.Optional. (link)
StringUtil.escapeProperty()was incorrectly applied to test names inCommonTestConfigUtils.findTestName(), converting non-ASCII characters (e.g. Japanese "テスト") to Unicode escape sequences. This causedflutter test --plain-nameto receive an escaped string that did not match the actual test name, preventing the test from running.Fix by removing the
escapeProperty()call, which is intended for.propertiesfile encoding and is unnecessary here.fixes #7985
Contribution guidelines:
dart format.