Skip to content

Commit

Permalink
Check org, name for null before package resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
gayaldassanayake committed May 9, 2024
1 parent 71a014e commit 8d3e8b1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1250,4 +1250,20 @@ private void validateBuildTimeInfo(String buildLog) {
"Missing testingExecutionDuration field in build time logs");
Assert.assertTrue(buildLog.contains("totalDuration"), "Missing totalDuration field in build time logs");
}

@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 @@ -208,7 +210,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

0 comments on commit 8d3e8b1

Please sign in to comment.