Skip to content

Commit

Permalink
Default analysis tests to use Python 3
Browse files Browse the repository at this point in the history
This matches the behavior outside of the tests.

Work towards #7903

PiperOrigin-RevId: 452140874
Change-Id: Icab37d01657b07ef6f56366a1e0b64f0f3d26c29
  • Loading branch information
rickeylev authored and copybara-github committed May 31, 2022
1 parent fcaa247 commit 93f6e51
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public void testPyBinaryMember() throws Exception {
addOptions("--compile_one_dependency");
// Make build super minimal.
addOptions("--nobuild_runfile_links");

write("package/BUILD", "py_binary(name='foo', srcs=['foo.py'])");
write("package/foo.py");
buildTarget("package/foo.py");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.google.devtools.build.lib.rules.python;

import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.rules.python.PythonTestUtils.assumesDefaultIsPY2;

import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
Expand Down Expand Up @@ -68,14 +67,13 @@ public void goodSrcsVersionValue() throws Exception {
}

@Test
public void versionIs2IfUnspecified() throws Exception {
assumesDefaultIsPY2();
public void versionIs3IfUnspecified() throws Exception {
scratch.file(
"pkg/BUILD", //
ruleName + "(",
" name = 'foo',",
" srcs = ['foo.py'])");
assertThat(getPythonVersion(getConfiguredTarget("//pkg:foo"))).isEqualTo(PythonVersion.PY2);
assertThat(getPythonVersion(getConfiguredTarget("//pkg:foo"))).isEqualTo(PythonVersion.PY3);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.google.devtools.build.lib.rules.python;

import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.rules.python.PythonTestUtils.assumesDefaultIsPY2;

import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
Expand Down Expand Up @@ -72,27 +71,26 @@ public void nonhermeticRuntime() throws Exception {

@Test
public void pythonVersionDefault() throws Exception {
assumesDefaultIsPY2();
// When using toolchains, the python_version attribute is mandatory.
useConfiguration("--incompatible_use_python_toolchains=false");
scratch.file(
"pkg/BUILD",
"py_runtime(",
" name = 'myruntime_default',",
" interpreter_path = '/system/interpreter',",
" interpreter_path = '/default/interpreter',",
")",
"py_runtime(",
" name = 'myruntime_explicit',",
" interpreter_path = '/system/interpreter',",
" python_version = 'PY3',",
" interpreter_path = '/explicit/interpreter',",
" python_version = 'PY2',",
")");
PyRuntimeInfo infoDefault =
getConfiguredTarget("//pkg:myruntime_default").get(PyRuntimeInfo.PROVIDER);
PyRuntimeInfo infoExplicit =
getConfiguredTarget("//pkg:myruntime_explicit").get(PyRuntimeInfo.PROVIDER);

assertThat(infoDefault.getPythonVersion()).isEqualTo(PythonVersion.PY2);
assertThat(infoExplicit.getPythonVersion()).isEqualTo(PythonVersion.PY3);
assertThat(infoDefault.getPythonVersion()).isEqualTo(PythonVersion.PY3);
assertThat(infoExplicit.getPythonVersion()).isEqualTo(PythonVersion.PY2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ public void getHost_Py3IsDefaultFlagChangesHost() throws Exception {

@Test
public void getNormalized() throws Exception {
assumesDefaultIsPY2();
PythonOptions opts = parsePythonOptions();
PythonOptions normalizedOpts = (PythonOptions) opts.getNormalized();
assertThat(normalizedOpts.pythonVersion).isEqualTo(PythonVersion.PY2);
assertThat(normalizedOpts.pythonVersion).isEqualTo(PythonVersion.PY3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void runtimeSandwich() throws Exception {
" name = 'pyruntime',",
" interpreter = ':intr',",
" files = ['data.txt'],",
" python_version = 'PY2',",
" python_version = 'PY3',",
")",
"userruntime(",
" name = 'userruntime',",
Expand All @@ -152,7 +152,7 @@ public void runtimeSandwich() throws Exception {
")",
"py_runtime_pair(",
" name = 'userruntime_pair',",
" py2_runtime = 'userruntime',",
" py3_runtime = 'userruntime',",
")",
"toolchain(",
" name = 'usertoolchain',",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ private PythonTestUtils() {}
* <p>Marking test cases that depend on the default Python version helps to diagnose failures. It
* also helps guard against accidentally making the test spuriously pass, e.g. if the expected
* value becomes the same as the default value..
*
* <p>Although the hard-coded default in {@link PythonOptions} has been flipped to PY3, we
* override this back to PY2 in our analysis-time tests and some of our integration tests. These
* tests will need to be ported in the future.
*/
public static void assumesDefaultIsPY2() {
// No-op.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ private TestConstants() {
"--target_platform_fallback=@bazel_tools//platforms:default_target",
"--platforms=@bazel_tools//platforms:default_target",
"--host_platform=@bazel_tools//platforms:default_host",
// TODO(#7903): Remove once our own tests are migrated.
"--incompatible_py3_is_default=false",
"--incompatible_py2_outputs_are_suffixed=false",
// TODO(#7849): Remove after flag flip.
"--incompatible_use_toolchain_resolution_for_java_rules");

Expand Down

0 comments on commit 93f6e51

Please sign in to comment.