Commit 26f3b189 by Michael Mifsud Committed by GitHub

Merge pull request #1792 from sass/feat/binding-loading

Stop messing with require.cache in tests
parents b15f0cd7 97cd61a9
/*!
* 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)'
);
});
});
});
});
var assert = require('assert'),
extensionsPath = process.env.NODESASS_COV
? require.resolve('../lib-cov/extensions')
: require.resolve('../lib/extensions');
sass = process.env.NODESASS_COV
? require('../lib-cov/extensions')
: require('../lib/extensions');
describe('runtime parameters', function() {
var packagePath = require.resolve('../package'),
var pkg = require('../package'),
// Let's use JSON to fake a deep copy
savedArgv = JSON.stringify(process.argv),
savedEnv = JSON.stringify(process.env);
beforeEach(function() {
require(packagePath);
delete require.cache[extensionsPath];
});
afterEach(function() {
delete require.cache[packagePath];
delete require.cache[extensionsPath];
process.argv = JSON.parse(savedArgv);
process.env = JSON.parse(savedEnv);
require(packagePath);
delete pkg.nodeSassConfig;
});
describe('configuration precedence should be respected', function() {
......@@ -29,24 +22,21 @@ describe('runtime parameters', function() {
process.argv.push('--sass-binary-name', 'aaa');
process.env.SASS_BINARY_NAME = 'bbb';
process.env.npm_config_sass_binary_name = 'ccc';
require.cache[packagePath].exports.nodeSassConfig = { binaryName: 'ddd' };
pkg.nodeSassConfig = { binaryName: 'ddd' };
});
it('command line argument', function() {
var sass = require(extensionsPath);
assert.equal(sass.getBinaryName(), 'aaa_binding.node');
});
it('environment variable', function() {
process.argv = [];
var sass = require(extensionsPath);
assert.equal(sass.getBinaryName(), 'bbb_binding.node');
});
it('npm config variable', function() {
process.argv = [];
process.env.SASS_BINARY_NAME = null;
var sass = require(extensionsPath);
assert.equal(sass.getBinaryName(), 'ccc_binding.node');
});
......@@ -54,7 +44,6 @@ describe('runtime parameters', function() {
process.argv = [];
process.env.SASS_BINARY_NAME = null;
process.env.npm_config_sass_binary_name = null;
var sass = require(extensionsPath);
assert.equal(sass.getBinaryName(), 'ddd_binding.node');
});
});
......@@ -64,18 +53,16 @@ describe('runtime parameters', function() {
process.argv.push('--sass-binary-site', 'http://aaa.example.com:9999');
process.env.SASS_BINARY_SITE = 'http://bbb.example.com:8888';
process.env.npm_config_sass_binary_site = 'http://ccc.example.com:7777';
require.cache[packagePath].exports.nodeSassConfig = { binarySite: 'http://ddd.example.com:6666' };
pkg.nodeSassConfig = { binarySite: 'http://ddd.example.com:6666' };
});
it('command line argument', function() {
var sass = require(extensionsPath);
var URL = 'http://aaa.example.com:9999';
assert.equal(sass.getBinaryUrl().substr(0, URL.length), URL);
});
it('environment variable', function() {
process.argv = [];
var sass = require(extensionsPath);
var URL = 'http://bbb.example.com:8888';
assert.equal(sass.getBinaryUrl().substr(0, URL.length), URL);
});
......@@ -83,7 +70,6 @@ describe('runtime parameters', function() {
it('npm config variable', function() {
process.argv = [];
process.env.SASS_BINARY_SITE = null;
var sass = require(extensionsPath);
var URL = 'http://ccc.example.com:7777';
assert.equal(sass.getBinaryUrl().substr(0, URL.length), URL);
});
......@@ -92,7 +78,6 @@ describe('runtime parameters', function() {
process.argv = [];
process.env.SASS_BINARY_SITE = null;
process.env.npm_config_sass_binary_site = null;
var sass = require(extensionsPath);
var URL = 'http://ddd.example.com:6666';
assert.equal(sass.getBinaryUrl().substr(0, URL.length), URL);
});
......@@ -103,24 +88,21 @@ describe('runtime parameters', function() {
process.argv.push('--sass-binary-path', 'aaa_binding.node');
process.env.SASS_BINARY_PATH = 'bbb_binding.node';
process.env.npm_config_sass_binary_path = 'ccc_binding.node';
require.cache[packagePath].exports.nodeSassConfig = { binaryPath: 'ddd_binding.node' };
pkg.nodeSassConfig = { binaryPath: 'ddd_binding.node' };
});
it('command line argument', function() {
var sass = require(extensionsPath);
assert.equal(sass.getBinaryPath(), 'aaa_binding.node');
});
it('environment variable', function() {
process.argv = [];
var sass = require(extensionsPath);
assert.equal(sass.getBinaryPath(), 'bbb_binding.node');
});
it('npm config variable', function() {
process.argv = [];
process.env.SASS_BINARY_PATH = null;
var sass = require(extensionsPath);
assert.equal(sass.getBinaryPath(), 'ccc_binding.node');
});
......@@ -128,7 +110,6 @@ describe('runtime parameters', function() {
process.argv = [];
process.env.SASS_BINARY_PATH = null;
process.env.npm_config_sass_binary_path = null;
var sass = require(extensionsPath);
assert.equal(sass.getBinaryPath(), 'ddd_binding.node');
});
});
......
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