-
Notifications
You must be signed in to change notification settings - Fork 10
Provide APIs for testing the Scan tool Java APIs #56
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
Merged
gayaldassanayake
merged 1 commit into
ballerina-platform:main
from
gayaldassanayake:test-apis
Jun 2, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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. | ||
| */ | ||
|
|
||
| import com.github.spotbugs.snom.Confidence | ||
| import com.github.spotbugs.snom.Effort | ||
|
|
||
| plugins { | ||
| id 'java-library' | ||
| id 'checkstyle' | ||
| id 'maven-publish' | ||
| id 'com.github.spotbugs' | ||
| id 'de.undercouch.download' | ||
| } | ||
|
|
||
| group = "${group}" | ||
| version = "${version}" | ||
|
|
||
| repositories { | ||
| mavenLocal() | ||
| mavenCentral() | ||
| maven { | ||
| url = 'https://maven.pkg.github.com/ballerina-platform/*' | ||
| credentials { | ||
| username System.getenv("packageUser") | ||
| password System.getenv("packagePAT") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation project(':scan-command') | ||
| implementation group: 'org.ballerinalang', name: 'ballerina-lang', version: "${ballerinaLangVersion}" | ||
| implementation group: 'org.ballerinalang', name: 'ballerina-cli', version: "${ballerinaLangVersion}" | ||
| implementation group: 'org.ballerinalang', name: 'ballerina-tools-api', version: "${ballerinaLangVersion}" | ||
| implementation group: 'org.testng', name: 'testng', version: "${testngVersion}" | ||
| } | ||
|
|
||
| // Publish scan-command-test-utils package to GitHub packages | ||
| publishing { | ||
| publications { | ||
| mavenJava(MavenPublication) { | ||
| from components.java | ||
| groupId group | ||
| artifactId project.name | ||
| version version | ||
| } | ||
| } | ||
|
|
||
| repositories { | ||
| maven { | ||
| name = "GitHubPackages" | ||
| url = uri("https://maven.pkg.github.com/ballerina-platform/static-code-analysis-tool") | ||
| credentials { | ||
| username = System.getenv("publishUser") | ||
| password = System.getenv("publishPAT") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| tasks.register('downloadCheckstyleRuleFiles', Download) { | ||
| src([ | ||
| 'https://raw.githubusercontent.com/wso2/code-quality-tools/v1.4/checkstyle/jdk-17/checkstyle.xml', | ||
| 'https://raw.githubusercontent.com/wso2/code-quality-tools/v1.4/checkstyle/jdk-17/suppressions.xml' | ||
| ]) | ||
| overwrite false | ||
| onlyIfNewer true | ||
| dest buildDir | ||
| } | ||
|
|
||
| artifacts.add('default', file("${project.buildDir}/checkstyle.xml")) { | ||
| builtBy(downloadCheckstyleRuleFiles) | ||
| } | ||
|
|
||
| artifacts.add('default', file("${project.buildDir}/suppressions.xml")) { | ||
| builtBy(downloadCheckstyleRuleFiles) | ||
| } | ||
|
|
||
| def excludePattern = '**/module-info.java' | ||
| tasks.withType(Checkstyle).configureEach { | ||
| exclude excludePattern | ||
| } | ||
|
|
||
| checkstyle { | ||
| toolVersion "${project.puppycrawlCheckstyleVersion}" | ||
| configFile rootProject.file("${project.buildDir}/checkstyle.xml") | ||
| configProperties = ["suppressionFile": file("${project.buildDir}/suppressions.xml")] | ||
| } | ||
|
|
||
| checkstyleMain.dependsOn(downloadCheckstyleRuleFiles) | ||
| checkstyleTest.dependsOn(downloadCheckstyleRuleFiles) | ||
|
|
||
| spotbugsMain { | ||
| effort = Effort.valueOf("MAX") | ||
| reportLevel = Confidence.valueOf("LOW") | ||
|
|
||
| reportsDir = file("$project.buildDir/reports/spotbugs") | ||
|
|
||
| reports { | ||
| html.required.set(true) | ||
| text.required.set(true) | ||
| } | ||
|
|
||
| def excludeFile = file("${projectDir}/spotbugs-exclude.xml") | ||
| if (excludeFile.exists()) { | ||
| excludeFilter = excludeFile | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <!-- | ||
| ~ Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. | ||
| ~ | ||
| ~ 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. | ||
| --> | ||
|
|
||
| <FindBugsFilter> | ||
| <Match> | ||
| <Class name="io.ballerina.scan.test.TestOptions$TestOptionsBuilder"/> | ||
| <Method name="setOutputStream"/> | ||
| <Bug code="EI2" /> | ||
| </Match> | ||
| </FindBugsFilter> |
90 changes: 90 additions & 0 deletions
90
scan-command-test-utils/src/main/java/io/ballerina/scan/test/Assertions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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. | ||
| */ | ||
|
|
||
| package io.ballerina.scan.test; | ||
|
|
||
| import io.ballerina.scan.Issue; | ||
| import io.ballerina.scan.Rule; | ||
| import io.ballerina.scan.RuleKind; | ||
| import io.ballerina.scan.Source; | ||
| import org.testng.Assert; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Test utility class to assert the issues found during a scan. | ||
| * | ||
| * @since 0.10.0 | ||
| */ | ||
| public class Assertions { | ||
|
|
||
| private Assertions() { | ||
| } | ||
|
|
||
| /** | ||
| * Assert that the list of issues contains an issue with the given rule ID, file name, start line, end line, | ||
| * and source at the given index. | ||
| * | ||
| * @param issues list of issues from which the issue should be found | ||
| * @param index index of the issue to check | ||
| * @param ruleId rule ID to check for | ||
| * @param fileName file name to check for | ||
| * @param startLine start line to check for | ||
| * @param endLine end line to check for | ||
| * @param source source to check for | ||
| */ | ||
| public static void assertIssue(List<Issue> issues, | ||
| int index, | ||
| String ruleId, | ||
| String fileName, | ||
| int startLine, | ||
| int endLine, | ||
| Source source) { | ||
| Assert.assertTrue(index < issues.size(), "Index out of bounds for the issues list"); | ||
| Issue issue = issues.get(index); | ||
| Assert.assertEquals(issue.rule().id(), ruleId, "Rule ID mismatch"); | ||
| Assert.assertEquals(issue.source(), source, "Source mismatch"); | ||
| Assert.assertEquals(issue.location().lineRange().fileName(), fileName, "File name mismatch"); | ||
| Assert.assertEquals(issue.location().lineRange().startLine().line(), startLine, "Start line mismatch"); | ||
| Assert.assertEquals(issue.location().lineRange().endLine().line(), endLine, "End line mismatch"); | ||
| } | ||
|
|
||
| /** | ||
| * Assert that the list of rules contains a rule with the given ID, description, and kind. | ||
| * | ||
| * @param rules list of rules from which the rule should be found | ||
| * @param id rule ID to check for | ||
| * @param description rule description to check for | ||
| * @param kind rule kind to check for | ||
| */ | ||
| public static void assertRule(List<Rule> rules, String id, String description, RuleKind kind) { | ||
| boolean found = rules.stream().anyMatch(rule -> | ||
| rule.id().equals(id) && | ||
| rule.description().equals(description) && | ||
| rule.kind() == kind); | ||
|
|
||
| if (!found) { | ||
| String summary = rules.stream() | ||
| .map(rule -> String.format("Rule ID: %s, Description: %s, Kind: %s", rule.id(), | ||
| rule.description(), rule.kind())) | ||
| .collect(Collectors.joining("\n")); | ||
| Assert.fail(String.format("Expected rule with ID '%s' not found.%nFound rules:%n%s", id, summary)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.