Skip to content

Commit

Permalink
Merge pull request #3 from nick-invision/add-nots
Browse files Browse the repository at this point in the history
feat: added not* comparisons for negative assertions
  • Loading branch information
nick-invision authored Oct 6, 2020
2 parents 244bad2 + a499d19 commit 8f6ce90
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 13 deletions.
41 changes: 40 additions & 1 deletion .actiongenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = buildConfig({
id: 'comparison',
description: {
short:
'Type of comparison to perform. Supports "exact" (default), "startsWith", "endsWith", "contains", "notEqual"',
'Type of comparison to perform. Supports `exact` (default), `startsWith`, `endsWith`, `contains`, `notEqual`, `notStartsWith`, `notEndsWith`, `notContains`',
},
required: false,
default: 'exact',
Expand Down Expand Up @@ -77,6 +77,19 @@ module.exports = buildConfig({
expected: test
actual: \${{ steps.test-data.outputs.value }}
comparison: startsWith
`.trim(),
},
{
title: 'Example usage w/ notStartsWith assertion',
codeLanguage: 'yaml',
codeBlock: `
- id: test-data
run: echo "::set-output name=value::testing"
- uses: nick-invision@assert-action@v1
with:
expected: abc
actual: \${{ steps.test-data.outputs.value }}
comparison: notStartsWith
`.trim(),
},
{
Expand All @@ -90,6 +103,19 @@ module.exports = buildConfig({
expected: ing
actual: \${{ steps.test-data.outputs.value }}
comparison: endsWith
`.trim(),
},
{
title: 'Example usage w/ notEndsWith assertion',
codeLanguage: 'yaml',
codeBlock: `
- id: test-data
run: echo "::set-output name=value::testing"
- uses: nick-invision@assert-action@v1
with:
expected: abc
actual: \${{ steps.test-data.outputs.value }}
comparison: notEndsWith
`.trim(),
},
{
Expand All @@ -103,6 +129,19 @@ module.exports = buildConfig({
expected: est
actual: \${{ steps.test-data.outputs.value }}
comparison: endsWith
`.trim(),
},
{
title: 'Example usage w/ notContains assertion',
codeLanguage: 'yaml',
codeBlock: `
- id: test-data
run: echo "::set-output name=value::testing"
- uses: nick-invision@assert-action@v1
with:
expected: est
actual: \${{ steps.test-data.outputs.value }}
comparison: notEndsWith
`.trim(),
},
{
Expand Down
44 changes: 43 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ jobs:
expected: 123
actual: ${{ steps.test-data.outputs.value }}
comparison: notEqual
- name: Assert notStartsWith
uses: ./
with:
expected: est
actual: ${{ steps.test-data.outputs.value }}
comparison: notStartsWith
- name: Assert notEndsWith
uses: ./
with:
expected: test
actual: ${{ steps.test-data.outputs.value }}
comparison: notEndsWith
- name: Assert notContains
uses: ./
with:
expected: 123
actual: ${{ steps.test-data.outputs.value }}
comparison: notContains
# negative tests to verify when assertion fails error is thrown
- name: Assert expected (fails)
uses: ./
Expand Down Expand Up @@ -82,11 +100,35 @@ jobs:
expected: testing
actual: ${{ steps.test-data.outputs.value }}
comparison: notEqual
- name: Assert notContains
id: notcontains-fail
uses: ./
continue-on-error: true
with:
expected: est
actual: ${{ steps.test-data.outputs.value }}
comparison: notContains
- name: Assert notStartsWith
id: notstartswith-fail
uses: ./
continue-on-error: true
with:
expected: tes
actual: ${{ steps.test-data.outputs.value }}
comparison: notStartsWith
- name: Assert notEndsWith
id: notendswith-fail
uses: ./
continue-on-error: true
with:
expected: ing
actual: ${{ steps.test-data.outputs.value }}
comparison: notEndsWith
# verify all expected failures threw error
- name: Verify expected statuses from previous tests
uses: ./
with:
expected: ${{ steps.expected-fail.outcome == 'failure' && steps.startswith-fail.outcome == 'failure' && steps.endswith-fail.outcome == 'failure' && steps.contains-fail.outcome == 'failure' && steps.notequal-fail.outcome == 'failure' }}
expected: ${{ steps.expected-fail.outcome == 'failure' && steps.startswith-fail.outcome == 'failure' && steps.endswith-fail.outcome == 'failure' && steps.contains-fail.outcome == 'failure' && steps.notequal-fail.outcome == 'failure' && steps.notcontains-fail.outcome == 'failure' && steps.notstartswith-fail.outcome == 'failure' && steps.notendswith-fail.outcome == 'failure' }}
actual: true

# runs on push to master only
Expand Down
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Various assertions to aide in validating action outputs

### **`comparison`**

**Optional** Type of comparison to perform. Supports "exact" (default), "startsWith", "endsWith", "contains", "notEqual"
**Optional** Type of comparison to perform. Supports `exact` (default), `startsWith`, `endsWith`, `contains`, `notEqual`, `notStartsWith`, `notEndsWith`, `notContains`

---

Expand Down Expand Up @@ -65,6 +65,18 @@ Result of the comparison. Can be either passed or failed
comparison: startsWith
```
### Example usage w/ notStartsWith assertion
```yaml
- id: test-data
run: echo "::set-output name=value::testing"
- uses: nick-invision@assert-action@v1
with:
expected: abc
actual: ${{ steps.test-data.outputs.value }}
comparison: notStartsWith
```
### Example usage w/ endsWith assertion
```yaml
Expand All @@ -77,6 +89,18 @@ Result of the comparison. Can be either passed or failed
comparison: endsWith
```
### Example usage w/ notEndsWith assertion
```yaml
- id: test-data
run: echo "::set-output name=value::testing"
- uses: nick-invision@assert-action@v1
with:
expected: abc
actual: ${{ steps.test-data.outputs.value }}
comparison: notEndsWith
```
### Example usage w/ contains assertion
```yaml
Expand All @@ -89,6 +113,18 @@ Result of the comparison. Can be either passed or failed
comparison: endsWith
```
### Example usage w/ notContains assertion
```yaml
- id: test-data
run: echo "::set-output name=value::testing"
- uses: nick-invision@assert-action@v1
with:
expected: est
actual: ${{ steps.test-data.outputs.value }}
comparison: notEndsWith
```
### Example usage w/ notEqual assertion
```yaml
Expand Down
117 changes: 117 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,121 @@ describe('runAction', () => {
});
}
});

describe('notstartswith', () => {
const comparison = 'notStartsWith';
const actual = 'testing';

beforeEach(() => {
mockComparison(comparison);
mockActual(actual);
});

const passTests = ['est', 'ing', 'TESTING'];

const failTests = ['t', 'tes', 'testing'];

for (const test of passTests) {
it(`success when ${actual} ${comparison} ${test}`, async () => {
mockExpected(test);
await runAction();

expect(actions.setOutput).toHaveBeenCalledWith('result', 'passed');
});
}

for (const test of failTests) {
it(`fails when ${actual} does not ${comparison} ${test}`, async () => {
let thrownErr;
mockExpected(test);

try {
await runAction();
} catch (error) {
thrownErr = error.message;
}

expect(actions.setOutput).toHaveBeenCalledWith('result', 'failed');
expect(thrownErr).toContain('Expected', 'not equal');
});
}
});

describe('notendswith', () => {
const comparison = 'notEndsWith';
const actual = 'testing';

beforeEach(() => {
mockComparison(comparison);
mockActual(actual);
});

const failTests = ['ing', 'testing'];

const passTests = ['t', 'tes', 'TESTING'];

for (const test of passTests) {
it(`success when ${actual} ${comparison} ${test}`, async () => {
mockExpected(test);
await runAction();

expect(actions.setOutput).toHaveBeenCalledWith('result', 'passed');
});
}

for (const test of failTests) {
it(`fails when ${actual} does not ${comparison} ${test}`, async () => {
let thrownErr;
mockExpected(test);

try {
await runAction();
} catch (error) {
thrownErr = error.message;
}

expect(actions.setOutput).toHaveBeenCalledWith('result', 'failed');
expect(thrownErr).toContain('Expected', 'not equal');
});
}
});

describe('notcontains', () => {
const comparison = 'notContains';
const actual = 'testing';

beforeEach(() => {
mockComparison(comparison);
mockActual(actual);
});

const failTests = ['testing', 'tes', 'ing', 'est'];

const passTests = ['TESTING', 'abc', '123'];

for (const test of passTests) {
it(`success when ${actual} ${comparison} ${test}`, async () => {
mockExpected(test);
await runAction();

expect(actions.setOutput).toHaveBeenCalledWith('result', 'passed');
});
}

for (const test of failTests) {
it(`fails when ${actual} does not ${comparison} ${test}`, async () => {
let thrownErr;
mockExpected(test);

try {
await runAction();
} catch (error) {
thrownErr = error.message;
}

expect(actions.setOutput).toHaveBeenCalledWith('result', 'failed');
expect(thrownErr).toContain('Expected', 'not equal');
});
}
});
});
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ inputs:
description: Actual value
required: true
comparison:
description: Type of comparison to perform. Supports "exact" (default), "startsWith", "endsWith", "contains", "notEqual"
description: Type of comparison to perform. Supports `exact` (default), `startsWith`, `endsWith`, `contains`, `notEqual`, `notStartsWith`, `notEndsWith`, `notContains`
required: false
default: exact
outputs:
Expand Down
29 changes: 25 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,14 @@ const { getInput, setFailed, setOutput } = __webpack_require__(470);
const DEFAULT_COMPARISON = 'exact';
const SUPPORTED_COMPARISONS = [
DEFAULT_COMPARISON,
'notexact',
'notequal',
'startsWith',
'notstartsWith',
'endsWith',
'notendsWith',
'contains',
'notEqual',
'notcontains',
];

function throwAssertError(expected, actual, comparison) {
Expand Down Expand Up @@ -151,28 +155,45 @@ async function runAction() {

switch (comparison.toLowerCase()) {
case 'exact':
case 'equal':
if (actual !== expected) {
throwAssertError(expected, actual, 'equal');
}
break;
case 'notequal':
case 'notexact':
if (actual === expected) {
throwAssertError(expected, actual, 'not equal');
}
break;
case 'startswith':
if (!actual.startsWith(expected)) {
throwAssertError(expected, actual, 'start with');
}
break;
case 'notstartswith':
if (actual.startsWith(expected)) {
throwAssertError(expected, actual, 'not start with');
}
break;
case 'endswith':
if (!actual.endsWith(expected)) {
throwAssertError(expected, actual, 'end with');
}
break;
case 'notendswith':
if (actual.endsWith(expected)) {
throwAssertError(expected, actual, 'not end with');
}
break;
case 'contains':
if (!actual.includes(expected)) {
throwAssertError(expected, actual, 'contain');
}
break;
case 'notequal':
if (actual === expected) {
throwAssertError(expected, actual, 'not equal');
case 'notcontains':
if (actual.includes(expected)) {
throwAssertError(expected, actual, 'not contain');
}
break;
default:
Expand Down
Loading

0 comments on commit 8f6ce90

Please sign in to comment.