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

Extend the test coverage of build tools #42417

Merged
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 @@ -30,6 +30,7 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.ballerinalang.util.RepoUtils;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -52,9 +53,13 @@ public class RunBuildToolsTaskTest extends BaseCommandTest {
private static final Path testDistCacheDirectory = testBuildDirectory.resolve(DIST_CACHE_DIRECTORY);
Path mockCentralBalaDirPath = testDistCacheDirectory.resolve("bala");

private static final long TWO_DAYS = 2 * 24 * 60 * 60 * 1000;
private static final long HALF_DAY = 12 * 60 * 60 * 1000;

@BeforeClass
public void setup() throws IOException {
super.setup();
// copy all test resources
try {
Path testResources = super.tmpDir.resolve("build-tool-test-resources");
this.buildToolResources = testResources.resolve("buildToolResources");
Expand All @@ -64,6 +69,7 @@ public void setup() throws IOException {
} catch (Exception e) {
Assert.fail("error loading resources");
}
// compile and cache tools
BCompileUtil.compileAndCacheBala(buildToolResources.resolve("tools")
.resolve("dummy-tool-pkg").toString(), testDistCacheDirectory);
BCompileUtil.compileAndCacheBala(buildToolResources.resolve("tools")
Expand All @@ -72,12 +78,21 @@ public void setup() throws IOException {
.resolve("ballerina-generate-file").toString(), testDistCacheDirectory);
BCompileUtil.compileAndCacheBala(buildToolResources.resolve("tools")
.resolve("hidden-cmd-tool-pkg").toString(), testDistCacheDirectory);
BCompileUtil.compileAndCacheBala(buildToolResources.resolve("tools")
.resolve("missing-interface-tool-pkg").toString(), testDistCacheDirectory);
BCompileUtil.compileAndCacheBala(buildToolResources.resolve("tools")
.resolve("no-options-tool-pkg").toString(), testDistCacheDirectory);
// add build.json files
addBuildJsonToProjects("project-lt-24h-with-build-tool", System.currentTimeMillis() - HALF_DAY);
addBuildJsonToProjects("project-gt-24h-with-build-tool", System.currentTimeMillis() - TWO_DAYS);
}

@Test(description = "Resolve a tool offline", dataProvider = "buildToolProvider")
public void testOfflineToolResolution(String projectName, String outputFileName) throws IOException {
@Test(description = "Resolve a tool offline", dataProvider = "buildToolOfflineProvider")
public void testOfflineToolResolution(String projectName, String outputFileName, boolean sticky)
throws IOException {
Path projectPath = buildToolResources.resolve(projectName);
Project project = BuildProject.load(projectPath, BuildOptions.builder().setOffline(true).build());
Project project = BuildProject.load(projectPath,
BuildOptions.builder().setOffline(true).setSticky(sticky).build());
RunBuildToolsTask runBuildToolsTask = new RunBuildToolsTask(printStream);
try (MockedStatic<BuildToolUtils> repoUtils = Mockito.mockStatic(
BuildToolUtils.class, Mockito.CALLS_REAL_METHODS)) {
Expand Down Expand Up @@ -114,45 +129,102 @@ public void testProjectForAddedGeneratedCode() throws IOException {
"Generated file not found. Project instance hasn't been updated after build tools task");
}

@DataProvider(name = "buildToolProvider")
public Object[][] buildToolProvider() {
@DataProvider(name = "buildToolOfflineProvider")
public Object[][] buildToolOfflineProvider() {
return new Object[][]{
{
"project-with-central-build-tool",
"build-tool-offline.txt"
"build-tool-offline.txt",
false
},
{
"project-with-non-existent-build-tool",
"build-tool-offline-resolve-failed.txt"
"build-tool-offline-resolve-failed.txt",
false
},
{
"fresh-project-with-central-build-tool",
"build-tool-offline-resolve-failed-wo-version.txt"
"build-tool-offline-resolve-failed-wo-version.txt",
false
},
{
"project-with-2.x-central-build-tool",
"build-tool-offline-with-new-major-version-locked.txt"
"build-tool-offline-with-new-major-version-locked.txt",
false
},
{
"project-with-non-existent-subcommand",
"build-tool-non-existent-subcommand.txt"
"build-tool-non-existent-subcommand.txt",
false
},
{
"project-with-invalid-name-build-tool",
"build-tool-invalid-name.txt"
"build-tool-invalid-name.txt",
false
},
{
"project-with-multilevel-subcommands",
"build-tool-multilevel-subcommands.txt"
"build-tool-multilevel-subcommands.txt",
false
},
{
"project-with-only-subcommands",
"build-tool-only-subcommands.txt"
"build-tool-only-subcommands.txt",
false
},
{
"project-with-hidden-commands",
"build-tool-hidden-commands.txt"
"build-tool-hidden-commands.txt",
false
},
{
"project-with-missing-interface-build-tool",
"build-tool-missing-interface.txt",
false
},
{
"project-with-no-options-build-tool",
"build-tool-no-options.txt",
false
},
{
"project-with-old-build-tool",
"build-tool-without-sticky.txt",
false
},
{
"project-with-old-build-tool",
"build-tool-with-sticky.txt",
true
},
{
"project-lt-24h-with-build-tool",
"build-tool-lt-24-build-file.txt",
false
},
{
"project-gt-24h-with-build-tool",
"build-tool-gt-24-build-file.txt",
false
},
};
}

private void addBuildJsonToProjects(String projectName, long time) {
Path buildJsonPath = buildToolResources.resolve(projectName).resolve("target").resolve("build");
String buildJsonContent = "{\n" +
" \"last_build_time\": 1710907945705,\n" +
" \"last_update_time\": " + time + ",\n" +
" \"distribution_version\": \"" + RepoUtils.getBallerinaShortVersion() + "\",\n" +
" \"last_modified_time\": {\n" +
" \"sample_build_tool_ballerina\": 1710907943604\n" +
" }\n" +
"}";
try {
Files.createDirectories(buildJsonPath.getParent());
Files.write(buildJsonPath, buildJsonContent.getBytes());
} catch (IOException e) {
Assert.fail("Error writing build.json file");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
org = "foo"
name = "winery"
version = "0.1.0"

[[tool.dummy_tool]]
id = "main_dummy"
filePath = "delivery.json"
targetModule = "mod_main"
options.mode = "client"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.5"

[[package]]
org = "foo"
name = "winery"
version = "0.1.0"
modules = [
{org = "foo", packageName = "winery", moduleName = "winery"}
]

[[tool]]
id = "dummy_tool"
org = "foo"
name = "dummypkg"
version = "1.3.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 LLC. 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.

public function main() {
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
org = "foo"
name = "winery"
version = "0.1.0"

[[tool.dummy_tool]]
id = "main_dummy"
filePath = "delivery.json"
targetModule = "mod_main"
options.mode = "client"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.5"

[[package]]
org = "foo"
name = "winery"
version = "0.1.0"
modules = [
{org = "foo", packageName = "winery", moduleName = "winery"}
]

[[tool]]
id = "dummy_tool"
org = "foo"
name = "dummypkg"
version = "1.3.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 LLC. 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.

public function main() {
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
org = "foo"
name = "winery"
version = "0.1.0"

[[tool.missing_interface_dummy_tool]]
id = "main_dummy"
filePath = "delivery.json"
targetModule = "mod_main"
options.mode = "client"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.5"

[[package]]
org = "foo"
name = "winery"
version = "0.1.0"
modules = [
{org = "foo", packageName = "winery", moduleName = "winery"}
]

[[tool]]
id = "missing_interface_dummy_tool"
org = "foo"
name = "missing_interface_pkg"
version = "1.4.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 LLC. 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.

public function main() {
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
org = "foo"
name = "winery"
version = "0.1.0"

[[tool.no_options_tool]]
id = "main_dummy"
filePath = "delivery.json"
targetModule = "mod_main"
options.mode = "client"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.5"

[[package]]
org = "foo"
name = "winery"
version = "0.1.0"
modules = [
{org = "foo", packageName = "winery", moduleName = "winery"}
]

[[tool]]
id = "no_options_tool"
org = "foo"
name = "no_options_pkg"
version = "1.4.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 LLC. 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.

public function main() {
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
org = "foo"
name = "winery"
version = "0.1.0"

[[tool.dummy_tool]]
id = "main_dummy"
filePath = "delivery.json"
targetModule = "mod_main"
options.mode = "client"
Loading
Loading