Skip to content

Commit

Permalink
fix for require('fs') in compositions
Browse files Browse the repository at this point in the history
  • Loading branch information
starpit committed Apr 27, 2018
1 parent 1eb1971 commit 9190027
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 4 deletions.
20 changes: 17 additions & 3 deletions app/plugins/modules/composer/lib/create-from-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ exports.compileToFSM = (src, opts={}) => new Promise((resolve, reject) => {
require: m => {
if (m === '@ibm-functions/composer') {
return openwhiskComposer
} else if (m.charAt(0) !== '.') {
return require(m)
} else {
return require(path.resolve(dir, m))

return require(path.resolve(m))
}
}
}
Expand All @@ -134,7 +135,20 @@ exports.compileToFSM = (src, opts={}) => new Promise((resolve, reject) => {
}
const sandboxWithComposer = Object.assign(sandbox, { composer: openwhiskComposer })

let res = vm.runInNewContext(code, sandboxWithComposer)
// we need to be in the directory of the
// source, in case it does relative requires
// or reads from that relative location
const curdir = process.cwd()
process.chdir(ui.findFile(dir))

let res
try {
res = vm.runInNewContext(code, sandboxWithComposer)
} finally {
// make sure we change back to where we started
process.chdir(curdir)
}

debug('res', typeof res, res)

if (typeof res === 'function') {
Expand Down
3 changes: 3 additions & 0 deletions tests/data/composer-source/author-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
1 change: 1 addition & 0 deletions tests/data/composer-source/echo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = n => `echo${n}`
8 changes: 8 additions & 0 deletions tests/data/composer-source/fs-read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function foo() {
const fs = require('fs');
const authorMap = JSON.parse(fs.readFileSync('author-map.json', 'utf8'));
return composer.let({ am: authorMap }, p => {
return am[p.author] == undefined ? {} : am[p.author]
})
}
composer.sequence(foo())
2 changes: 2 additions & 0 deletions tests/data/composer-source/require-absolute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('fs')
composer.sequence('echo1','echo2')
2 changes: 2 additions & 0 deletions tests/data/composer-source/require-relative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.sequence(require('./echo')(1),
require('./echo')(2))
3 changes: 2 additions & 1 deletion tests/tests/passes/07/composer-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ describe('app create and sessions', function() {
fs.readdirSync(srcDir).forEach((file,idx) => {
const name = `${seqName2}-${idx}`

if (file.endsWith('.js')) {
// echo.js is used by require-relative.js, it isn't a composition on its own
if (file.endsWith('.js') && file !== 'echo.js') {
it(`should create a composer sequence from source ${file}`, () => cli.do(`app create ${name} ${path.join(srcDir, file)}`, this.app)
.then(cli.expectOK)
.then(sidecar.expectOpen)
Expand Down
24 changes: 24 additions & 0 deletions tests/tests/passes/07/composer-viz.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const fsm = input('fsm.json'), fsmStruct = JSON.parse(fs.readFileSync(fsm.path).
demo = composerInput('demo.js'),
demoRetain = composerInput('demo-retain.js'),
mask = composerInput('mask.js'),
requireAbsolute = composerInput('require-absolute.js'),
requireRelative = composerInput('require-relative.js'),
fsRead = composerInput('fs-read.js'),
addSubscription = composerErrorInput('addSubscription.js')

/**
Expand Down Expand Up @@ -237,6 +240,27 @@ describe('show the composer visualization without creating openwhisk assets', fu
.then(verifyEdgeExists('echo1', 'echo2'))
.catch(common.oops(this)))

/** test: from the openwhisk-composer/samples directory */
it(`show visualization from javascript source ${requireAbsolute.path}`, () => cli.do(`app viz ${requireAbsolute.path}`, this.app)
.then(verifyTheBasicStuff(requireAbsolute.file, 'composerLib'))
.then(verifyNodeExists('echo1'))
.then(verifyNodeExists('echo2'))
.then(verifyEdgeExists('echo1', 'echo2'))
.catch(common.oops(this)))

/** test: from the openwhisk-composer/samples directory */
it(`show visualization from javascript source ${requireRelative.path}`, () => cli.do(`app viz ${requireRelative.path}`, this.app)
.then(verifyTheBasicStuff(requireRelative.file, 'composerLib'))
.then(verifyNodeExists('echo1'))
.then(verifyNodeExists('echo2'))
.then(verifyEdgeExists('echo1', 'echo2'))
.catch(common.oops(this)))

/** test: from the openwhisk-composer/samples directory */
it(`show visualization from javascript source ${fsRead.path}`, () => cli.do(`app viz ${fsRead.path}`, this.app)
.then(verifyTheBasicStuff(fsRead.file, 'composerLib'))
.catch(common.oops(this)))

it(`fail to show visualization for addSubscription without -e for env var assignment`, () => cli.do(`preview ${addSubscription.path}`, this.app)
.then(cli.expectError(0, 'SLACK_TOKEN required in environment'))
.catch(common.oops(this)))
Expand Down

0 comments on commit 9190027

Please sign in to comment.