Skip to content

Commit fcb9d1a

Browse files
authored
Merge pull request #72 from GrantBirki/feat/non-existing-exclude-file
Feature: Allow non-existing exclude file
2 parents 5074bb1 + bdfd2f8 commit fcb9d1a

File tree

7 files changed

+77
-5
lines changed

7 files changed

+77
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Here is a quick example of how to install this action in any workflow:
5050
| `yaml_exclude_regex` | `false` | `""` | A regex to exclude files from validation (e.g. `".*\.schema\.yaml$"` to exclude all files ending with `.schema.yaml`) - Default is `""` which doesn't exclude any files |
5151
| `yaml_as_json` | `false` | `"false"` | Whether or not to treat and validate YAML files as JSON files - `"true"` or `"false"` - Default is `"false"`. If this is true, the JSON schema will be used to validate YAML files. Any YAML schemas will be ignored. For this context, a YAML file is any file which matches the yaml_extension or yaml_extension_short inputs. See the [docs](docs/yaml_as_json.md) for more details |
5252
| `exclude_file` | `false` | `""` | The full path to a file in the repository where this Action is running that contains a list of '.gitignore'-style patterns to exclude files from validation (e.g. ./exclude.txt) |
53+
| `exclude_file_required` | `true` | `"true"` | Whether or not the `exclude_file` must exist if it is used. If this is `true` and the `exclude_file` does not exist, the Action will fail. Set this to `"false"` if you do not care when the `exclude_file` exists or not |
5354
| `use_gitignore` | `true` | `"true"` | Whether or not to use the .gitignore file in the root of the repository to exclude files from validation - `"true"` or `"false"` - Default is `"true"` |
5455
| `git_ignore_path` | `false` | `".gitignore"` | The full path to the .gitignore file to use if use_gitignore is set to "true" (e.g. ./src/.gitignore) - Default is ".gitignore" which uses the .gitignore file in the root of the repository |
5556
| `allow_multiple_documents` | `false` | `"false"` | Whether or not to allow multiple documents in a single YAML file - `"true"` or `"false"` - Default is `"false"`. Useful for k8s documents. |

__tests__/functions/exclude.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ import * as core from '@actions/core'
22
import {Exclude} from '../../src/functions/exclude'
33

44
const warningMock = jest.spyOn(core, 'warning').mockImplementation(() => {})
5+
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation(() => {})
6+
const infoMock = jest.spyOn(core, 'info').mockImplementation(() => {})
7+
const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})
58

69
var exclude
710
beforeEach(() => {
811
jest.clearAllMocks()
912
jest.spyOn(core, 'debug').mockImplementation(() => {})
13+
jest.spyOn(core, 'setFailed').mockImplementation(() => {})
14+
jest.spyOn(core, 'info').mockImplementation(() => {})
1015
process.env.INPUT_EXCLUDE_FILE = '__tests__/fixtures/exclude/exclude.txt'
1116
process.env.INPUT_GIT_IGNORE_PATH = '.gitignore'
1217
process.env.INPUT_USE_GITIGNORE = 'true'
18+
process.env.INPUT_EXCLUDE_FILE_REQUIRED = 'true'
1319
exclude = new Exclude()
1420
})
1521

@@ -44,6 +50,40 @@ test('does not exclude any files when no exclude file is used', () => {
4450
expect(exclude.isExcluded('exclude-me.json')).toBe(false)
4551
})
4652

