Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add route matcher to tests #377

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion node/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { env } from 'process'

import { test, expect, vi } from 'vitest'

import { getRouteMatcher } from '../test/util.js'

import { BundleFormat } from './bundle.js'
import { Cache, FunctionConfig } from './config.js'
import { Declaration } from './declaration.js'
Expand Down Expand Up @@ -73,24 +75,32 @@ test('Generates a manifest with excluded paths and patterns', () => {
const functions = [
{ name: 'func-1', path: '/path/to/func-1.ts' },
{ name: 'func-2', path: '/path/to/func-2.ts' },
{ name: 'func-3', path: '/path/to/func-3.ts' },
]
const declarations: Declaration[] = [
{ function: 'func-1', path: '/f1/*', excludedPath: '/f1/exclude' },
{ function: 'func-2', pattern: '^/f2/.*/?$', excludedPattern: '^/f2/exclude$' },
{ function: 'func-3', path: '/*', excludedPath: '/**/*.html' },
]
const manifest = generateManifest({ bundles: [], declarations, functions })

const expectedRoutes = [
{ function: 'func-1', pattern: '^/f1/.*/?$' },
{ function: 'func-2', pattern: '^/f2/.*/?$' },
{ function: 'func-3', pattern: '^/.*/?$' },
]

expect(manifest.routes).toEqual(expectedRoutes)
expect(manifest.function_config).toEqual({
'func-1': { excluded_patterns: ['^/f1/exclude/?$'] },
'func-2': { excluded_patterns: ['^/f2/exclude$'] },
'func-3': { excluded_patterns: ['^/.*/.*\\.html/?$'] },
})
expect(manifest.bundler_version).toBe(env.npm_package_version as string)

const matcher = getRouteMatcher(manifest)

expect(matcher('/f1/hello')?.function).toBe('func-1')
expect(matcher('/grandparent/parent/child/grandchild.html')?.function).toBeUndefined()
})

test('TOML-defined paths can be combined with ISC-defined excluded paths', () => {
Expand Down
2 changes: 1 addition & 1 deletion node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,4 @@ const writeManifest = async ({ distDirectory, ...rest }: WriteManifestOptions) =
return manifest
}

export { generateManifest, Manifest, writeManifest }
export { generateManifest, Manifest, Route, writeManifest }
17 changes: 16 additions & 1 deletion test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { execa } from 'execa'
import tmp from 'tmp-promise'

import { getLogger } from '../node/logger.js'
import type { Manifest } from '../node/manifest.js'

const testLogger = getLogger(() => {
// no-op
Expand Down Expand Up @@ -43,6 +44,20 @@ const inspectFunction = (path: string) => `
console.log(JSON.stringify(responses));
`

const getRouteMatcher = (manifest: Manifest) => (candidate: string) =>
manifest.routes.find((route) => {
const regex = new RegExp(route.pattern)

if (!regex.test(candidate)) {
return false
}

const excludedPattern = manifest.function_config[route.function].excluded_patterns
const isExcluded = excludedPattern.some((pattern) => new RegExp(pattern).test(candidate))

return !isExcluded
})

const runESZIP = async (eszipPath: string) => {
const tmpDir = await tmp.dir({ unsafeCleanup: true })

Expand Down Expand Up @@ -86,4 +101,4 @@ const runESZIP = async (eszipPath: string) => {
return JSON.parse(result.stdout)
}

export { fixturesDir, testLogger, runESZIP, useFixture }
export { fixturesDir, getRouteMatcher, testLogger, runESZIP, useFixture }