Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark platform as any for testOnly platform libs #41033

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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 @@
}
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;

Check warning on line 277 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java#L276-L277

Added lines #L276 - L277 were not covered by tests
}
}
Loading