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

[2201.8.x] Check org, name for null before package resolution when resolving dependencies in Ballerina.toml #42721

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 @@ -1315,4 +1315,20 @@ public void testGraalVMCompatibilityOfAnyProject() throws IOException {
&& !buildLog.contains("WARNING: Package is not verified with GraalVM"));
}
}

@Test(description = "Build a project with invalid fields in TOML array 'dependency'")
public void testBuildProjectWithInvalidDependencyArrayInBallerinaToml() throws IOException {
Path projectPath = this.testResources.resolve("invalid-dependency-array-project");
System.setProperty("user.dir", projectPath.toString());
BuildCommand buildCommand = new BuildCommand(projectPath, printStream, printStream, false);
new CommandLine(buildCommand).parseArgs();
try {
buildCommand.execute();
} catch (BLauncherException e) {
Assert.assertEquals("error: compilation contains errors", e.getDetailedMessages().get(0));
String buildLog = readOutput(true);
Assert.assertEquals(buildLog.replaceAll("\r", ""),
getOutput("build-bal-project-with-invalid-dependency-array.txt"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Compiling source
foo/winery:0.1.0
ERROR [Ballerina.toml:(7:7,7:19)] invalid 'org' under [[dependency]]: 'org' can only contain alphanumerics and underscores
ERROR [Ballerina.toml:(8:8,8:15)] invalid 'name' under [[dependency]]: 'name' can only contain alphanumerics, underscores and periods
ERROR [Ballerina.toml:(9:11,9:19)] invalid 'version' under [[dependency]]: 'version' should be compatible with semver
ERROR [Ballerina.toml:(12:1,16:21)] 'name' under [[dependency]] is missing
ERROR [Ballerina.toml:(12:1,16:21)] 'org' under [[dependency]] is missing
ERROR [Ballerina.toml:(12:1,16:21)] 'version' under [[dependency]] is missing
ERROR [Ballerina.toml:(13:1,13:19)] key 'org1' not supported in schema 'dependency'
ERROR [Ballerina.toml:(14:1,14:15)] key 'name2' not supported in schema 'dependency'
ERROR [Ballerina.toml:(15:1,15:19)] key 'version3' not supported in schema 'dependency'
ERROR [main.bal:(2:13,2:20)] incompatible types: expected 'int', found 'string'
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Compiling source
foo/winery:0.1.0
ERROR [Ballerina.toml:(7:7,7:19)] invalid 'org' under [[dependency]]: 'org' can only contain alphanumerics and underscores
ERROR [Ballerina.toml:(8:8,8:15)] invalid 'name' under [[dependency]]: 'name' can only contain alphanumerics, underscores and periods
ERROR [Ballerina.toml:(9:11,9:19)] invalid 'version' under [[dependency]]: 'version' should be compatible with semver
ERROR [Ballerina.toml:(12:1,16:21)] 'name' under [[dependency]] is missing
ERROR [Ballerina.toml:(12:1,16:21)] 'org' under [[dependency]] is missing
ERROR [Ballerina.toml:(12:1,16:21)] 'version' under [[dependency]] is missing
ERROR [Ballerina.toml:(13:1,13:19)] key 'org1' not supported in schema 'dependency'
ERROR [Ballerina.toml:(14:1,14:15)] key 'name2' not supported in schema 'dependency'
ERROR [Ballerina.toml:(15:1,15:19)] key 'version3' not supported in schema 'dependency'
ERROR [main.bal:(2:13,2:20)] incompatible types: expected 'int', found 'string'
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
org = "foo"
name = "winery"
version = "0.1.0"

[[dependency]]
org = "ballerina#"
name = "dat$a"
version = "0.1.0r"
repository = "local"

[[dependency]]
org1 = "ballerina"
name2 = "data"
version3 = "0.1.0"
repository = "maven"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public function main() {
int x = "hello";
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -138,6 +137,9 @@ void updateDeprecatedStatusForPackage(PackageDescriptor descriptor) {
public boolean isPackageExists(PackageOrg org,
PackageName name,
PackageVersion version) {
if (org.value() == null || name.value() == null) {
return false;
}
Path balaPath = getPackagePath(org.value(), name.value(), version.value().toString());
return Files.exists(balaPath);
}
Expand Down Expand Up @@ -209,7 +211,7 @@ protected List<PackageVersion> getPackageVersions(PackageOrg org, PackageName na
Path balaPackagePath = bala.resolve(org.value()).resolve(name.value());
if (Files.exists(balaPackagePath)) {
try (Stream<Path> collect = Files.list(balaPackagePath)) {
versions.addAll(collect.collect(Collectors.toList()));
versions.addAll(collect.toList());
}
}
} catch (IOException e) {
Expand Down
Loading