diff --git a/project-api-tests/src/test/java/org/ballerina/projectapi/BuildNewCommandTest.java b/project-api-tests/src/test/java/org/ballerina/projectapi/BuildNewCommandTest.java new file mode 100644 index 0000000000..73f7d9bd70 --- /dev/null +++ b/project-api-tests/src/test/java/org/ballerina/projectapi/BuildNewCommandTest.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2024, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you 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.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import static org.ballerina.projectapi.CentralTestUtils.createSettingToml; +import static org.ballerina.projectapi.CentralTestUtils.deleteFiles; +import static org.ballerina.projectapi.CentralTestUtils.getEnvVariables; +import static org.ballerina.projectapi.CentralTestUtils.getString; +import static org.ballerina.projectapi.TestUtils.addEnvVariables; +import static org.ballerina.projectapi.TestUtils.executeBuildCommand; +import static org.ballerina.projectapi.TestUtils.executeNewCommand; + +/** + * Tests related new command compiling. + */ +public class BuildNewCommandTest { + public static final Path MAVEN_VERSION = Paths.get(System.getProperty("maven.version")); + public static final Path CODE_NAME = Paths.get(System.getProperty("code.name")); + public static final String DISTRIBUTION_FILE_NAME = "ballerina-" + MAVEN_VERSION + "-" + CODE_NAME; + + private Path tempHomeDirectory; + private Path tempWorkspaceDirectory; + Map envProperties; + + @BeforeClass() + public void setUp() throws IOException { + TestUtils.setupDistributions(); + tempHomeDirectory = Files.createTempDirectory("bal-test-integration-packaging-home-"); + tempWorkspaceDirectory = Files.createTempDirectory("bal-test-integration-packaging-workspace-"); + createSettingToml(tempHomeDirectory); + envProperties = addEnvVariables(getEnvVariables(), tempHomeDirectory); + } + + @Test(description = "Build package created from new command with default template") + public void testCompilingNewCommandDefaultTempProject() throws IOException, InterruptedException { + List args = new LinkedList<>(); + String projectName = "project_sample"; + args.add(projectName); + executeNewCommand(DISTRIBUTION_FILE_NAME, tempWorkspaceDirectory, args, envProperties); + Path projectPath = tempWorkspaceDirectory.resolve(projectName); + Assert.assertTrue(Files.isDirectory(projectPath)); + Assert.assertTrue(Files.exists(projectPath.resolve("Ballerina.toml"))); + + List buildArgs = new LinkedList<>(); + Process build = executeBuildCommand(DISTRIBUTION_FILE_NAME, projectPath, buildArgs, envProperties); + String buildOutput = getString(build.getInputStream()); + Assert.assertTrue(buildOutput.contains("Compiling source")); + Assert.assertTrue(buildOutput.contains("Generating executable")); + } + + @Test(description = "Build package created from new command with main template") + public void testCompilingNewCommandMainTempProject() throws IOException, InterruptedException { + List args = new LinkedList<>(); + String projectName = "main_sample"; + args.add(projectName); + args.add("-t"); + args.add("main"); + executeNewCommand(DISTRIBUTION_FILE_NAME, tempWorkspaceDirectory, args, envProperties); + Path projectPath = tempWorkspaceDirectory.resolve(projectName); + Assert.assertTrue(Files.isDirectory(projectPath)); + Assert.assertTrue(Files.exists(projectPath.resolve("Ballerina.toml"))); + + List buildArgs = new LinkedList<>(); + Process build = executeBuildCommand(DISTRIBUTION_FILE_NAME, projectPath, buildArgs, envProperties); + String buildOutput = getString(build.getInputStream()); + Assert.assertTrue(buildOutput.contains("Compiling source")); + Assert.assertTrue(buildOutput.contains("Generating executable")); + } + + @Test(description = "Build package created from new command with service template") + public void testCompilingNewCommandServiceTempProject() throws IOException, InterruptedException { + List args = new LinkedList<>(); + String projectName = "service_sample"; + args.add(projectName); + args.add("-t"); + args.add("service"); + executeNewCommand(DISTRIBUTION_FILE_NAME, tempWorkspaceDirectory, args, envProperties); + Path projectPath = tempWorkspaceDirectory.resolve(projectName); + Assert.assertTrue(Files.isDirectory(projectPath)); + Assert.assertTrue(Files.exists(projectPath.resolve("Ballerina.toml"))); + + List buildArgs = new LinkedList<>(); + Process build = executeBuildCommand(DISTRIBUTION_FILE_NAME, projectPath, buildArgs, envProperties); + String buildOutput = getString(build.getInputStream()); + Assert.assertTrue(buildOutput.contains("Compiling source")); + Assert.assertTrue(buildOutput.contains("Generating executable")); + } + + @Test(description = "Build package created from new command with lib template") + public void testCompilingNewCommandLibTempProject() throws IOException, InterruptedException { + List args = new LinkedList<>(); + String projectName = "lib_sample"; + args.add(projectName); + args.add("-t"); + args.add("lib"); + executeNewCommand(DISTRIBUTION_FILE_NAME, tempWorkspaceDirectory, args, envProperties); + Path projectPath = tempWorkspaceDirectory.resolve(projectName); + Assert.assertTrue(Files.isDirectory(projectPath)); + Assert.assertTrue(Files.exists(projectPath.resolve("Ballerina.toml"))); + + List buildArgs = new LinkedList<>(); + Process build = executeBuildCommand(DISTRIBUTION_FILE_NAME, projectPath, buildArgs, envProperties); + String buildOutput = getString(build.getInputStream()); + Assert.assertTrue(buildOutput.contains("Compiling source")); + Assert.assertTrue(buildOutput.contains("Generating executable")); + } + + @AfterClass + private void cleanup() throws IOException { + deleteFiles(tempHomeDirectory); + deleteFiles(tempWorkspaceDirectory); + } +} diff --git a/project-api-tests/src/test/java/org/ballerina/projectapi/TestUtils.java b/project-api-tests/src/test/java/org/ballerina/projectapi/TestUtils.java index 8f9c047dba..67cc0ef516 100644 --- a/project-api-tests/src/test/java/org/ballerina/projectapi/TestUtils.java +++ b/project-api-tests/src/test/java/org/ballerina/projectapi/TestUtils.java @@ -118,6 +118,12 @@ public static Process executeHelpCommand(String distributionName, Path sourceDir return executeCommand("help", distributionName, sourceDirectory, args, envProperties); } + public static Process executeNewCommand(String distributionName, Path sourceDirectory, + List args, Map envProperties) + throws IOException, InterruptedException { + return executeCommand("new", distributionName, sourceDirectory, args, envProperties); + } + /** * Clean and setup the distribution. *