Commit ef5c28fd by Marcin Cieslak

Raise an error when no string/file provided

Intended to fix https://github.com/sass/node-sass/issues/924
parent 1479af67
......@@ -179,8 +179,9 @@ function getLinefeed(options) {
function getOptions(options, cb) {
options = options || {};
options.sourceComments = options.sourceComments || false;
options.data = options.data || null;
if (options.hasOwnProperty('file')) {
options.file = getInputFile(options);
}
options.outFile = getOutputFile(options);
options.includePaths = (options.includePaths || []).join(path.delimiter);
options.precision = parseInt(options.precision) || 5;
......@@ -351,7 +352,13 @@ module.exports.render = function(options, cb) {
});
}
return options.data ? binding.render(options) : binding.renderFile(options);
if (options.data) {
binding.render(options);
} else if (options.file) {
binding.renderFile(options);
} else {
cb({status: 3, message: 'No input specified: provide a file name or a source string to process' });
}
};
/**
......@@ -398,7 +405,15 @@ module.exports.renderSync = function(options) {
});
}
var status = options.data ? binding.renderSync(options) : binding.renderFileSync(options);
var status;
if (options.data) {
status = binding.renderSync(options);
} else if (options.file) {
status = binding.renderFileSync(options);
} else {
throw new Error('No input specified: provide a file name or a source string to process');
}
var result = options.result;
if (status) {
......
......@@ -76,6 +76,22 @@ describe('api', function() {
});
});
it('should NOT compile empty data string', function(done) {
sass.render({
data: ''
}, function(error) {
assert.equal(error.message, 'No input specified: provide a file name or a source string to process');
done();
});
});
it('should NOT compile without parameters', function(done) {
sass.render({ }, function(error) {
assert.equal(error.message, 'No input specified: provide a file name or a source string to process');
done();
});
});
it('should compile sass to css using indented syntax', function(done) {
var src = read(fixture('indent/index.sass'), 'utf8');
var expected = read(fixture('indent/expected.css'), 'utf8').trim();
......@@ -1074,6 +1090,20 @@ describe('api', function() {
done();
});
it('should NOT compile empty data string', function(done) {
assert.throws(function() {
sass.renderSync({ data: '' });
}, /No input specified: provide a file name or a source string to process/ );
done();
});
it('should NOT compile without any input', function(done) {
assert.throws(function() {
sass.renderSync({});
}, /No input specified: provide a file name or a source string to process/);
done();
});
it('should throw error for bad input', function(done) {
assert.throws(function() {
sass.renderSync('somestring');
......
......@@ -12,6 +12,90 @@ describe('lowlevel', function() {
done();
});
it('data context with options.data not provided', function(done) {
var options = {
/* data: */
sourceComments: false,
file: null,
outFile: null,
includePaths: '',
precision: 5,
sourceMap: null,
style: 0,
indentWidth: 2,
indentType: 0,
linefeed: '\n',
result: { stats: {} } };
binding.renderSync(options);
assert(/Data context created without a source string/.test(options.result.error),
'Should fail with error message "Data context created without a source string"');
done();
});
it('data context with both options.data and options.file not provided', function(done) {
var options = {
/* data: */
sourceComments: false,
/* file: null, */
outFile: null,
includePaths: '',
precision: 5,
sourceMap: null,
style: 0,
indentWidth: 2,
indentType: 0,
linefeed: '\n',
result: { stats: {} } };
binding.renderSync(options);
assert(/Data context created without a source string/.test(options.result.error),
'Should fail with error message "Data context created without a source string"');
done();
});
it('file context with both options.data and options.file not provided', function(done) {
var options = {
/* data: */
sourceComments: false,
/* file: null, */
outFile: null,
includePaths: '',
precision: 5,
sourceMap: null,
style: 0,
indentWidth: 2,
indentType: 0,
linefeed: '\n',
result: { stats: {} } };
binding.renderFileSync(options);
assert(/File context created without an input path/.test(options.result.error),
'Should fail with error message "File context created without an input path"');
done();
});
it('file context with options.file not provided, options.data given', function(done) {
var options = {
data: 'div { width: 10px; } ',
sourceComments: false,
/* file: null, */
outFile: null,
includePaths: '',
precision: 5,
sourceMap: null,
style: 0,
indentWidth: 2,
indentType: 0,
linefeed: '\n',
result: { stats: {} } };
binding.renderFileSync(options);
assert(/File context created without an input path/.test(options.result.error),
'Should fail with error message "File context created without an input path"');
done();
});
it('fail with options.result not provided', function(done) {
var options = { data: 'div { width: 10px; } ',
sourceComments: false,
......@@ -96,7 +180,7 @@ describe('lowlevel', function() {
});
it('options.indentWidth not provided', function(done) {
var options = { data: 'div { width: 10px; } ',
var options = { data: 'div { width: 10px; }',
sourceComments: false,
file: null,
outFile: null,
......@@ -110,8 +194,49 @@ describe('lowlevel', function() {
result: { stats: {} } };
binding.renderSync(options);
assert(options.result.css);
done();
});
it('empty data string', function(done) {
var options = { data: '',
sourceComments: false,
file: null,
outFile: null,
includePaths: '',
precision: 5,
sourceMap: null,
style: 0,
/* indentWidth */
indentType: 0,
linefeed: '\n',
result: { stats: {} } };
binding.renderSync(options);
assert(/empty source string/.test(options.result.error),
'Should fail with error message "Data context created with empty source string"');
done();
});
it('empty file string', function(done) {
var options = {
sourceComments: false,
file: '',
outFile: null,
includePaths: '',
precision: 5,
sourceMap: null,
style: 0,
/* indentWidth */
indentType: 0,
linefeed: '\n',
result: { stats: {} } };
binding.renderFileSync(options);
assert(/empty input path/.test(options.result.error),
'Should fail with error message "File context created with empty input path"');
done();
});
}); // lowlevel
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