Skip to content

Commit

Permalink
t2 run/push: check both module's found path and our own pre-compiled …
Browse files Browse the repository at this point in the history
…module known path. Fixes gh-990 (#991)

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Sep 20, 2016
1 parent 87ee3a6 commit 1079bf3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 35 deletions.
36 changes: 26 additions & 10 deletions lib/tessel/deployment/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ exportables.postRun = function(tessel, options) {
return Promise.resolve();
};

function logMissingBinaryModuleWarning(name) {
exportables.logMissingBinaryModuleWarning = function(name) {
var warning = tags.stripIndent `
Pre-compiled module is missing: ${name}.
This might be caused by any of the following:
Expand All @@ -94,7 +94,7 @@ function logMissingBinaryModuleWarning(name) {
`;

log.warn(warning.trim());
}
};

exportables.resolveBinaryModules = function(opts) {
var cwd = process.cwd();
Expand Down Expand Up @@ -346,6 +346,7 @@ exportables.injectBinaryModules = function(globRoot, tempBundlePath, options) {
// For every binary module in use...
binaryModulesUsed.forEach(details => {
if (details.resolved) {
var isCopied = false;
var translations = binaryPathTranslations.slice().concat(
lists.binaryPathTranslations[details.name] || []
);
Expand All @@ -362,18 +363,33 @@ exportables.injectBinaryModules = function(globRoot, tempBundlePath, options) {
return accum.replace(translation.find, translation.replace);
}, tempTargetBinaryPath);

fs.copySync(sourceBinaryPath, tempTargetBinaryPath);
try {
fs.copySync(sourceBinaryPath, tempTargetBinaryPath);
isCopied = true;
} catch (error) {
sourceBinaryPath = path.join(details.extractPath, details.buildType, details.binName);

// Also ensure that package.json was copied.
fs.copySync(
path.join(globRoot, details.modulePath, 'package.json'),
path.join(tempTargetModulePath, 'package.json')
);
try {
fs.copySync(sourceBinaryPath, tempTargetBinaryPath);
isCopied = true;
} catch (error) {
exportables.logMissingBinaryModuleWarning(details.name);
log.error(error);
}
}

if (isCopied) {
// Also ensure that package.json was copied.
fs.copySync(
path.join(globRoot, details.modulePath, 'package.json'),
path.join(tempTargetModulePath, 'package.json')
);
}
} else {
// In the future we may allow users to log the ignored modules here
if (!details.ignored) {
logMissingBinaryModuleWarning(details.name);
exportables.logMissingBinaryModuleWarning(details.name);
}
// In the future we may allow users to log the ignored modules here
}
});

Expand Down
97 changes: 72 additions & 25 deletions test/unit/deployment/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2659,37 +2659,84 @@ exports['deployment.js.injectBinaryModules'] = {
});
},

tryTheirPathAndOurPath: function(test) {
test.expect(3);

throwError: function(test) {
test.expect(1);
this.copySync.restore();
this.copySync = sandbox.stub(fs, 'copySync', () => {
// Fail the first try/catch on THEIR PATH
if (this.copySync.callCount === 1) {
throw new Error('ENOENT: no such file or directory');
}
});

var errorMessage = 'Test Error';
this.copySync.onCall(0).throws(errorMessage);
this.forEach = sandbox.stub(Map.prototype, 'forEach', (handler) => {
handler({
binName: 'node_sqlite3.node',
// This path doesn't match our precompiler's output paths.
// Will result in:
// ERR! Error: ENOENT: no such file or directory, stat '~/.tessel/binaries/sqlite3-3.1.4-Release/node-v46-something-else/node_sqlite3.node'
buildPath: path.normalize('/lib/binding/node-v46-something-else/'),
buildType: 'Release',
globPath: path.normalize('node_modules/sqlite3/lib/binding/node-v46-something-else/node_sqlite3.node'),
ignored: false,
name: 'sqlite3',
modulePath: path.normalize('node_modules/sqlite3'),
resolved: true,
version: '3.1.4',
extractPath: path.normalize('~/.tessel/binaries/sqlite3-3.1.4-Release'),
});
});

this.globSync.restore();
this.globSync = sandbox.stub(glob, 'sync', () => {
return [
path.normalize('node_modules/release/build/Release/release.node'),
];
deployment.js.injectBinaryModules(this.globRoot, fsTemp.mkdirSync(), {}).then(() => {
// 2 calls: 1 call for each try/catch fs.copySync
// 1 call: copy the package.json
test.equal(fs.copySync.callCount, 3);
// THEIR PATH
test.equal(this.copySync.getCall(0).args[0].endsWith(path.normalize('node-v46-something-else/node_sqlite3.node')), true);
// OUR PATH
test.equal(this.copySync.getCall(1).args[0].endsWith(path.normalize('Release/node_sqlite3.node')), true);
test.done();
});
},

deployment.js.resolveBinaryModules({
target: this.target,
tessel: {
versions: {
modules: 46
},
},
}).then(() => {
deployment.js.injectBinaryModules(this.globRoot, fsTemp.mkdirSync(), {}).then(() => {
test.fail('Should not pass');
test.done();
}).catch(error => {
test.equal(error, errorMessage);
test.done();
tryCatchTwiceAndFailGracefullyWithMissingBinaryMessage: function(test) {
test.expect(4);

this.copySync.restore();
this.copySync = sandbox.stub(fs, 'copySync', () => {
throw new Error('E_THIS_IS_NOT_REAL');
});

this.forEach = sandbox.stub(Map.prototype, 'forEach', (handler) => {
handler({
binName: 'not-a-thing.node',
// This path doesn't match our precompiler's output paths.
// Will result in:
// ERR! Error: ENOENT: no such file or directory, stat '~/.tessel/binaries/not-a-thing-3.1.4-Release/node-v46-something-else/not-a-thing.node'
buildPath: path.normalize('/lib/binding/node-v46-something-else/'),
buildType: 'Release',
globPath: path.normalize('node_modules/not-a-thing/lib/binding/node-v46-something-else/not-a-thing.node'),
ignored: false,
name: 'not-a-thing',
modulePath: path.normalize('node_modules/not-a-thing'),
resolved: true,
version: '3.1.4',
extractPath: path.normalize('~/.tessel/binaries/not-a-thing-3.1.4-Release'),
});
}).catch(error => {
test.ok(false, error.toString());
});

this.error = sandbox.stub(log, 'error');
this.logMissingBinaryModuleWarning = sandbox.stub(deployment.js, 'logMissingBinaryModuleWarning');

deployment.js.injectBinaryModules(this.globRoot, fsTemp.mkdirSync(), {}).then(() => {
// 2 calls: 1 call for each try/catch fs.copySync
test.equal(this.copySync.callCount, 2);

// Result of failing both attempts to copy
test.equal(this.logMissingBinaryModuleWarning.callCount, 1);
test.equal(this.error.callCount, 1);
test.equal(String(this.error.lastCall.args[0]).includes('E_THIS_IS_NOT_REAL'), true);
test.done();
});
}
Expand Down

0 comments on commit 1079bf3

Please sign in to comment.