Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dead code elimination removing fpLoadInstructions from test modules #43559

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ private BIRNode.BIRFunction lookupBirFunction(PackageID pkgId, String funcName)

public InvocationData getInvocationData(PackageID pkgId) {
if (currentPkgID.equals(pkgId)) {
return pkgId.isTestPkg ? currentInvocationData.testablePkgInvocationData : currentInvocationData;
return isTestablePkgAnalysis ? currentInvocationData.testablePkgInvocationData : currentInvocationData;
}
return pkgCache.getInvocationData(pkgId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.ballerinalang.testerina.test;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.ballerinalang.test.context.BMainInstance;
import org.ballerinalang.test.context.BallerinaTestException;
import org.ballerinalang.testerina.test.utils.AssertionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
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.HashMap;

/**
* Test case to check `bal test --eliminate-dead-code` command with an empty buildPkg and a test with a FP to a lang
* module function.
*
* @since 2201.11.0
*/
public class OptimizedTestRunWithEmptyBuildPkgTest extends BaseTestCase {

private static final Logger LOGGER = LoggerFactory.getLogger(OptimizedExecutableTestingTest.class);
private BMainInstance balClient;
private String projectPath;
private static final String TARGET = "target";
private static final String PROJECT_NAME = "optimized-test-run-with-empty-build-pkg-test";
private static final String DEAD_CODE_ELIMINATION_REPORT = "dead_code_elimination_report.json";
private static final Path EXPECTED_CODEGEN_OPTIMIZATION_REPORTS_DIR =
Paths.get("src", "test", "resources", "codegen-optimization-reports", PROJECT_NAME);

@BeforeClass
public void setup() throws BallerinaTestException {
balClient = new BMainInstance(balServer);
projectPath = projectBasedTestsPath.resolve(PROJECT_NAME).toString();
}

@Test()
public void testWithEmptyBuildPkg() throws BallerinaTestException, IOException {
String output = balClient.runMainAndReadStdOut("test",
new String[]{"--eliminate-dead-code", "--dead-code-elimination-report"}, new HashMap<>(), projectPath,
true);
AssertionUtils.assertOutput("OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt",
AssertionUtils.replaceBIRNodeAnalysisTime(output));
assertBuildProjectJsonReportsAreSimilar(Path.of(projectPath));
}

private void assertBuildProjectJsonReportsAreSimilar(Path buildProjectPath) {
Path actualJsonPath = buildProjectPath.resolve(TARGET).resolve(DEAD_CODE_ELIMINATION_REPORT);
JsonObject expectedJsonObject =
fileContentAsObject(EXPECTED_CODEGEN_OPTIMIZATION_REPORTS_DIR.resolve(DEAD_CODE_ELIMINATION_REPORT));
JsonObject actualJsonObject = fileContentAsObject(actualJsonPath);
Assert.assertEquals(actualJsonObject, expectedJsonObject);
}

private static JsonObject fileContentAsObject(Path filePath) {
String contentAsString = "";
try {
contentAsString = new String(Files.readAllBytes(filePath));
} catch (IOException ex) {
LOGGER.error(ex.getMessage());
}
return JsonParser.parseString(contentAsString).getAsJsonObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"test_org/optimized_test_run_with_empty_build_pkg_test:0.1.0": {
"usedFunctionNames": {
"sourceFunctions": [],
"virtualFunctions": [
".<init>",
".<start>",
".<stop>"
]
},
"unusedFunctionNames": {
"sourceFunctions": [],
"virtualFunctions": []
},
"usedTypeDefNames": {
"sourceTypeDefinitions": [],
"virtualTypeDefinitions": []
},
"unusedTypeDefNames": {
"sourceTypeDefinitions": [],
"virtualTypeDefinitions": []
},
"usedNativeClassPaths": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Compiling source
test_org/optimized_test_run_with_empty_build_pkg_test:0.1.0
Running Tests

optimized_test_run_with_empty_build_pkg_test

ttt: has failed.


[fail] ttt:

error {ballerina/test:0}TestError ("Assertion Failed!")
callableName: createBallerinaError moduleName: ballerina.test.0 fileName: assert.bal lineNumber: 41
callableName: assertTrue moduleName: ballerina.test.0 fileName: assert.bal lineNumber: 61
callableName: ttt moduleName: test_org.optimized_test_run_with_empty_build_pkg_test$test.0.tests.tests_test fileName: tests/tests_test.bal lineNumber: 6
callableName: ttt$lambda0$ moduleName: test_org.optimized_test_run_with_empty_build_pkg_test$test.0.tests.test_execute-generated_1 fileName: tests/test_execute-generated_1.bal lineNumber: 4



0 passing
1 failing
0 skipped

Test execution time :*****s
error: there are test failures
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Compiling source
test_org/optimized_test_run_with_empty_build_pkg_test:0.1.0
Running Tests

optimized_test_run_with_empty_build_pkg_test

ttt: has failed.


[fail] ttt:

error {ballerina/test:0}TestError ("Assertion Failed!")
callableName: createBallerinaError moduleName: ballerina.test.0 fileName: assert.bal lineNumber: 41
callableName: assertTrue moduleName: ballerina.test.0 fileName: assert.bal lineNumber: 61
callableName: ttt moduleName: test_org.optimized_test_run_with_empty_build_pkg_test$test.0.tests.tests_test fileName: tests/tests_test.bal lineNumber: 6
callableName: ttt$lambda0$ moduleName: test_org.optimized_test_run_with_empty_build_pkg_test$test.0.tests.test_execute-generated_1 fileName: tests/test_execute-generated_1.bal lineNumber: 4



0 passing
1 failing
0 skipped

Test execution time :*****s
error: there are test failures
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "test_org"
name = "optimized_test_run_with_empty_build_pkg_test"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/test;

@test:Config
public function ttt() {
string result = "a";
test:assertTrue(result.includes(" "));
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ under the License.
<class name="org.ballerinalang.testerina.test.TestExecutionWithInitFailuresTest"/>
<class name="org.ballerinalang.testerina.test.EscapedIdentifiersValidationTest"/>
<class name="org.ballerinalang.testerina.test.OptimizedExecutableTestingTest" />
<class name="org.ballerinalang.testerina.test.OptimizedTestRunWithEmptyBuildPkgTest" />
</classes>
</test>
</suite>