-
Notifications
You must be signed in to change notification settings - Fork 4k
/
PyBinaryConfiguredTargetTest.java
218 lines (198 loc) · 7.19 KB
/
PyBinaryConfiguredTargetTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// 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.configuredtargets.FileConfiguredTarget;
import com.google.devtools.build.lib.testutil.TestConstants;
import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for {@code py_binary}. */
@RunWith(JUnit4.class)
public class PyBinaryConfiguredTargetTest extends PyExecutableConfiguredTargetTestBase {
public PyBinaryConfiguredTargetTest() {
super("py_binary");
}
/**
* Creates a target //pkg:bin with the given version attr and that depends on a target //pkg:lib
* having the given sources version attr.
*/
private void declareBinDependingOnLibWithVersions(String binVersion, String libSrcsVersion)
throws Exception {
scratch.file(
"pkg/BUILD",
"py_library(name = 'lib',",
" srcs = [],",
" srcs_version = '" + libSrcsVersion + "')",
"py_binary(name = 'bin',",
" srcs = ['bin.py'],",
" deps = [':lib'],",
" python_version = '" + binVersion + "')");
}
@Test
public void python2WithPy3SrcsVersionDependency() throws Exception {
PythonTestUtils.assumeIsBazel(); // Google has py2 disabled
setBuildLanguageOptions(
"--experimental_builtins_injection_override=-py_test,-py_binary,-py_library");
declareBinDependingOnLibWithVersions("PY2", "PY3");
assertThat(getPyExecutableDeferredError("//pkg:bin"))
.startsWith(
"//pkg:bin: This target is being built for Python 2 but (transitively) "
+ "includes Python 3-only sources");
}
@Test
public void python2WithPy3OnlySrcsVersionDependency() throws Exception {
PythonTestUtils.assumeIsBazel(); // Google has py2 disabled
setBuildLanguageOptions(
"--experimental_builtins_injection_override=-py_test,-py_binary,-py_library");
declareBinDependingOnLibWithVersions("PY2", "PY3ONLY");
assertThat(getPyExecutableDeferredError("//pkg:bin"))
.contains("being built for Python 2 but (transitively) includes Python 3-only sources");
}
@Test
public void python3WithPy2OnlySrcsVersionDependency_NewSemantics() throws Exception {
PythonTestUtils.assumeIsBazel(); // Google has py2 disabled
setBuildLanguageOptions(
"--experimental_builtins_injection_override=-py_test,-py_binary,-py_library");
declareBinDependingOnLibWithVersions("PY3", "PY2ONLY");
assertThat(getPyExecutableDeferredError("//pkg:bin"))
.contains("being built for Python 3 but (transitively) includes Python 2-only sources");
}
@Test
public void filesToBuild() throws Exception {
scratch.file("pkg/BUILD",
"py_binary(",
" name = 'foo',",
" srcs = ['foo.py'])");
ConfiguredTarget target = getOkPyTarget("//pkg:foo");
FileConfiguredTarget srcFile = getFileConfiguredTarget("//pkg:foo.py");
assertThat(getFilesToBuild(target).toList())
.containsExactly(getExecutable(target), srcFile.getArtifact());
assertThat(getExecutable(target).getExecPath().getPathString())
.containsMatch(TestConstants.PRODUCT_NAME + "-out/.*/bin/pkg/foo");
}
@Test
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'",
// build file:
"py_binary(",
" name = 'foo',",
" deps = [':bar'])",
"py_library(",
" name = 'bar',",
" srcs = ['bar.py'])");
}
@Test
public void defaultMainMustBeInSrcs() throws Exception {
checkError("pkg", "app",
// error:
"corresponding default 'app.py' does not appear",
// build file:
"py_binary(",
" name = 'app',",
" srcs = ['foo.py', 'bar.py'])");
}
@Test
public void explicitMain() throws Exception {
scratch.file("pkg/BUILD",
"py_binary(",
" name = 'foo',",
" main = 'foo.py',",
" srcs = ['foo.py', 'bar.py'])");
getOkPyTarget("//pkg:foo"); // should not fail
}
@Test
public void explicitMainMustBeInSrcs() throws Exception {
checkError("pkg", "foo",
// error:
"could not find 'foo.py'",
// build file:
"py_binary(",
" name = 'foo',",
" main = 'foo.py',",
" srcs = ['bar.py', 'baz.py'])");
}
@Test
public void defaultMainCannotBeAmbiguous() throws Exception {
scratch.file("pkg1/BUILD",
"py_binary(",
" name = 'foo',",
" srcs = ['bar.py'])");
checkError(
"pkg2",
"bar",
// error:
Pattern.compile(".*bar.py.*matches multiple.*"),
// build file:
"py_binary(",
" name = 'bar',",
" srcs = ['bar.py', '//pkg1:bar.py'])");
}
@Test
public void explicitMainCannotBeAmbiguous() throws Exception {
scratch.file("pkg1/BUILD",
"py_binary(",
" name = 'foo',",
" srcs = ['bar.py'])");
checkError(
"pkg2",
"foo",
// error:
Pattern.compile(".*bar.py.*matches multiple.*"),
// build file:
"py_binary(",
" name = 'foo',",
" main = 'bar.py',",
" srcs = ['bar.py', '//pkg1:bar.py'])");
}
@Test
public void nameCannotEndInPy() throws Exception {
checkError("pkg", "foo.py",
// error:
"name must not end in '.py'",
// build file:
"py_binary(",
" name = 'foo.py',",
" srcs = ['bar.py'])");
}
@Test
public void defaultMainCanBeGenerated() throws Exception {
scratch.file("pkg/BUILD",
"genrule(",
" name = 'gen_py',",
" cmd = 'touch $(location foo.py)',",
" outs = ['foo.py'])",
"py_binary(",
" name = 'foo',",
" srcs = [':gen_py'])");
getOkPyTarget("//pkg:foo"); // should not fail
}
@Test
public void defaultMainCanHaveMultiplePathSegments() throws Exception {
// Regression test for crash caused by use of getChild on a multi-segment rule name.
scratch.file("pkg/BUILD",
"py_binary(",
" name = 'foo/bar',",
" srcs = ['foo/bar.py'])");
getOkPyTarget("//pkg:foo/bar"); // should not fail
}
// TODO(brandjon): Add tests for content of stub Python script (particularly for choosing python
// 2 or 3).
}