Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
[fixed] Compiler#fsRead - do not proceed to reading the ".map" file…
Browse files Browse the repository at this point in the history
… after failing to read the main file
  • Loading branch information
thealjey committed Jul 14, 2016
1 parent fed33d9 commit 730b979
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 65 deletions.
22 changes: 21 additions & 1 deletion docs/Compiler.html
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Compiler.js.html">Compiler.js</a>,
<a href="Compiler.js.html#sunlight-1-line-90">line 90</a>
<a href="Compiler.js.html#sunlight-1-line-86">line 86</a>
</li>
</ul>
</dd>
Expand All @@ -704,6 +704,26 @@ <h5>Parameters:</h5>



<h5>Returns:</h5>




<dl>
<dt>
Type
</dt>
<dd>

<span class="param-type">void</span>



</dd>
</dl>





<h5>Example</h5>
Expand Down
14 changes: 7 additions & 7 deletions docs/Compiler.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,10 @@ <h1 class="page-title">Source: Compiler.js</h1>
* @param {string} outPath - the output path
* @param {ProgramData} data - processed application code with source maps
* @param {Function} callback - a callback function
* @return {void}
* @example
* Compiler.writeAndCallDone('/path/to/an/input/file', '/path/to/the/output/file', data, callback);
*/
static writeAndCallDone(inPath: string, outPath: string, data: ProgramData, callback: () => void) {
if (!data.code) {
return Compiler.done(inPath, callback);
}
Compiler.fsWrite(outPath, data, () => {
Compiler.done(inPath, callback);
});
Expand All @@ -180,10 +176,14 @@ <h1 class="page-title">Source: Compiler.js</h1>
* @param {string} path - the output path
* @param {ProgramData} data - the data to write
* @param {Function} callback - a callback function
* @return {void}
* @example
* Compiler.fsWrite('/path/to/an/output/file', data, callback);
*/
static fsWrite(path: string, data: ProgramData, callback: () => void) {
if (!data.code) {
return callback();
}
Compiler.mkdir(path, () => {
writeFile(path, data.code, scriptErr => {
if (scriptErr) {
Expand Down Expand Up @@ -261,12 +261,12 @@ <h1 class="page-title">Source: Compiler.js</h1>
*/
fsRead(path: string, callback: ProgramDataCallback) {
readFile(path, (scriptErr, scriptData) => {
if (scriptErr) {
return callback({code: '', map: ''});
}
readFile(`${path}.map`, 'utf8', (mapErr, mapData) => {
const map = mapErr ? '' : mapData;

if (scriptErr) {
return callback({code: '', map});
}
if (!this.compress) {
return callback({code: scriptData.toString('utf8'), map});
}
Expand Down
2 changes: 1 addition & 1 deletion docs/quicksearch.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,12 @@ var Compiler = exports.Compiler = function () {
* @param {string} outPath - the output path
* @param {ProgramData} data - processed application code with source maps
* @param {Function} callback - a callback function
* @return {void}
* @example
* Compiler.writeAndCallDone('/path/to/an/input/file', '/path/to/the/output/file', data, callback);
*/


Compiler.writeAndCallDone = function writeAndCallDone(inPath, outPath, data, callback) {
if (!data.code) {
return Compiler.done(inPath, callback);
}
Compiler.fsWrite(outPath, data, function () {
Compiler.done(inPath, callback);
});
Expand All @@ -118,12 +114,16 @@ var Compiler = exports.Compiler = function () {
* @param {string} path - the output path
* @param {ProgramData} data - the data to write
* @param {Function} callback - a callback function
* @return {void}
* @example
* Compiler.fsWrite('/path/to/an/output/file', data, callback);
*/


Compiler.fsWrite = function fsWrite(path, data, callback) {
if (!data.code) {
return callback();
}
Compiler.mkdir(path, function () {
(0, _fs.writeFile)(path, data.code, function (scriptErr) {
if (scriptErr) {
Expand Down Expand Up @@ -209,12 +209,12 @@ var Compiler = exports.Compiler = function () {
var _this = this;

(0, _fs.readFile)(path, function (scriptErr, scriptData) {
if (scriptErr) {
return callback({ code: '', map: '' });
}
(0, _fs.readFile)(path + '.map', 'utf8', function (mapErr, mapData) {
var map = mapErr ? '' : mapData;

if (scriptErr) {
return callback({ code: '', map: map });
}
if (!_this.compress) {
return callback({ code: scriptData.toString('utf8'), map: map });
}
Expand Down
14 changes: 7 additions & 7 deletions src/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,10 @@ export class Compiler {
* @param {string} outPath - the output path
* @param {ProgramData} data - processed application code with source maps
* @param {Function} callback - a callback function
* @return {void}
* @example
* Compiler.writeAndCallDone('/path/to/an/input/file', '/path/to/the/output/file', data, callback);
*/
static writeAndCallDone(inPath: string, outPath: string, data: ProgramData, callback: () => void) {
if (!data.code) {
return Compiler.done(inPath, callback);
}
Compiler.fsWrite(outPath, data, () => {
Compiler.done(inPath, callback);
});
Expand All @@ -96,10 +92,14 @@ export class Compiler {
* @param {string} path - the output path
* @param {ProgramData} data - the data to write
* @param {Function} callback - a callback function
* @return {void}
* @example
* Compiler.fsWrite('/path/to/an/output/file', data, callback);
*/
static fsWrite(path: string, data: ProgramData, callback: () => void) {
if (!data.code) {
return callback();
}
Compiler.mkdir(path, () => {
writeFile(path, data.code, scriptErr => {
if (scriptErr) {
Expand Down Expand Up @@ -177,12 +177,12 @@ export class Compiler {
*/
fsRead(path: string, callback: ProgramDataCallback) {
readFile(path, (scriptErr, scriptData) => {
if (scriptErr) {
return callback({code: '', map: ''});
}
readFile(`${path}.map`, 'utf8', (mapErr, mapData) => {
const map = mapErr ? '' : mapData;

if (scriptErr) {
return callback({code: '', map});
}
if (!this.compress) {
return callback({code: scriptData.toString('utf8'), map});
}
Expand Down
155 changes: 113 additions & 42 deletions test/Compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,82 +126,137 @@ describe('Compiler', () => {
beforeEach(() => {
stub(Compiler, 'fsWrite').callsArg(2);
stub(Compiler, 'done');
Compiler.writeAndCallDone('/path/to/the/input/file', '/path/to/the/output/file',
{code: 'some code', map: 'source map'}, callback);
});

afterEach(() => {
Compiler.fsWrite.restore();
Compiler.done.restore();
});

describe('no code', () => {

beforeEach(() => {
Compiler.writeAndCallDone('/path/to/the/input/file', '/path/to/the/output/file',
{code: '', map: 'source map'}, callback);
});

it('calls done', () => {
expect(Compiler.done).calledWith('/path/to/the/input/file', callback);
});

it('does not call fsWrite', () => {
expect(Compiler.fsWrite).not.called;
});
it('calls fsWrite', () => {
expect(Compiler.fsWrite).calledWith('/path/to/the/output/file', {code: 'some code', map: 'source map'},
match.func);
});

it('calls done', () => {
expect(Compiler.done).calledWith('/path/to/the/input/file', callback);
});

describe('code', () => {
});

beforeEach(() => {
Compiler.writeAndCallDone('/path/to/the/input/file', '/path/to/the/output/file',
{code: 'some code', map: 'source map'}, callback);
});
describe('fsRead readFile error', () => {

it('calls fsWrite', () => {
expect(Compiler.fsWrite).calledWith('/path/to/the/output/file', {code: 'some code', map: 'source map'},
match.func);
beforeEach(() => {
stub(fs, 'readFile', (path, callback) => {
callback('failed to read the script file');
});
cmp.fsRead('/path/to/the/output/file', callback);
});

it('calls done', () => {
expect(Compiler.done).calledWith('/path/to/the/input/file', callback);
});
afterEach(() => {
fs.readFile.restore();
});

it('attempt to read script', () => {
expect(fs.readFile).calledOnce;
expect(fs.readFile).calledWith('/path/to/the/output/file', match.func);
});

it('calls the callback', () => {
expect(callback).calledWith({code: '', map: ''});
});

});

describe('fsRead readFile error', () => {
describe('fsRead readFile map error', () => {

beforeEach(() => {
stub(fs, 'readFile', (path, options, callback) => {
if (!callback) {
callback = options;
if (/\.map$/.test(path)) {
callback('failed to read the map file');
} else {
options(null, 'some code');
}
callback(/\.map$/.test(path) ? 'failed to read the map file' : 'failed to read the script file');
});
stub(zlib, 'gunzip');
cmp.fsRead('/path/to/the/output/file', callback);
});

afterEach(() => {
fs.readFile.restore();
zlib.gunzip.restore();
});

it('attempt to read script', () => {
expect(fs.readFile).calledWith('/path/to/the/output/file', match.func);
});
describe('not compress', () => {

it('attempt to read map', () => {
expect(fs.readFile).calledWith('/path/to/the/output/file.map', 'utf8', match.func);
});
beforeEach(() => {
stub(zlib, 'gunzip');
cmp.compress = false;
cmp.fsRead('/path/to/the/output/file', callback);
});

afterEach(() => {
zlib.gunzip.restore();
});

it('attempt to read map', () => {
expect(fs.readFile).calledTwice;
expect(fs.readFile).calledWith('/path/to/the/output/file.map', 'utf8', match.func);
});

it('calls the callback', () => {
expect(callback).calledWith({code: 'some code', map: ''});
});

it('does not call gunzip', () => {
expect(zlib.gunzip).not.called;
});

it('calls the callback', () => {
expect(callback).calledWith({code: '', map: ''});
});

it('does not call gunzip', () => {
expect(zlib.gunzip).not.called;
describe('compress', () => {

beforeEach(() => {
cmp.compress = true;
});

describe('gunzip error', () => {

beforeEach(() => {
stub(zlib, 'gunzip').callsArgWith(1, 'failed to uncompress');
cmp.fsRead('/path/to/the/output/file', callback);
});

afterEach(() => {
zlib.gunzip.restore();
});

it('calls gunzip', () => {
expect(zlib.gunzip).calledWith('some code', match.func);
});

it('calls the callback', () => {
expect(callback).calledWith({code: '', map: ''});
});

});

describe('gunzip success', () => {

beforeEach(() => {
stub(zlib, 'gunzip').callsArgWith(1, null, 'uncompressed code');
cmp.fsRead('/path/to/the/output/file', callback);
});

afterEach(() => {
zlib.gunzip.restore();
});

it('calls the callback', () => {
expect(callback).calledWith({code: 'uncompressed code', map: ''});
});

});

});

});
Expand Down Expand Up @@ -317,6 +372,22 @@ describe('Compiler', () => {
Compiler.mkdir.restore();
});

describe('no code', () => {

beforeEach(() => {
Compiler.fsWrite('/path/to/the/output/file', {code: ''}, callback);
});

it('calls the callback', () => {
expect(callback).called;
});

it('does not call mkdir', () => {
expect(Compiler.mkdir).not.called;
});

});

describe('script write error', () => {

beforeEach(() => {
Expand Down

0 comments on commit 730b979

Please sign in to comment.