add support for multiple paths in path input variable

This allows users to provide multiple filepaths to include in their action, whether they're files or folders.
This commit is contained in:
Conor Sloan
2023-11-22 16:02:01 +00:00
parent 56b7ee3ceb
commit c2bb735a45
8 changed files with 3644 additions and 1571 deletions
+44
View File
@@ -170,3 +170,47 @@ describe('removeDir', () => {
expect(fs.existsSync(dir)).toEqual(false)
})
})
describe('bundleFilesintoDirectory', () => {
let sourceDir: string
let targetDir: string
beforeEach(() => {
sourceDir = fsHelper.createTempDir()
targetDir = fsHelper.createTempDir()
})
afterEach(() => {
fs.rmSync(sourceDir, { recursive: true })
fs.rmSync(targetDir, { recursive: true })
})
it('bundles files and folders into a directory', () => {
// Create some test files and folders in the sourceDir
const file1 = `${sourceDir}/file1.txt`
const folder1 = `${sourceDir}/folder1`
const file2 = `${folder1}/file3.txt`
fs.mkdirSync(folder1)
fs.writeFileSync(file1, fileContent)
fs.writeFileSync(file2, fileContent)
// Bundle the files and folders into the targetDir
fsHelper.bundleFilesintoDirectory([file1, folder1], targetDir)
// Check that the files and folders were copied
expect(fs.existsSync(file1)).toEqual(true)
expect(fsHelper.readFileContents(file1).toString()).toEqual(fileContent)
expect(fs.existsSync(`${targetDir}/folder1`)).toEqual(true)
expect(fs.existsSync(file2)).toEqual(true)
expect(fsHelper.readFileContents(file2).toString()).toEqual(fileContent)
})
it('throws an error if a file or directory does not exist', () => {
expect(() => {
fsHelper.bundleFilesintoDirectory(['/does/not/exist'], targetDir)
}).toThrow('File /does/not/exist does not exist')
})
})
+28 -12
View File
@@ -23,6 +23,7 @@ let createTempDirMock: jest.SpyInstance
let isDirectoryMock: jest.SpyInstance
let createArchivesMock: jest.SpyInstance
let removeDirMock: jest.SpyInstance
let bundleFilesintoDirectoryMock: jest.SpyInstance
// Mock the GHCR Client
let publishOCIArtifactMock: jest.SpyInstance
@@ -45,6 +46,9 @@ describe('action', () => {
.spyOn(fsHelper, 'createArchives')
.mockImplementation()
removeDirMock = jest.spyOn(fsHelper, 'removeDir').mockImplementation()
bundleFilesintoDirectoryMock = jest
.spyOn(fsHelper, 'bundleFilesintoDirectory')
.mockImplementation()
// GHCR Client mocks
publishOCIArtifactMock = jest
@@ -97,7 +101,7 @@ describe('action', () => {
)
})
it('fails if path is not a directory', async () => {
it('fails if multiple paths are provided and staging files fails', async () => {
// Mock the environment
process.env.GITHUB_REPOSITORY = 'test/test'
github.context.eventName = 'release'
@@ -109,23 +113,24 @@ describe('action', () => {
}
getInputMock.mockImplementation((name: string) => {
if (name === 'path') {
return 'not-a-directory'
return 'directory1 directory2'
} else if (name === 'registry') {
return 'https://ghcr.io'
}
return ''
})
isDirectoryMock.mockImplementation(() => false)
isDirectoryMock.mockImplementation(() => true)
bundleFilesintoDirectoryMock.mockImplementation(() => {
throw new Error('Something went wrong')
})
// Run the action
await main.run()
// Check the results
expect(isDirectoryMock).toHaveBeenCalledWith('not-a-directory')
expect(setFailedMock).toHaveBeenCalledWith(
'The path not-a-directory is not a directory. Please provide a path to a valid directory.'
)
expect(setFailedMock).toHaveBeenCalledWith('Something went wrong')
})
it('fails if an error is thrown from dependent code', async () => {
@@ -167,16 +172,20 @@ describe('action', () => {
})
it('successfully uploads if the release tag is a semver without v prefix', async () => {
await testHappyPath('1.0.0')
await testHappyPath('1.0.0', 'test')
})
it('successfully uploads if the release tag is a semver with v prefix', async () => {
await testHappyPath('v1.0.0')
await testHappyPath('v1.0.0', 'test')
})
it('successfully uploads if multiple paths are provided', async () => {
await testHappyPath('v1.0.0', 'test test2')
})
})
// Test that main successfully uploads and returns the manifest & package URL
async function testHappyPath(version: string): Promise<void> {
async function testHappyPath(version: string, path: string): Promise<void> {
// Mock the environment
process.env.GITHUB_REPOSITORY = 'test/test'
github.context.eventName = 'release'
@@ -188,7 +197,7 @@ async function testHappyPath(version: string): Promise<void> {
}
getInputMock.mockImplementation((name: string) => {
if (name === 'path') {
return 'test'
return path
} else if (name === 'registry') {
return 'https://ghcr.io'
}
@@ -197,6 +206,10 @@ async function testHappyPath(version: string): Promise<void> {
isDirectoryMock.mockImplementation(() => true)
bundleFilesintoDirectoryMock.mockImplementation(() => {
return '/tmp/test'
})
createTempDirMock.mockImplementation(() => '/tmp/test')
createArchivesMock.mockImplementation(() => {
@@ -246,6 +259,9 @@ async function testHappyPath(version: string): Promise<void> {
'actions_oci_pkg'
)
// Expect the files to be cleaned up
// Expect all the temp files to be cleaned up
expect(removeDirMock).toHaveBeenCalledWith('/tmp/test')
expect(removeDirMock).toHaveBeenCalledTimes(
createTempDirMock.mock.calls.length
)
}