Skip to content

Commit

Permalink
Add basic unit test coverage for Starlark BigInteger compilation
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 425973920
  • Loading branch information
anakanemison authored and copybara-github committed Feb 2, 2022
1 parent 4399004 commit 2bfac86
Showing 1 changed file with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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<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]");
}
}
}

0 comments on commit 2bfac86

Please sign in to comment.