electron/spec/asar-spec.js

796 lines
27 KiB
JavaScript
Raw Normal View History

2016-01-19 22:49:40 +00:00
const assert = require('assert');
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
2016-01-12 02:40:23 +00:00
2016-01-19 22:49:40 +00:00
const nativeImage = require('electron').nativeImage;
const remote = require('electron').remote;
2016-01-12 02:40:23 +00:00
2016-01-19 22:49:40 +00:00
const ipcMain = remote.require('electron').ipcMain;
const BrowserWindow = remote.require('electron').BrowserWindow;
2016-01-12 02:40:23 +00:00
describe('asar package', function() {
var fixtures = path.join(__dirname, 'fixtures');
2016-01-12 02:40:23 +00:00
describe('node api', function() {
describe('fs.readFileSync', function() {
it('does not leak fd', function() {
2016-01-19 22:49:40 +00:00
var readCalls = 1;
while(readCalls <= 10000) {
fs.readFileSync(path.join(process.resourcesPath, 'atom.asar', 'renderer', 'api', 'lib', 'ipc.js'));
readCalls++;
2016-01-12 02:40:23 +00:00
}
});
2016-01-12 02:40:23 +00:00
it('reads a normal file', function() {
2016-02-17 01:39:11 +00:00
var file1 = path.join(fixtures, 'asar', 'a.asar', 'file1');
2016-01-12 02:40:23 +00:00
assert.equal(fs.readFileSync(file1).toString().trim(), 'file1');
2016-02-17 01:39:11 +00:00
var file2 = path.join(fixtures, 'asar', 'a.asar', 'file2');
2016-01-12 02:40:23 +00:00
assert.equal(fs.readFileSync(file2).toString().trim(), 'file2');
2016-02-17 01:39:11 +00:00
var file3 = path.join(fixtures, 'asar', 'a.asar', 'file3');
assert.equal(fs.readFileSync(file3).toString().trim(), 'file3');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('reads from a empty file', function() {
2016-02-17 01:39:11 +00:00
var file = path.join(fixtures, 'asar', 'empty.asar', 'file1');
var buffer = fs.readFileSync(file);
2016-01-12 02:40:23 +00:00
assert.equal(buffer.length, 0);
2016-02-17 01:39:11 +00:00
assert.equal(buffer.toString(), '');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('reads a linked file', function() {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link1');
assert.equal(fs.readFileSync(p).toString().trim(), 'file1');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('reads a file from linked directory', function() {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'file1');
2016-01-12 02:40:23 +00:00
assert.equal(fs.readFileSync(p).toString().trim(), 'file1');
p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1');
2016-02-17 01:39:11 +00:00
assert.equal(fs.readFileSync(p).toString().trim(), 'file1');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('throws ENOENT error when can not find file', function() {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
var throws = function() {
fs.readFileSync(p);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
assert.throws(throws, /ENOENT/);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('passes ENOENT error to callback when can not find file', function() {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
var async = false;
2016-01-12 02:40:23 +00:00
fs.readFile(p, function(e) {
assert(async);
2016-02-17 01:39:11 +00:00
assert(/ENOENT/.test(e));
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
async = true;
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('reads a normal file with unpacked files', function() {
var p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt');
assert.equal(fs.readFileSync(p).toString().trim(), 'a');
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('fs.readFile', function() {
it('reads a normal file', function(done) {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'file1');
fs.readFile(p, function(err, content) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(String(content).trim(), 'file1');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('reads from a empty file', function(done) {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'empty.asar', 'file1');
fs.readFile(p, function(err, content) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(String(content), '');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('reads a linked file', function(done) {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link1');
fs.readFile(p, function(err, content) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(String(content).trim(), 'file1');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('reads a file from linked directory', function(done) {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1');
fs.readFile(p, function(err, content) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(String(content).trim(), 'file1');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function(done) {
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
fs.readFile(p, function(err) {
2016-01-12 02:40:23 +00:00
assert.equal(err.code, 'ENOENT');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('fs.lstatSync', function() {
it('handles path with trailing slash correctly', function() {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1');
2016-01-12 02:40:23 +00:00
fs.lstatSync(p);
2016-02-17 01:39:11 +00:00
fs.lstatSync(p + '/');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('returns information of root', function() {
2016-02-17 17:27:25 +00:00
var p = path.join(fixtures, 'asar', 'a.asar');
var stats = fs.lstatSync(p);
2016-01-12 02:40:23 +00:00
assert.equal(stats.isFile(), false);
assert.equal(stats.isDirectory(), true);
assert.equal(stats.isSymbolicLink(), false);
2016-02-17 01:39:11 +00:00
assert.equal(stats.size, 0);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('returns information of a normal file', function() {
2016-02-17 01:45:18 +00:00
var file, j, len, p, ref2, stats;
2016-01-12 02:40:23 +00:00
ref2 = ['file1', 'file2', 'file3', path.join('dir1', 'file1'), path.join('link2', 'file1')];
for (j = 0, len = ref2.length; j < len; j++) {
file = ref2[j];
p = path.join(fixtures, 'asar', 'a.asar', file);
stats = fs.lstatSync(p);
assert.equal(stats.isFile(), true);
assert.equal(stats.isDirectory(), false);
assert.equal(stats.isSymbolicLink(), false);
2016-02-17 01:45:18 +00:00
assert.equal(stats.size, 6);
2016-01-12 02:40:23 +00:00
}
});
2016-01-12 02:40:23 +00:00
it('returns information of a normal directory', function() {
2016-02-17 01:45:18 +00:00
var file, j, len, p, ref2, stats;
2016-01-12 02:40:23 +00:00
ref2 = ['dir1', 'dir2', 'dir3'];
for (j = 0, len = ref2.length; j < len; j++) {
file = ref2[j];
p = path.join(fixtures, 'asar', 'a.asar', file);
stats = fs.lstatSync(p);
assert.equal(stats.isFile(), false);
assert.equal(stats.isDirectory(), true);
assert.equal(stats.isSymbolicLink(), false);
2016-02-17 01:45:18 +00:00
assert.equal(stats.size, 0);
2016-01-12 02:40:23 +00:00
}
});
2016-01-12 02:40:23 +00:00
it('returns information of a linked file', function() {
2016-02-17 01:45:18 +00:00
var file, j, len, p, ref2, stats;
2016-01-12 02:40:23 +00:00
ref2 = ['link1', path.join('dir1', 'link1'), path.join('link2', 'link2')];
for (j = 0, len = ref2.length; j < len; j++) {
file = ref2[j];
p = path.join(fixtures, 'asar', 'a.asar', file);
stats = fs.lstatSync(p);
assert.equal(stats.isFile(), false);
assert.equal(stats.isDirectory(), false);
assert.equal(stats.isSymbolicLink(), true);
2016-02-17 01:45:18 +00:00
assert.equal(stats.size, 0);
2016-01-12 02:40:23 +00:00
}
});
2016-01-12 02:40:23 +00:00
it('returns information of a linked directory', function() {
2016-02-17 01:45:18 +00:00
var file, j, len, p, ref2, stats;
2016-01-12 02:40:23 +00:00
ref2 = ['link2', path.join('dir1', 'link2'), path.join('link2', 'link2')];
for (j = 0, len = ref2.length; j < len; j++) {
file = ref2[j];
p = path.join(fixtures, 'asar', 'a.asar', file);
stats = fs.lstatSync(p);
assert.equal(stats.isFile(), false);
assert.equal(stats.isDirectory(), false);
assert.equal(stats.isSymbolicLink(), true);
2016-02-17 01:45:18 +00:00
assert.equal(stats.size, 0);
2016-01-12 02:40:23 +00:00
}
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function() {
var file, j, len, p, ref2, throws;
2016-01-12 02:40:23 +00:00
ref2 = ['file4', 'file5', path.join('dir1', 'file4')];
for (j = 0, len = ref2.length; j < len; j++) {
file = ref2[j];
p = path.join(fixtures, 'asar', 'a.asar', file);
throws = function() {
2016-02-17 01:39:11 +00:00
fs.lstatSync(p);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
assert.throws(throws, /ENOENT/);
2016-01-12 02:40:23 +00:00
}
});
});
2016-01-12 02:40:23 +00:00
describe('fs.lstat', function() {
it('handles path with trailing slash correctly', function(done) {
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1');
2016-02-17 01:39:11 +00:00
fs.lstat(p + '/', done);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('returns information of root', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar');
fs.lstat(p, function(err, stats) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(stats.isFile(), false);
assert.equal(stats.isDirectory(), true);
assert.equal(stats.isSymbolicLink(), false);
assert.equal(stats.size, 0);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns information of a normal file', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'file1');
fs.lstat(p, function(err, stats) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(stats.isFile(), true);
assert.equal(stats.isDirectory(), false);
assert.equal(stats.isSymbolicLink(), false);
assert.equal(stats.size, 6);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns information of a normal directory', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'dir1');
fs.lstat(p, function(err, stats) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(stats.isFile(), false);
assert.equal(stats.isDirectory(), true);
assert.equal(stats.isSymbolicLink(), false);
assert.equal(stats.size, 0);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns information of a linked file', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link1');
fs.lstat(p, function(err, stats) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(stats.isFile(), false);
assert.equal(stats.isDirectory(), false);
assert.equal(stats.isSymbolicLink(), true);
assert.equal(stats.size, 0);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns information of a linked directory', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2');
fs.lstat(p, function(err, stats) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(stats.isFile(), false);
assert.equal(stats.isDirectory(), false);
assert.equal(stats.isSymbolicLink(), true);
assert.equal(stats.size, 0);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'file4');
fs.lstat(p, function(err) {
2016-01-12 02:40:23 +00:00
assert.equal(err.code, 'ENOENT');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('fs.realpathSync', function() {
it('returns real path root', function() {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = 'a.asar';
var r = fs.realpathSync(path.join(parent, p));
2016-02-17 01:39:11 +00:00
assert.equal(r, path.join(parent, p));
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('returns real path of a normal file', function() {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'file1');
var r = fs.realpathSync(path.join(parent, p));
2016-02-17 01:39:11 +00:00
assert.equal(r, path.join(parent, p));
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('returns real path of a normal directory', function() {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'dir1');
var r = fs.realpathSync(path.join(parent, p));
2016-02-17 01:39:11 +00:00
assert.equal(r, path.join(parent, p));
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('returns real path of a linked file', function() {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'link2', 'link1');
var r = fs.realpathSync(path.join(parent, p));
2016-02-17 01:39:11 +00:00
assert.equal(r, path.join(parent, 'a.asar', 'file1'));
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('returns real path of a linked directory', function() {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'link2', 'link2');
var r = fs.realpathSync(path.join(parent, p));
2016-02-17 01:39:11 +00:00
assert.equal(r, path.join(parent, 'a.asar', 'dir1'));
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function() {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'not-exist');
var throws = function() {
2016-02-17 01:39:11 +00:00
fs.realpathSync(path.join(parent, p));
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
assert.throws(throws, /ENOENT/);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('fs.realpath', function() {
it('returns real path root', function(done) {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = 'a.asar';
2016-02-17 01:39:11 +00:00
fs.realpath(path.join(parent, p), function(err, r) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(r, path.join(parent, p));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns real path of a normal file', function(done) {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'file1');
2016-02-17 01:39:11 +00:00
fs.realpath(path.join(parent, p), function(err, r) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(r, path.join(parent, p));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns real path of a normal directory', function(done) {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'dir1');
2016-02-17 01:39:11 +00:00
fs.realpath(path.join(parent, p), function(err, r) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(r, path.join(parent, p));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns real path of a linked file', function(done) {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'link2', 'link1');
2016-02-17 01:39:11 +00:00
fs.realpath(path.join(parent, p), function(err, r) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(r, path.join(parent, 'a.asar', 'file1'));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns real path of a linked directory', function(done) {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'link2', 'link2');
2016-02-17 01:39:11 +00:00
fs.realpath(path.join(parent, p), function(err, r) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(r, path.join(parent, 'a.asar', 'dir1'));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function(done) {
var parent = fs.realpathSync(path.join(fixtures, 'asar'));
var p = path.join('a.asar', 'not-exist');
2016-02-17 01:39:11 +00:00
fs.realpath(path.join(parent, p), function(err) {
2016-01-12 02:40:23 +00:00
assert.equal(err.code, 'ENOENT');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
describe('fs.readdirSync', function() {
it('reads dirs from root', function() {
var p = path.join(fixtures, 'asar', 'a.asar');
var dirs = fs.readdirSync(p);
2016-02-17 01:39:11 +00:00
assert.deepEqual(dirs, ['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('reads dirs from a normal dir', function() {
var p = path.join(fixtures, 'asar', 'a.asar', 'dir1');
var dirs = fs.readdirSync(p);
2016-02-17 01:39:11 +00:00
assert.deepEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2']);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('reads dirs from a linked dir', function() {
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2');
var dirs = fs.readdirSync(p);
2016-02-17 01:39:11 +00:00
assert.deepEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2']);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function() {
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
var throws = function() {
2016-02-17 01:39:11 +00:00
fs.readdirSync(p);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
assert.throws(throws, /ENOENT/);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('fs.readdir', function() {
it('reads dirs from root', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar');
fs.readdir(p, function(err, dirs) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.deepEqual(dirs, ['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('reads dirs from a normal dir', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'dir1');
fs.readdir(p, function(err, dirs) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.deepEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2']);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
it('reads dirs from a linked dir', function(done) {
2016-01-19 22:49:40 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2');
fs.readdir(p, function(err, dirs) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.deepEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2']);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function(done) {
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
2016-02-17 01:39:11 +00:00
fs.readdir(p, function(err) {
2016-01-12 02:40:23 +00:00
assert.equal(err.code, 'ENOENT');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('fs.openSync', function() {
it('opens a normal/linked/under-linked-directory file', function() {
2016-02-17 01:45:18 +00:00
var buffer, fd, file, j, len, p, ref2;
2016-01-12 02:40:23 +00:00
ref2 = ['file1', 'link1', path.join('link2', 'file1')];
for (j = 0, len = ref2.length; j < len; j++) {
file = ref2[j];
p = path.join(fixtures, 'asar', 'a.asar', file);
fd = fs.openSync(p, 'r');
buffer = new Buffer(6);
fs.readSync(fd, buffer, 0, 6, 0);
assert.equal(String(buffer).trim(), 'file1');
2016-02-17 01:39:11 +00:00
fs.closeSync(fd);
2016-01-12 02:40:23 +00:00
}
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function() {
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
var throws = function() {
2016-02-17 01:39:11 +00:00
fs.openSync(p);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
assert.throws(throws, /ENOENT/);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('fs.open', function() {
it('opens a normal file', function(done) {
var p = path.join(fixtures, 'asar', 'a.asar', 'file1');
2016-02-17 01:39:11 +00:00
fs.open(p, 'r', function(err, fd) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
2016-02-17 01:39:11 +00:00
var buffer = new Buffer(6);
fs.read(fd, buffer, 0, 6, 0, function(err) {
2016-01-12 02:40:23 +00:00
assert.equal(err, null);
assert.equal(String(buffer).trim(), 'file1');
2016-02-17 01:39:11 +00:00
fs.close(fd, done);
2016-01-12 02:40:23 +00:00
});
});
});
2016-02-17 01:39:11 +00:00
it('throws ENOENT error when can not find file', function(done) {
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
2016-02-17 01:39:11 +00:00
fs.open(p, 'r', function(err) {
2016-01-12 02:40:23 +00:00
assert.equal(err.code, 'ENOENT');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('fs.mkdir', function() {
2016-02-17 01:39:11 +00:00
it('throws error when calling inside asar archive', function(done) {
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
2016-02-17 01:39:11 +00:00
fs.mkdir(p, function(err) {
2016-01-12 02:40:23 +00:00
assert.equal(err.code, 'ENOTDIR');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('fs.mkdirSync', function() {
2016-02-17 01:39:11 +00:00
it('throws error when calling inside asar archive', function() {
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
2016-02-17 01:39:11 +00:00
assert.throws((function() {
fs.mkdirSync(p);
2016-01-12 02:40:23 +00:00
}), new RegExp('ENOTDIR'));
});
});
2016-01-12 02:40:23 +00:00
describe('child_process.fork', function() {
it('opens a normal js file', function(done) {
var child = child_process.fork(path.join(fixtures, 'asar', 'a.asar', 'ping.js'));
2016-01-12 02:40:23 +00:00
child.on('message', function(msg) {
assert.equal(msg, 'message');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('supports asar in the forked js', function(done) {
var file = path.join(fixtures, 'asar', 'a.asar', 'file1');
var child = child_process.fork(path.join(fixtures, 'module', 'asar.js'));
2016-01-12 02:40:23 +00:00
child.on('message', function(content) {
assert.equal(content, fs.readFileSync(file).toString());
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send(file);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('child_process.execFile', function() {
var echo, execFile, execFileSync, ref2;
if (process.platform !== 'darwin') {
return;
}
ref2 = require('child_process'), execFile = ref2.execFile, execFileSync = ref2.execFileSync;
echo = path.join(fixtures, 'asar', 'echo.asar', 'echo');
2016-01-12 02:40:23 +00:00
it('executes binaries', function(done) {
2016-01-19 22:49:40 +00:00
execFile(echo, ['test'], function(error, stdout) {
2016-01-12 02:40:23 +00:00
assert.equal(error, null);
assert.equal(stdout, 'test\n');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
xit('execFileSync executes binaries', function() {
var output = execFileSync(echo, ['test']);
assert.equal(String(output), 'test\n');
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('internalModuleReadFile', function() {
var internalModuleReadFile = process.binding('fs').internalModuleReadFile;
2016-01-12 02:40:23 +00:00
it('read a normal file', function() {
2016-02-17 01:39:11 +00:00
var file1 = path.join(fixtures, 'asar', 'a.asar', 'file1');
2016-01-12 02:40:23 +00:00
assert.equal(internalModuleReadFile(file1).toString().trim(), 'file1');
2016-02-17 01:39:11 +00:00
var file2 = path.join(fixtures, 'asar', 'a.asar', 'file2');
2016-01-12 02:40:23 +00:00
assert.equal(internalModuleReadFile(file2).toString().trim(), 'file2');
2016-02-17 01:39:11 +00:00
var file3 = path.join(fixtures, 'asar', 'a.asar', 'file3');
assert.equal(internalModuleReadFile(file3).toString().trim(), 'file3');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('reads a normal file with unpacked files', function() {
var p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt');
assert.equal(internalModuleReadFile(p).toString().trim(), 'a');
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
describe('process.noAsar', function() {
var errorName = process.platform === 'win32' ? 'ENOENT' : 'ENOTDIR';
2016-01-12 02:40:23 +00:00
beforeEach(function() {
2016-02-17 01:39:11 +00:00
process.noAsar = true;
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
afterEach(function() {
2016-02-17 01:39:11 +00:00
process.noAsar = false;
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('disables asar support in sync API', function() {
var file = path.join(fixtures, 'asar', 'a.asar', 'file1');
var dir = path.join(fixtures, 'asar', 'a.asar', 'dir1');
2016-01-12 02:40:23 +00:00
assert.throws((function() {
2016-02-17 01:39:11 +00:00
fs.readFileSync(file);
2016-01-12 02:40:23 +00:00
}), new RegExp(errorName));
assert.throws((function() {
2016-02-17 01:39:11 +00:00
fs.lstatSync(file);
2016-01-12 02:40:23 +00:00
}), new RegExp(errorName));
assert.throws((function() {
2016-02-17 01:39:11 +00:00
fs.realpathSync(file);
2016-01-12 02:40:23 +00:00
}), new RegExp(errorName));
2016-02-17 01:39:11 +00:00
assert.throws((function() {
fs.readdirSync(dir);
2016-01-12 02:40:23 +00:00
}), new RegExp(errorName));
});
2016-01-12 02:40:23 +00:00
it('disables asar support in async API', function(done) {
2016-02-17 17:27:25 +00:00
var file = path.join(fixtures, 'asar', 'a.asar', 'file1');
var dir = path.join(fixtures, 'asar', 'a.asar', 'dir1');
2016-02-17 01:39:11 +00:00
fs.readFile(file, function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error.code, errorName);
2016-02-17 01:39:11 +00:00
fs.lstat(file, function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error.code, errorName);
2016-02-17 01:39:11 +00:00
fs.realpath(file, function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error.code, errorName);
2016-02-17 01:39:11 +00:00
fs.readdir(dir, function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error.code, errorName);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
});
});
2016-02-17 01:39:11 +00:00
it('treats *.asar as normal file', function() {
var originalFs = require('original-fs');
var asar = path.join(fixtures, 'asar', 'a.asar');
var content1 = fs.readFileSync(asar);
var content2 = originalFs.readFileSync(asar);
2016-01-12 02:40:23 +00:00
assert.equal(content1.compare(content2), 0);
2016-02-17 01:39:11 +00:00
assert.throws((function() {
fs.readdirSync(asar);
2016-01-12 02:40:23 +00:00
}), /ENOTDIR/);
});
});
});
2016-01-12 02:40:23 +00:00
describe('asar protocol', function() {
var url = require('url');
2016-01-12 02:40:23 +00:00
it('can request a file in package', function(done) {
var p = path.resolve(fixtures, 'asar', 'a.asar', 'file1');
2016-02-17 01:39:11 +00:00
$.get("file://" + p, function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data.trim(), 'file1');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('can request a file in package with unpacked files', function(done) {
var p = path.resolve(fixtures, 'asar', 'unpack.asar', 'a.txt');
2016-02-17 01:39:11 +00:00
$.get("file://" + p, function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data.trim(), 'a');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('can request a linked file in package', function(done) {
var p = path.resolve(fixtures, 'asar', 'a.asar', 'link2', 'link1');
2016-02-17 01:39:11 +00:00
$.get("file://" + p, function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data.trim(), 'file1');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('can request a file in filesystem', function(done) {
var p = path.resolve(fixtures, 'asar', 'file');
2016-02-17 01:39:11 +00:00
$.get("file://" + p, function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data.trim(), 'file');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('gets 404 when file is not found', function(done) {
var p = path.resolve(fixtures, 'asar', 'a.asar', 'no-exist');
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: "file://" + p,
error: function(err) {
assert.equal(err.status, 404);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
});
2016-01-12 02:40:23 +00:00
it('sets __dirname correctly', function(done) {
after(function() {
w.destroy();
2016-02-17 01:39:11 +00:00
ipcMain.removeAllListeners('dirname');
2016-01-12 02:40:23 +00:00
});
var w = new BrowserWindow({
2016-01-12 02:40:23 +00:00
show: false,
width: 400,
height: 400
});
var p = path.resolve(fixtures, 'asar', 'web.asar', 'index.html');
var u = url.format({
2016-01-12 02:40:23 +00:00
protocol: 'file',
slashed: true,
pathname: p
});
ipcMain.once('dirname', function(event, dirname) {
assert.equal(dirname, path.dirname(p));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
w.loadURL(u);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('loads script tag in html', function(done) {
2016-01-12 02:40:23 +00:00
after(function() {
w.destroy();
2016-02-17 01:39:11 +00:00
ipcMain.removeAllListeners('ping');
2016-01-12 02:40:23 +00:00
});
var w = new BrowserWindow({
2016-01-12 02:40:23 +00:00
show: false,
width: 400,
height: 400
});
var p = path.resolve(fixtures, 'asar', 'script.asar', 'index.html');
var u = url.format({
2016-01-12 02:40:23 +00:00
protocol: 'file',
slashed: true,
pathname: p
});
w.loadURL(u);
2016-02-17 01:39:11 +00:00
ipcMain.once('ping', function(event, message) {
2016-01-12 02:40:23 +00:00
assert.equal(message, 'pong');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('original-fs module', function() {
var originalFs = require('original-fs');
2016-01-12 02:40:23 +00:00
it('treats .asar as file', function() {
2016-02-17 17:27:25 +00:00
var file = path.join(fixtures, 'asar', 'a.asar');
var stats = originalFs.statSync(file);
2016-02-17 01:39:11 +00:00
assert(stats.isFile());
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('is available in forked scripts', function(done) {
2016-02-17 17:27:25 +00:00
var child = child_process.fork(path.join(fixtures, 'module', 'original-fs.js'));
2016-01-12 02:40:23 +00:00
child.on('message', function(msg) {
assert.equal(msg, 'object');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('graceful-fs module', function() {
var gfs = require('graceful-fs');
2016-01-12 02:40:23 +00:00
it('recognize asar archvies', function() {
2016-02-17 01:39:11 +00:00
var p = path.join(fixtures, 'asar', 'a.asar', 'link1');
assert.equal(gfs.readFileSync(p).toString().trim(), 'file1');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('does not touch global fs object', function() {
assert.notEqual(fs.readdir, gfs.readdir);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('mkdirp module', function() {
var mkdirp = require('mkdirp');
2016-02-17 01:39:11 +00:00
it('throws error when calling inside asar archive', function() {
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist');
assert.throws((function() {
mkdirp.sync(p);
2016-01-12 02:40:23 +00:00
}), new RegExp('ENOTDIR'));
});
});
2016-02-17 01:39:11 +00:00
describe('native-image', function() {
2016-01-12 02:40:23 +00:00
it('reads image from asar archive', function() {
var p = path.join(fixtures, 'asar', 'logo.asar', 'logo.png');
var logo = nativeImage.createFromPath(p);
2016-02-17 01:39:11 +00:00
assert.deepEqual(logo.getSize(), {
2016-01-12 02:40:23 +00:00
width: 55,
height: 55
});
});
2016-02-17 01:39:11 +00:00
it('reads image from asar archive with unpacked files', function() {
var p = path.join(fixtures, 'asar', 'unpack.asar', 'atom.png');
var logo = nativeImage.createFromPath(p);
2016-02-17 01:39:11 +00:00
assert.deepEqual(logo.getSize(), {
2016-01-12 02:40:23 +00:00
width: 1024,
height: 1024
});
});
});
});