Commit d569bd9b by xzyfer

Moving binding loading a function

This removes the need to mess with `require.cache` in tests to test
the unsupported platform error message. I've run into issues a couple
times related to messing with the require cache.
parent b15f0cd7
/*!
* node-sass: lib/binding.js
*/
var errors = require('./errors');
/**
* Require binding
*/
module.exports = function(ext) {
if (!ext.hasBinary(ext.getBinaryPath())) {
if (!ext.isSupportedEnvironment()) {
throw new Error(errors.unsupportedEnvironment());
} else {
throw new Error(errors.missingBinary());
}
}
return require(ext.getBinaryPath());
};
......@@ -4,23 +4,14 @@
var path = require('path'),
clonedeep = require('lodash.clonedeep'),
errors = require('./errors'),
assign = require('lodash.assign'),
sass = require('./extensions');
if (!sass.hasBinary(sass.getBinaryPath())) {
if (!sass.isSupportedEnvironment()) {
throw new Error(errors.unsupportedEnvironment());
} else {
throw new Error(errors.missingBinary());
}
}
/**
* Require binding
*/
var binding = require(sass.getBinaryPath());
var binding = require('./binding')(sass);
/**
* Get input file
......
......@@ -1839,133 +1839,4 @@ describe('api', function() {
done();
});
});
describe('binding', function() {
beforeEach(function() {
delete require.cache[sassPath];
});
afterEach(function() {
delete require.cache[sassPath];
});
describe('missing error', function() {
it('should be useful', function() {
process.env.SASS_BINARY_NAME = 'Linux-x64-48';
assert.throws(
function() { require(sassPath); },
new RegExp('Missing binding.*?\\' + path.sep + 'vendor\\' + path.sep)
);
});
it('should list currently installed bindings', function() {
assert.throws(
function() { require(sassPath); },
function(err) {
var etx = require('../lib/extensions');
delete process.env.SASS_BINARY_NAME;
if ((err instanceof Error)) {
return err.message.indexOf(
etx.getHumanEnvironment(etx.getBinaryName())
) !== -1;
}
}
);
});
});
describe('on unsupported environment', function() {
describe('with an unsupported architecture', function() {
var prevValue;
beforeEach(function() {
prevValue = process.arch;
Object.defineProperty(process, 'arch', {
get: function () { return 'foo'; }
});
});
afterEach(function() {
process.arch = prevValue;
});
it('should error', function() {
assert.throws(
function() { require(sassPath); },
'Node Sass does not yet support your current environment'
);
});
it('should inform the user the architecture is unsupported', function() {
assert.throws(
function() { require(sassPath); },
'Unsupported architecture (foo)'
);
});
});
describe('with an unsupported platform', function() {
var prevValue;
beforeEach(function() {
prevValue = process.platform;
Object.defineProperty(process, 'platform', {
get: function () { return 'bar'; }
});
});
afterEach(function() {
process.platform = prevValue;
});
it('should error', function() {
assert.throws(
function() { require(sassPath); },
'Node Sass does not yet support your current environment'
);
});
it('should inform the user the platform is unsupported', function() {
assert.throws(
function() { require(sassPath); },
'Unsupported platform (bar)'
);
});
});
describe('with an unsupported runtime', function() {
var prevValue;
beforeEach(function() {
prevValue = process.versions.modules;
Object.defineProperty(process.versions, 'modules', {
get: function () { return 'baz'; }
});
});
afterEach(function() {
process.versions.modules = prevValue;
});
it('should error', function() {
assert.throws(
function() { require(sassPath); },
'Node Sass does not yet support your current environment'
);
});
it('should inform the user the runtime is unsupported', function() {
assert.throws(
function() { require(sassPath); },
'Unsupported runtime (baz)'
);
});
});
});
});
});
/*eslint new-cap: ["error", {"capIsNewExceptions": ["Color"]}]*/
var assert = require('assert'),
path = require('path'),
etx = require('../lib/extensions'),
binding = process.env.NODESASS_COV
? require('../lib-cov/binding')
: require('../lib/binding');
describe('binding', function() {
describe('missing error', function() {
it('should be useful', function() {
process.env.SASS_BINARY_NAME = 'Linux-x64-48';
assert.throws(
function() { binding(etx); },
function(err) {
var re = new RegExp('Missing binding.*?\\' + path.sep + 'vendor\\' + path.sep);
if ((err instanceof Error)) {
return re.test(err);
}
}
);
});
it('should list currently installed bindings', function() {
assert.throws(
function() { binding(etx); },
function(err) {
var etx = require('../lib/extensions');
delete process.env.SASS_BINARY_NAME;
if ((err instanceof Error)) {
return err.message.indexOf(
etx.getHumanEnvironment(etx.getBinaryName())
) !== -1;
}
}
);
});
});
describe('on unsupported environment', function() {
describe('with an unsupported architecture', function() {
var prevValue;
beforeEach(function() {
prevValue = process.arch;
Object.defineProperty(process, 'arch', {
get: function () { return 'foo'; }
});
});
afterEach(function() {
process.arch = prevValue;
});
it('should error', function() {
assert.throws(
function() { binding(etx); },
'Node Sass does not yet support your current environment'
);
});
it('should inform the user the architecture is unsupported', function() {
assert.throws(
function() { binding(etx); },
'Unsupported architecture (foo)'
);
});
});
describe('with an unsupported platform', function() {
var prevValue;
beforeEach(function() {
prevValue = process.platform;
Object.defineProperty(process, 'platform', {
get: function () { return 'bar'; }
});
});
afterEach(function() {
process.platform = prevValue;
});
it('should error', function() {
assert.throws(
function() { binding(etx); },
'Node Sass does not yet support your current environment'
);
});
it('should inform the user the platform is unsupported', function() {
assert.throws(
function() { binding(etx); },
'Unsupported platform (bar)'
);
});
});
describe('with an unsupported runtime', function() {
var prevValue;
beforeEach(function() {
prevValue = process.versions.modules;
Object.defineProperty(process.versions, 'modules', {
get: function () { return 'baz'; }
});
});
afterEach(function() {
process.versions.modules = prevValue;
});
it('should error', function() {
assert.throws(
function() { binding(etx); },
'Node Sass does not yet support your current environment'
);
});
it('should inform the user the runtime is unsupported', function() {
assert.throws(
function() { binding(etx); },
'Unsupported runtime (baz)'
);
});
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment