Skip to content

Commit

Permalink
Update Lambda population unit tests to use mock-tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanblock committed Dec 5, 2023
1 parent 4796b8f commit 7626d86
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions test/unit/src/config/pragmas/populate-lambda/index-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let { join } = require('path')
let mockFs = require('mock-fs')
let mockTmp = require('mock-tmp')
let test = require('tape')
let cwd = process.cwd()
let _defaults = join(cwd, 'src', 'defaults')
Expand Down Expand Up @@ -275,11 +275,8 @@ test('Plugin population errors', t => {

test('Per-function AWS/ARC config (including custom handlers)', t => {
t.plan(13)
let arc, config, errors, inventory, lambdas, modified
inventory = defaultConfig()
inventory._project.cwd = '/nada'
inventory._project.src = '/nada/src'
let configPath = `${inventory._project.cwd}/src/events/configured-event/config.arc`
let arc, config, cwd, errors, inventory, lambdas, modified
let configPath = 'src/events/configured-event/config.arc'

// Node.js default
config = `@aws
Expand All @@ -289,7 +286,9 @@ memory 128
@arc
custom setting
`
mockFs({ [configPath]: config })

cwd = mockTmp({ [configPath]: config })
inventory = defaultConfig({ cwd })
inventory.events = [
'unconfigured-event',
'configured-event',
Expand All @@ -307,7 +306,7 @@ custom setting
}
t.deepEqual(lambdas[1].config, { ...inventory._project.defaultFunctionConfig, ...modified }, 'Config was correctly upserted')
t.notOk(errors.length, 'No errors returned')
mockFs.restore()
mockTmp.restore()

// Node.js custom configured handler
config = `@aws
Expand All @@ -318,7 +317,8 @@ handler lambda.handler
@arc
custom setting
`
mockFs({ [configPath]: config })
cwd = mockTmp({ [configPath]: config })
inventory = defaultConfig({ cwd })
errors = []
lambdas = populateLambda.events({ arc, inventory, errors })
t.deepEqual(lambdas[0].config, inventory._project.defaultFunctionConfig, 'Config was unmodified')
Expand All @@ -331,7 +331,7 @@ custom setting
}
t.deepEqual(lambdas[1].config, { ...inventory._project.defaultFunctionConfig, ...modified }, 'Config was correctly upserted')
t.notOk(errors.length, 'No errors returned')
mockFs.restore()
mockTmp.restore()

// Python
config = `@aws
Expand All @@ -342,7 +342,8 @@ runtime python3.8
@arc
custom setting
`
mockFs({ [configPath]: config })
cwd = mockTmp({ [configPath]: config })
inventory = defaultConfig({ cwd })
errors = []
lambdas = populateLambda.events({ arc, inventory, errors })
t.deepEqual(lambdas[0].config, inventory._project.defaultFunctionConfig, 'Config was unmodified')
Expand All @@ -355,7 +356,7 @@ custom setting
}
t.deepEqual(lambdas[1].config, { ...inventory._project.defaultFunctionConfig, ...modified }, 'Config was correctly upserted')
t.notOk(errors.length, 'No errors returned')
mockFs.restore()
mockTmp.restore()

// Ruby
config = `@aws
Expand All @@ -366,7 +367,8 @@ runtime ruby2.7
@arc
custom setting
`
mockFs({ [configPath]: config })
cwd = mockTmp({ [configPath]: config })
inventory = defaultConfig({ cwd })
errors = []
lambdas = populateLambda.events({ arc, inventory, errors })
t.deepEqual(lambdas[0].config, inventory._project.defaultFunctionConfig, 'Config was unmodified')
Expand All @@ -379,12 +381,13 @@ custom setting
}
t.deepEqual(lambdas[1].config, { ...inventory._project.defaultFunctionConfig, ...modified }, 'Config was correctly upserted')
t.notOk(errors.length, 'No errors returned')
mockFs.restore()
mockTmp.restore()

// Now return a Lambda config error
config = `lolidk`
mockFs({ [configPath]: config })
cwd = mockTmp({ [configPath]: config })
inventory = defaultConfig({ cwd })
lambdas = populateLambda.events({ arc, inventory, errors })
t.equal(errors.length, 1, `Invalid Lambda config returned error: ${errors[0]}`)
mockFs.restore()
mockTmp.restore()
})

0 comments on commit 7626d86

Please sign in to comment.