Skip to content

Commit 4815132

Browse files
authored
test support for root package targets (#416)
1 parent 3ac372a commit 4815132

File tree

6 files changed

+119
-30
lines changed

6 files changed

+119
-30
lines changed

bundles/com.salesforce.bazel-java-sdk.testframework/src/com/salesforce/bazel/sdk/workspace/test/TestBazelWorkspaceFactory.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,17 @@ protected void buildJavaWorkspaceElements() throws Exception {
178178

179179
/**
180180
* Subclassable hook for creating all of the Java packages.
181-
*
182-
* @param packageParentDir
183-
* the //projects/libs directory on the file system, where you are expected to place the Bazel packages
184-
* @param packageParentDirBazelRelPath
185-
* the relative path for the Bazel label (not File separated) for the Bazel packages (e.g. projects/libs)
186181
*/
187182
protected void buildJavaPackages() throws Exception {
188183
int numJavaPackages = workspaceDescriptor.testOptions.numberOfJavaPackages;
189184

185+
// does the test want a BUILD file in the root of the workspace with targets?
186+
if (workspaceDescriptor.testOptions.hasRootPackage) {
187+
String packageName = "";
188+
javaPackageFactory.createJavaPackage(workspaceDescriptor, packageName, "",
189+
workspaceDescriptor.workspaceRootDirectory, 0, true);
190+
}
191+
190192
for (int i = 0; i < numJavaPackages; i++) {
191193

192194
// Do the heavy lifting to fully simulate a Java package with source, test source code
@@ -204,23 +206,13 @@ protected void buildJavaPackages() throws Exception {
204206

205207
/**
206208
* Subclassable hook for creating all of the Genrule elements of the workspace.
207-
*
208-
* @param packageParentDir
209-
* the //projects/libs directory on the file system, where you are expected to place the Bazel packages
210-
* @param packageParentDirBazelRelPath
211-
* the relative path for the Bazel label (not File separated) for the Bazel packages (e.g. projects/libs)
212209
*/
213210
protected void buildGenruleWorkspaceElements() throws Exception {
214211
// Genrule usually does not require anything loaded into the WORKSPACE
215212
}
216213

217214
/**
218215
* Subclassable hook for creating all of the Genrule packages.
219-
*
220-
* @param packageParentDir
221-
* the //projects/libs directory on the file system, where you are expected to place the Bazel packages
222-
* @param packageParentDirBazelRelPath
223-
* the relative path for the Bazel label (not File separated) for the Bazel packages (e.g. projects/libs)
224216
*/
225217
protected void buildGenrulePackages() throws Exception {
226218
for (int i = 0; i < workspaceDescriptor.testOptions.numberGenrulePackages; i++) {

bundles/com.salesforce.bazel-java-sdk.testframework/src/com/salesforce/bazel/sdk/workspace/test/TestOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public TestOptions bazelVersion(String version) {
5858
return this;
5959
}
6060

61+
// should the Workspace root contain a BUILD file with targets?
62+
public boolean hasRootPackage = false;
63+
64+
public TestOptions hasRootPackage(boolean rootPackage) {
65+
hasRootPackage = rootPackage;
66+
return this;
67+
}
68+
6169
// number of packages with Java rules to create in the test workspace
6270
public int numberOfJavaPackages = 0;
6371

bundles/com.salesforce.bazel-java-sdk.testframework/src/com/salesforce/bazel/sdk/workspace/test/java/TestJavaPackageFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public void createJavaPackage(TestBazelWorkspaceDescriptor workspaceDescriptor,
6161

6262
String packageRelativeBazelPath = packageRelativePath + "/" + packageName; // $SLASH_OK bazel path
6363
String packageRelativeFilePath = FSPathHelper.osSeps(packageRelativeBazelPath);
64+
if (packageName.equals("")) {
65+
// hacky signal that this package is the root package //
66+
packageRelativeBazelPath = "";
67+
packageRelativeFilePath = "";
68+
packageName = "rootLib";
69+
}
70+
71+
// create the package directory
6472
javaPackageDir.mkdir();
6573

6674
// create the catalog entries

tests/com.salesforce.bazel.eclipse.core.tests/src/com/salesforce/bazel/eclipse/classpath/BazelClasspathContainerFTest.java

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ public void testClasspath_BazelJavaProject_implicitdeps() throws Exception {
9999
// javalib0 is the base Java library created by our test harness
100100
// javalib1 is the second Java library created by our test harness, and it is made to depend on javalib0
101101
boolean explicitJavaTestDeps = false;
102+
boolean nonstandardlayout = false;
103+
boolean nonstandardlayout_multipledirs = false;
104+
boolean hasRootPackage = false;
105+
boolean addJavaImport = false;
102106

103-
setupMockEnvironmentForClasspathTest("tcpbjp_im", explicitJavaTestDeps, false, false, false);
107+
setupMockEnvironmentForClasspathTest("tcpbjp_im", explicitJavaTestDeps, nonstandardlayout,
108+
nonstandardlayout_multipledirs, hasRootPackage, addJavaImport);
104109
JavaCoreHelper javaHelper = ComponentContext.getInstance().getJavaCoreHelper();
105110

106111
// FIRST check that the project raw classpath has 4 entries for javalib0:
@@ -148,8 +153,14 @@ public void testClasspath_BazelJavaProject_explicitdeps() throws Exception {
148153
// javalib0 is the base Java library created by our test harness
149154
// javalib1 is the second Java library created by our test harness, and it is made to depend on javalib0
150155
boolean explicitJavaTestDeps = true;
156+
boolean nonstandardlayout = false;
157+
boolean nonstandardlayout_multipledirs = false;
158+
boolean hasRootPackage = false;
159+
boolean addJavaImport = false;
160+
161+
setupMockEnvironmentForClasspathTest("tcpbjp_ex", explicitJavaTestDeps, nonstandardlayout,
162+
nonstandardlayout_multipledirs, hasRootPackage, addJavaImport);
151163

152-
setupMockEnvironmentForClasspathTest("tcpbjp_ex", explicitJavaTestDeps, false, false, false);
153164
JavaCoreHelper javaHelper = ComponentContext.getInstance().getJavaCoreHelper();
154165

155166
// FIRST check that the project raw classpath has 6 entries for javalib0:
@@ -206,10 +217,11 @@ public void testClasspath_BazelJavaProject_nonstandardLayout() throws Exception
206217
boolean explicitJavaTestDeps = true;
207218
boolean nonstandardlayout = true;
208219
boolean nonstandardlayout_multipledirs = false;
220+
boolean hasRootPackage = false;
209221
boolean addJavaImport = false;
210222

211223
setupMockEnvironmentForClasspathTest("tcpbjp_ex", explicitJavaTestDeps, nonstandardlayout,
212-
nonstandardlayout_multipledirs, addJavaImport);
224+
nonstandardlayout_multipledirs, hasRootPackage, addJavaImport);
213225

214226
JavaCoreHelper javaHelper = ComponentContext.getInstance().getJavaCoreHelper();
215227

@@ -240,10 +252,11 @@ public void testClasspath_BazelJavaProject_nonstandardLayout_multiple() throws E
240252
boolean explicitJavaTestDeps = true;
241253
boolean nonstandardlayout = true;
242254
boolean nonstandardlayout_multipledirs = true;
255+
boolean hasRootPackage = false;
243256
boolean addJavaImport = false;
244257

245258
setupMockEnvironmentForClasspathTest("tcpbjp_ex", explicitJavaTestDeps, nonstandardlayout,
246-
nonstandardlayout_multipledirs, addJavaImport);
259+
nonstandardlayout_multipledirs, hasRootPackage, addJavaImport);
247260

248261
JavaCoreHelper javaHelper = ComponentContext.getInstance().getJavaCoreHelper();
249262

@@ -275,10 +288,11 @@ public void testClasspath_BazelJavaProject_javaimport() throws Exception {
275288
boolean explicitJavaTestDeps = true;
276289
boolean nonstandardlayout = false;
277290
boolean nonstandardlayout_multipledirs = false;
291+
boolean hasRootPackage = false;
278292
boolean addJavaImport = true;
279293

280294
setupMockEnvironmentForClasspathTest("tcpbjp_ex", explicitJavaTestDeps, nonstandardlayout,
281-
nonstandardlayout_multipledirs, addJavaImport);
295+
nonstandardlayout_multipledirs, hasRootPackage, addJavaImport);
282296

283297
JavaCoreHelper javaHelper = ComponentContext.getInstance().getJavaCoreHelper();
284298

@@ -312,29 +326,62 @@ public void testClasspath_BazelJavaProject_javaimport() throws Exception {
312326

313327
/**
314328
* We create an Eclipse project for the Bazel Workspace as a container, make sure we return empty results for
315-
* classpath entries for the Workspace project.
329+
* classpath entries for the Workspace project if there is no root BUILD file.
316330
*/
317331
@Test
318332
public void testClasspath_BazelWorkspaceProject() throws Exception {
319333
boolean explicitJavaTestDeps = false;
320-
MockEclipse mockEclipse =
321-
setupMockEnvironmentForClasspathTest("tcpbwp", explicitJavaTestDeps, false, false, false);
334+
boolean nonstandardlayout = false;
335+
boolean nonstandardlayout_multipledirs = false;
336+
boolean hasRootPackage = false;
337+
boolean addJavaImport = false;
338+
339+
MockEclipse mockEclipse = setupMockEnvironmentForClasspathTest("tcpbwp", explicitJavaTestDeps,
340+
nonstandardlayout, nonstandardlayout_multipledirs, hasRootPackage, addJavaImport);
341+
322342
ResourceHelper resourceHelper = mock(ResourceHelper.class);
323343
when(resourceHelper.isBazelRootProject(workspace_IProject)).thenReturn(true);
324344

325345
BazelClasspathContainer classpathContainer = new BazelClasspathContainer(workspace_IProject, resourceHelper,
326346
mockEclipse.getMockJavaCoreHelper(), mockEclipse.getProjectManager(), mockEclipse.getOsEnvStrategy(),
327-
EclipseBazelWorkspaceContext.getInstance().getBazelWorkspace());
347+
EclipseBazelWorkspaceContext.getInstance().getBazelWorkspace());
328348
IClasspathEntry[] entries = classpathContainer.getClasspathEntries();
329349

330350
assertNotNull(entries);
331351
assertEquals(0, entries.length);
332352
}
333353

354+
/**
355+
* We create an Eclipse project for the Bazel Workspace as a container, make sure we return results for classpath
356+
* entries for the Workspace project if there is a root BUILD file.
357+
*/
358+
@Test
359+
public void testClasspath_BazelWorkspaceProject_withRootPackage() throws Exception {
360+
boolean explicitJavaTestDeps = false;
361+
boolean nonstandardlayout = false;
362+
boolean nonstandardlayout_multipledirs = false;
363+
boolean hasRootPackage = true;
364+
boolean addJavaImport = false;
365+
366+
MockEclipse mockEclipse = setupMockEnvironmentForClasspathTest("tcpbwp-wrp", explicitJavaTestDeps,
367+
nonstandardlayout, nonstandardlayout_multipledirs, hasRootPackage, addJavaImport);
368+
ResourceHelper resourceHelper = mock(ResourceHelper.class);
369+
when(resourceHelper.isBazelRootProject(workspace_IProject)).thenReturn(true);
370+
371+
BazelClasspathContainer classpathContainer = new BazelClasspathContainer(workspace_IProject, resourceHelper,
372+
mockEclipse.getMockJavaCoreHelper(), mockEclipse.getProjectManager(), mockEclipse.getOsEnvStrategy(),
373+
EclipseBazelWorkspaceContext.getInstance().getBazelWorkspace());
374+
IClasspathEntry[] entries = classpathContainer.getClasspathEntries();
375+
376+
assertNotNull(entries);
377+
assertEquals(2, entries.length);
378+
}
379+
334380
// HELPERS
335381

336382
private MockEclipse setupMockEnvironmentForClasspathTest(String testName, boolean explicitJavaTestDeps,
337-
boolean nonstandardLayout, boolean nonstandardMultipleDirs, boolean addJavaImport) throws Exception {
383+
boolean nonstandardLayout, boolean nonstandardMultipleDirs, boolean hasRootPackage, boolean addJavaImport)
384+
throws Exception {
338385
File testDir = tmpFolder.newFolder();
339386
File testTempDir = new File(testDir, testName);
340387
testTempDir.mkdirs();
@@ -347,7 +394,8 @@ private MockEclipse setupMockEnvironmentForClasspathTest(String testName, boolea
347394

348395
TestOptions testOptions = new TestOptions().uniqueKey(testName).numberOfJavaPackages(2)
349396
.explicitJavaTestDeps(explicitJavaTestDeps).nonStandardJavaLayout_enabled(nonstandardLayout)
350-
.nonStandardJavaLayout_multipledirs(nonstandardMultipleDirs).addJavaImportRule(addJavaImport);
397+
.nonStandardJavaLayout_multipledirs(nonstandardMultipleDirs).addJavaImportRule(addJavaImport)
398+
.hasRootPackage(hasRootPackage);
351399
String wsName = MockEclipse.BAZEL_WORKSPACE_NAME + "-" + testOptions.uniqueKey;
352400

353401
boolean computeClasspaths = true;
@@ -362,7 +410,9 @@ private MockEclipse setupMockEnvironmentForClasspathTest(String testName, boolea
362410
javalib1_IJavaProject =
363411
ComponentContext.getInstance().getJavaCoreHelper().getJavaProjectForProject(javalib1_IProject);
364412

365-
assertEquals(2, mockEclipse.getBazelWorkspaceCreator().workspaceDescriptor.createdPackages.size());
413+
int expectedCreatedPackagesCount = hasRootPackage ? 3 : 2;
414+
assertEquals(expectedCreatedPackagesCount,
415+
mockEclipse.getBazelWorkspaceCreator().workspaceDescriptor.createdPackages.size());
366416

367417
return mockEclipse;
368418
}

tests/com.salesforce.bazel.eclipse.core.tests/src/com/salesforce/bazel/eclipse/config/BazelEclipseProjectFactoryFTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class BazelEclipseProjectFactoryFTest {
5555
public TemporaryFolder tmpFolder = new TemporaryFolder();
5656

5757
private IProject workspace_IProject;
58+
private IJavaProject workspace_IJavaProject;
5859
private IProject javalib0_IProject;
5960
private IJavaProject javalib0_IJavaProject;
6061
private IProject javalib1_IProject;
@@ -119,6 +120,34 @@ public void testImportWorkspace() throws Exception {
119120
assertEquals("javalib0", javalib1_refs[0].getName());
120121
}
121122

123+
@Test
124+
public void testImportWorkspace_withRootPackage() throws Exception {
125+
File testTempDir = tmpFolder.newFolder();
126+
// during test development, it can be useful to have a stable location on disk for the Bazel workspace contents
127+
//testTempDir = new File("/tmp/bef/bazelws");
128+
//testTempDir.mkdirs();
129+
130+
// create the mock Eclipse runtime in the correct state, which is two java projects root-package0 and javalib0
131+
TestOptions testOptions = new TestOptions().uniqueKey("imws").numberOfJavaPackages(1)
132+
.explicitJavaTestDeps(false).hasRootPackage(true);
133+
String wsName = MockEclipse.BAZEL_WORKSPACE_NAME + "-imws";
134+
135+
boolean computeClasspaths = true;
136+
MockEclipse mockEclipse = EclipseFunctionalTestEnvironmentFactory
137+
.createMockEnvironment_Imported_All_JavaPackages(testTempDir, testOptions, computeClasspaths);
138+
workspace_IProject = mockEclipse.getImportedProject("Bazel Workspace (" + wsName + ")");
139+
assertNotNull(workspace_IProject);
140+
workspace_IJavaProject = mockEclipse.getMockJavaCoreHelper().getJavaProjectForProject(workspace_IProject);
141+
assertNotNull(workspace_IJavaProject);
142+
143+
// Eclipse project for the Bazel workspace
144+
assertNotNull(workspace_IProject);
145+
assertEquals("Bazel Workspace (" + wsName + ")", workspace_IProject.getName());
146+
IProjectDescription workspace_description =
147+
ComponentContext.getInstance().getResourceHelper().getProjectDescription(workspace_IProject);
148+
hasNature("workspace", workspace_description.getNatureIds(), true, true);
149+
}
150+
122151
private void hasNature(String projectName, String[] natureIds, boolean expectJava, boolean expectBazel) {
123152
boolean hasJava = false;
124153
boolean hasBazel = false;

tests/com.salesforce.bazel.eclipse.core.tests/src/com/salesforce/bazel/eclipse/mock/EclipseFunctionalTestEnvironmentFactory.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ public static MockEclipse createMockEnvironment_Imported_All_JavaPackages(File t
103103

104104
// choose the list of Bazel packages to import, in this case we assume the user selected all Java packages
105105
List<BazelPackageLocation> bazelPackagesToImport = new ArrayList<>();
106-
bazelPackagesToImport.add(workspaceRootProject);
107-
addBazelPackageInfosToSelectedList(workspaceRootProject, bazelPackagesToImport);
106+
if (testOptions.hasRootPackage) {
107+
bazelPackagesToImport.add(workspaceRootProject);
108+
}
109+
addChildPackagesToImportList(workspaceRootProject, bazelPackagesToImport);
108110

109111
ProjectImporterFactory projectImporterFactory =
110112
new ProjectImporterFactory(workspaceRootProject, bazelPackagesToImport);
@@ -127,7 +129,7 @@ public static MockEclipse createMockEnvironment_Imported_All_JavaPackages(File t
127129
return mockEclipse;
128130
}
129131

130-
private static void addBazelPackageInfosToSelectedList(BazelPackageInfo currentNode,
132+
private static void addChildPackagesToImportList(BazelPackageInfo currentNode,
131133
List<BazelPackageLocation> bazelPackagesToImport) {
132134
Collection<BazelPackageInfo> children = currentNode.getChildPackageInfos();
133135
bazelPackagesToImport.addAll(children);

0 commit comments

Comments
 (0)