Skip to content

Commit

Permalink
Open-source some Python rules unit tests
Browse files Browse the repository at this point in the history
This is a precursor to fixing #1446.

RELNOTES: None
PiperOrigin-RevId: 212267168
  • Loading branch information
brandjon authored and Copybara-Service committed Sep 10, 2018
1 parent c1d8776 commit 1cdc3bf
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ A string specifying the Python major version(s) that the <code>.py</code> source
.add(attr("srcs_version", STRING)
.value(PythonVersion.defaultSrcsVersion().toString())
.allowedValues(new AllowedValueSet(PythonVersion.getAllValues())))
// TODO(brandjon): Consider adding to py_interpreter a .mandatoryNativeProviders() of
// BazelPyRuntimeProvider. (Add a test case to PythonConfigurationTest for violations
// of this requirement.)
.add(attr(":py_interpreter", LABEL).value(PY_INTERPRETER))
// do not depend on lib2to3:2to3 rule, because it creates circular dependencies
// 2to3 is itself written in Python and depends on many libraries.
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ filegroup(
"//src/test/java/com/google/devtools/build/lib/rules/cpp:srcs",
"//src/test/java/com/google/devtools/build/lib/rules/objc:srcs",
"//src/test/java/com/google/devtools/build/lib/rules/platform:srcs",
"//src/test/java/com/google/devtools/build/lib/rules/python:srcs",
"//src/test/java/com/google/devtools/build/lib/rules/repository:srcs",
"//src/test/java/com/google/devtools/build/lib/skyframe/packages:srcs",
"//src/test/java/com/google/devtools/build/lib/skyframe/serialization:srcs",
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/com/google/devtools/build/lib/rules/python/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package(
default_testonly = True,
)

filegroup(
name = "srcs",
testonly = 0,
srcs = glob(["**"]),
visibility = ["//src:__subpackages__"],
)

test_suite(
name = "PythonTests",
tests = [
":BazelPythonConfigurationTest",
":PythonConfigurationTest",
],
)

java_test(
name = "BazelPythonConfigurationTest",
srcs = ["BazelPythonConfigurationTest.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/common/options",
"//src/test/java/com/google/devtools/build/lib:analysis_testutil",
"//src/test/java/com/google/devtools/build/lib:testutil",
"//third_party:junit4",
"//third_party:truth",
],
)

java_test(
name = "PythonConfigurationTest",
srcs = ["PythonConfigurationTest.java"],
deps = [
"//src/main/java/com/google/devtools/common/options",
"//src/test/java/com/google/devtools/build/lib:analysis_testutil",
"//src/test/java/com/google/devtools/build/lib:testutil",
"//third_party:junit4",
"//third_party:truth",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;

import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
import com.google.devtools.build.lib.analysis.util.ConfigurationTestCase;
import com.google.devtools.build.lib.bazel.rules.python.BazelPythonConfiguration;
import com.google.devtools.common.options.OptionsParsingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link BazelPythonConfiguration}. */
@RunWith(JUnit4.class)
public class BazelPythonConfigurationTest extends ConfigurationTestCase {

@Test
public void pythonTop() throws Exception {
scratch.file(
"a/BUILD",
"py_runtime(name='b', files=[], interpreter='c')");
BazelPythonConfiguration config = create("--python_top=//a:b")
.getFragment(BazelPythonConfiguration.class);
assertThat(config.getPythonTop()).isNotNull();
}

@Test
public void pythonTop_malformedLabel() {
OptionsParsingException expected =
assertThrows(
OptionsParsingException.class,
() -> create("--python_top=//a:!b:"));
assertThat(expected).hasMessageThat().contains("While parsing option --python_top");
}

@Test
public void pythonPath() throws Exception {
BazelPythonConfiguration config = create("--python_path=/pajama")
.getFragment(BazelPythonConfiguration.class);
assertThat(config.getPythonPath()).isEqualTo("/pajama");
}

@Test
public void pythonPathMustBeAbsolute() {
InvalidConfigurationException expected = assertThrows(
InvalidConfigurationException.class,
() -> create("--python_path=pajama"));
assertThat(expected).hasMessageThat().contains("python_path must be an absolute path");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;

import com.google.devtools.build.lib.analysis.util.ConfigurationTestCase;
import com.google.devtools.common.options.OptionsParsingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link PythonConfiguration}. */
@RunWith(JUnit4.class)
public class PythonConfigurationTest extends ConfigurationTestCase {

@Test
public void invalidForcePythonValue_NotATargetValue() throws Exception {
checkError("'--force_python' argument must be 'PY2' or 'PY3'", "--force_python=PY2AND3");
}

@Test
public void invalidForcePythonValue_UnknownValue() {
OptionsParsingException expected =
assertThrows(
OptionsParsingException.class,
() -> create("--force_python=BEETLEJUICE"));
assertThat(expected).hasMessageThat()
.contains("While parsing option --force_python=BEETLEJUICE: Not a valid Python version");
}
}

0 comments on commit 1cdc3bf

Please sign in to comment.