Skip to content

Commit

Permalink
Merge pull request #41033 from ShammiL/testonly-any-dependency
Browse files Browse the repository at this point in the history
Mark platform as any for testOnly platform libs
  • Loading branch information
azinneera authored Aug 15, 2023
2 parents 8c04b7b + 1748cb7 commit cebd881
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void testPackStandaloneFile() throws IOException {

@Test(description = "Pack a package with platform libs")
public void testPackageWithPlatformLibs() throws IOException {
Path projectPath = this.testResources.resolve("validProjectWithPlatformLibs");
Path projectPath = this.testResources.resolve("validGraalvmCompatibleProjectWithPlatformLibs");
System.setProperty("user.dir", projectPath.toString());
PackCommand packCommand = new PackCommand(projectPath, printStream, printStream, false, true);
new CommandLine(packCommand).parseArgs();
Expand All @@ -168,6 +168,36 @@ public void testPackageWithPlatformLibs() throws IOException {
.toFile().exists());
}

@Test(description = "Pack a package with testOnly platform libs")
public void testPackageWithTestOnlyPlatformLibs() throws IOException {
Path projectPath = this.testResources.resolve("projectWithTestOnlyPlatformLibs");
System.setProperty("user.dir", projectPath.toString());
PackCommand packCommand = new PackCommand(projectPath, printStream, printStream, false, true);
new CommandLine(packCommand).parseArgs();
packCommand.execute();
String buildLog = readOutput(true);

Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("pack-project-with-test-only-platform-libs.txt"));
Assert.assertTrue(projectPath.resolve("target").resolve("bala").resolve("sameera-myproject-any-0.1.0.bala")
.toFile().exists());
}

@Test(description = "Pack a package with ballerina/java imports only in tests")
public void testPackageWithTestOnlyJavaImports() throws IOException {
Path projectPath = this.testResources.resolve("projectWithTestOnlyJavaImports");
System.setProperty("user.dir", projectPath.toString());
PackCommand packCommand = new PackCommand(projectPath, printStream, printStream, false, true);
new CommandLine(packCommand).parseArgs();
packCommand.execute();
String buildLog = readOutput(true);

Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("pack-project-with-test-only-platform-libs.txt"));
Assert.assertTrue(projectPath.resolve("target").resolve("bala").resolve("sameera-myproject-any-0.1.0.bala")
.toFile().exists());
}

@Test(description = "Pack a package with an empty Dependencies.toml")
public void testPackageWithEmptyDependenciesToml() throws IOException {
Path projectPath = this.testResources.resolve("validProjectWithDependenciesToml");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Compiling source
sameera/myproject:0.1.0

Creating bala
target/bala/sameera-myproject-any-0.1.0.bala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Compiling source
sameera/myproject:0.1.0

Creating bala
target\bala\sameera-myproject-any-0.1.0.bala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "sameera"
name = "myproject"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

public function main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/jballerina.java;

public function printInternal(handle receiver, handle strValue) = @java:Method {
name: "println",
'class: "java/io/PrintStream",
paramTypes: ["java.lang.String"]
} external;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
org = "sameera"
name = "myproject"
version = "0.1.0"

[platform.java11]
graalvmCompatible = true

[[platform.java11.dependency]]
path = "./libs/one-1.0.0.jar"
scope = "testOnly"
graalvmCompatible = true
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

public function main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
org = "sameera"
name = "myproject"
version = "0.1.0"

[platform.java11]
graalvmCompatible = true

[[platform.java11.dependency]]
path = "./libs/one-1.0.0.jar"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

public function main() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.zip.ZipOutputStream;

Expand Down Expand Up @@ -227,15 +228,18 @@ private CompilerBackend.TargetPlatform getTargetPlatform(PackageResolution pkgRe
// 1) Check direct dependencies of imports in the package have any `ballerina/java` dependency
for (ResolvedPackageDependency dependency : resolvedPackageDependencies) {
if (dependency.packageInstance().packageOrg().value().equals(Names.BALLERINA_ORG.value) &&
dependency.packageInstance().packageName().value().equals(Names.JAVA.value)) {
dependency.packageInstance().packageName().value().equals(Names.JAVA.value) &&
!dependency.scope().equals(PackageDependencyScope.TEST_ONLY)) {
return this.backend.targetPlatform();
}
}

// 2) Check package has defined any platform dependency
PackageManifest manifest = this.packageContext.project().currentPackage().manifest();
if (manifest.platform(this.backend.targetPlatform().code()) != null &&
!manifest.platform(this.backend.targetPlatform().code()).dependencies().isEmpty()) {
PackageManifest.Platform manifestPlatform = manifest.platform(this.backend.targetPlatform().code());
if (manifestPlatform != null &&
!manifestPlatform.dependencies().isEmpty() &&
!isPlatformDependenciesTestOnly(manifestPlatform.dependencies())) {
return this.backend.targetPlatform();
}

Expand Down Expand Up @@ -263,4 +267,13 @@ private Optional<BalToolDescriptor> readBalToolToml() {
}
return Optional.empty();
}

private boolean isPlatformDependenciesTestOnly(List<Map<String, Object>> dependencies) {
for (Map<String, Object> dependency : dependencies) {
if (null == dependency.get("scope") || dependency.get("scope").equals("default")) {
return false;
}
}
return true;
}
}

0 comments on commit cebd881

Please sign in to comment.