Skip to content

Commit 9103da6

Browse files
feat!: stop throwing error when directories do not exist (#6556)
* feat: stop throwing error when directories do not exist * chore: re-add eslint rule * chore: update test * chore: fix assertion
1 parent d4eb578 commit 9103da6

File tree

4 files changed

+66
-28
lines changed

4 files changed

+66
-28
lines changed

packages/zip-it-and-ship-it/src/utils/fs.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ export const safeUnlink = async (path: string) => {
5454

5555
// Takes a list of absolute paths and returns an array containing all the
5656
// filenames within those directories, if at least one of the directories
57-
// exists. If not, an error is thrown.
57+
// exists.
5858
export const listFunctionsDirectories = async function (srcFolders: string[]) {
5959
const filenamesByDirectory = await Promise.allSettled(
6060
srcFolders.map((srcFolder) => listFunctionsDirectory(srcFolder)),
6161
)
62-
const errorMessages: string[] = []
6362
const validDirectories = filenamesByDirectory
6463
.map((result) => {
6564
if (result.status === 'rejected') {
@@ -77,11 +76,6 @@ export const listFunctionsDirectories = async function (srcFolders: string[]) {
7776
})
7877
.filter(nonNullable)
7978

80-
if (validDirectories.length === 0) {
81-
throw new Error(`Functions folders do not exist: ${srcFolders.join(', ')}
82-
${errorMessages.join('\n')}`)
83-
}
84-
8579
return validDirectories.flat()
8680
}
8781

packages/zip-it-and-ship-it/tests/bin.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ describe('CLI', () => {
3535

3636
test('Error execution', async () => {
3737
const tmpDir = await tmpName({ prefix: 'zip-it-bin-test' })
38-
const { exitCode, stderr } = await exec(['doesNotExist', join(tmpDir, 'destFolder')], { reject: false })
38+
const { stdout } = await exec(['doesNotExist', join(tmpDir, 'destFolder')], { reject: false })
39+
const zipped = JSON.parse(stdout)
3940

40-
expect(exitCode).toBe(1)
41-
expect(stderr).not.toBe('')
41+
expect(zipped).toHaveLength(0)
4242
})
4343

4444
test('Should throw on missing srcFolder', async () => {

packages/zip-it-and-ship-it/tests/list_functions_files.test.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,13 @@ describe('listFunctionsFiles', () => {
4040
},
4141
)
4242

43-
test('listFunctionsFiles throws if all function directories do not exist', async () => {
44-
await expect(
45-
async () =>
46-
await listFunctionsFiles([
47-
join(FIXTURES_DIR, 'missing-functions-folder', 'functions'),
48-
join(FIXTURES_DIR, 'missing-functions-folder', 'functions2'),
49-
]),
50-
).rejects.toThrow(/Functions folders do not exist: /)
51-
})
43+
test('listFunctionsFiles returns an empty array if none of the function directories exist', async () => {
44+
const files = await listFunctionsFiles([
45+
join(FIXTURES_DIR, 'missing-functions-folder', 'functions'),
46+
join(FIXTURES_DIR, 'missing-functions-folder', 'functions2'),
47+
])
5248

53-
test('listFunctionsFiles does not hide errors that have nothing todo with folder existents', async () => {
54-
// @ts-expect-error test
55-
await expect(() => listFunctionsFiles([true])).rejects.toThrow(
56-
expect.not.stringContaining('Functions folders do not exist:'),
57-
)
49+
expect(files).toStrictEqual([])
5850
})
5951

6052
test('listFunctionsFiles includes in-source config declarations', async () => {

packages/zip-it-and-ship-it/tests/main.test.ts

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,6 @@ describe('zip-it-and-ship-it', () => {
708708
})
709709
})
710710

711-
testMany('Throws when the source folder does not exist', [...allBundleConfigs, 'bundler_none'], async (options) => {
712-
await expect(zipNode('does-not-exist', { opts: options })).rejects.toThrowError(/Functions folders do not exist/)
713-
})
714-
715711
testMany(
716712
'Works even if destination folder does not exist',
717713
[...allBundleConfigs, 'bundler_none'],
@@ -3059,3 +3055,59 @@ test('Supports functions inside the plugins modules path', async () => {
30593055

30603056
await tmpDir.cleanup()
30613057
})
3058+
3059+
test('Supports individual functions even when none of the given function directories exist', async () => {
3060+
const tmpDir = await getTmpDir({
3061+
// Cleanup the folder even if there are still files in them
3062+
unsafeCleanup: true,
3063+
})
3064+
const basePath = join(FIXTURES_ESM_DIR, 'v2-api-files-and-directories')
3065+
const files = await zipFunctions(
3066+
{
3067+
generated: {
3068+
directories: [join(basePath, 'does-not-exist/functions')],
3069+
functions: [
3070+
join(basePath, 'cat.jpg'),
3071+
join(basePath, 'func2.mjs'),
3072+
join(basePath, 'func3'),
3073+
join(basePath, 'func4'),
3074+
],
3075+
},
3076+
user: {
3077+
directories: [join(basePath, 'does-not-exist-either/functions')],
3078+
},
3079+
},
3080+
tmpDir.path,
3081+
{
3082+
basePath,
3083+
},
3084+
)
3085+
3086+
expect(files.length).toBe(3)
3087+
3088+
const unzippedFunctions = await unzipFiles(files)
3089+
const functions = getFunctionResultsByName(unzippedFunctions)
3090+
3091+
const func2 = await importFunctionFile(`${tmpDir.path}/${functions.func2.name}/${functions.func2.entryFilename}`)
3092+
const func2Result = await invokeLambda(func2)
3093+
expect(func2Result.statusCode).toBe(200)
3094+
expect(await readAsBuffer(func2Result.body)).toStrictEqual(
3095+
JSON.stringify({ func: 2, mod3: 'module-3', mod4: 'module-4' }),
3096+
)
3097+
3098+
const func3 = await importFunctionFile(`${tmpDir.path}/${functions.func3.name}/${functions.func3.entryFilename}`)
3099+
const func3Result = await invokeLambda(func3)
3100+
expect(func3Result.statusCode).toBe(200)
3101+
expect(await readAsBuffer(func3Result.body)).toStrictEqual(
3102+
JSON.stringify({ func: 3, mod3: 'module-3', mod4: 'module-4' }),
3103+
)
3104+
3105+
const func4 = await importFunctionFile(`${tmpDir.path}/${functions.func4.name}/${functions.func4.entryFilename}`)
3106+
const func4Result = await invokeLambda(func4)
3107+
expect(func4Result.statusCode).toBe(200)
3108+
expect(await readAsBuffer(func4Result.body)).toStrictEqual(
3109+
JSON.stringify({ func: 4, mod3: 'module-3', mod4: 'module-4' }),
3110+
)
3111+
3112+
await tmpDir.cleanup()
3113+
})

0 commit comments

Comments
 (0)