From 2bfac86a62fbeafb1d0463cd60d56978ad4b15bf Mon Sep 17 00:00:00 2001 From: mschaller Date: Wed, 2 Feb 2022 14:08:50 -0800 Subject: [PATCH] Add basic unit test coverage for Starlark BigInteger compilation PiperOrigin-RevId: 425973920 --- .../lib/skyframe/BzlCompileFunctionTest.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/BzlCompileFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/BzlCompileFunctionTest.java index 2258aeb3a3c782..295ccc7de14b9b 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/BzlCompileFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/BzlCompileFunctionTest.java @@ -17,6 +17,7 @@ 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.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 @@ @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 void testLoadOfNonexistentFile() throws Exception { 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 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]"); + } + } }