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

enable junit reporting for the pre-commit stage #154

Merged
merged 7 commits into from
Jul 26, 2019
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
12 changes: 9 additions & 3 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,24 @@ pipeline {
HOME = "${env.WORKSPACE}"
}
steps {
withGithubNotify(context: 'Check pre-commit') {
withGithubNotify(context: 'Check pre-commit', tab: 'tests') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh '''
env | sort
curl https://pre-commit.com/install-local.py | python -
git diff-tree --no-commit-id --name-only -r ${GIT_BASE_COMMIT} | xargs pre-commit run --files
git diff-tree --no-commit-id --name-only -r ${GIT_BASE_COMMIT} | xargs pre-commit run --files | tee pre-commit.out
'''
}
}
}
post {
always {
preCommitToJunit(input: "${BASE_DIR}/pre-commit.out", output: "${BASE_DIR}/pre-commit-junit.xml")
archiveArtifacts artifacts: "${BASE_DIR}/pre-commit.out", allowEmptyArchive: true, defaultExcludes: false
junit testResults: "${BASE_DIR}/pre-commit-junit.xml", allowEmptyResults: true, keepLongStdio: true
}
}
}
/**
Checkout the code and stash it, to use it on other stages.
Expand Down
12 changes: 12 additions & 0 deletions src/test/groovy/PreCommitToJunitStepTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,16 @@ class PreCommitToJunitStepTests extends BasePipelineTest {
new File("${compareWith}/${file}"),
new File("target/${file}"), 'UTF-8'))
}

@Test
void testSuccess3() throws Exception {
v1v marked this conversation as resolved.
Show resolved Hide resolved
def script = loadScript(scriptName)
def file = 'skipped.xml'
script.call(input: 'skipped.txt', output: file)
printCallStack()
assertJobStatusSuccess()
assertTrue("The files differ!", FileUtils.contentEqualsIgnoreEOL(
new File("${compareWith}/${file}"),
new File("target/${file}"), 'UTF-8'))
}
}
2 changes: 1 addition & 1 deletion src/test/resources/preCommitToJunit/output/simple.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<testsuite><testcase classname="pre_commit.lint" name="Check for case conflicts" /><testcase classname="pre_commit.lint" name="Check that executables have shebangs"><skipped message="skipped"/><system-out><![CDATA[]]></system-out></testcase><testcase classname="pre_commit.lint" name="Detect the EXTREMELY confusing unicode character U2013"><error message="error"/><system-out><![CDATA[hookid: forbid-en-dashes
<testsuite><testcase classname="pre_commit.lint" name="Check for case conflicts" /><testcase classname="pre_commit.lint" name="Check that executables have shebangs"><skipped message="skipped"/><system-out><![CDATA[]]></system-out></testcase><testcase classname="pre_commit.lint" name="isortno files to check"><skipped message="skipped"/><system-out><![CDATA[]]></system-out></testcase><testcase classname="pre_commit.lint" name="Detect the EXTREMELY confusing unicode character U2013"><error message="error"/><system-out><![CDATA[hookid: forbid-en-dashes
]]></system-out></testcase></testsuite>
1 change: 1 addition & 0 deletions src/test/resources/preCommitToJunit/output/skipped.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<testsuite><testcase classname="pre_commit.lint" name="isortno files to check"><skipped message="skipped"/><system-out><![CDATA[]]></system-out></testcase><testcase classname="pre_commit.lint" name="blackno files to check"><skipped message="skipped"/><system-out><![CDATA[]]></system-out></testcase><testcase classname="pre_commit.lint" name="Flake8no files to check"><skipped message="skipped"/><system-out><![CDATA[]]></system-out></testcase><testcase classname="pre_commit.lint" name="License header checkno files to check"><skipped message="skipped"/><system-out><![CDATA[]]></system-out></testcase></testsuite>
1 change: 1 addition & 0 deletions src/test/resources/preCommitToJunit/simple.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Check for case conflicts........................................................................Passed
Check that executables have shebangs............................................................Skipped
isort................................................(no files to check)Skipped
Detect the EXTREMELY confusing unicode character U+2013.........................................Failed
hookid: forbid-en-dashes
16 changes: 16 additions & 0 deletions src/test/resources/preCommitToJunit/skipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[INFO] Initializing environment for https://github.com/pre-commit/mirrors-isort.
[INFO] Initializing environment for https://github.com/ambv/black.
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/mirrors-isort.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/ambv/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
isort................................................(no files to check)Skipped
black................................................(no files to check)Skipped
Flake8...............................................(no files to check)Skipped
License header check.................................(no files to check)Skipped
7 changes: 3 additions & 4 deletions vars/preCommitToJunit.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ def call(Map params = [:]) {
def output = params.containsKey('output') ? params.output : error('preCommitToJunit: output parameter is required.')

def content = readFile(file: input)

def id, status, message = '', inprogress = false
def data = '<testsuite>'
content.split('\n').each { line ->
def matcher = line =~ '(.+)\\.(Passed|Skipped|Failed)$'
def matcher = line =~ '(.+)(\\.Passed|\\)Skipped|\\.Skipped|\\.Failed)$'
if (matcher.find()) {
if (id) {
data += toJunit(id, status, message)
Expand All @@ -52,9 +51,9 @@ def call(Map params = [:]) {

def toJunit(String name, String status, String message) {
String output = "<testcase classname=\"pre_commit.lint\" name=\"${name}\""
if (status?.toLowerCase().equals('skipped')) {
if (status?.toLowerCase().contains('skipped')) {
output += "><skipped message=\"skipped\"/><system-out><![CDATA[${message}]]></system-out></testcase>"
} else if (status?.toLowerCase().equals('failed')) {
} else if (status?.toLowerCase().contains('failed')) {
output += "><error message=\"error\"/><system-out><![CDATA[${message}]]></system-out></testcase>"
} else {
output += " />"
Expand Down