Skip to content

Commit

Permalink
normalizePath
Browse files Browse the repository at this point in the history
  • Loading branch information
kilianc committed May 15, 2024
1 parent 6223238 commit 049a06f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/check-threshold.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require('fs')
const normalizePath = require('./normalize-path')

const checkThreshold = module.exports = async ({ threshold, path, revision }) => {
path = (path.replace('./', '') + '/').replace('//', '/')
path = normalizePath(path)

const coverageText = fs.readFileSync(`go-cover/${path}revisions/${revision}.txt`, 'utf8').split('\n').slice(0, -1)
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()
Expand Down
62 changes: 62 additions & 0 deletions src/normalize-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const path = require('path')

const normalizePath = module.exports = (dir) => {
dir = path.normalize(dir)

if (dir === '/' || dir === './' || dir === '.') {
return ''
}

if (dir.startsWith('./')) {
dir = dir.substring(2)
}

if (dir.startsWith('/')) {
dir = dir.substring(1)
}

if (dir.endsWith('/')) {
dir = dir.substring(0, dir.length - 1)
}

return dir
}

const test = async () => {
let pass = true

const tests = [
{ input: '/', expected: '' },
{ input: '/foo', expected: 'foo' },

{ input: './', expected: '' },
{ input: './foo', expected: 'foo' },
{ input: './foo/', expected: 'foo' },
{ input: './foo/bar', expected: 'foo/bar' },
{ input: './foo/bar/', expected: 'foo/bar' },

{ input: '', expected: '' },
{ input: 'foo', expected: 'foo' },
{ input: 'foo/', expected: 'foo' },
{ input: 'foo/bar', expected: 'foo/bar' },
{ input: 'foo/bar/', expected: 'foo/bar' },
]

for (const { input, expected } of tests) {
const result = normalizePath(input)
if (result !== expected) {
console.error(`error("${input}"): expected "${expected}" but got "${result}"`)
pass = false
}
}

if (!pass) {
process.exit(1)
} else {
console.log('All tests passed')
}
}

if (require.main === module) {
test()
}
11 changes: 6 additions & 5 deletions src/update-comment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs')
const normalizePath = require('./normalize-path')

const updateCodeCoverageComment = module.exports = async ({ context, github, inputs, path, revision, threshold }) => {
const updateCodeCoverageComment = module.exports = async ({ context, github, path, revision, threshold }) => {
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -12,14 +13,14 @@ const updateCodeCoverageComment = module.exports = async ({ context, github, inp
return comment.body.startsWith(`<!-- coverage (${path})-->`)
}) || {}

path = (path.replace('./', '') + '/').replace('//', '/')
path = normalizePath(path)

const coverageText = fs.readFileSync(`go-cover/${path}revisions/${revision}.txt`, 'utf8').split('\n').slice(0, -1)
const coverageText = fs.readFileSync(`go-cover/${path}/revisions/${revision}.txt`, 'utf8').split('\n').slice(0, -1)
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()
const coverage = parseFloat(coverageTextSummary.replace('%', ''), 10)
const coverageEmoji = coverage >= threshold ? '' : `<kbd>🔻 ${(coverage - threshold).toFixed(1)}%</kbd> `
const pathText = (path !== '' ? ` for <kbd>${path}</kbd>` : '')
const url = `https://${context.repo.owner}.github.io/${context.repo.repo}/${path}?hash=${revision}`
const pathText = (path !== '/' ? ` for <kbd>${path}/</kbd>` : '')
const url = `https://${context.repo.owner}.github.io/${context.repo.repo}/${path}/?hash=${revision}`

const commentBody = [
`<!-- coverage (${path})-->`,
Expand Down

0 comments on commit 049a06f

Please sign in to comment.