Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thevakumar-Luheerathan committed Aug 18, 2023
1 parent c938081 commit e7c9444
Show file tree
Hide file tree
Showing 18 changed files with 391 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/daily-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}
TEST_MODE_ACTIVE: true
run: ./gradlew clean build --stacktrace --scan --console=plain --no-daemon --continue -x project-api-tests:test
Expand Down Expand Up @@ -235,6 +236,7 @@ jobs:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}
TEST_MODE_ACTIVE: true
run: ./gradlew :project-api-tests:test --stacktrace --scan --console=plain --no-daemon --continue
Expand All @@ -256,6 +258,7 @@ jobs:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}
TEST_MODE_ACTIVE: true
run: ./gradlew.bat clean build --stacktrace --scan --console=plain --no-daemon -x test
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}
run: ./gradlew clean build --stacktrace --scan --console=plain --no-daemon --continue -x project-api-tests:test
- name: Build Ballerina Distribution Skip Tests
Expand All @@ -47,6 +48,7 @@ jobs:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew clean build --stacktrace --scan --console=plain --no-daemon --continue -x test
- name: Archive Ballerina ZIP
uses: actions/upload-artifact@v2
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish-release-artifacts-1.2.x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}

- name: Generate metadata json
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish-release-artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}

- name: Generate metadata json
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}
run: |
./gradlew build -Pversion=${VERSION}
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}
run: ./gradlew clean build --stacktrace --scan --console=plain --no-daemon --continue -x :ballerina:testExamples -x :project-api-tests:test

Expand All @@ -50,6 +51,7 @@ jobs:
packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}
run: ./gradlew clean :ballerina:testExamples --stacktrace --scan --console=plain --no-daemon --continue -x :project-api-tests:test
windows-build-without-tests:
Expand All @@ -72,5 +74,6 @@ jobs:
packagePAT: ${{ secrets.GITHUB_TOKEN }}
JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
devCentralToken: ${{ secrets.BALLERINA_DEV_CENTRAL_ACCESS_TOKEN }}
githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
ballerinaBotWorkflow: $ {{ secrets.BALLERINA_BOT_WORKFLOW }}
run: ./gradlew.bat clean build --stacktrace --scan --console=plain --no-daemon --continue -x test
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ballerina.projectapi;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


import static org.ballerina.projectapi.MavenCustomRepoTestUtils.createSettingToml;
import static org.ballerina.projectapi.MavenCustomRepoTestUtils.deleteArtifacts;
import static org.ballerina.projectapi.MavenCustomRepoTestUtils.deleteFiles;
import static org.ballerina.projectapi.MavenCustomRepoTestUtils.getEnvVariables;
import static org.ballerina.projectapi.MavenCustomRepoTestUtils.getString;
import static org.ballerina.projectapi.TestUtils.DISTRIBUTION_FILE_NAME;
import static org.ballerina.projectapi.TestUtils.OUTPUT_CONTAIN_ERRORS;
import static org.ballerina.projectapi.TestUtils.executeBuildCommand;
import static org.ballerina.projectapi.TestUtils.executePullCommand;
import static org.ballerina.projectapi.TestUtils.executePushCommand;

