From d3e7badbfd9f628913195294f207035920ed52c6 Mon Sep 17 00:00:00 2001 From: Thushara-Piyasekara Date: Fri, 8 Nov 2024 14:33:43 +0530 Subject: [PATCH 1/5] Fix fpLoadInstructions being deleted from test pkgs --- .../main/java/io/ballerina/projects/UsedBIRNodeAnalyzer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/UsedBIRNodeAnalyzer.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/UsedBIRNodeAnalyzer.java index f49e0808b6f6..c42a470c5eb8 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/UsedBIRNodeAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/UsedBIRNodeAnalyzer.java @@ -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); } From 66779d48850f183614d967d1a14fd319893779c9 Mon Sep 17 00:00:00 2001 From: Thushara-Piyasekara Date: Fri, 8 Nov 2024 16:32:15 +0530 Subject: [PATCH 2/5] Add tests --- ...OptimizedTestRunWithEmptyBuildPkgTest.java | 70 +++++++++++++++++++ .../dead_code_elimination_report.json | 25 +++++++ ...tableTestingTest-testWithEmptyBuildPkg.txt | 25 +++++++ ...tableTestingTest-testWithEmptyBuildPkg.txt | 25 +++++++ .../Ballerina.toml | 4 ++ .../main.bal | 0 .../tests/tests_test.bal | 7 ++ .../src/test/resources/testng.xml | 1 + 8 files changed, 157 insertions(+) create mode 100644 tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java create mode 100644 tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json create mode 100644 tests/testerina-integration-test/src/test/resources/command-outputs/unix/OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt create mode 100644 tests/testerina-integration-test/src/test/resources/command-outputs/windows/OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt create mode 100644 tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/Ballerina.toml create mode 100644 tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/main.bal create mode 100644 tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal diff --git a/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java b/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java new file mode 100644 index 000000000000..2fa7f8a80369 --- /dev/null +++ b/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java @@ -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(); + } +} diff --git a/tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json b/tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json new file mode 100644 index 000000000000..6fa0e29f4f11 --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json @@ -0,0 +1,25 @@ +{ + "test_org/optimized_test_run_with_empty_build_pkg_test:0.1.0": { + "usedFunctionNames": { + "sourceFunctions": [], + "virtualFunctions": [ + ".", + ".", + "." + ] + }, + "unusedFunctionNames": { + "sourceFunctions": [], + "virtualFunctions": [] + }, + "usedTypeDefNames": { + "sourceTypeDefinitions": [], + "virtualTypeDefinitions": [] + }, + "unusedTypeDefNames": { + "sourceTypeDefinitions": [], + "virtualTypeDefinitions": [] + }, + "usedNativeClassPaths": [] + } +} \ No newline at end of file diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/unix/OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/unix/OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt new file mode 100644 index 000000000000..6f228101cc8d --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/unix/OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt @@ -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 diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/windows/OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt new file mode 100644 index 000000000000..aa17bf6dff31 --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/OptimizedExecutableTestingTest-testWithEmptyBuildPkg.txt @@ -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 diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/Ballerina.toml b/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/Ballerina.toml new file mode 100644 index 000000000000..60d6ae4ae7b8 --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "test_org" +name = "optimized_test_run_with_empty_build_pkg_test" +version = "0.1.0" diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/main.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/main.bal new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal new file mode 100644 index 000000000000..c318866121ea --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal @@ -0,0 +1,7 @@ +import ballerina/test; + +@test:Config +public function ttt() { + string result = "a"; + test:assertTrue(result.includes(" ")); +} \ No newline at end of file diff --git a/tests/testerina-integration-test/src/test/resources/testng.xml b/tests/testerina-integration-test/src/test/resources/testng.xml index df0127693587..dd2a2bd97aae 100644 --- a/tests/testerina-integration-test/src/test/resources/testng.xml +++ b/tests/testerina-integration-test/src/test/resources/testng.xml @@ -62,6 +62,7 @@ under the License. + From dac67970af84592409fb72d77c4a118c02768db8 Mon Sep 17 00:00:00 2001 From: Gimantha Bandara Date: Fri, 8 Nov 2024 17:45:53 +0530 Subject: [PATCH 3/5] Update tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json --- .../dead_code_elimination_report.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json b/tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json index 6fa0e29f4f11..4604ce1370cf 100644 --- a/tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json +++ b/tests/testerina-integration-test/src/test/resources/codegen-optimization-reports/optimized-test-run-with-empty-build-pkg-test/dead_code_elimination_report.json @@ -22,4 +22,4 @@ }, "usedNativeClassPaths": [] } -} \ No newline at end of file +} From 3f08de3110d3af803261c65f50fc07a6f07e2765 Mon Sep 17 00:00:00 2001 From: Gimantha Bandara Date: Fri, 8 Nov 2024 17:53:29 +0530 Subject: [PATCH 4/5] Update tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java --- .../testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java b/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java index 2fa7f8a80369..909667d10816 100644 --- a/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java +++ b/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/OptimizedTestRunWithEmptyBuildPkgTest.java @@ -23,7 +23,7 @@ * * @since 2201.11.0 */ -public class OptimizedTestRunWithEmptyBuildPkgTest extends BaseTestCase{ +public class OptimizedTestRunWithEmptyBuildPkgTest extends BaseTestCase { private static final Logger LOGGER = LoggerFactory.getLogger(OptimizedExecutableTestingTest.class); private BMainInstance balClient; From 5b88083ece0ff9f090bbd761b26ed83132d21c1b Mon Sep 17 00:00:00 2001 From: Gimantha Bandara Date: Fri, 8 Nov 2024 17:53:38 +0530 Subject: [PATCH 5/5] Update tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal --- .../tests/tests_test.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal index c318866121ea..0e05dc852856 100644 --- a/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/optimized-test-run-with-empty-build-pkg-test/tests/tests_test.bal @@ -4,4 +4,4 @@ import ballerina/test; public function ttt() { string result = "a"; test:assertTrue(result.includes(" ")); -} \ No newline at end of file +}