Skip to content

Commit

Permalink
Fix #554: Add diff support
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Nov 23, 2022
1 parent 7dd14c7 commit e5b6c4e
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 224 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"bracketSpacing": true,
"arrowParens": "avoid"
}
220 changes: 139 additions & 81 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFile } from 'fs/promises'
import os from 'os'
import { join } from 'path'
import { execPath } from 'process'
import { parse, satisfies } from 'semver'
import { diff, parse, satisfies, sort } from 'semver'
import { FileResult, fileSync as tmpFile } from 'tmp'
import { promisify } from 'util'

Expand All @@ -12,92 +12,112 @@ const exec = promisify(execFile)
interface TestData {
version: string
compareTo?: string
diffWith?: string
satisfies?: string
identifier?: string
lenient?: boolean
}

const data: TestData[] = [
{version: 'Forty-two'},
{version: 'Forty-two', lenient: true},
{version: 'Forty-two', lenient: false},
{version: '1.2'},
{version: '1'},
{ version: 'Forty-two' },
{ version: 'Forty-two', lenient: true },
{ version: 'Forty-two', lenient: false },
{ version: '1.2' },
{ version: '1' },

{version: '0.0.0', satisfies: ''},
{version: '0.0.0', satisfies: '', identifier: 'alpha'},
{version: '0.0.0', satisfies: '', identifier: 'alpha.0'},
{version: '0.0.0', satisfies: '', identifier: 'alpha.beta'},
{version: '0.0.0', compareTo: '0.0.0', satisfies: '*'},
{version: '0.0.0', compareTo: '0.0'},
{version: '0.0.0', compareTo: '0'},
{version: '0.0.0', compareTo: ''},
{version: '0.0.0', compareTo: 'Forty-two'},
{version: '0.0.1'},
{version: '0.0.1', compareTo: '0.0.2'},
{version: '0.1.2'},
{version: '0.1.2', compareTo: '0.2.0'},
{version: '0.1.2', compareTo: '0.1.1'},
{version: '1.2.3'},
{version: '1.2.3', identifier: 'snapshot'},
{version: '1.2.3', identifier: 'snapshot.alpha'},
{version: '1.2.3', identifier: 'snapshot.0'},
{version: '1.2.3', identifier: 'snapshot.1.2'},
{version: '1.2.3', identifier: ''},
{version: '1.2.3', identifier: ' '},
{version: '1.2.3', identifier: ' '},
{version: '1.2.3', compareTo: '1.2.3'},
{version: '1.2.3', compareTo: '1.2.3+42'},
{version: '1.2.3', compareTo: '1.2.3+42.24'},
{version: '1.2.3', compareTo: '1.2.3-42'},
{version: '1.2.3', compareTo: '1.2.3-42.24'},
{version: '1.2.3', compareTo: '1.2.3-alpha'},
{version: '1.2.3', compareTo: '1.2.3-beta'},
{version: '1.2.3', compareTo: '1.2.4-alpha+3'},
{version: '1.2.3', compareTo: '1.2.4'},
{version: '1.2.3', compareTo: '1.3.0'},
{version: '1.2.3', compareTo: '2.0.0'},
{version: '1.2.3', compareTo: '1.2.2'},
{version: '1.2.3', compareTo: '1.1.99'},
{version: '1.2.3', compareTo: '0.99.99'},
{version: '1.2.3', compareTo: ''},
{version: '1.2.3', compareTo: ' '},
{version: '1.2.3', compareTo: 'XXX'},
{version: '1.2.3', satisfies: '>1.0.0'},
{version: '1.2.3-alpha', satisfies: '>1.0.0'},
{version: '1.2.3', satisfies: '<2.0.0'},
{version: '1.2.3-alpha', satisfies: '<2.0.0'},
{version: '1.2.3', satisfies: '>=1.2.3'},
{version: '1.2.3-alpha', satisfies: '>=1.2.3'},
{version: '1.2.3', satisfies: '<=1.2.3'},
{version: '1.2.3-alpha', satisfies: '<=1.2.3'},
{version: '1.2.3', satisfies: '=1.2.3'},
{version: '1.2.3', satisfies: '>1.0.0 <2.0.0'},
{version: '1.2.3', satisfies: '1.2.x'},
{version: '1.2.3', satisfies: '1.x'},
{version: '2.2.3', satisfies: '<2.0.0'},
{version: '2.2.3', satisfies: '<=2.0.0'},
{version: '2.2.3', satisfies: '=1.2.3'},
{version: '2.2.3', satisfies: '>1.0.0 < 2.0.0'},
{version: '2.2.3', satisfies: '1.2.x'},
{version: '2.2.3', satisfies: '1.x'},
{version: '2.2.3', satisfies: ''},
{version: '2.2.3', satisfies: ' '},
{version: '2.2.3', satisfies: ' '},
{version: '2.2.3', satisfies: 'XXX'},
{version: '1.2.3+alpha'},
{version: '1.2.3+alpha.beta'},
{version: '1.2.3+42'},
{version: '1.2.3+42.24'},
{version: '1.2.3-alpha'},
{version: '1.2.3-alpha.beta'},
{version: '1.2.3-42'},
{version: '1.2.3-42.24'},
{version: '1.2.3-alpha+42.24'},
{version: '1.2.3-alpha.beta+123456'},
{version: '1.2.3-42+24'},
{version: '1.2.3-42.24+24.42'},
{version: '1.2.3-alpha.gamma+4.5.6', satisfies: '1.x'}
{ version: '0.0.0', satisfies: '' },
{ version: '0.0.0', satisfies: '', identifier: 'alpha' },
{ version: '0.0.0', satisfies: '', identifier: 'alpha.0' },
{ version: '0.0.0', satisfies: '', identifier: 'alpha.beta' },
{ version: '0.0.0', compareTo: '0.0.0', satisfies: '*' },
{ version: '0.0.0', compareTo: '0.0' },
{ version: '0.0.0', compareTo: '0' },
{ version: '0.0.0', compareTo: '' },
{ version: '0.0.0', compareTo: 'Forty-two' },
{ version: '0.0.1' },
{ version: '0.0.1', compareTo: '0.0.2' },
{ version: '0.1.2' },
{ version: '0.1.2', compareTo: '0.2.0' },
{ version: '0.1.2', compareTo: '0.1.1' },
{ version: '1.2.3' },
{ version: '1.2.3', identifier: 'snapshot' },
{ version: '1.2.3', identifier: 'snapshot.alpha' },
{ version: '1.2.3', identifier: 'snapshot.0' },
{ version: '1.2.3', identifier: 'snapshot.1.2' },
{ version: '1.2.3', identifier: '' },
{ version: '1.2.3', identifier: ' ' },
{ version: '1.2.3', identifier: ' ' },
{ version: '1.2.3', compareTo: '1.2.3', diffWith: '1.2.4' },
{ version: '1.2.3', compareTo: '1.2.3', diffWith: '1.3.0' },
{ version: '1.2.3', compareTo: '1.2.3', diffWith: '2.0.0' },
{ version: '1.2.3', compareTo: '1.2.3+42' },
{ version: '1.2.3', compareTo: '1.2.3+42.24' },
{ version: '1.2.3', compareTo: '1.2.3-42' },
{ version: '1.2.3', compareTo: '1.2.3-42.24' },
{ version: '1.2.3', compareTo: '1.2.3-alpha' },
{ version: '1.2.3', compareTo: '1.2.3-beta' },
{ version: '1.2.3', compareTo: '1.2.4-alpha+3' },
{ version: '1.2.3', compareTo: '1.2.4' },
{ version: '1.2.3', compareTo: '1.3.0' },
{ version: '1.2.3', compareTo: '2.0.0' },
{ version: '1.2.3', compareTo: '1.2.2' },
{ version: '1.2.3', compareTo: '1.1.99' },
{ version: '1.2.3', compareTo: '0.99.99' },
{ version: '1.2.3', compareTo: '' },
{ version: '1.2.3', compareTo: ' ' },
{ version: '1.2.3', diffWith: '1.2.4' },
{ version: '1.2.3', diffWith: '1.3.0' },
{ version: '1.2.3', diffWith: '2.0.0' },
{ version: '2.0.0', diffWith: '1.2.3' },
{ version: '2.0.0', diffWith: '1.3.0' },
{ version: '1.2.3', diffWith: '1.2.3-alpha' },
{ version: '1.2.3', diffWith: '1.2.4-alpha' },
{ version: '1.2.3-alpha', diffWith: '1.2.3' },
{ version: '1.2.4-alpha', diffWith: '1.2.3' },
{ version: '1.2.3', diffWith: '1.2.3+42' },
{ version: '1.2.3+42', diffWith: '1.2.3' },
{ version: '1.2.3', diffWith: '1.2.4+42' },
{ version: '1.2.4+42', diffWith: '1.2.3' },
{ version: '1.2.3', diffWith: '1.3.0+42' },
{ version: '1.3.0+42', diffWith: '1.2.3' },
{ version: '1.3.0+42', diffWith: '' },
{ version: '1.3.0+42', diffWith: ' ' },
{ version: '1.2.3', compareTo: 'XXX' },
{ version: '1.2.3', satisfies: '>1.0.0' },
{ version: '1.2.3-alpha', satisfies: '>1.0.0' },
{ version: '1.2.3', satisfies: '<2.0.0' },
{ version: '1.2.3-alpha', satisfies: '<2.0.0' },
{ version: '1.2.3', satisfies: '>=1.2.3' },
{ version: '1.2.3-alpha', satisfies: '>=1.2.3' },
{ version: '1.2.3', satisfies: '<=1.2.3' },
{ version: '1.2.3-alpha', satisfies: '<=1.2.3' },
{ version: '1.2.3', satisfies: '=1.2.3' },
{ version: '1.2.3', satisfies: '>1.0.0 <2.0.0' },
{ version: '1.2.3', satisfies: '1.2.x' },
{ version: '1.2.3', satisfies: '1.x' },
{ version: '2.2.3', satisfies: '<2.0.0' },
{ version: '2.2.3', satisfies: '<=2.0.0' },
{ version: '2.2.3', satisfies: '=1.2.3' },
{ version: '2.2.3', satisfies: '>1.0.0 < 2.0.0' },
{ version: '2.2.3', satisfies: '1.2.x' },
{ version: '2.2.3', satisfies: '1.x' },
{ version: '2.2.3', satisfies: '' },
{ version: '2.2.3', satisfies: ' ' },
{ version: '2.2.3', satisfies: ' ' },
{ version: '2.2.3', satisfies: 'XXX' },
{ version: '1.2.3+alpha' },
{ version: '1.2.3+alpha.beta' },
{ version: '1.2.3+42' },
{ version: '1.2.3+42.24' },
{ version: '1.2.3-alpha' },
{ version: '1.2.3-alpha.beta' },
{ version: '1.2.3-42' },
{ version: '1.2.3-42.24' },
{ version: '1.2.3-alpha+42.24' },
{ version: '1.2.3-alpha.beta+123456' },
{ version: '1.2.3-42+24' },
{ version: '1.2.3-42.24+24.42' },
{ version: '1.2.3-alpha.gamma+4.5.6', satisfies: '1.x' }
]

