Skip to content

Commit

Permalink
Exclude hidden files by default backport
Browse files Browse the repository at this point in the history
  • Loading branch information
SrRyan authored Aug 22, 2024
1 parent a8a3f3a commit afc7e4a
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 69 deletions.
51 changes: 51 additions & 0 deletions __tests__/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ const lonelyFilePath = path.join(
'lonely-file.txt'
)

const hiddenFile = path.join(root, '.hidden-file.txt')
const fileInHiddenFolderPath = path.join(
root,
'.hidden-folder',
'folder-in-hidden-folder',
'file.txt'
)
const fileInHiddenFolderInFolderA = path.join(
root,
'folder-a',
'.hidden-folder-in-folder-a',
'file.txt'
)

describe('Search', () => {
beforeAll(async () => {
// mock all output so that there is less noise when running tests
Expand Down Expand Up @@ -92,6 +106,13 @@ describe('Search', () => {
await fs.mkdir(path.join(root, 'folder-h', 'folder-j', 'folder-k'), {
recursive: true
})
await fs.mkdir(
path.join(root, '.hidden-folder', 'folder-in-hidden-folder'),
{recursive: true}
)
await fs.mkdir(path.join(root, 'folder-a', '.hidden-folder-in-folder-a'), {
recursive: true
})

await fs.writeFile(searchItem1Path, 'search item1 file')
await fs.writeFile(searchItem2Path, 'search item2 file')
Expand All @@ -110,10 +131,19 @@ describe('Search', () => {
await fs.writeFile(amazingFileInFolderHPath, 'amazing file')

await fs.writeFile(lonelyFilePath, 'all by itself')

await fs.writeFile(hiddenFile, 'hidden file')
await fs.writeFile(fileInHiddenFolderPath, 'file in hidden directory')
await fs.writeFile(fileInHiddenFolderInFolderA, 'file in hidden directory')
/*
Directory structure of files that get created:
root/
.hidden-folder/
folder-in-hidden-folder/
file.txt
folder-a/
.hidden-folder-in-folder-a/
file.txt
folder-b/
folder-c/
search-item1.txt
Expand All @@ -136,6 +166,7 @@ describe('Search', () => {
folder-j/
folder-k/
lonely-file.txt
.hidden-file.txt
search-item5.txt
*/
})
Expand Down Expand Up @@ -352,4 +383,24 @@ describe('Search', () => {
)
expect(searchResult.filesToUpload.includes(lonelyFilePath)).toEqual(true)
})

it('Hidden files ignored by default', async () => {
const searchPath = path.join(root, '**/*')
const searchResult = await findFilesToUpload(searchPath)

expect(searchResult.filesToUpload).not.toContain(hiddenFile)
expect(searchResult.filesToUpload).not.toContain(fileInHiddenFolderPath)
expect(searchResult.filesToUpload).not.toContain(
fileInHiddenFolderInFolderA
)
})

it('Hidden files included', async () => {
const searchPath = path.join(root, '**/*')
const searchResult = await findFilesToUpload(searchPath, true)

expect(searchResult.filesToUpload).toContain(hiddenFile)
expect(searchResult.filesToUpload).toContain(fileInHiddenFolderPath)
expect(searchResult.filesToUpload).toContain(fileInHiddenFolderInFolderA)
})
})
155 changes: 102 additions & 53 deletions dist/index.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"@actions/artifact": "^1.1.2",
"@actions/core": "^1.10.0",
"@actions/glob": "^0.3.0",
"@actions/glob": "^0.5.0",
"@actions/io": "^1.1.2"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export enum Inputs {
Name = 'name',
Path = 'path',
IfNoFilesFound = 'if-no-files-found',
RetentionDays = 'retention-days'
RetentionDays = 'retention-days',
IncludeHiddenFiles = 'include-hidden-files'
}

export enum NoFileOptions {
Expand Down
4 changes: 3 additions & 1 deletion src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function getInputs(): UploadInputs {

const ifNoFilesFound = core.getInput(Inputs.IfNoFilesFound)
const noFileBehavior: NoFileOptions = NoFileOptions[ifNoFilesFound]
const includeHiddenFiles = core.getBooleanInput(Inputs.IncludeHiddenFiles)

if (!noFileBehavior) {
core.setFailed(
Expand All @@ -25,7 +26,8 @@ export function getInputs(): UploadInputs {
const inputs = {
artifactName: name,
searchPath: path,
ifNoFilesFound: noFileBehavior
ifNoFilesFound: noFileBehavior,
includeHiddenFiles
} as UploadInputs

const retentionDaysStr = core.getInput(Inputs.RetentionDays)
Expand Down
9 changes: 5 additions & 4 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ export interface SearchResult {
rootDirectory: string
}

function getDefaultGlobOptions(): glob.GlobOptions {
function getDefaultGlobOptions(includeHiddenFiles: boolean): glob.GlobOptions {
return {
followSymbolicLinks: true,
implicitDescendants: true,
omitBrokenSymbolicLinks: true
omitBrokenSymbolicLinks: true,
excludeHiddenFiles: !includeHiddenFiles
}
}

Expand Down Expand Up @@ -80,12 +81,12 @@ function getMultiPathLCA(searchPaths: string[]): string {

export async function findFilesToUpload(
searchPath: string,
globOptions?: glob.GlobOptions
includeHiddenFiles?: boolean
): Promise<SearchResult> {
const searchResults: string[] = []
const globber = await glob.create(
searchPath,
globOptions || getDefaultGlobOptions()
getDefaultGlobOptions(includeHiddenFiles || false)
)
const rawSearchResults: string[] = await globber.glob()

Expand Down
5 changes: 5 additions & 0 deletions src/upload-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ export interface UploadInputs {
* Duration after which artifact will expire in days
*/
retentionDays: number

/**
* Whether or not to include hidden files in the artifact
*/
includeHiddenFiles: boolean
}

0 comments on commit afc7e4a

Please sign in to comment.