Skip to content

Commit

Permalink
Merge pull request #66 from GrantBirki/better-gitignore-support
Browse files Browse the repository at this point in the history
Better gitignore support
  • Loading branch information
GrantBirki authored May 20, 2024
2 parents d7814b9 + 65c3073 commit 3a3d883
Show file tree
Hide file tree
Showing 8 changed files with 689 additions and 210 deletions.
2 changes: 1 addition & 1 deletion __tests__/fixtures/exclude/exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ dog.txt
!dog.txt

# test with a regex pattern match
/^.*cars.*\.txt$/
*cars*.txt
41 changes: 3 additions & 38 deletions __tests__/functions/exclude.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as core from '@actions/core'
import {Exclude} from '../../src/functions/exclude'

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

var exclude
beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(core, 'debug').mockImplementation(() => {})
process.env.INPUT_EXCLUDE_FILE = '__tests__/fixtures/exclude/exclude.txt'
process.env.INPUT_GIT_IGNORE_PATH = '.gitignore'
process.env.INPUT_USE_GITIGNORE = 'true'
Expand All @@ -15,60 +15,26 @@ beforeEach(() => {

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

test('successfully excludes a with a glob match', () => {
expect(exclude.isExcluded('src/dev/app/nope.exclude')).toBe(true)
expect(debugMock).toHaveBeenCalledWith(
`file matches exclude glob pattern: *.exclude`
)
})

test('successfully does not exclude a negate pattern match', () => {
expect(exclude.isExcluded('cat.txt')).toBe(false)
expect(debugMock).toHaveBeenCalledWith(
`file matches exclude negation pattern: !cat.txt`
)
})

test('successfully excludes a file where the negate pattern matches after', () => {
expect(exclude.isExcluded('dog.txt')).toBe(true)
expect(debugMock).toHaveBeenCalledWith(
`file exactly matches exclude pattern: dog.txt`
)
})

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

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

test('successfully excludes a file in a dir two levels down', () => {
expect(exclude.isExcluded('./evil-base-dir/sub-dir/exclude-me.json')).toBe(
true
)
expect(debugMock).toHaveBeenCalledWith(
`file is in exclude directory: evil-base-dir/`
)
expect(exclude.isExcluded('evil-base-dir/sub-dir/exclude-me.json')).toBe(true)
})

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

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

test('fails to read the .gitignore file', () => {
Expand Down
Loading

0 comments on commit 3a3d883

Please sign in to comment.