Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Nov 5, 2024
1 parent a887796 commit b378d1f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -916,42 +915,29 @@ 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)
Iterables.getOnlyElement(
ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions());
List<Substitution> 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
Expand Down

0 comments on commit b378d1f

Please sign in to comment.