53+
test('raises an exception when no exclude file is found', () => {
54+
process.env.INPUT_EXCLUDE_FILE = 'oh_no_i_dont_exist.txt'
55+
process.env.INPUT_EXCLUDE_FILE_REQUIRED = 'true'
56+
57+
expect(() => {
58+
new Exclude()
59+
}).toThrow(
60+
`Error: ENOENT: no such file or directory, open 'oh_no_i_dont_exist.txt'`
61+
)
62+
expect(setFailedMock).toHaveBeenCalledWith(
63+
`error reading exclude_file: oh_no_i_dont_exist.txt`
64+
)
65+
})
66+
67+
test('does not raise an exception when no exclude file is found and it is not required', () => {
68+
process.env.INPUT_EXCLUDE_FILE = 'oh_no_i_dont_exist.txt'
69+
process.env.INPUT_EXCLUDE_FILE_REQUIRED = 'false'
70+
71+
const exclude = new Exclude()
72+
expect(exclude.isExcluded('exclude-me.json')).toBe(false)
73+
expect(infoMock).toHaveBeenCalledWith(
74+
`exclude_file was not found, but it is not required - OK`
75+
)
76+
})
77+
78+
test('does not raise an exception when no exclude file is found and it is required', () => {
79+
process.env.INPUT_EXCLUDE_FILE = ''
80+
process.env.INPUT_EXCLUDE_FILE_REQUIRED = 'true'
81+
82+
const exclude = new Exclude()
83+
expect(exclude.isExcluded('exclude-me.json')).toBe(false)
84+
expect(debugMock).toHaveBeenCalledWith(`exclude_file was not provided - OK`)
85+
})
86+
4787
test('excludes a file that is not tracked by git', () => {
4888
process.env.INPUT_EXCLUDE_FILE = ''
4989
const exclude = new Exclude()

__tests__/main.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import * as core from '@actions/core'
12
import {run} from '../src/main'
23
import * as jsonValidator from '../src/functions/json-validator'
34
import * as yamlValidator from '../src/functions/yaml-validator'
45
import * as processResults from '../src/functions/process-results'
56

67
beforeEach(() => {
78
jest.clearAllMocks()
9+
jest.spyOn(core, 'debug').mockImplementation(() => {})
810
jest.spyOn(jsonValidator, 'jsonValidator').mockImplementation(() => {
911
return {success: true, failed: 0, passed: 8, skipped: 0, violations: []}
1012
})
@@ -16,6 +18,7 @@ beforeEach(() => {
1618
})
1719

1820
process.env.INPUT_USE_GITIGNORE = 'false'
21+
process.env.INPUT_EXCLUDE_FILE_REQUIRED = 'true'
1922
})
2023

2124
test('successfully runs the action', async () => {

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ inputs:
7373
description: The full path to a file in the repository where this Action is running that contains a list of '.gitignore'-style patterns to exclude files from validation (e.g. ./exclude.txt)
7474
required: false
7575
default: ""
76+
exclude_file_required:
77+
description: Whether or not the exclude_file must exist if it is used. If this is true and the exclude_file does not exist, the Action will fail - "true" or "false" - Default is "true"
78+
required: false
79+
default: "true"
7680
use_gitignore:
7781
description: Whether or not to use the .gitignore file in the root of the repository to exclude files from validation - "true" or "false" - Default is "true"
7882
required: true

dist/index.js

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions/exclude.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ignore from 'ignore'
55
export class Exclude {
66
constructor() {
77
this.path = core.getInput('exclude_file')
8+
this.required = core.getBooleanInput('exclude_file_required')
89
this.gitTrackedOnly = core.getBooleanInput('use_gitignore')
910

1011
// initialize the exclude array
@@ -13,8 +14,19 @@ export class Exclude {
1314
// read the exclude file if it was used
1415
if (this.path && this.path !== '') {
1516
core.debug(`loading exclude_file: ${this.path}`)
16-
this.ignore.add(readFileSync(this.path, 'utf8').toString())
17-
core.debug(`loaded custom exclude patterns`)
17+
try {
18+
this.ignore.add(readFileSync(this.path, 'utf8').toString())
19+
core.debug(`loaded custom exclude patterns`)
20+
} catch (error) {
21+
if (this.required === true) {
22+
core.setFailed(`error reading exclude_file: ${this.path}`)
23+
throw new Error(error)
24+
}
25+
26+
core.info(`exclude_file was not found, but it is not required - OK`)
27+
}
28+
} else {
29+
core.debug(`exclude_file was not provided - OK`)
1830
}
1931

2032
// if gitTrackOnly is true, add the git exclude patterns from the .gitignore file if it exists

0 commit comments

Comments
 (0)