diff --git a/src/test/java/com/google/devtools/build/lib/analysis/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/BUILD index 23e94f45f74a2c..399e20ada5beed 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/BUILD +++ b/src/test/java/com/google/devtools/build/lib/analysis/BUILD @@ -154,6 +154,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/util:detailed_exit_code", "//src/main/java/com/google/devtools/build/lib/util:filetype", "//src/main/java/com/google/devtools/build/lib/util:os", + "//src/main/java/com/google/devtools/build/lib/util:string_encoding", "//src/main/java/com/google/devtools/build/lib/util/io", "//src/main/java/com/google/devtools/build/lib/vfs", "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", diff --git a/src/test/java/com/google/devtools/build/lib/analysis/actions/TemplateExpansionActionTest.java b/src/test/java/com/google/devtools/build/lib/analysis/actions/TemplateExpansionActionTest.java index 15f208d16b6e82..1ebd5b08e4449f 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/actions/TemplateExpansionActionTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/actions/TemplateExpansionActionTest.java @@ -39,6 +39,7 @@ import com.google.devtools.build.lib.exec.util.TestExecutorBuilder; import com.google.devtools.build.lib.testutil.FoundationTestCase; import com.google.devtools.build.lib.util.Fingerprint; +import com.google.devtools.build.lib.util.StringEncoding; import com.google.devtools.build.lib.util.io.FileOutErr; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; @@ -252,7 +253,10 @@ public void testWithSpecialCharacters() throws Exception { // scratch.overwriteFile appends a newline, so we need an additional \n here String expected = String.format("%s%s\n", SPECIAL_CHARS, SPECIAL_CHARS); - executeTemplateExpansion(expected, ImmutableList.of(Substitution.of("%key%", SPECIAL_CHARS))); + executeTemplateExpansion( + expected, + ImmutableList.of( + Substitution.of("%key%", StringEncoding.unicodeToInternal(SPECIAL_CHARS)))); } private String computeKey(TemplateExpansionAction action) throws EvalException { diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleImplementationFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleImplementationFunctionsTest.java index 32905c49100876..753b62f955cd44 100644 --- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleImplementationFunctionsTest.java +++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleImplementationFunctionsTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertWithMessage; import static com.google.devtools.build.lib.bazel.bzlmod.BzlmodTestUtil.createModuleKey; import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuild; +import static java.nio.charset.StandardCharsets.ISO_8859_1; import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; @@ -68,8 +69,6 @@ import com.google.devtools.build.lib.testutil.TestConstants; import com.google.devtools.build.lib.util.Fingerprint; import com.google.devtools.build.lib.util.OsUtils; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; @@ -916,33 +915,20 @@ public void testCreateTemplateAction() throws Exception { assertThat(action.makeExecutable()).isFalse(); } - /** - * Simulates the fact that the Parser currently uses Latin1 to read BUILD files, while users - * usually write those files using UTF-8 encoding. Currently, the string-valued 'substitutions' - * parameter of the template_action function contains a hack that assumes its input is a UTF-8 - * encoded string which has been ingested as Latin 1. The hack converts the string to its - * "correct" UTF-8 value. Once Blaze starts calling {@link - * net.starlark.java.syntax.ParserInput#fromUTF8} instead of {@code fromLatin1} and the hack for - * the substitutions parameter is removed, this test will fail. - */ @Test - public void testCreateTemplateActionWithWrongEncoding() throws Exception { + public void testCreateTemplateActionUnicode() throws Exception { // The following array contains bytes that represent a string of length two when treated as // UTF-8 and a string of length four when treated as ISO-8859-1 (a.k.a. Latin 1). - byte[] bytesToDecode = {(byte) 0xC2, (byte) 0xA2, (byte) 0xC2, (byte) 0xA2}; - Charset latin1 = StandardCharsets.ISO_8859_1; - Charset utf8 = StandardCharsets.UTF_8; + String internalString = + new String(new byte[] {(byte) 0xC2, (byte) 0xA2, (byte) 0xC2, (byte) 0xA2}, ISO_8859_1); StarlarkRuleContext ruleContext = createRuleContext("//foo:foo"); setRuleContext(ruleContext); + // In production, Bazel parses Starlark as raw bytes encoded as Latin-1. ev.exec( "ruleContext.actions.expand_template(", " template = ruleContext.files.srcs[0],", " output = ruleContext.files.srcs[1],", - " substitutions = {'a" - + new String(bytesToDecode, latin1) - + "': '" - + new String(bytesToDecode, latin1) - + "'},", + " substitutions = {'a" + internalString + "': '" + internalString + "'},", " is_executable = False)"); TemplateExpansionAction action = (TemplateExpansionAction) @@ -950,8 +936,8 @@ public void testCreateTemplateActionWithWrongEncoding() throws Exception { ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); List substitutions = action.getSubstitutions(); assertThat(substitutions).hasSize(1); - assertThat(substitutions.get(0).getKey()).isEqualTo("a" + new String(bytesToDecode, utf8)); - assertThat(substitutions.get(0).getValue()).isEqualTo(new String(bytesToDecode, utf8)); + assertThat(substitutions.get(0).getKey()).isEqualTo("a" + internalString); + assertThat(substitutions.get(0).getValue()).isEqualTo(internalString); } @Test