Skip to content

Commit

Permalink
Merge pull request #20 from vegaprotocol/feature/detect-duplicate-codes
Browse files Browse the repository at this point in the history
Detect duplicate codes
  • Loading branch information
edd authored Mar 1, 2022
2 parents b6f9134 + 0660ee8 commit f0ed3b1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vegaprotocol/approbation",
"version": "2.1.0",
"version": "2.1.1",
"description": "Match Acceptance Criteria Codes with the tests that test them",
"main": "./bin/approbation.js",
"engine": ">= 16",
Expand Down
59 changes: 46 additions & 13 deletions src/check-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ function * chunks (arr, n = 3) {
}
}

/**
* Find the unique codes in a set
* Source: https://stackoverflow.com/questions/840781/get-all-non-unique-values-i-e-duplicate-more-than-one-occurrence-in-an-array
* Could be replaced with lodash, but it's not worth the extra dep
*
* @return String[] Duplicate codes
*/
function findDuplicates(codes) {
const uniq = codes.flat().map(code => {
return {
count: 1,
code: code
}
})
.reduce((a, b) => {
a[b.code] = (a[b.code] || 0) + b.count
return a
}, {})

return Object.keys(uniq).filter((a) => uniq[a] > 1)
}


// Outputs acceptance criteria count if it's acceptable
const isVerbose = false

Expand Down Expand Up @@ -80,11 +103,21 @@ function checkPath (files) {
const chunkedMatches = [...chunks(matchedContent)]

// Then get a count of unique elements in each of those array. They should all be one
const totalAcceptanceCriteria = chunkedMatches.map(c => [...new Set(c)].length)
const totalAcceptanceCriteria = chunkedMatches.map(c => [...new Set(c)])

const dupes = findDuplicates(totalAcceptanceCriteria)
if (dupes.length > 0) {
// There are multiple uses of the same code, warn
countErrorFiles++
console.group(file)
console.error('Found multiple uses of the same code:')
console.dir(dupes)

}

// If all of the arrays aren't 1, there's probably a mistake. Output all chunks to
// point to where the error is
const unbalancedChunks = totalAcceptanceCriteria.filter(i => i !== 1)
const unbalancedChunks = totalAcceptanceCriteria.filter(i => i.length !== 1)

countAcceptanceCriteria += totalAcceptanceCriteria.length

Expand All @@ -95,19 +128,19 @@ function checkPath (files) {
console.log(`${totalAcceptanceCriteria.length} acceptance criteria`)
console.error('Found something odd:')
console.dir(chunkedMatches)
} else {
// The files are *valid*, at least. But do they have enough ACs?
if (totalAcceptanceCriteria.length >= minimumAcceptableACsPerSpec) {
countAcceptableFiles++
if (isVerbose) {
console.group(file)
console.log(`${totalAcceptanceCriteria.length} acceptance criteria`)
}
} else {
countErrorFiles++
}

// The files are *valid*, at least. But do they have enough ACs?
if (totalAcceptanceCriteria.length >= minimumAcceptableACsPerSpec) {
countAcceptableFiles++
if (isVerbose) {
console.group(file)
console.error(`${totalAcceptanceCriteria.length} acceptance criteria`)
console.log(`${totalAcceptanceCriteria.length} acceptance criteria`)
}
} else {
countErrorFiles++
console.group(file)
console.error(`${totalAcceptanceCriteria.length} acceptance criteria`)
}

console.groupEnd(file)
Expand Down
9 changes: 9 additions & 0 deletions test/check-codes/duplicate-code/0028-GOVE-governance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin finibus quam a gravida blandit. Curabitur ornare eu erat id porttitor. Ut id accumsan mi, nec eleifend justo. Nulla suscipit massa eu arcu semper placerat. Quisque lobortis porttitor pellentesque. Nam sit amet lobortis odio, ac rhoncus massa. Donec eget venenatis tellus. Aenean vel neque ut nisl iaculis tempor. Phasellus ut magna enim.

Suspendisse eget pretium lacus. Praesent ac metus felis. Fusce eu luctus magna, ut mattis nulla. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam mollis augue eget odio convallis facilisis. Proin sed lorem sit amet sem faucibus sagittis. In pharetra metus molestie enim pellentesque imperdiet. Nulla quis leo nec sem ornare tincidunt. Cras vel sapien ut justo luctus sollicitudin. Praesent neque lacus, accumsan a elementum ut, elementum in ipsum. Donec vel risus in sapien eleifend commodo sit amet eget nisl. Mauris ac magna tortor. Nunc quam libero, aliquet in felis auctor, mollis vulputate lectus. Duis ac tellus in augue pulvinar sollicitudin.


- A criteria that is labelled properly (<a name="0028-GOVE-001" href="#0028-GOVE-001">0028-GOVE-001</a>)
- Another criteria that is labelled properly (<a name="0028-GOVE-002" href="#0028-GOVE-002">0028-GOVE-002</a>)
- This one is a duplicate of the above (<a name="0028-GOVE-002" href="#0028-GOVE-002">0028-GOVE-002</a>)
- This one is valid, but should probably be 4 (<a name="0028-GOVE-003" href="#0028-GOVE-003">0028-GOVE-003</a>)
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ test('check-codes: An invalid file', t => {
t.equal(res.countErrorFiles, 1, 'One file has an error')
})

test('check-codes: Detect duplicate codes as an error', t => {
t.plan(2)

const { exitCode, res } = checkCodes('./test/check-codes/duplicate-code/**/*.md')
t.equal(exitCode, 1, 'Expected failure')
t.equal(res.countErrorFiles, 1, 'One file has an error')
})

test('check-codes: Readme is always ignored', t => {
t.plan(4)

Expand Down

0 comments on commit f0ed3b1

Please sign in to comment.