forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace BazelPythonStarlarkApiTest with PythonStarlarkApiTest
The rewritten test is non-Bazel specific and directly checks the sandwich use case. There's a slight loss in coverage that will be addressed with bazelbuild#7054. Work towards bazelbuild#1446. RELNOTES: None PiperOrigin-RevId: 230002328
- Loading branch information
1 parent
4559782
commit 5bce925
Showing
3 changed files
with
118 additions
and
96 deletions.
There are no files selected for viewing
96 changes: 0 additions & 96 deletions
96
...est/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonStarlarkApiTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/com/google/devtools/build/lib/rules/python/PythonStarlarkApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2019 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.util.BuildViewTestCase; | ||
import com.google.devtools.build.lib.packages.StructImpl; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests the ability of Starlark code to interact with the Python rules via the py provider. */ | ||
@RunWith(JUnit4.class) | ||
public class PythonStarlarkApiTest extends BuildViewTestCase { | ||
|
||
// TODO(#7054): Imports aren't propagated correctly. When we fix that, add it to the fields | ||
// tested here. | ||
|
||
/** Defines userlib in //pkg:rules.bzl, which acts as a Starlark-defined version of py_library. */ | ||
private void defineUserlibRule() throws Exception { | ||
scratch.file( | ||
"pkg/rules.bzl", | ||
"def _userlib_impl(ctx):", | ||
" dep_infos = [dep.py for dep in ctx.attr.deps]", | ||
" transitive_sources = depset(", | ||
" direct=ctx.files.srcs,", | ||
" transitive=[py.transitive_sources for py in dep_infos],", | ||
" order='postorder')", | ||
" uses_shared_libraries = \\", | ||
" any([py.uses_shared_libraries for py in dep_infos]) or \\", | ||
" ctx.attr.uses_shared_libraries", | ||
" has_py2_only_sources = \\", | ||
" any([py.has_py2_only_sources for py in dep_infos]) or \\", | ||
" ctx.attr.has_py2_only_sources", | ||
" has_py3_only_sources = \\", | ||
" any([py.has_py3_only_sources for py in dep_infos]) or \\", | ||
" ctx.attr.has_py3_only_sources", | ||
" info = struct(", | ||
" transitive_sources = transitive_sources,", | ||
" uses_shared_libraries = uses_shared_libraries,", | ||
" has_py2_only_sources = has_py2_only_sources,", | ||
" has_py3_only_sources = has_py3_only_sources)", | ||
" return struct(py=info)", | ||
"", | ||
"userlib = rule(", | ||
" implementation = _userlib_impl,", | ||
" attrs = {", | ||
" 'srcs': attr.label_list(allow_files=True),", | ||
" 'deps': attr.label_list(providers=['py']),", | ||
" 'uses_shared_libraries': attr.bool(),", | ||
" 'has_py2_only_sources': attr.bool(),", | ||
" 'has_py3_only_sources': attr.bool(),", | ||
" },", | ||
")"); | ||
} | ||
|
||
@Test | ||
public void librarySandwich() throws Exception { | ||
// Use new version semantics so we don't validate source versions in py_library. | ||
useConfiguration("--experimental_allow_python_version_transitions=true"); | ||
defineUserlibRule(); | ||
scratch.file( | ||
"pkg/BUILD", | ||
"load(':rules.bzl', 'userlib')", | ||
"userlib(", | ||
" name = 'loweruserlib',", | ||
" srcs = ['loweruserlib.py'],", | ||
" uses_shared_libraries = True,", | ||
" has_py2_only_sources = True,", | ||
")", | ||
"py_library(", | ||
" name = 'pylib',", | ||
" srcs = ['pylib.py'],", | ||
" deps = [':loweruserlib'],", | ||
" srcs_version = 'PY3ONLY'", | ||
")", | ||
"userlib(", | ||
" name = 'upperuserlib',", | ||
" srcs = ['upperuserlib.py'],", | ||
" deps = [':pylib'],", | ||
")"); | ||
StructImpl info = PyProvider.getProvider(getConfiguredTarget("//pkg:upperuserlib")); | ||
assertThat(PyProvider.getTransitiveSources(info)) | ||
.containsExactly( | ||
getSourceArtifact("pkg/loweruserlib.py"), | ||
getSourceArtifact("pkg/pylib.py"), | ||
getSourceArtifact("pkg/upperuserlib.py")); | ||
assertThat(PyProvider.getUsesSharedLibraries(info)).isTrue(); | ||
assertThat(PyProvider.getHasPy2OnlySources(info)).isTrue(); | ||
assertThat(PyProvider.getHasPy3OnlySources(info)).isTrue(); | ||
} | ||
} |