This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
BazelBaseTestCaseTest.java
141 lines (122 loc) · 5.43 KB
/
BazelBaseTestCaseTest.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
// Copyright 2017 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 build.bazel.tests.integration;
import static com.google.common.truth.Truth.assertThat;
import static org.hamcrest.core.Is.is;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
/** {@link BazelBaseTestCase}Test */
// suppress since same parameter value is ok for tests readability, tests should encapsulate and not
// hide
@SuppressWarnings("SameParameterValue")
public final class BazelBaseTestCaseTest extends BazelBaseTestCase {
private static final String WORKSPACE_NAME =
"workspace(name = 'build_bazel_integration_testing')";
@Test
public void testVersion() throws Exception {
BazelCommand cmd = driver.bazel("info", "release").mustRunSuccessfully();
assertThat(cmd.outputLines())
.contains("release " + WorkspaceDriver.globalBazelProperties().getProperty("bazel.version"));
}
@Test
public void testRunningBazelInGivenRelativeDirectory() throws Exception {
driver.scratchFile("foo/BUILD", "sh_test(name = \"bar\",\n" + "srcs = [\"bar.sh\"])");
driver.scratchExecutableFile("foo/bar.sh", "echo \"in bar\"");
// --enable_runfiles has no effect on Linux and macOS, but it enables runfiles symlink tree on Windows.
BazelCommand cmd =
driver.bazel("run", "bar", "--enable_runfiles").inWorkingDirectory(Paths.get("foo")).mustRunSuccessfully();
assertThat(cmd.outputLines()).contains("in bar");
}
@Test
public void testTestSuiteExists() throws Exception {
loadIntegrationTestRuleIntoWorkspace();
setupPassingTest("IntegrationTestSuiteTest");
// --enable_runfiles has no effect on Linux and macOS, but it enables runfiles symlink tree on Windows.
driver.bazel("test", "//:IntegrationTestSuiteTest", "--enable_runfiles").mustRunSuccessfully();
}
@Test
public void testTestSuitePropagatesTags() throws Exception {
loadIntegrationTestRuleIntoWorkspace();
setupPassingTestWithTags("IntegrationTestSuiteTest", "manual");
// --enable_runfiles has no effect on Linux and macOS, but it enables runfiles symlink tree on Windows.
// exit code 4 means testing was requested but NO tests were found
// which means manual propagated to the test suite as well as the specific tests
driver.bazel("test", "//...", "--enable_runfiles").mustRunAndReturnExitCode(4);
}
@Test
public void testJvmFlags() {
org.hamcrest.MatcherAssert.assertThat(System.getProperty("foo.bar"), is("true"));
}
private void setupPassingTestWithTags(final String testName, final String tag) throws IOException {
writePassingTestJavaSource(testName);
writeTestBuildFile(testName, "'" + tag + "'");
}
private void setupPassingTest(final String testName) throws IOException {
writePassingTestJavaSource(testName);
writeTestBuildFile(testName, "");
}
private void writeTestBuildFile(final String testName, final String tag) throws IOException {
driver.scratchFile(
"BUILD",
"load('//tools:bazel_java_integration_test.bzl', 'bazel_java_integration_test')",
"",
"bazel_java_integration_test(",
" name = '" + testName + "',",
" test_class = '" + testName + "',",
" srcs = ['" + testName + ".java'],",
" tags = [" + tag + "],",
// inside the sandbox we don't have access to full bazel
// and we don't need it since it's prepared in advance for us
" add_bazel_data_dependency = False,",
")");
}
private void loadIntegrationTestRuleIntoWorkspace() throws IOException {
setupRuleSkylarkFiles();
setupRuleCode();
driver.scratchFile("./WORKSPACE", WORKSPACE_NAME);
}
private void setupRuleCode() throws IOException {
driver.copyFromRunfiles(
"build_bazel_integration_testing/java/build/bazel/tests/integration/libworkspace_driver.jar",
"java/build/bazel/tests/integration/libworkspace_driver.jar");
driver.scratchFile(
"java/build/bazel/tests/integration/BUILD.bazel",
"java_import(",
" name = 'workspace_driver',",
" jars = ['libworkspace_driver.jar'],",
" visibility = ['//visibility:public']",
")");
}
private void setupRuleSkylarkFiles() throws IOException {
driver.copyDirectoryFromRunfiles(
"build_bazel_integration_testing/tools", "build_bazel_integration_testing");
//overwrite it since it relates to targets we don't need
driver.scratchFile("tools/BUILD","");
}
private void writePassingTestJavaSource(final String testName) throws IOException {
driver.scratchFile("" + testName + ".java", somePassingTestNamed(testName));
}
private List<String> somePassingTestNamed(final String testName) {
return Arrays.asList(
"import org.junit.Test;",
"public class " + testName + " {",
" @Test",
" public void testSuccess() {",
" }",
"}");
}
}