async function validate(env: NodeJS.ProcessEnv, validate: (githubOutput: FileResult) => Promise<void>) {
Expand Down Expand Up @@ -160,6 +180,9 @@ describe('parse', () => {
expect(output).not.toMatch(new RegExp(`build<<.+${os.EOL}.+${os.EOL}`))
expect(output).not.toMatch(new RegExp(`prerelease<<.+${os.EOL}.+${os.EOL}`))
}

expect(output).not.toMatch(new RegExp(`comparison-result`))
expect(output).not.toMatch(new RegExp(`diff-result`))
})
})
})
Expand All @@ -172,7 +195,8 @@ describe('compare', () => {
await validate(
{
INPUT_VERSION: item.version,
'INPUT_COMPARE-TO': item.compareTo
'INPUT_COMPARE-TO': item.compareTo,
'INPUT_DIFF-WITH': item.diffWith
},
async (githubOutput: FileResult) => {
const output = await readFile(githubOutput.name, 'utf8')
Expand All @@ -188,6 +212,12 @@ describe('compare', () => {
} else if (parsedCompareTo.compare(item.version) === 1) {
expect(output).toMatch(new RegExp(`comparison-result<<.+${os.EOL}<${os.EOL}`))
}

if (item.diffWith) {
expect(output).toMatch(new RegExp(`diff-result<<.+${os.EOL}${diff(item.version, item.diffWith) || ''}${os.EOL}`))
} else {
expect(output).toMatch(new RegExp(`diff-result<<.+${os.EOL}${diff(item.version, item.compareTo) || ''}${os.EOL}`))
}
}
} else {
expect(output).not.toMatch(new RegExp(`comparison-result<<.+${os.EOL}.+${os.EOL}`))
Expand All @@ -199,6 +229,34 @@ describe('compare', () => {
})
})

describe('diff', () => {
data.forEach(item => {
if (item.diffWith !== undefined) {
test(`diff(${item.version}, ${item.diffWith})`, async () => {
await validate(
{
INPUT_VERSION: item.version,
'INPUT_DIFF-WITH': item.diffWith
},
async (githubOutput: FileResult) => {
const output = await readFile(githubOutput.name, 'utf8')

if (item.diffWith) {
const parsedDiffTo = parse(item.diffWith)

if (parsedDiffTo) {
expect(output).toMatch(new RegExp(`diff-result<<.+${os.EOL}${diff(item.version, parsedDiffTo) || ''}${os.EOL}`))
}
} else {
expect(output).not.toMatch(new RegExp(`diff-result<<.+${os.EOL}.+${os.EOL}`))
}
}
)
})
}
})
})

describe('satisfies', () => {
data.forEach(item => {
if (item.satisfies !== undefined) {
Expand Down
4 changes: 4 additions & 0 deletions action-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ inputs:
type: string
compare-to:
type: string
diff-with:
type: string
satisfies:
type: string
identifier:
Expand All @@ -28,6 +30,8 @@ outputs:
type: integer
comparison-result:
type: string
diff-result:
type: string
satisfies:
type: boolean
inc-major:
Expand Down
9 changes: 7 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
compare-to:
required: false
description: A version to compare with, if any
diff-with:
required: false
description: A version to diff with. If not specified, the version will be diffed with the `compare-to` input. If `compare-to` is not specified either, nothing happens.
satisfies:
required: false
description: A range to check against
Expand Down Expand Up @@ -37,6 +40,8 @@ outputs:
description: Number of components in version's pre-release. Individual сomponents are returned as `prerelease-N` outputs, where an is an index from zero to `prerelease-parts` - 1.
comparison-result:
description: If the compare-to was provided, this output will contain "<" if comes after the version, ">" if it preceeds it, and "=" if they are equal
diff-result:
description: If the diff-to or compare-to were provided, this output will contain the diff result
satisfies:
description: true if the version satisfies the given range
inc-major:
Expand All @@ -54,8 +59,8 @@ outputs:
inc-prerelease:
description: A result of the call of the semver's `inc` function with `prerelease` increment
runs:
using: 'node16'
main: 'dist/index.js'
using: node16
main: dist/index.js
branding:
icon: hash
color: gray-dark
Loading

0 comments on commit e5b6c4e

Please sign in to comment.