function(done) { //ensure all entries returned are files var f = sfs.scoped(SFS_DIR, true), foundMatch = false, foundBadFile = false; f.readEachFileMatching(REGEX_PACKAGE_JSON, '.', true, function(err, file, stat, content) { expect(!err).toBeTruthy(); if (file.match(REGEX_PACKAGE_JSON)) { foundMatch = true; expect(stat).toBeDefined(); //all package.json are expected to be non-empty: expect(content).toBeDefined(); expect(content.length).toBeGreaterThan(0); var pkgName = REGEX_PKG_NAME.exec(file); if (pkgName) //skip scopedfs' package.json { //try to match package.json content to expected name expect(JSON.parse(content).name).toEqual(pkgName[1]); } } else { foundBadFile = true; } }, function() { expect(foundMatch).toBeTruthy(); expect(foundBadFile).toBeFalsy(); //should not have any files not matching the search expression done(); }); });
function(done) { //ensure all entries returned are files var f = sfs.scoped(SFS_DIR, true), fileCnt = 0, foundFile = false, foundNonFile = false; f.eachFile('.', true, function(err, file, stat) { expect(!err).toBeTruthy(); file = path.normalize(file).replace(/\\/g, '/'); if (stat.isFile()) { ++fileCnt; foundFile = true; } else { foundNonFile = true; } }, function() { expect(foundFile).toBeTruthy(); expect(foundNonFile).toBeFalsy(); expect(fileCntInSfs).toBeLessThan(fileCnt); //compare against fs.readdirSync done(); }); });
function(done) { var f = sfs.scoped(winDir, true), returnCnt = 0, i; function callback(err, data) { expect(!err).toBeTruthy(); //can access notepad in windows folder expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(1); //no matter how many times the same file is read, only 1 (system) fs.readFile call should have been made if (++returnCnt >= REPEAT_CNT) { sfs.purgeCache(); //using global instance f.readFile(NOTEPAD, 'utf8', true, function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(2); //refetch as cache was purged f.readFile(NOTEPAD, 'utf8', true, function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(2); //file is back in cache, so no new call to fs.readFile done(); }); }); } } //read the same file REPEAT_CNT times for (i = 0; i < REPEAT_CNT; ++i) { f.readFile(NOTEPAD, 'utf8', true, callback); } });
function(done) { var f = sfs.scoped(winDir, true); f.readFile(null, null, true, function(err, data) { expect(!err).toBeFalsy(); expect(data).toBeUndefined(); done(); }); });
function(done) { var f = sfs.scoped(winDir, true); f.readFile('*** no such file ***', null, true, function(err, data) { expect(!err).toBeFalsy(); //can't access non-existent file expect(data).toBeUndefined(); done(); }); });
function() { var f = sfs.scoped(); expect(!!f.statSync('node_modules/scopedfs/lib/scopedfs.js')).toBeTruthy(); //can access root drive, windows, users folders expect(!!sfs.statSync(winDir)).toBeTruthy(); expect(!!sfs.statSync(rootDrive)).toBeTruthy(); expect(!!sfs.statSync(rootDrive + '/users')).toBeTruthy(); });
function(done) { var f = sfs.scoped(winDir, true); f.readFile(NOTEPAD, true, function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(data instanceof Buffer).toBeTruthy(); expect(data.length).toBeGreaterThan(0); done(); }); });
function(done) { var f = sfs.scoped(winDir, true); f.readFile(NOTEPAD, {encoding:'binary'}, true, function(err, data) { expect(!err).toBeTruthy(); //can access notepad in windows folder expect(data).toBeDefined(); expect(typeof data).toEqual('string'); //Note: even though encoding is specified as 'binary', data is returned as string! expect(data.length).toBeGreaterThan(0); done(); }); });
function(done) { var f = sfs.scoped(winDir, true); f.readFile(NOTEPAD, 'ascii', function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); done(); }); });
function(done) { var f = sfs.scoped(winDir, true); f.readFile(NOTEPAD, null, true, function(err, data) { expect(!err).toBeTruthy(); //can access notepad in windows folder expect(data).toBeDefined(); expect(data instanceof Buffer).toBeTruthy(); //Note: encoding must be null to get data in a Buffer! expect(data.length).toBeGreaterThan(0); done(); }); });
function(done) { //We'll try to find the 3 files in our test folder var f = sfs.scoped(SFS_DIR, true), fileCnt = 0, fileCnt2 = 0, foundTestDir = false, foundTestFile = false, foundTestBat = false, otherFile = false; f.eachFileOrDirectory('.', true, function(err, file, stat) { expect(!err).toBeTruthy(); ++fileCnt; file = path.normalize(file).replace(/\\/g, '/'); if (stat.isDirectory()) { if (file === TEST_DIR) { foundTestDir = true; } else { otherFile = true; } } else { if (file === TEST_BAT) { foundTestBat = true; } else if (file === TEST_FILE) { foundTestFile = true; } else { otherFile = true; } } }, function() { expect(foundTestDir && foundTestFile && foundTestBat && otherFile).toBeTruthy(); expect(fileCntInSfs).toBeLessThan(fileCnt); //compare against fs.readdirSync done(); }); //no recursion f.eachFileOrDirectory('.', false, function(err, file, stat) { expect(!err).toBeTruthy(); ++fileCnt2; }, function() { expect(fileCntInSfs).toEqual(fileCnt2); //compare against fs.readdirSync done(); }); });
function(done) { var f = sfs.scoped(winDir, true); f.stat(rootDrive, function(err, stats) { expect(!err).toBeFalsy(); //can't access root drive when lock to windows folder expect(stats).toBeUndefined(); f.stat(NOTEPAD, function(err, stats) { expect(!err).toBeTruthy(); //can access notepad in windows folder expect(stats).toBeDefined(); f.stat('../users', function(err, stats) { expect(!err).toBeFalsy(); //can't use '..' to reach 'above' locked folder expect(stats).toBeUndefined(); done(); }); }); }); });
f.readFile(NOTEPAD, null, true, function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(data instanceof Buffer).toBeTruthy(); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(4); //get one of the earlier cached copies & also demonstrate cache is 'global' var f2 = sfs.scoped(winDir, true); f2.readFile(NOTEPAD, 'binary', true, function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(4); //no change, as retrieved from cache done(); }); });
function(done) { var f = sfs.scoped(winDir, true); f.readFile(NOTEPAD, {encoding:'binary'}, true, function(err, data) { expect(!err).toBeTruthy(); //can access notepad in windows folder expect(data).toBeDefined(); expect(typeof data).toEqual('string'); //Note: even though encoding is specified as 'binary', data is returned as string! expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(1); //use different options f.readFile(NOTEPAD, 'binary', true, function(err, data) { //Note: considered different from earlier, even if both encoding are the same expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(2); f.readFile(NOTEPAD, 'utf8', true, function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(3); f.readFile(NOTEPAD, null, true, function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(data instanceof Buffer).toBeTruthy(); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(4); //get one of the earlier cached copies & also demonstrate cache is 'global' var f2 = sfs.scoped(winDir, true); f2.readFile(NOTEPAD, 'binary', true, function(err, data) { expect(!err).toBeTruthy(); expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); expect(readFileCallCnt).toBe(4); //no change, as retrieved from cache done(); }); }); }); }); }); });
function(done) { readFileCallCnt = 0; //reset counter var f = sfs.scoped(winDir, true), returnCnt = 0, i; function callback(err, data) { expect(!err).toBeTruthy(); //can access notepad in windows folder expect(data).toBeDefined(); expect(typeof data).toEqual('string'); expect(data.length).toBeGreaterThan(0); if (++returnCnt >= REPEAT_CNT) { expect(readFileCallCnt).toBe(REPEAT_CNT); //no cache, so multiple calls expected done(); } } //read the same file REPEAT_CNT times for (i = 0; i < REPEAT_CNT; ++i) { f.readFile(NOTEPAD, 'utf8', false, callback); } });
function(done) { //ensure all entries returned are files var f = sfs.scoped(SFS_DIR, true), foundMatch = false, foundBadFile = false; f.eachFileMatching(REGEX_PACKAGE_JSON, '.', true, function(err, file, stat) { expect(!err).toBeTruthy(); if (file.match(REGEX_PACKAGE_JSON)) { foundMatch = true; expect(stat).toBeDefined(); } else { foundBadFile = true; } }, function() { expect(foundMatch).toBeTruthy(); expect(foundBadFile).toBeFalsy(); //should not have any files not matching the search expression done(); }); });
function(done) { //ensure all entries returned are files var f = sfs.scoped(SFS_DIR, true), foundDir = false, foundNonDir = false; f.eachDirectory('.', true, function(err, file, stat) { expect(!err).toBeTruthy(); file = path.normalize(file).replace(/\\/g, '/'); if (stat.isDirectory()) { foundDir = true; } else { foundNonDir = true; } }, function() { expect(foundDir).toBeTruthy(); expect(foundNonDir).toBeFalsy(); done(); }); });
function() { sfs.purgeCache(); var f = sfs.scoped(winDir, true); f.purgeCache(); });
temp.mkdir(options.root, function (err, dirPath) { if (err) { return cb(err); } var env = {}; env.log = minilog(dirPath); env.dirPath = dirPath; env.log.info('build-env: setting up build environment...'); // // "scoped" fs operations // env.fs = sfs.scoped(dirPath); // // "spawn" is always in that cwd // env.spawn = function () { var argv = [].slice.call(arguments), _env = JSON.parse(JSON.stringify(process.env)); _env.path = path.resolve(__dirname + '/node_modules/.bin'); if (!argv[1]) { argv[1] = []; } if (!argv[2]) { argv[2] = {}; } argv[2].cwd = argv[2].cwd ? path.join(dirPath, argv[2].cwd) : dirPath ; argv[2].env = _env; return spawn.apply(spawn, argv); }; // // same with exec // env.exec = function () { var argv = [].slice.call(arguments), _env = JSON.parse(JSON.stringify(process.env)); if (argv.length === 2) { argv = [ argv[0] ].concat({ cwd: dirPath }).concat(argv[1]); } else { argv[1].cwd = argv[1].cwd ? path.join(dirPath, argv[1].cwd) : dirPath ; } argv[1].env = _env; return exec.apply(exec, argv); }; // // remove the folder // env.teardown = function (cb) { cb = cb || function () {}; env.log.info('build-env: removing `' + dirPath + '`...'); return rimraf(dirPath, function (err) { if (err) { env.log.warn('build-env: no dice while trying to remove `' + dirPath + '`:'); env.log.warn(err.stack); } env.log.info('build-env: successfully removed `' + dirPath + '`.'); cb(err); }); } env.mkdirp = function (p, cb) { return mkdirp(path.join(dirPath, p), cb); }; env.glob = function (g, opts, cb) { opts.cwd = path.join(dirPath, opts.cwd); return glob.call(glob, g, opts, cb); }; env.log.info('build-env: set up build environment.'); cb(null, env); });
function(done) { var f = sfs.scoped(winDir, true); f.readFile(NOTEPAD, 'utf8', true); done(); //ensure no crash });