/**
* Tests related to Maven repositories.
*/
public class MavenCustomRepoTest {

private static final String org = "luheerathan";
private static final String packagename = "pact";
private static final String version = "0.2.0";
private static final String GITHUB_REPO_ID = "github1";
private Path actualHomeDirectory;
private Path tempWorkspaceDirectory;
private Path actualHomeDirectoryClone;
private Map<String, String> envVariables;
@BeforeClass()
public void setUp() throws IOException {
TestUtils.setupDistributions();
actualHomeDirectory = Paths.get(System.getProperty("user.home")).resolve(".ballerina");
actualHomeDirectoryClone = Files.createTempDirectory("bal-test-integration-packaging-home-")
.resolve(".ballerina");
Files.walkFileTree(actualHomeDirectory,
new MavenCustomRepoTest.Copy(actualHomeDirectory, actualHomeDirectoryClone));
deleteFiles(actualHomeDirectory, true);
tempWorkspaceDirectory = Files.createTempDirectory("bal-test-integration-packaging-workspace-");

createSettingToml(actualHomeDirectory);
System.setProperty("user.home", actualHomeDirectory.getParent().toString());
envVariables = getEnvVariables();

// Copy test resources to temp workspace directory
try {
URI testResourcesURI = Objects.requireNonNull(getClass().getClassLoader()
.getResource("maven-repos")).toURI();
Files.walkFileTree(Paths.get(testResourcesURI), new MavenCustomRepoTest.Copy(Paths.get(testResourcesURI),
this.tempWorkspaceDirectory));
} catch (URISyntaxException e) {
Assert.fail("error loading resources");
}
}

@Test(description = "Push package to Github packages")
public void testPushBalaGithub() throws IOException, InterruptedException {
List<String> args = new ArrayList<>();
args.add("--repository=" + GITHUB_REPO_ID);
Process build = executePushCommand(DISTRIBUTION_FILE_NAME, this.tempWorkspaceDirectory.resolve(packagename),
args, this.envVariables);
String buildErrors = getString(build.getErrorStream());
if (!buildErrors.isEmpty()) {
Assert.fail(OUTPUT_CONTAIN_ERRORS + buildErrors);
}

String buildOutput = getString(build.getInputStream());
Assert.assertTrue(buildOutput.contains("Successfully pushed target/bala/" + org + "-"
+ packagename + "-any-" + version + ".bala to " + "'" + GITHUB_REPO_ID + "' repository."));
}

@Test(description = "Pull package from Github packages", dependsOnMethods = "testPushBalaGithub")
public void testPullBalaGithub() throws IOException, InterruptedException {
List<String> args = new ArrayList<>();
args.add(org + "/" + packagename + ":" + version);
args.add("--repository=" + GITHUB_REPO_ID);
Process build = executePullCommand(DISTRIBUTION_FILE_NAME, this.tempWorkspaceDirectory.resolve(packagename),
args, this.envVariables);
String buildErrors = getString(build.getErrorStream());
if (!buildErrors.isEmpty()) {
Assert.fail(OUTPUT_CONTAIN_ERRORS + buildErrors);
}

String buildOutput = getString(build.getInputStream());
Assert.assertTrue(buildOutput.contains("Successfully pulled the package from the custom repository"));

Path packagePath = this.actualHomeDirectory.resolve("repositories")
.resolve(GITHUB_REPO_ID).resolve("bala").resolve(org).resolve(packagename).resolve(version);
Assert.assertTrue(Files.exists(packagePath.resolve("any")));
deleteFiles(this.actualHomeDirectory.resolve("repositories").resolve(GITHUB_REPO_ID), false);

}

@Test(description = "Build a package offline using a module from Github packages",
dependsOnMethods = "testPullBalaGithub")
public void testBuildBalaGithubOffline() throws IOException, InterruptedException {
List<String> args = new ArrayList<>();
args.add("--offline=true");
Process build = executeBuildCommand(DISTRIBUTION_FILE_NAME, this.tempWorkspaceDirectory
.resolve("test-resolution"),
args, this.envVariables);
String buildErrors = getString(build.getErrorStream());
Assert.assertTrue(buildErrors.contains("cannot resolve module 'luheerathan/pact as _'"));
}

@Test(description = "Build a package Online using a module from Github packages",
dependsOnMethods = "testBuildBalaGithubOffline")
public void testBuildBalaGithubOnline() throws IOException, InterruptedException {
List<String> args = new ArrayList<>();
args.add("--offline=false");
Process build = executeBuildCommand(DISTRIBUTION_FILE_NAME, this.tempWorkspaceDirectory
.resolve("test-resolution"),
args, this.envVariables);
String buildErrors = getString(build.getErrorStream());
if (!buildErrors.isEmpty()) {
Assert.fail(OUTPUT_CONTAIN_ERRORS + buildErrors);
}

String buildOutput = getString(build.getInputStream());
Assert.assertTrue(buildOutput.contains("Generating executable\n\ttarget/bin/test.jar"));
}


@AfterClass
private void cleanup() throws IOException {
deleteFiles(actualHomeDirectory, true);
Files.walkFileTree(actualHomeDirectoryClone,
new MavenCustomRepoTest.Copy(actualHomeDirectoryClone, actualHomeDirectory));
deleteFiles(actualHomeDirectoryClone, false);
deleteFiles(tempWorkspaceDirectory, false);
deleteArtifacts(org, packagename);
}



/**
* Copy test resources to temp directory.
*/
static class Copy extends SimpleFileVisitor<Path> {
private final Path fromPath;
private final Path toPath;
private final StandardCopyOption copyOption;

Copy(Path fromPath, Path toPath, StandardCopyOption copyOption) {
this.fromPath = fromPath;
this.toPath = toPath;
this.copyOption = copyOption;
}

Copy(Path fromPath, Path toPath) {
this(fromPath, toPath, StandardCopyOption.REPLACE_EXISTING);
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

Path targetPath = toPath.resolve(fromPath.relativize(dir).toString());
if (!Files.exists(targetPath)) {
Files.createDirectory(targetPath);
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

Files.copy(file, toPath.resolve(fromPath.relativize(file).toString()), copyOption);
return FileVisitResult.CONTINUE;
}
}
}
Loading

0 comments on commit e7c9444

Please sign in to comment.