Skip to content

Commit

Permalink
Move BuildNewCommandTest to project api
Browse files Browse the repository at this point in the history
  • Loading branch information
gayaldassanayake committed Aug 20, 2024
1 parent d0b3fac commit 1d6406b
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args, Map<String, String> envProperties)
throws IOException, InterruptedException {
return executeCommand("new", distributionName, sourceDirectory, args, envProperties);
}

/**
* Clean and setup the distribution.
*
Expand Down

0 comments on commit 1d6406b

Please sign in to comment.