Add basic unit test coverage for Starlark BigInteger compilation
@@ -17,6 +17,7 @@ package com.google.devtools.build.lib.skyframe;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
+import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
@@ -34,7 +35,13 @@ import com.google.devtools.build.skyframe.ErrorInfo;
import com.google.devtools.build.skyframe.EvaluationResult;
import com.google.devtools.build.skyframe.SkyKey;
import java.io.IOException;
+import java.math.BigInteger;
import java.util.List;
+import net.starlark.java.eval.Module;
+import net.starlark.java.eval.Mutability;
+import net.starlark.java.eval.Starlark;
+import net.starlark.java.eval.StarlarkSemantics;
+import net.starlark.java.eval.StarlarkThread;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -43,7 +50,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class BzlCompileFunctionTest extends BuildViewTestCase {
- private class MockFileSystem extends InMemoryFileSystem {
+ private static class MockFileSystem extends InMemoryFileSystem {
PathFragment throwIOExceptionFor = null;
MockFileSystem() {
@@ -130,4 +137,33 @@ public class BzlCompileFunctionTest extends BuildViewTestCase {
assertThat(result.get(skyKey).lookupSuccessful()).isFalse();
assertThat(result.get(skyKey).getError()).contains("cannot load '//pkg:foo.bzl': no such file");
}
+
+ @Test
+ public void testBigIntegerLiterals() throws Exception {
+ // This test ensures that numerical literals with values that can't be expressed as Java longs
+ // can be compiled. Regression test for b/217548647.
+ SkyKey skyKey = BzlCompileValue.key(root, Label.parseAbsoluteUnchecked("//pkg:bigint.bzl"));
+ scratch.file("pkg/BUILD");
+ scratch.file(
+ "pkg/bigint.bzl",
+ String.format(
+ "[%s, %s]",
+ BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE),
+ BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)));
+
+ EvaluationResult<BzlCompileValue> result =
+ SkyframeExecutorTestUtils.evaluate(
+ getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter);
+ BzlCompileValue bzlCompileValue = result.get(skyKey);
+ assertThat(bzlCompileValue.lookupSuccessful()).isTrue();
+
+ try (Mutability mu = Mutability.create()) {
+ Object val =
+ Starlark.execFileProgram(
+ bzlCompileValue.getProgram(),
+ Module.withPredeclared(StarlarkSemantics.DEFAULT, ImmutableMap.of()),
+ new StarlarkThread(mu, StarlarkSemantics.DEFAULT));
+ assertThat(val.toString()).isEqualTo("[-9223372036854775809, 9223372036854775808]");
+ }
+ }
}
