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

run/push: support "subargs" for forwarding to remote process. Fixes gh-732 #806

Merged
merged 1 commit into from
Jul 22, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ makeCommand('run')
options.push = false;
// Overridden in tarBundle if options.full is `true`
options.slim = true;
options.subargs = parser.subargs || [];

callControllerWith('deploy', options);
})
Expand Down Expand Up @@ -241,6 +242,7 @@ makeCommand('push')
options.push = true;
// Overridden in tarBundle if options.full is `true`
options.slim = true;
options.subargs = parser.subargs || [];

callControllerWith('deploy', options);
})
Expand Down Expand Up @@ -516,6 +518,42 @@ makeCommand('root')


module.exports = function(args) {
var sIndexOfSA = -1;
var eIndexOfSA = -1;

// Check to see if there are any subargs...
// It would've been nice to use subarg to parse this stuff,
// but in reality we don't actually want to parse these yet
// because there is no clear path to reassembling them as
// the string they will need to be when the remote process is invoked.
for (var i = 0; i < args.length; i++) {
var arg = args[i];

if (arg.startsWith('[') && sIndexOfSA === -1) {
// Remove the leading '[', replace existing arg at this position
args[i] = arg.slice(1, arg.length);
sIndexOfSA = i;
}

if (arg.endsWith(']') && sIndexOfSA !== -1) {
// Remove the trailing ']', replace existing arg at this position
args[i] = arg.slice(0, arg.length - 1);
eIndexOfSA = i;
}
}
Copy link
Contributor Author

@rwaldron rwaldron Jul 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: I deleted the line note because I'm adding the content as a comment in the source


// If there are, remove them from the `args`
// that get passed to parser.parse().
//
// If these are not removed, they will be
// treated like they are part of the t2-cli args
// themselves, which is undesirable.
if (sIndexOfSA !== -1 && eIndexOfSA !== -1) {
// Splice the subargs from the args that will be passed to nomnom,
// store on parser so we can get to them later.
parser.subargs = args.splice(sIndexOfSA, eIndexOfSA);
}

// Clear the spec from one call to the next. This is
// only necessary for testing the CLI (each call must be "fresh")
parser.specs = {};
Expand Down
6 changes: 3 additions & 3 deletions lib/tessel/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ module.exports.app = {
*
*/
module.exports.js = {
execute: (filepath, relpath) => ['node', filepath + relpath],
execute: (filepath, relpath, args) => ['node', filepath + relpath].concat(args),
};

module.exports.rs = {
execute: (filepath, relpath) => [filepath + relpath],
execute: (filepath, relpath, args) => [filepath + relpath].concat(args),
};

module.exports.py = {
execute: (filepath, relpath) => ['python', filepath + relpath],
execute: (filepath, relpath, args) => ['python', filepath + relpath].concat(args),
};

module.exports.readFile = function(filepath) {
Expand Down
4 changes: 3 additions & 1 deletion lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Tessel.prototype.deploy = function(opts) {

// Resolve the application's language/runtime
opts.lang = deployment.resolveLanguage(opts.lang || opts.entryPoint);
opts.subargs = opts.subargs || [];

return new Promise((resolve, reject) => {
// Stop running an existing applications
Expand Down Expand Up @@ -373,7 +374,7 @@ exportables.run = function(tessel, options) {

return preRun.then(() => {
tessel.connection.exec(
commands[lang.meta.extname].execute(Tessel.REMOTE_RUN_PATH, options.resolvedEntryPoint), {
commands[lang.meta.extname].execute(Tessel.REMOTE_RUN_PATH, options.resolvedEntryPoint, options.subargs), {
pty: true
}, (error, remoteProcess) => {
if (error) {
Expand Down Expand Up @@ -438,6 +439,7 @@ exportables.createShellScript = function(tessel, opts) {
});
});
});

remoteProcess.stdin.end(new Buffer(opts.lang.meta.shell(opts).trim()));
});
});
Expand Down
4 changes: 2 additions & 2 deletions lib/tessel/deployment/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ var exportables = {
program
};
},
shell: (opts) => {
shell: (options) => {
return tags.stripIndent `
#!/bin/sh
exec node /app/remote-script/${opts.resolvedEntryPoint}
exec node /app/remote-script/${options.resolvedEntryPoint} ${options.subargs.join(' ')}
`;
},
},
Expand Down
4 changes: 2 additions & 2 deletions lib/tessel/deployment/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ var exportables = {
program
};
},
shell: (opts) => {
shell: (options) => {
return tags.stripIndent `
#!/bin/sh
exec python /app/remote-script/${opts.resolvedEntryPoint}
exec python /app/remote-script/${options.resolvedEntryPoint} ${options.subargs.join(' ')}
`;
},
},
Expand Down
4 changes: 2 additions & 2 deletions lib/tessel/deployment/rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ var exportables = {
program
};
},
shell: (opts) => {
shell: (options) => {
return tags.stripIndent `
#!/bin/sh
exec /app/remote-script/${opts.resolvedEntryPoint}
exec /app/remote-script/${options.resolvedEntryPoint} ${options.subargs.join(' ')}
`;
},
},
Expand Down
12 changes: 9 additions & 3 deletions test/unit/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ exports['Tessel.prototype.deploy'] = {
test.expect(12);
deployTestCode(this.tessel, {
push: true,
single: false
single: false,
subargs: [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should pass without the subargs property as well, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because this test runs code that exists at a point in which all default properties are resolved, so the subargs property will always be present, either empty or with contents

}, (error) => {
if (error) {
test.ok(false, `deployTestCode failed: ${error.toString()}`);
Expand Down Expand Up @@ -538,8 +539,10 @@ exports['deploy.run'] = {
deploy.run(this.tessel, {
entryPoint: entryPoint,
lang: deployment.js,
subargs: ['--key=value'],
}).then(() => {
test.deepEqual(this.exec.lastCall.args[0], [deployment.js.meta.binary, Tessel.REMOTE_RUN_PATH + entryPoint]);
test.deepEqual(
this.exec.lastCall.args[0], [deployment.js.meta.binary, Tessel.REMOTE_RUN_PATH + entryPoint, '--key=value']);
test.done();
});
},
Expand Down Expand Up @@ -572,6 +575,7 @@ exports['deploy.run'] = {
deploy.run(this.tessel, {
entryPoint: entryPoint,
lang: deployment.rs,
subargs: [],
}).then(() => {
test.done();
});
Expand All @@ -591,6 +595,7 @@ exports['deploy.run'] = {
deploy.run(this.tessel, {
resolvedEntryPoint: 'foo',
lang: deployment.js,
subargs: [],
}).then(() => {
test.equal(this.postRun.callCount, 1);
test.done();
Expand Down Expand Up @@ -640,7 +645,8 @@ exports['deploy.createShellScript'] = {

var opts = {
lang: deployment.js,
resolvedEntryPoint: 'foo'
resolvedEntryPoint: 'foo',
subargs: ['--key=value'],
};

deploy.createShellScript(this.tessel, opts).then(() => {
Expand Down
53 changes: 51 additions & 2 deletions test/unit/deployment/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,39 @@ exports['Deployment: JavaScript'] = {
createShellScriptDefaultEntryPoint: function(test) {
test.expect(1);

var shellScript = tags.stripIndent `
#!/bin/sh
exec node /app/remote-script/index.js --key=value
`;
var opts = {
lang: deployment.js,
resolvedEntryPoint: 'index.js',
subargs: ['--key=value'],
};
this.end.restore();
this.end = sandbox.stub(this.tessel._rps.stdin, 'end', (buffer) => {
test.equal(buffer.toString(), shellScript);
test.done();
});

this.exec = sandbox.stub(this.tessel.connection, 'exec', (command, callback) => {
return callback(null, this.tessel._rps);
});

deploy.createShellScript(this.tessel, opts);
},

createShellScriptDefaultEntryPointNoSubargs: function(test) {
test.expect(1);

var shellScript = tags.stripIndent `
#!/bin/sh
exec node /app/remote-script/index.js
`;
var opts = {
lang: deployment.js,
resolvedEntryPoint: 'index.js',
subargs: [],
};
this.end.restore();
this.end = sandbox.stub(this.tessel._rps.stdin, 'end', (buffer) => {
Expand All @@ -120,11 +146,12 @@ exports['Deployment: JavaScript'] = {

var shellScript = tags.stripIndent `
#!/bin/sh
exec node /app/remote-script/index.js
exec node /app/remote-script/index.js --key=value
`;
var opts = {
lang: deployment.js,
resolvedEntryPoint: 'index.js',
subargs: ['--key=value'],
};
this.end.restore();
this.end = sandbox.stub(this.tessel._rps.stdin, 'end', (buffer) => {
Expand Down Expand Up @@ -2552,7 +2579,29 @@ exports['deploy.createShellScript'] = {

var opts = {
lang: deployment.js,
resolvedEntryPoint: 'foo'
resolvedEntryPoint: 'foo',
subargs: [],
};

deploy.createShellScript(this.tessel, opts).then(() => {
test.deepEqual(this.exec.firstCall.args[0], ['dd', 'of=/app/start']);
test.deepEqual(this.exec.lastCall.args[0], ['chmod', '+x', '/app/start']);
test.done();
});
},

remoteShellScriptPathIsNotPathNormalizedWithSubargs: function(test) {
test.expect(2);

this.exec = sandbox.stub(this.tessel.connection, 'exec', (command, callback) => {
callback(null, this.tessel._rps);
this.tessel._rps.emit('close');
});

var opts = {
lang: deployment.js,
resolvedEntryPoint: 'foo',
subargs: ['--key=value'],
};

deploy.createShellScript(this.tessel, opts).then(() => {
Expand Down