Skip to content

Commit

Permalink
Issue checkstyle#273: converts diff.groovy to use Checkstyle's CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Jan 10, 2023
1 parent 0b0711d commit 1d28d12
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 82 deletions.
9 changes: 6 additions & 3 deletions .ci/validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ checkstyle-tester-diff-groovy-patch)
cp projects-to-test-on.properties ../.ci-temp/projects-to-test-on.properties
sed -i'' 's/^guava/#guava/' ../.ci-temp/projects-to-test-on.properties
sed -i'' 's/#checkstyle/checkstyle/' ../.ci-temp/projects-to-test-on.properties
export JDK_JAVA_OPTIONS="-Xmx2048m"
groovy diff.groovy -l ../.ci-temp/projects-to-test-on.properties \
-c my_check.xml -b master -p patch-branch -r ../.ci-temp/checkstyle
;;
Expand All @@ -49,6 +50,7 @@ checkstyle-tester-diff-groovy-base-patch)
cd .ci-temp/checkstyle
git checkout -b patch-branch
cd ../../checkstyle-tester
export JDK_JAVA_OPTIONS="-Xmx2048m"
groovy diff.groovy -l projects-to-test-on.properties \
-bc my_check.xml -pc my_check.xml -b master -p patch-branch -r ../.ci-temp/checkstyle
;;
Expand All @@ -58,6 +60,7 @@ checkstyle-tester-diff-groovy-patch-only)
cd .ci-temp/checkstyle
git checkout -b patch-branch
cd ../../checkstyle-tester
export JDK_JAVA_OPTIONS="-Xmx2048m"
groovy diff.groovy -l projects-to-test-on.properties \
-pc my_check.xml -p patch-branch -r ../.ci-temp/checkstyle -m single
;;
Expand All @@ -72,10 +75,10 @@ checkstyle-tester-diff-groovy-regression-single)
cd .ci-temp/contribution/checkstyle-tester
sed -i'' 's/^guava/#guava/' projects-to-test-on.properties
sed -i'' 's/#checkstyle|/checkstyle|/' projects-to-test-on.properties
export MAVEN_OPTS="-Xmx2048m"
export JDK_JAVA_OPTIONS="-Xmx2048m"
groovy ./diff.groovy --listOfProjects projects-to-test-on.properties \
-pc ../../../checkstyle-tester/diff-groovy-regression-config.xml \
-r ../../checkstyle -xm "-Dcheckstyle.failsOnError=false" \
-r ../../checkstyle \
-m single -p master

# Run report with current branch
Expand All @@ -85,7 +88,7 @@ checkstyle-tester-diff-groovy-regression-single)
rm -rf reports repositories
groovy ./diff.groovy --listOfProjects projects-to-test-on.properties \
-pc diff-groovy-regression-config.xml -r ../.ci-temp/checkstyle/ \
-m single -p master -xm "-Dcheckstyle.failsOnError=false"
-m single -p master

cd ..
# We need to ignore file paths below, since they will be different between reports
Expand Down
5 changes: 2 additions & 3 deletions checkstyle-tester/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ will be analyzed by Checkstyle during report generation.
This option is useful for Windows users where they are restricted to maximum directory depth
(optional, default is false).

**extraMvnRegressionOptions** (xm) - this option can be used to supply extra command line arguments
to maven during the Checkstyle regression run. For example, if you want to skip site generation, you
would add `--extraMvnRegressionOptions "-Dmaven.site.skip=true"` to `diff.groovy` execution.
**extraRegressionOptions** (xo) - this option can be used to supply extra command line arguments
to Checkstyle during the regression run.

