Skip to content

Commit c355820

Browse files
committed
use the ignore npm package
1 parent b1e9ae9 commit c355820

File tree

3 files changed

+14
-119
lines changed

3 files changed

+14
-119
lines changed

__tests__/fixtures/exclude/exclude.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ dog.txt
1717
!dog.txt
1818

1919
# test with a regex pattern match
20-
/^.*cars.*\.txt$/
20+
*cars*.txt

__tests__/functions/exclude.test.js

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as core from '@actions/core'
22
import {Exclude} from '../../src/functions/exclude'
33

4-
const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})
54
const warningMock = jest.spyOn(core, 'warning').mockImplementation(() => {})
65

76
var exclude
87
beforeEach(() => {
98
jest.clearAllMocks()
9+
jest.spyOn(core, 'debug').mockImplementation(() => {})
1010
process.env.INPUT_EXCLUDE_FILE = '__tests__/fixtures/exclude/exclude.txt'
1111
process.env.INPUT_GIT_IGNORE_PATH = '.gitignore'
1212
process.env.INPUT_USE_GITIGNORE = 'true'
@@ -15,60 +15,28 @@ beforeEach(() => {
1515

1616
test('successfully excludes a file', () => {
1717
expect(exclude.isExcluded('exclude-me.json')).toBe(true)
18-
expect(debugMock).toHaveBeenCalledWith(
19-
`file exactly matches exclude pattern: exclude-me.json`
20-
)
2118
})
2219

2320
test('successfully excludes a with a glob match', () => {
2421
expect(exclude.isExcluded('src/dev/app/nope.exclude')).toBe(true)
25-
expect(debugMock).toHaveBeenCalledWith(
26-
`file matches exclude glob pattern: *.exclude`
27-
)
28-
})
29-
30-
test('successfully does not exclude a negate pattern match', () => {
31-
expect(exclude.isExcluded('cat.txt')).toBe(false)
32-
expect(debugMock).toHaveBeenCalledWith(
33-
`file matches exclude negation pattern: !cat.txt`
34-
)
35-
})
36-
37-
test('successfully excludes a file where the negate pattern matches after', () => {
38-
expect(exclude.isExcluded('dog.txt')).toBe(true)
39-
expect(debugMock).toHaveBeenCalledWith(
40-
`file exactly matches exclude pattern: dog.txt`
41-
)
4222
})
4323

4424
test('successfully excludes with a regex pattern match', () => {
4525
expect(exclude.isExcluded('src/app/cars-and-a-bus.txt')).toBe(true)
46-
expect(debugMock).toHaveBeenCalledWith(
47-
`file matches exclude regex pattern: /^.*cars.*\\.txt$/`
48-
)
4926
})
5027

5128
test('successfully excludes a file in a dir one level down', () => {
52-
expect(exclude.isExcluded('./evil-base-dir/exclude-me.json')).toBe(true)
53-
expect(debugMock).toHaveBeenCalledWith(
54-
`file is in exclude directory: evil-base-dir/`
55-
)
29+
expect(exclude.isExcluded('evil-base-dir/exclude-me.json')).toBe(true)
5630
})
5731

5832
test('successfully excludes a file in a dir two levels down', () => {
59-
expect(exclude.isExcluded('./evil-base-dir/sub-dir/exclude-me.json')).toBe(
33+
expect(exclude.isExcluded('evil-base-dir/sub-dir/exclude-me.json')).toBe(
6034
true
6135
)
62-
expect(debugMock).toHaveBeenCalledWith(
63-
`file is in exclude directory: evil-base-dir/`
64-
)
6536
})
6637

6738
test('successfully checks a file and finds that it is not excluded', () => {
6839
expect(exclude.isExcluded('exclude-me-nope.json')).toBe(false)
69-
expect(debugMock).toHaveBeenCalledWith(
70-
`file 'exclude-me-nope.json' did not match any exclude patterns`
71-
)
7240
})
7341

7442
test('does not exclude any files when no exclude file is used', () => {
@@ -82,7 +50,6 @@ test('excludes a file that is not tracked by git', () => {
8250
process.env.INPUT_EXCLUDE_FILE = ''
8351
const exclude = new Exclude()
8452
expect(exclude.isExcluded('tmp/test.json')).toBe(true)
85-
expect(debugMock).toHaveBeenCalledWith(`file is in exclude directory: tmp/`)
8653
})
8754

8855
test('fails to read the .gitignore file', () => {

src/functions/exclude.js

Lines changed: 10 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,33 @@
11
import * as core from '@actions/core'
22
import {readFileSync} from 'fs'
3+
import ignore from 'ignore'
34

45
export class Exclude {
56
constructor() {
67
this.path = core.getInput('exclude_file')
78
this.gitTrackedOnly = core.getBooleanInput('use_gitignore')
89

910
// initialize the exclude array
10-
this.exclude = []
11+
this.ignore = ignore()
1112

1213
// read the exclude file if it was used
1314
if (this.path && this.path !== '') {
1415
core.debug(`loading exclude_file: ${this.path}`)
15-
16-
this.exclude = readFileSync(this.path, 'utf8')
17-
// split the exclude file into an array of strings and trim each string
18-
this.exclude = this.exclude.split('\n').map(item => item.trim())
19-
// remove any empty strings
20-
this.exclude = this.exclude.filter(item => item !== '')
21-
// remove any comments
22-
this.exclude = this.exclude.filter(item => !item.startsWith('#'))
23-
24-
core.debug(`loaded exclude patterns: ${this.exclude}`)
16+
this.ignore.add(readFileSync(this.path, 'utf8').toString())
17+
core.debug(`loaded custom exclude patterns`)
2518
}
2619

2720
// if gitTrackOnly is true, add the git exclude patterns from the .gitignore file if it exists
2821
if (this.gitTrackedOnly) {
2922
core.debug(
30-
`use_gitignore: ${this.gitTrackedOnly} - only using git tracked files`
23+
`use_gitignore: ${this.gitTrackedOnly}`
3124
)
3225

3326
const gitIgnorePath = core.getInput('git_ignore_path')
34-
var gitIgnoreExclude = []
35-
try {
36-
const gitIgnore = readFileSync(gitIgnorePath, 'utf8')
37-
// split the git ignore file into an array of strings and trim each string
38-
const gitIgnorePatterns = gitIgnore.split('\n').map(item => item.trim())
39-
// remove any empty strings
40-
gitIgnoreExclude = gitIgnorePatterns.filter(item => item !== '')
41-
// remove any comments
42-
gitIgnoreExclude = gitIgnoreExclude.filter(
43-
item => !item.startsWith('#')
44-
)
27+
core.debug(`loading .gitignore file from path: ${gitIgnorePath}`)
4528

46-
// add the git ignore patterns to the exclude patterns
47-
this.exclude = this.exclude.concat(gitIgnoreExclude)
29+
try {
30+
this.ignore.add(readFileSync(gitIgnorePath).toString())
4831
} catch (error) {
4932
core.warning(`error reading .gitignore file: ${error}`)
5033
}
@@ -54,62 +37,7 @@ export class Exclude {
5437
isExcluded(file) {
5538
// use .gitignore style matching
5639
// https://git-scm.com/docs/gitignore
57-
58-
if (this.exclude.length === 0) {
59-
return false
60-
}
61-
62-
// remove the leading ./ if it exists
63-
if (file.startsWith('./')) {
64-
file = file.replace('./', '')
65-
}
66-
67-
// loop through each exclude pattern
68-
for (const pattern of this.exclude) {
69-
// if the file exactly matches the pattern, return true
70-
if (file === pattern) {
71-
core.debug(`file exactly matches exclude pattern: ${pattern}`)
72-
return true
73-
}
74-
75-
// if the pattern is a negation, check if the file matches the negation
76-
if (pattern.startsWith('!')) {
77-
const regex = new RegExp(pattern.replace(/^!/, ''))
78-
if (file.match(regex)) {
79-
core.debug(`file matches exclude negation pattern: ${pattern}`)
80-
return false
81-
}
82-
}
83-
84-
// if the pattern is a directory, check if the file is in that directory
85-
if (pattern.endsWith('/')) {
86-
if (file.startsWith(pattern)) {
87-
core.debug(`file is in exclude directory: ${pattern}`)
88-
return true
89-
}
90-
}
91-
92-
// if the pattern is a glob, check if the file matches the glob
93-
if (pattern.includes('*')) {
94-
const regex = new RegExp(pattern.replace(/\*/g, '.*'))
95-
if (file.match(regex)) {
96-
core.debug(`file matches exclude glob pattern: ${pattern}`)
97-
return true
98-
}
99-
}
100-
101-
// if the pattern is a regex, check if the file matches the regex
102-
if (pattern.startsWith('/') && pattern.endsWith('/')) {
103-
const regex = new RegExp(pattern.replace(/\//g, ''))
104-
if (file.match(regex)) {
105-
core.debug(`file matches exclude regex pattern: ${pattern}`)
106-
return true
107-
}
108-
}
109-
}
110-
111-
// if the file did not match any exclude patterns, return false
112-
core.debug(`file '${file}' did not match any exclude patterns`)
113-
return false
40+
// https://github.com/kaelzhang/node-ignore
41+
return this.ignore.ignores(file)
11442
}
11543
}

0 commit comments

Comments
 (0)