Skip to content

Commit

Permalink
Factor out unit tests common to py_binary, py_test, and py_library
Browse files Browse the repository at this point in the history
The moved tests used to cover binary/test and now also cover library.

Also added some test cases for srcs_version.

Work toward #1446.

RELNOTES: None
PiperOrigin-RevId: 212999264
  • Loading branch information
brandjon authored and Copybara-Service committed Sep 14, 2018
1 parent 5f432e0 commit 799b034
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 110 deletions.
16 changes: 15 additions & 1 deletion src/test/java/com/google/devtools/build/lib/rules/python/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,27 @@ java_test(
],
)

java_library(
name = "PyBaseTestBase",
srcs = ["PyBaseConfiguredTargetTestBase.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib:bazel-rules",
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/main/java/com/google/devtools/build/lib:python-rules",
"//src/test/java/com/google/devtools/build/lib:analysis_testutil",
"//third_party:junit4",
"//third_party:truth",
],
)

java_library(
name = "PyExecutableTestBase",
srcs = ["PyExecutableConfiguredTargetTestBase.java"],
deps = [
":PyBaseTestBase",
"//src/main/java/com/google/devtools/build/lib:bazel-rules",
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/main/java/com/google/devtools/build/lib:python-rules",
"//src/test/java/com/google/devtools/build/lib:analysis_testutil",
"//third_party:junit4",
"//third_party:truth",
],
Expand Down Expand Up @@ -84,6 +97,7 @@ java_test(
name = "PyLibraryConfiguredTargetTest",
srcs = ["PyLibraryConfiguredTargetTest.java"],
deps = [
":PyBaseTestBase",
"//src/main/java/com/google/devtools/build/lib:build-base",
"//src/test/java/com/google/devtools/build/lib:actions_testutil",
"//src/test/java/com/google/devtools/build/lib:analysis_testutil",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.rules.python;

import static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import org.junit.Before;
import org.junit.Test;

/** Tests that are common to {@code py_binary}, {@code py_test}, and {@code py_library}. */
public abstract class PyBaseConfiguredTargetTestBase extends BuildViewTestCase {

private final String ruleName;

protected PyBaseConfiguredTargetTestBase(String ruleName) {
this.ruleName = ruleName;
}

@Before
public final void setUpPython() throws Exception {
analysisMock.pySupport().setup(mockToolsConfig);
}

protected PythonVersion getPythonVersion(ConfiguredTarget ct) {
return getConfiguration(ct).getOptions().get(PythonOptions.class).getPythonVersion();
}

@Test
public void badSrcsVersionValue() throws Exception {
checkError("pkg", "foo",
// error:
"invalid value in 'srcs_version' attribute: "
+ "has to be one of 'PY2', 'PY3', 'PY2AND3', 'PY2ONLY' "
+ "or 'PY3ONLY' instead of 'doesnotexist'",
// build file:
ruleName + "(",
" name = 'foo',",
" srcs_version = 'doesnotexist',",
" srcs = ['foo.py'])");
}

@Test
public void goodSrcsVersionValue() throws Exception {
scratch.file("pkg/BUILD",
ruleName + "(",
" name = 'foo',",
" srcs_version = 'PY2',",
" srcs = ['foo.py'])");
getConfiguredTarget("//pkg:foo");
assertNoEvents();
}

@Test
public void srcsVersionClashesWithForcePythonFlag() throws Exception {
useConfiguration("--force_python=PY3");
checkError("pkg", "foo",
// error:
"'//pkg:foo' can only be used with Python 2",
// build file:
ruleName + "(",
" name = 'foo',",
" srcs = [':foo.py'],",
" srcs_version = 'PY2ONLY')");
}

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

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

@Test
public void packageNameCannotHaveHyphen() throws Exception {
checkError("pkg-hyphenated", "foo",
// error:
"paths to Python packages may not contain '-'",
// build file:
ruleName + "(",
" name = 'foo',",
" srcs = ['foo.py'])");
}

@Test
public void srcsPackageNameCannotHaveHyphen() throws Exception {
scratch.file("pkg-hyphenated/BUILD",
"exports_files(['bar.py'])");
checkError("otherpkg", "foo",
// error:
"paths to Python packages may not contain '-'",
// build file:
ruleName + "(",
" name = 'foo',",
" srcs = ['foo.py', '//pkg-hyphenated:bar.py'])");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public void filesToBuild() throws Exception {
}

@Test
public void emptySrcsNotAllowed() throws Exception {
public void srcsIsMandatory() throws Exception {
// This case is somewhat dominated by the test that the default main must be in srcs, but the
// error message is different here.
checkError("pkg", "foo",
// error:
"missing value for mandatory attribute 'srcs'",
Expand Down Expand Up @@ -214,30 +216,6 @@ public void defaultMainCanHaveMultiplePathSegments() throws Exception {
getConfiguredTarget("//pkg:foo/bar"); // should not fail
}

@Test
public void packageNameCannotHaveHyphen() throws Exception {
checkError("pkg-hyphenated", "foo",
// error:
"paths to Python packages may not contain '-'",
// build file:
"py_binary(",
" name = 'foo',",
" srcs = ['foo.py'])");
}

@Test
public void srcsPackageNameCannotHaveHyphen() throws Exception {
scratch.file("pkg-hyphenated/BUILD",
"exports_files(['bar.py'])");
checkError("otherpkg", "foo",
// error:
"paths to Python packages may not contain '-'",
// build file:
"py_binary(",
" name = 'foo',",
" srcs = ['foo.py', '//pkg-hyphenated:bar.py'])");
}

// TODO(brandjon): Add tests for content of stub Python script (particularly for choosing python
// 2 or 3).
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,80 +16,57 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import org.junit.Before;
import org.junit.Test;

/** Tests that are common to {@code py_binary} and {@code py_test}. */
public abstract class PyExecutableConfiguredTargetTestBase extends BuildViewTestCase {
public abstract class PyExecutableConfiguredTargetTestBase extends PyBaseConfiguredTargetTestBase {

private final String ruleName;

protected PyExecutableConfiguredTargetTestBase(String ruleName) {
super(ruleName);
this.ruleName = ruleName;
}

@Before
public final void setUpPython() throws Exception {
analysisMock.pySupport().setup(mockToolsConfig);
}

private PythonVersion getPythonVersion(ConfiguredTarget ct) {
return getConfiguration(ct).getOptions().get(PythonOptions.class).getPythonVersion();
}

@Test
public void badDefaultPythonVersion() throws Exception {
public void unknownDefaultPythonVersionValue() throws Exception {
checkError("pkg", "foo",
// error:
"invalid value in 'default_python_version' attribute: "
+ "has to be one of 'PY2' or 'PY3' instead of 'PY2AND3'",
+ "has to be one of 'PY2' or 'PY3' instead of 'doesnotexist'",
// build file:
ruleName + "(",
" name = 'foo',",
" default_python_version = 'PY2AND3',",
" default_python_version = 'doesnotexist',",
" srcs = ['foo.py'])");
}

@Test
public void goodDefaultPythonVersion() throws Exception {
scratch.file("foo/BUILD",
ruleName + "(",
" name = 'foo',",
" default_python_version = 'PY2',",
" srcs = ['foo.py'])");
getConfiguredTarget("//foo:foo");
assertNoEvents();
}

@Test
public void badSrcsVersion() throws Exception {
public void badDefaultPythonVersionValue() throws Exception {
checkError("pkg", "foo",
// error:
"invalid value in 'srcs_version' attribute: "
+ "has to be one of 'PY2', 'PY3', 'PY2AND3', 'PY2ONLY' "
+ "or 'PY3ONLY' instead of 'invalid'",
"invalid value in 'default_python_version' attribute: "
+ "has to be one of 'PY2' or 'PY3' instead of 'PY2AND3'",
// build file:
ruleName + "(",
" name = 'foo',",
" srcs_version = 'invalid',",
" srcs = ['foo.py'])");
" name = 'foo',",
" default_python_version = 'PY2AND3',",
" srcs = ['foo.py'])");
}

@Test
public void goodSrcsVersion() throws Exception {
public void goodDefaultPythonVersionValue() throws Exception {
scratch.file("foo/BUILD",
ruleName + "(",
" name = 'foo',",
" srcs_version = 'PY2',",
" srcs = ['foo.py'])");
" name = 'foo',",
" default_python_version = 'PY2',",
" srcs = ['foo.py'])");
getConfiguredTarget("//foo:foo");
assertNoEvents();
}

@Test
public void pythonVersionWith3AsDefault() throws Exception {
public void versionIs3WhenSetByDefaultPythonVersion() throws Exception {
scratch.file("foo/BUILD",
ruleName + "(",
" name = 'foo',",
Expand All @@ -99,7 +76,7 @@ public void pythonVersionWith3AsDefault() throws Exception {
}

@Test
public void pythonVersionWith2AsDefault() throws Exception {
public void versionIs2WhenSetByDefaultPythonVersion() throws Exception {
scratch.file("foo/BUILD",
ruleName + "(",
" name = 'foo',",
Expand All @@ -109,16 +86,7 @@ public void pythonVersionWith2AsDefault() throws Exception {
}

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

@Test
public void pythonVersionsWithMixedDefaults() throws Exception {
public void canBuildTwoTargetsSpecifyingDifferentVersions() throws Exception {
scratch.file("foo/BUILD",
ruleName + "(",
" name = 'foo_v2',",
Expand All @@ -134,7 +102,7 @@ public void pythonVersionsWithMixedDefaults() throws Exception {
}

@Test
public void forcePython3Version() throws Exception {
public void flagOverridesDefaultPythonVersionFrom2To3() throws Exception {
useConfiguration("--force_python=PY3");
scratch.file("foo/BUILD",
ruleName + "(",
Expand All @@ -145,7 +113,7 @@ public void forcePython3Version() throws Exception {
}

@Test
public void forcePython2Version() throws Exception {
public void flagOverridesDefaultPythonVersionFrom3To2() throws Exception {
useConfiguration("--force_python=PY2");
scratch.file("foo/BUILD",
ruleName + "(",
Expand All @@ -156,7 +124,7 @@ public void forcePython2Version() throws Exception {
}

@Test
public void forcePython2VersionMultiple() throws Exception {
public void canBuildTwoTargetsSpecifyingDifferentVersions_ForcedTo2() throws Exception {
useConfiguration("--force_python=PY2");
scratch.file("foo/BUILD",
ruleName + "(",
Expand All @@ -172,7 +140,7 @@ public void forcePython2VersionMultiple() throws Exception {
}

@Test
public void forcePython3VersionMultiple() throws Exception {
public void canBuildTwoTargetsSpecifyingDifferentVersions_ForcedTo3() throws Exception {
useConfiguration("--force_python=PY3");
scratch.file("foo/BUILD",
ruleName + "(",
Expand All @@ -187,4 +155,34 @@ public void forcePython3VersionMultiple() throws Exception {
assertThat(getPythonVersion(getConfiguredTarget("//foo:foo_v3"))).isEqualTo(PythonVersion.PY3);
}

@Test
public void srcsVersionClashesWithDefaultVersionAttr() throws Exception {
checkError("pkg", "foo",
// error:
"'//pkg:foo' can only be used with Python 2",
// build file:
ruleName + "(",
" name = 'foo',",
" srcs = [':foo.py'],",
" srcs_version = 'PY2ONLY',",
" default_python_version = 'PY3')");
}

@Test
public void srcsVersionClashesWithDefaultVersionAttr_Implicitly() throws Exception {
// Canary assertion: This'll fail when we flip the default to PY3. At that point change this
// test to use srcs_version = 'PY2ONLY' instead.
assertThat(PythonVersion.defaultTargetPythonVersion()).isEqualTo(PythonVersion.PY2);

// Fails because default_python_version is PY2 by default, so the config is set to PY2
// regardless of srcs_version.
checkError("pkg", "foo",
// error:
"'//pkg:foo' can only be used with Python 3",
// build file:
ruleName + "(",
" name = 'foo',",
" srcs = [':foo.py'],",
" srcs_version = 'PY3ONLY')");
}
}
Loading

0 comments on commit 799b034

Please sign in to comment.