**allowExcludes** (g) - this option tells `diff.groovy` to allow paths and files defined in the file
specified for the `--listOfProjects (-l)` argument to be excluded from report generation
Expand Down
114 changes: 55 additions & 59 deletions checkstyle-tester/diff.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def getCliOptions(args) {
+ ' as a shorter version to prevent long paths. (optional, default is false)')
m(longOpt: 'mode', args: 1, required: false, argName: 'mode', 'The mode of the tool:' \
+ ' \'diff\' or \'single\'. (optional, default is \'diff\')')
xm(longOpt: 'extraMvnRegressionOptions', args: 1, required: false, 'Extra arguments to pass to Maven' \
+ 'for Checkstyle Regression run (optional, ex: -Dmaven.prop=true)')
xo(longOpt: 'extraRegressionOptions', args: 1, required: false, 'Extra arguments to pass ' \
+ 'for Checkstyle Regression run (optional, ex: -Dprop=true)')
}
return cli.parse(args)
}
Expand Down Expand Up @@ -218,11 +218,11 @@ def launchCheckstyleReport(cfg) {

// If "no exception" testing, these may not be defined in repos other than checkstyle
if (isRegressionTesting) {
println "Installing Checkstyle artifact ($cfg.branch) into local Maven repository ..."
println "Building Checkstyle artifact ($cfg.branch) ..."
executeCmd("git checkout $cfg.branch", cfg.localGitRepo)
executeCmd("git log -1 --pretty=MSG:%s%nSHA-1:%H", cfg.localGitRepo)
executeCmd("mvn -e --no-transfer-progress --batch-mode -Pno-validations clean install",
cfg.localGitRepo)
executeCmd("mvn -e --no-transfer-progress --batch-mode -Pno-validations clean package -Passembly " +
"-Dassembly.skipAssembly=true -Dmaven.test.skip=true", cfg.localGitRepo)
}

cfg.checkstyleVersion =
Expand All @@ -247,11 +247,10 @@ def launchCheckstyleReport(cfg) {
def generateCheckstyleReport(cfg) {
println 'Testing Checkstyle started'

def targetDir = 'target'
def srcDir = getOsSpecificPath("src", "main", "java")
def allJar = getOsSpecificPath(cfg.localGitRepo.path, "target", "checkstyle-" + cfg.checkstyleVersion + "-all.jar")
def reposDir = 'repositories'
def reportsDir = 'reports'
makeWorkDirsIfNotExist(srcDir, reposDir, reportsDir)
makeWorkDirsIfNotExist(reposDir, reportsDir)

final REPO_NAME_PARAM_NO = 0
final REPO_TYPE_PARAM_NO = 1
Expand All @@ -261,11 +260,10 @@ def generateCheckstyleReport(cfg) {
final FULL_PARAM_LIST_SIZE = 5

def checkstyleConfig = cfg.checkstyleCfg
def checkstyleVersion = cfg.checkstyleVersion
def allowExcludes = cfg.allowExcludes
def listOfProjectsFile = new File(cfg.listOfProjects)
def projects = listOfProjectsFile.readLines()
def extraMvnRegressionOptions = cfg.extraMvnRegressionOptions
def extraRegressionOptions = cfg.extraRegressionOptions

projects.each {
project ->
Expand All @@ -287,27 +285,26 @@ def generateCheckstyleReport(cfg) {
excludes = params[REPO_EXCLUDES_PARAM_NO]
}

deleteDir(srcDir)
def srcDir
def saveDir = getOsSpecificPath("$reportsDir", "$repoName")
if (repoType == 'local') {
copyDir(repoUrl, getOsSpecificPath("$srcDir", "$repoName"))
srcDir = new File("$repoUrl").getCanonicalPath()
} else {
cloneRepository(repoName, repoType, repoUrl, commitId, reposDir)
copyDir(getOsSpecificPath("$reposDir", "$repoName"), getOsSpecificPath("$srcDir", "$repoName"))
srcDir = getOsSpecificPath("$reposDir", "$repoName")
}
runMavenExecution(srcDir, excludes, checkstyleConfig,
checkstyleVersion, extraMvnRegressionOptions)
def repoPath = repoUrl
if (repoType != 'local') {
repoPath = new File(getOsSpecificPath("$reposDir", "$repoName")).absolutePath
}
postProcessCheckstyleReport(targetDir, repoName, repoPath)
deleteDir(getOsSpecificPath("$srcDir", "$repoName"))
moveDir(targetDir, getOsSpecificPath("$reportsDir", "$repoName"))

def mainClassOptions = [
srcDir: srcDir,
excludes: excludes,
checkstyleConfig: checkstyleConfig,
saveDir: saveDir,
extraRegressionOptions: extraRegressionOptions,
]

runCliExecution(allJar, mainClassOptions)
}
}

// restore empty_file to make src directory tracked by git
new File(getOsSpecificPath("$srcDir", "empty_file")).createNewFile()
}

def getLastCheckstyleCommitSha(gitRepo, branch) {
Expand Down Expand Up @@ -537,11 +534,7 @@ def getFilenameWithoutExtension(filename) {
return filenameWithoutExtension
}

def makeWorkDirsIfNotExist(srcDirPath, repoDirPath, reportsDirPath) {
def srcDir = new File(srcDirPath)
if (!srcDir.exists()) {
srcDir.mkdirs()
}
def makeWorkDirsIfNotExist(repoDirPath, reportsDirPath) {
def repoDir = new File(repoDirPath)
if (!repoDir.exists()) {
repoDir.mkdir()
Expand Down Expand Up @@ -628,36 +621,39 @@ def getProjectsStatistic(diffDir) {
return projectsStatistic
}

def runMavenExecution(srcDir, excludes, checkstyleConfig,
checkstyleVersion, extraMvnRegressionOptions) {
println "Running 'mvn clean' on $srcDir ..."
def mvnClean = "mvn -e --no-transfer-progress --batch-mode clean"
executeCmd(mvnClean)
println "Running Checkstyle on $srcDir ... with excludes {$excludes}"
def mvnSite = "mvn -e --no-transfer-progress --batch-mode site " +
"-Dcheckstyle.config.location=$checkstyleConfig -Dcheckstyle.excludes=$excludes"
if (checkstyleVersion) {
mvnSite = mvnSite + " -Dcheckstyle.version=$checkstyleVersion"
def runCliExecution(allJar, mainClassOptions) {
def saveLoc = new File(mainClassOptions.saveDir)
if (!saveLoc.exists()) {
saveLoc.mkdirs()
}

println "Running Checkstyle CLI on ${mainClassOptions.srcDir} ... with excludes {${mainClassOptions.excludes}}"
def cliCommand = "java -jar $allJar -c ${mainClassOptions.checkstyleConfig} " +
"${mainClassOptions.srcDir} -f xml -o " +
getOsSpecificPath("${mainClassOptions.saveDir}", "checkstyle-result.xml") +
" -e " + getOsSpecificPath("${mainClassOptions.srcDir}", ".git") +
" -e " + getOsSpecificPath("${mainClassOptions.srcDir}", ".hg")

if (mainClassOptions.excludes.length() > 0) {
for (String singleExclude : mainClassOptions.excludes.split(',')) {
if (isWindows()) {
singleExclude = singleExclude.replace("/", "\\")
}
cliCommand = cliCommand + " -x " + singleExclude
}
}
if (extraMvnRegressionOptions) {
mvnSite = mvnSite + " "

if (!extraMvnRegressionOptions.startsWith("-")) {
mvnSite = mvnSite + "-"
if (mainClassOptions.extraRegressionOptions) {
cliCommand = cliCommand + " "

if (!mainClassOptions.extraRegressionOptions.startsWith("-")) {
cliCommand = cliCommand + "-"
}
mvnSite = mvnSite + extraMvnRegressionOptions
cliCommand = cliCommand + mainClassOptions.extraRegressionOptions
}
println(mvnSite)
executeCmd(mvnSite)
println "Running Checkstyle on $srcDir - finished"
}

def postProcessCheckstyleReport(targetDir, repoName, repoPath) {
new AntBuilder().replace(
file: getOsSpecificPath("$targetDir", "checkstyle-result.xml"),
token: new File(getOsSpecificPath("src", "main", "java", "$repoName")).absolutePath,
value: getOsSpecificPath("$repoPath")
)
executeCmd(cliCommand)
println "Running Checkstyle CLI on ${mainClassOptions.srcDir} - finished"
}

def copyDir(source, destination) {
Expand Down Expand Up @@ -760,7 +756,7 @@ class Config {
def tmpMasterReportsDir
def tmpPatchReportsDir
def diffDir
def extraMvnRegressionOptions
def extraRegressionOptions

def checkstyleVersion
def sevntuVersion
Expand All @@ -773,7 +769,7 @@ class Config {

shortFilePaths = cliOptions.shortFilePaths
listOfProjects = cliOptions.listOfProjects
extraMvnRegressionOptions = cliOptions.extraMvnRegressionOptions
extraRegressionOptions = cliOptions.extraRegressionOptions

checkstyleVersion = cliOptions.checkstyleVersion
allowExcludes = cliOptions.allowExcludes
Expand Down Expand Up @@ -823,7 +819,7 @@ class Config {
checkstyleCfg: baseConfig,
listOfProjects: listOfProjects,
destDir: tmpMasterReportsDir,
extraMvnRegressionOptions: extraMvnRegressionOptions,
extraRegressionOptions: extraRegressionOptions,
allowExcludes:allowExcludes,
]
}
Expand All @@ -835,7 +831,7 @@ class Config {
checkstyleCfg: patchConfig,
listOfProjects: listOfProjects,
destDir: tmpPatchReportsDir,
extraMvnRegressionOptions: extraMvnRegressionOptions,
extraRegressionOptions: extraRegressionOptions,
allowExcludes: allowExcludes,
]
}
Expand Down
4 changes: 2 additions & 2 deletions checkstyle-tester/projects-for-circle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ guava|git|https://github.com/google/guava|v28.2||
#openjdk7|hg|http://hg.openjdk.java.net/jdk7/jdk7/jdk/|||
#openjdk8|hg|http://hg.openjdk.java.net/jdk8/jdk8/jdk/|default|
#All details at checkstyle/checkstyle#3033: ModalDialogActivationTest till JDK-8166015 ; 'jxc/8073519/**' not compilable by design ; ', jhsdb/**' - checkstyle do not support unicode identifiers
#openjdk9|hg|http://hg.openjdk.java.net/jdk9/jdk9/jdk/|default|**/test/java/awt/Focus/ModalDialogActivationTest/ModalDialogActivationTest.java,**/test/javax/xml/bind/jxc/8073519/**,**/test/sun/tools/jhsdb/**
#openjdk9|hg|http://hg.openjdk.java.net/jdk9/jdk9/jdk/|default|.*/test/java/awt/Focus/ModalDialogActivationTest/ModalDialogActivationTest.java,.*/test/javax/xml/bind/jxc/8073519/,.*/test/sun/tools/jhsdb/
#infinispan|git|https://github.com/infinispan/infinispan|7.2.5.Final||
#protonpack|git|https://github.com/poetix/protonpack|protonpack-1.7||
#jOOL|git|https://github.com/jOOQ/jOOL|version-0.9.7||
#lucene-solr|git|https://github.com/apache/lucene-solr|releases/lucene-solr/5.4.0||
#tapestry-5|git|https://github.com/apache/tapestry-5|5.4.0|**/archetype-resources/**/*|
#tapestry-5|git|https://github.com/apache/tapestry-5|5.4.0|.*/archetype-resources/|
#storm|git|https://github.com/apache/storm|v0.9.6||
#cassandra|git|https://github.com/apache/cassandra|cassandra-3.2||
#apache-commons|git|https://github.com/apache/commons-lang|LANG_3_4||
Expand Down

0 comments on commit 1d28d12

Please sign in to comment.