Skip to content

Commit

Permalink
Migrate tests to testarina
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhashinee committed Aug 26, 2020
1 parent e466f43 commit 1c1caf2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 83 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.gradle.caching=true
group=org.ballerinalang
version=0.6.0-SNAPSHOT
ballerinaLangVersion=2.0.0-Preview2
ballerinaLangVersion=2.0.0-Preview4-SNAPSHOT
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {

rootProject.name = 'system'
include(':build-config:checkstyle')
include ':system-native', ':socket-test-utils', ':system-ballerina'
include ':system-native', ':system-test-utils', ':system-ballerina'
include 'system-test-utils'
include 'system-native'

Expand Down
31 changes: 14 additions & 17 deletions system-ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ description = 'Ballerina - System Ballerina Generator'

configurations {
jbalTools
systemBallerinaBirDir
systemBallerinaJar
externalJars
}

Expand Down Expand Up @@ -141,21 +139,20 @@ task ballerinaBuild {
into file("$artifactLibParent/libs")
}

// Disable publish to central until it confirmed
// // Publish to central
// if (!project.version.endsWith('-SNAPSHOT') && ballerinaCentralAccessToken != null && project.hasProperty("publishToCentral")) {
// println("Publishing to the ballerina central..")
// exec {
// workingDir project.projectDir
// environment "JAVA_OPTS", "-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true"
// if (Os.isFamily(Os.FAMILY_WINDOWS)) {
// commandLine 'cmd', '/c', "$distributionBinPath/ballerina.bat push ${moduleName}"
// } else {
// commandLine 'sh', '-c', "$distributionBinPath/ballerina push ${moduleName}"
// }
// }
//
// }
// Publish to central
if (!project.version.endsWith('-SNAPSHOT') && ballerinaCentralAccessToken != null && project.hasProperty("publishToCentral")) {
println("Publishing to the ballerina central..")
exec {
workingDir project.projectDir
environment "JAVA_OPTS", "-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true"
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'cmd', '/c', "$distributionBinPath/ballerina.bat push ${moduleName}"
} else {
commandLine 'sh', '-c', "$distributionBinPath/ballerina push ${moduleName}"
}
}

}
// Doc creation and packing
exec {
workingDir project.projectDir
Expand Down
30 changes: 15 additions & 15 deletions system-ballerina/src/system/system.bal
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ function nativeUuid() returns handle = @java:Method {
class: "java.util.UUID"
} external;

//# Executes an operating system command as a subprocess of the current process.
//# ```ballerina
//# system:Process|system:Error proc = system:exec("ls", {}, "/", "-la")
//# ```
//#
//# + command - The name of the command to be executed
//# + env - Environment variables to be set to the process
//# + dir - The current working directory to be set to the process
//# + args - Command arguments to be passed in
//# + return - A `system:Process` object if successful or else a `system:Error` if a failure occurs
//public function exec(@untainted string command, @untainted map<string> env = {},
// @untainted string? dir = (), @untainted string... args) returns Process|Error = @java:Method {
// name: "exec",
// class: "org.ballerinalang.stdlib.system.nativeimpl.Exec"
//} external;
# Executes an operating system command as a subprocess of the current process.
# ```ballerina
# system:Process|system:Error proc = system:exec("ls", {}, "/", "-la")
# ```
#
# + command - The name of the command to be executed
# + env - Environment variables to be set to the process
# + dir - The current working directory to be set to the process
# + args - Command arguments to be passed in
# + return - A `system:Process` object if successful or else a `system:Error` if a failure occurs
public function exec(@untainted string command, @untainted map<string> env = {},
@untainted string? dir = (), @untainted string... args) returns Process|Error = @java:Method {
name: "exec",
class: "org.ballerinalang.stdlib.system.nativeimpl.Exec"
} external;
94 changes: 52 additions & 42 deletions system-ballerina/src/system/tests/unit-tests/system-test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,58 @@ function testRandomString() {
test:assertEquals(uuid().length(), 36);
}

@test:Config {}
function testExecInUnixLike1() returns error? {
Process x1 = check exec("env", { "BAL_EXEC_TEST_VAR":"X" });
Process x2 = check exec("grep", {}, (), "BAL_EXEC_TEST_VAR");
var x2out = x1.pipe(x2).stdout();
int ec1 = check x2.waitForExit();
int ec2 = check x2.exitCode();
string result = check toString(x2out);
test:assertEquals(result.trim(), "BAL_EXEC_TEST_VAR=X");
test:assertEquals(ec1, 0);
test:assertEquals(ec2, 0);
}

@test:Config {}
function testExecInUnixLike2() returns error? {
Process x1 = check exec("pwd", {}, "/");
var x1out = x1.stdout();
string result = check toString(x1out);
test:assertEquals(result.trim(), "/");
}

@test:Config {}
function testExecInUnixLike3() returns error? {
Process x1 = check exec("grep", {}, (), "BAL_TEST");
io:WritableDataChannel ch = new(x1.stdin());
check ch.writeString("BAL_TEST", "UTF-8");
check ch.close();
string result = check toString(x1.stdout());
test:assertEquals(result.trim(), "BAL_TEST");
}

@test:Config {}
function testExecInUnixLike4() returns error? {
Process x1 = check exec("env", { "BAL_EXEC_TEST_VAR":"X" });
Process x2 = check exec("grep", {}, (), "BAL_EXEC_TEST_VAR");
Process x3 = check exec("wc", {}, (), "-l");
var x3out = x1.pipe(x2).pipe(x3).stdout();
string result = check toString(x3out);
test:assertEquals(result.trim(), "1");
}

@test:Config {}
function testExecWithError() returns error? {
string expected = "Cannot run program \"eee\": error=2, No such file or directory";
Process|error x1 = exec("eee", {}, (), "BAL_EXEC_TEST_VAR");
if (x1 is error) {
test:assertEquals(x1.message(), expected);
} else {
test:assertFail("Didn't receive the expected error message: " + expected);
}
}

function toString(io:ReadableByteChannel input) returns string|error {
string result = "";
io:ReadableCharacterChannel charIn = new(input, "UTF-8");
Expand All @@ -56,48 +108,6 @@ function toString(io:ReadableByteChannel input) returns string|error {
return <@untainted> result;
}

//function testExecInUnixLike1() returns [string, int, int]|error {
// Process x1 = check exec("env", { "BAL_EXEC_TEST_VAR":"X" });
// Process x2 = check exec("grep", {}, (), "BAL_EXEC_TEST_VAR");
// var x2out = x1.pipe(x2).stdout();
// var ec1 = check x2.waitForExit();
// var ec2 = check x2.exitCode();
// var result = check toString(x2out);
// return [result, ec1, ec2];
//}

//function testExecInUnixLike2() returns string|error {
// system:Process x1 = check system:exec("pwd", {}, "/");
// var x1out = x1.stdout();
// var result = check toString(x1out);
// return result;
//}
//
//function testExecInUnixLike3() returns string|error {
// system:Process x1 = check system:exec("grep", {}, (), "BAL_TEST");
// io:WritableDataChannel ch = new(x1.stdin());
// check ch.writeString("BAL_TEST", "UTF-8");
// check ch.close();
// var result = check toString(x1.stdout());
// return result;
//}
//
//function testExecInUnixLike4() returns string|error {
// system:Process x1 = check system:exec("env", { "BAL_EXEC_TEST_VAR":"X" });
// system:Process x2 = check system:exec("grep", {}, (), "BAL_EXEC_TEST_VAR");
// system:Process x3 = check system:exec("wc", {}, (), "-l");
// var x3out = x1.pipe(x2).pipe(x3).stdout();
// var result = check toString(x3out);
// return result;
//}
//
//function testExecWithError() returns string|error {
// system:Process x1 = check system:exec("eee", {}, (), "BAL_EXEC_TEST_VAR");
// var x3out = x1.stdout();
// var result = check toString(x3out);
// return result;
//}

function getExpectedValidEnv() returns string = @java:Method {
name: "testValidEnv",
class: "org.ballerinalang.stdlib.system.testutils.SystemTestUtils"
Expand Down
12 changes: 5 additions & 7 deletions system-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ plugins {
description = 'Ballerina - System Java Utils'

dependencies {
implementation group: 'org.ballerinalang', name: 'ballerina-lang', version: "${ballerinaLangVersion}"
implementation group: 'org.ballerinalang', name: 'ballerina-runtime', version: "${ballerinaLangVersion}"
implementation group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jVersion}"
implementation group: 'org.ballerinalang', name: 'ballerina-runtime-api', version: "${ballerinaLangVersion}"
implementation group: 'org.ballerinalang', name: 'ballerina-time', version: "${ballerinaLangVersion}"
// implementation group: 'org.ballerinalang', name: 'ballerina-lang:internal', version: "${ballerinaLangVersion}"
implementation group: 'org.ballerinalang', name: 'ballerina-io', version: "${ballerinaLangVersion}"
compile group: 'org.ballerinalang', name: 'ballerina-lang', version: "${ballerinaLangVersion}"
compile group: 'org.ballerinalang', name: 'ballerina-runtime', version: "${ballerinaLangVersion}"
compile group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jVersion}"
compile group: 'org.ballerinalang', name: 'ballerina-time', version: "${ballerinaLangVersion}"
compile group: 'org.ballerinalang', name: 'ballerina-io', version: "${ballerinaLangVersion}"
}


0 comments on commit 1c1caf2

Please sign in to comment.