From 36842353b7ee4278127210e7230cb4afa99c08d8 Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Wed, 12 Apr 2023 11:43:17 +0200 Subject: [PATCH] test: add test for duplicate function precedence --- tests/unit/lib/functions/registry.test.mjs | 81 +++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/tests/unit/lib/functions/registry.test.mjs b/tests/unit/lib/functions/registry.test.mjs index 78b60167abe..820039201f1 100644 --- a/tests/unit/lib/functions/registry.test.mjs +++ b/tests/unit/lib/functions/registry.test.mjs @@ -1,8 +1,53 @@ -import { expect, test, vi } from 'vitest' +import { mkdir, mkdtemp, writeFile } from 'fs/promises' +import { tmpdir } from 'os' +import { join } from 'path' + +import { describe, expect, test, vi } from 'vitest' import { FunctionsRegistry } from '../../../../src/lib/functions/registry.mjs' import { watchDebounced } from '../../../../src/utils/command-helpers.mjs' +const duplicateFunctions = [ + { + filename: 'hello.js', + content: `exports.handler = async (event) => ({ statusCode: 200, body: JSON.stringify({ message: 'Hello World from .js' }) })`, + }, + { + filename: 'hello.ts', + content: `exports.handler = async (event) => ({ statusCode: 200, body: JSON.stringify({ message: 'Hello World from .ts' }) })`, + }, + { + filename: 'hello/main.go', + subDir: 'hello', + content: `package main + import ( + "fmt" + ) + + func main() { + fmt.Println("Hello, world from a go function!") + } + `, + }, + { + filename: 'hello2.ts', + content: `exports.handler = async (event) => ({ statusCode: 200, body: JSON.stringify({ message: 'Hello World from .ts' }) })`, + }, + { + filename: 'hello2/main.go', + subDir: 'hello2', + content: `package main + import ( + "fmt" + ) + + func main() { + fmt.Println("Hello, world from a go function!") + } + `, + }, +] + vi.mock('../../../../src/utils/command-helpers.mjs', async () => { const helpers = await vi.importActual('../../../../src/utils/command-helpers.mjs') @@ -35,6 +80,40 @@ test('registry should only pass functions config to zip-it-and-ship-it', async ( await prepareDirectoryScanStub.mockRestore() }) +describe('the registry handles duplicate functions based on extension precedence', () => { + test('where .js takes precedence over .go, and .go over .ts', async () => { + const projectRoot = await mkdtemp(join(tmpdir(), 'functions-extension-precedence')) + const functionsDirectory = join(projectRoot, 'functions') + await mkdir(functionsDirectory) + + duplicateFunctions.forEach(async (func) => { + if (func.subDir) { + const subDir = join(functionsDirectory, func.subDir) + await mkdir(subDir) + } + const file = join(functionsDirectory, func.filename) + await writeFile(file, func.content) + }) + const functionsRegistry = new FunctionsRegistry({ + projectRoot, + config: {}, + timeouts: { syncFunctions: 1, backgroundFunctions: 1 }, + settings: { port: 8888 }, + }) + const prepareDirectoryScanStub = vi.spyOn(FunctionsRegistry, 'prepareDirectoryScan').mockImplementation(() => {}) + const setupDirectoryWatcherStub = vi.spyOn(functionsRegistry, 'setupDirectoryWatcher').mockImplementation(() => {}) + + await functionsRegistry.scan([functionsDirectory]) + const { functions } = functionsRegistry + + expect(functions.get('hello').runtime.name).toBe('js') + expect(functions.get('hello2').runtime.name).toBe('go') + + await setupDirectoryWatcherStub.mockRestore() + await prepareDirectoryScanStub.mockRestore() + }) +}) + test('should add included_files to watcher', async () => { const registry = new FunctionsRegistry({}) const func = {