Skip to content

Commit

Permalink
Add support for additional Ruby Lambda handler filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanblock committed Aug 14, 2023
1 parent 2086104 commit a81d98c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
5 changes: 3 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

### Added

- Added support for additional Python Lambda handler filenames, including `lambda.py`, `handler.py`, and `__main__.py`
- Architect's legacy `index.py` root filename remains
- Added support for additional Python + Ruby Lambda handler filenames, including:
- Python: `lambda.py`, `handler.py` (and legacy `index.py`)
- Ruby: `lambda.rb`, `handler.rb` (and legacy `index.rb`)

---

Expand Down
8 changes: 6 additions & 2 deletions src/config/pragmas/populate-lambda/get-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ let nodeHandlers = [ 'index.js', 'index.mjs', 'index.cjs' ]
let denoHandlers = [ 'mod.ts', 'mod.js' ]
// TODO: these are all prob going away
.concat([ 'mod.tsx', 'index.ts', 'index.js', 'index.tsx' ])
let snekHandlers = [ 'lambda.py', 'handler.py', '__main__.py', 'index.py' ]
let rubyHandlers = [ 'lambda.rb', 'handler.rb', 'index.rb' ]
let snekHandlers = [ 'lambda.py', 'handler.py', 'index.py' ]

function getExt ({ runtime, src, errors }) {
try {
Expand Down Expand Up @@ -76,7 +77,10 @@ function getExt ({ runtime, src, errors }) {
let { file = 'lambda', ext = 'py' } = findHandler(snekHandlers, src)
return { file, ext }
}
if (runtime.startsWith('ruby')) return { ext: 'rb' }
if (runtime.startsWith('ruby')) {
let { file = 'lambda', ext = 'rb' } = findHandler(rubyHandlers, src)
return { file, ext }
}
if (runtime.startsWith('deno')) {
let { file = 'mod', ext = 'ts' } = findHandler(denoHandlers, src)
return { file, ext }
Expand Down
47 changes: 37 additions & 10 deletions test/unit/src/config/pragmas/populate-lambda/get-handler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ test('Set up env', t => {
})

test('Handler properties (built-in runtimes)', t => {
t.plan(32)
let config, errors, pythonHandler, result
t.plan(38)
let config, errors, pythonHandler, rubyHandler, result

// Defaults to Node.js
config = defaultFunctionConfig()
Expand Down Expand Up @@ -82,9 +82,10 @@ test('Handler properties (built-in runtimes)', t => {
t.equal(result.handlerMethod, handler, `Got correct handlerMethod: ${result.handlerMethod}`)
mockFs.restore()

// Old school Architect default
config = defaultFunctionConfig()
errors = []
pythonHandler = '__main__.py'
pythonHandler = 'index.py'
mockFs(fakeFile(pythonHandler))
config.runtime = 'python3.8'
result = getHandler({ config, src, errors })
Expand All @@ -93,26 +94,52 @@ test('Handler properties (built-in runtimes)', t => {
t.equal(result.handlerMethod, handler, `Got correct handlerMethod: ${result.handlerMethod}`)
mockFs.restore()

// Old school Architect default
// Ruby
config = defaultFunctionConfig()
errors = []
pythonHandler = 'index.py'
mockFs(fakeFile(pythonHandler))
config.runtime = 'python3.8'
rubyHandler = 'lambda.rb'
config.runtime = 'ruby2.7'
result = getHandler({ config, src, errors })
t.notOk(errors.length, 'Did not get handler errors')
t.equal(result.handlerFile, srcPath(pythonHandler), `Got correct handlerFile: ${result.handlerFile}`)
t.equal(result.handlerFile, srcPath(rubyHandler), `Got correct handlerFile: ${result.handlerFile}`)
t.equal(result.handlerMethod, handler, `Got correct handlerMethod: ${result.handlerMethod}`)

// Verify priority of the updated default handler name
config = defaultFunctionConfig()
errors = []
mockFs({ [src]: {
[rubyHandler]: 'hi',
'index.rb': 'hi',
} })
config.runtime = 'ruby2.7'
result = getHandler({ config, src, errors })
t.notOk(errors.length, 'Did not get handler errors')
t.equal(result.handlerFile, srcPath(rubyHandler), `Got correct handlerFile: ${result.handlerFile}`)
t.equal(result.handlerMethod, handler, `Got correct handlerMethod: ${result.handlerMethod}`)
mockFs.restore()

// Ruby
config = defaultFunctionConfig()
errors = []
rubyHandler = 'handler.rb'
mockFs(fakeFile(rubyHandler))
config.runtime = 'ruby2.7'
result = getHandler({ config, src, errors })
t.notOk(errors.length, 'Did not get handler errors')
t.equal(result.handlerFile, srcPath(`${file}.rb`), `Got correct handlerFile: ${result.handlerFile}`)
t.equal(result.handlerFile, srcPath(rubyHandler), `Got correct handlerFile: ${result.handlerFile}`)
t.equal(result.handlerMethod, handler, `Got correct handlerMethod: ${result.handlerMethod}`)
mockFs.restore()

// Old school Architect default
config = defaultFunctionConfig()
errors = []
rubyHandler = 'index.rb'
mockFs(fakeFile(rubyHandler))
config.runtime = 'ruby2.7'
result = getHandler({ config, src, errors })
t.notOk(errors.length, 'Did not get handler errors')
t.equal(result.handlerFile, srcPath(rubyHandler), `Got correct handlerFile: ${result.handlerFile}`)
t.equal(result.handlerMethod, handler, `Got correct handlerMethod: ${result.handlerMethod}`)
mockFs.restore()

// Deno
config = defaultFunctionConfig()
Expand Down

0 comments on commit a81d98c

Please sign in to comment.