function * mvGen(srcPath, destPath) { if (!destPath.replace(/\s/g, '')) throw new Error('the destination path is an empty string'); srcPath = path.resolve(srcPath); destPath = path.resolve(destPath); if (!(yield fs.exists.bind(fs, srcPath))[0]) throw new Error('the source file is not exists'); // 判断目标目录是不是存在 var destDir = destPath.slice(0, destPath.lastIndexOf(path.sep)); if (!(yield fs.exists.bind(fs, destDir))) { throw new Error('the destination directory is not exists'); } // 创建目标文件 throwError(yield fs.writeFile.bind(fs, destPath, '')); // 判断是不是在同一个硬盘区域 var srcDevice = throwError(yield fs.stat.bind(fs, srcPath)).dev; var destDevice = throwError(yield fs.stat.bind(fs, destPath)).dev; // 在同一个硬盘区域 if (srcDevice === destDevice) { throwError(yield fs.unlink.bind(fs, destPath)); throwError(yield fs.rename.bind(fs, srcPath, destPath)); } // 在不同的硬盘区域 else { throwError(yield executeGeneratorFn.bind(null, cpGen.bind(null, srcPath, destPath))); throwError(yield fs.unlink.bind(null, srcPath)); } }
it('should call fs.unlink', function() { sinon.spy(fs, 'unlink'); var tmp = new Tempfile; tmp.unlink(); fs.unlink.getCall(0).args[0].should.eql(tmp.path); fs.unlink.restore(); });
fs.exists(outname, function (exists) { if (exists) return callback(null, stdout, stderr, cmd); parallel([ fs.unlink.bind(fs, outname + '.0'), fs.unlink.bind(fs, outname + '.2'), fs.rename.bind(fs, outname + '.1', outname) ], function (err) { callback(err, stdout, stderr, cmd); }) })
], function (err) { if (err) debug('failed to write .deb file') // remove temp files async.parallel([ fs.unlink.bind(fs, path.join(tempPath, 'control.tar.gz')), fs.unlink.bind(fs, path.join(tempPath, 'data.tar.gz')), fs.unlink.bind(fs, path.join(tempPath, 'debian-binary')), ], function (err) { fs.rmdir(tempPath) done() }) })
function deleteTempFiles(bytes, cb){ // Ignore error debug('converted to binary'); async.each(usedFiles, fs.unlink.bind(fs), function(){ cb(null, bytes); }); }
afterEach(function () { fs.unlink.restore(); apiUtils.checkFileExists.restore(); apiUtils.checkFileIsValid.restore(); serverUtils.readCSV.restore(); scope.csvError = false; });
function * rmdirGen(dir) { var stack = [dir]; var dirs = [dir]; while (stack.length) { var top = stack.pop(); var files = throwError(yield fs.readdir.bind(fs, top)); for (var i = 0, il = files.length; i < il; i += 1) { var fullPath = path.join(top, files[i]); var stats = throwError(yield fs.stat.bind(fs, fullPath)); if (stats.isDirectory()) { stack.push(fullPath); dirs.push(fullPath); } else { throwError(yield fs.unlink.bind(fs, fullPath)); } } } for (var i = dirs.length - 1; i >= 0; i -= 1) { throwError(yield fs.rmdir.bind(fs, dirs[i])); } }
describe('basic test', function() { before(function(done) { var logger = accesslog({path: tmpLog}); http.createServer(function(req, res) { logger(req, res, function() { var content = JSON.stringify({'hello': 'world'}); res.writeHead(200, {'Content-Type': 'application/json', 'Content-Length': content.length}); res.write(content); res.end(); }); }).listen(port, done); }); after(fs.unlink.bind(null, tmpLog)); it('performs a request', function(done) { request({url: baseUrl + '/test/url', json: true}, function(err, res, data) { assert.equal(res.statusCode, 200); assert.strictEqual(data.hello, 'world'); done(); }); }); it('can tail the log', function(done) { exec('tail ' + tmpLog, function(err, stdout, stderr) { assert(/^127\.0\.0\.1 \- \- \[[0-9]{2}\/\w{3}\/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2} (\+|\-)[0-9]{4}\] "GET \/test\/url HTTP\/1\.1" 200 17 "\-" "\-"\n$/.exec(stdout)); done(); }); }); });
it("should throw other errors", function () { fs.unlink.yields({ code: "FOO" }); /*jshint immed:false */ (function () { pidfiles.removePidFile(PIDS_DIR, "worker1"); }).should.throwError(); });
constructor() { this.mkdirp = mkdirp; this.mkdir = fs.mkdir.bind(fs); this.rmdir = fs.rmdir.bind(fs); this.unlink = fs.unlink.bind(fs); this.writeFile = fs.writeFile.bind(fs); this.join = path.join.bind(path); }
beforeAll(() => { fs.mkdir.mockImplementation((p, opts, cb) => (cb || opts)(undefined)); fs.readdir.mockImplementation((d, opts, cb) => (cb || opts)(undefined, [])); fs.rmdir.mockImplementation((p, cb) => cb(undefined)); fs.readFile.mockImplementation((f, opts, cb) => (cb || opts)(undefined, mockFileContents)); fs.rename.mockImplementation((a, b, cb) => cb(undefined)); fs.unlink.mockImplementation((f, cb) => cb(undefined)); fs.writeFile.mockImplementation((f, d, opts, cb) => (cb || opts)(undefined)); });
beforeAll(() => { fs.mkdir.mockImplementation((p, cb) => cb(mockErr)); fs.readdir.mockImplementation((d, opts, cb) => (cb || opts)(mockErr)); fs.rmdir.mockImplementation((p, cb) => cb(mockErr)); fs.readFile.mockImplementation((f, cb) => cb(mockErr)); fs.rename.mockImplementation((a, b, cb) => cb(mockErr)); fs.unlink.mockImplementation((f, cb) => cb(mockErr)); fs.writeFile.mockImplementation((f, d, cb) => cb(mockErr)); });
killed: function* (task) { //this.stream.end(); // Ensure the stream is completely written prior to uploading the temp file. yield streamClosed(this.stream); var queue = task.runtime.queue; // Create date when this artifact should expire (see config). var expiration = new Date(Date.now() + task.runtime.logging.bulkLogExpires); var artifact = yield queue.createArtifact( task.status.taskId, task.runId, this.artifactName, { // Why s3? It's currently cheaper to store data in s3 this could easily // be used with azure simply by changing s3 -> azure. storageType: 's3', expires: expiration.toJSON(), contentType: 'text/plain' } ); var stat = yield fs.stat.bind(fs, this.file.path); // Open a new stream to read the entire log from disk (this in theory could // be a huge file). var diskStream = fs.createReadStream(this.file.path); // Stream the entire file to S3 it's important to set the content length and // content type (in particular the content-type must be identical to what is // sent over in the artifact creation.) var req = request.put(artifact.putUrl).set({ 'Content-Type': 'text/plain', 'Content-Length': stat.size, 'Content-Encoding': 'gzip' }); diskStream.pipe(req); req.end(); // Wait until the request has completed and the file has been uploaded... var result = yield waitForEvent(req, 'end'); // Unlink the temp file. yield fs.unlink.bind(fs, this.file.path); var url = queue.buildUrl( queue.getArtifact, task.status.taskId, task.runId, this.artifactName ); return url; }
it("completes and calls fs.existsSync, fs.unlink", async function() { expect.assertions(2); fs.existsSync.mockImplementation(() => true); fs.unlink.mockImplementation((path, callback) => callback()); await unlinkObservable("path2").toPromise(); expect(fs.existsSync).toBeCalledWith("path2"); expect(fs.unlink).toBeCalled(); });
beforeAll(() => { const throwErr = () => { throw mockErr; }; fs.mkdir.mockImplementation(throwErr); fs.readdir.mockImplementation(throwErr); fs.rmdir.mockImplementation(throwErr); fs.readFile.mockImplementation(throwErr); fs.rename.mockImplementation(throwErr); fs.unlink.mockImplementation(throwErr); fs.writeFile.mockImplementation(throwErr); });
it("completes and calls fs.existsSync, fs.unlink", () => { expect.assertions(2); fs.existsSync.mockImplementation(() => true); fs.unlink.mockImplementation(() => true); unlinkObservable("path2") .pipe(toArray()) .subscribe(() => {}, err => done.fail(err), () => {}); expect(fs.existsSync).toBeCalledWith("path2"); expect(fs.unlink).toBeCalled(); });
it("should not require callback", function () { fs.unlink .withArgs(path.join(PIDS_DIR, "worker1.pid")) .yields(null); /*jshint immed:false */ (function () { pidfiles.removePidFile(PIDS_DIR, "worker1"); }).should.not.throwError(); });
it("should pass errors to callback", function () { fs.unlink .withArgs(path.join(PIDS_DIR, "worker1.pid"), sinon.match.func) .yields({ code: "ENOENT" }); fs.unlink .withArgs(path.join(PIDS_DIR, "worker2.pid"), sinon.match.func) .yields({ code: "FOO" }); pidfiles.removePidFile(PIDS_DIR, "worker1", function (err) { should.exist(err); err.should.have.property("code", "ENOENT"); }); pidfiles.removePidFile(PIDS_DIR, "worker2", function (err) { should.exist(err); err.should.have.property("code", "FOO"); }); });
it("should log ENOENT errors", function () { var consoleError = sinon.stub(console, "error"); fs.unlink.yields({ code: "ENOENT" }); pidfiles.removePidFile(PIDS_DIR, "worker1"); consoleError.calledOnce.should.be.ok; consoleError.calledWith('Could not find pidfile: %s', "worker1.pid"); consoleError.restore(); });
fs.readdir(dir, function (err, files) { if (err) return callback(null); var garbage = []; files.forEach(function (file) { if (file.match(/^tmp_/)) garbage.push(path.join(dir, file)); }); forEachNParallel(5, garbage, fs.unlink.bind(fs), callback); });
it('should log an error if there is a problem copying the vast zip in s3', (done) => { awsClient.copyS3Object.throws(new Error('copy s3 object test error')); fs.unlink .callsFake((dir, cb) => { cb(null); }); ctrl(mockServer).then(() => { expect(mockServer.log).to.have.been.calledWith('error', 'Error in cleanup step - copy s3 object test error'); done(); }); });
function dataBagSet(bagName, obj, options, callback) { if (!callback) { callback = options; options = null; } var filename = sprintf('/tmp/databag-%s.json', misc.randstr(8)); async.series([ fs.writeFile.bind(null, filename, JSON.stringify(obj)), dataBagFromFile.bind(null, bagName, filename, options), fs.unlink.bind(null, filename) ], callback); }
CertificateAuthority.prototype.createSelfSignedCertificate = function(certificate_file,private_key,params,callback) { var self = this; assert(typeof private_key === "string"); assert(fs.existsSync(private_key)); assert(_.isFunction(callback)); if (!toolbox.check_certificate_filename(certificate_file,params)) { return callback(); } toolbox.adjustDate(params); toolbox.adjustApplicationUri(params); toolbox.processAltNames(params); var csr_file = certificate_file + "_csr"; assert(csr_file); var configFile = toolbox.generateStaticConfig(self.configFile); var options = {cwd: self.rootDir, openssl_conf: toolbox.make_path(configFile) }; //xx this require OPENSSL_CONF to be set //xx assert(fs.existsSync(process.env.OPENSSL_CONF)); var configOption = ""; var tasks = []; tasks.push(displaySubtitle.bind(null, "- the certificate signing request")); tasks.push(execute_openssl.bind(null, "req " + " -new -sha256 -text " + configOption + " -batch -key " + q(n(private_key)) + " -out " + q(n(csr_file)), options)); tasks.push(displaySubtitle.bind(null, "- creating the self signed certificate")); tasks.push(execute_openssl.bind(null, "ca " + " -selfsign " + " -keyfile " + q(n(private_key)) + " -startdate " + toolbox.x509Date(params.startDate) + " -enddate " + toolbox.x509Date(params.endDate) + " -batch -out " + q(n(certificate_file)) + " -in " + q(n(csr_file)), options)); tasks.push(displaySubtitle.bind(null, "- dump the certificate for a check")); tasks.push(execute_openssl.bind(null, "x509 -in " + q(n(certificate_file)) + " -dates -fingerprint -purpose -noout", {})); tasks.push(displaySubtitle.bind(null, "- verify self-signed certificate")); tasks.push(toolbox.execute_openssl_no_failure.bind(null, "verify -verbose -CAfile " + q(n(certificate_file)) + " " + q(n(certificate_file)), options)); tasks.push(fs.unlink.bind(null,csr_file)); async.series(tasks, function(err){ callback(err); }); };
it('should log an error if there is a problem removing the local files', (done) => { awsClient.copyS3Object.resolves(); awsClient.deleteS3Object.resolves(); fs.unlink .callsFake((dir, cb) => { cb(new Error('remove local files test error')); }); ctrl(mockServer).then(() => { expect(mockServer.log).to.have.been.calledWith('error', 'Error removing local file - remove local files test error'); done(); }); });
setTimeout(function() { try { var logEvents = helper.log().args.filter(function(evt) { return evt[0].type == "file"; }); //console.log(logEvents); logEvents.should.have.length(1); logEvents[0][0].should.have.a.property('msg'); logEvents[0][0].msg.toString().should.startWith("Failed to delete"); done(); } catch(e) { done(e); } finally { fs.unlink.restore(); } },wait);
it("errors on unlink issue", () => { expect.assertions(3); fs.existsSync.mockImplementation(() => true); fs.unlink.mockImplementation((path, error) => error({ message: "lol" })); unlinkObservable("path").subscribe( () => {}, err => { expect(err.message).toBe("lol"); }, () => done.fail() ); expect(fs.existsSync).toBeCalledWith("path"); expect(fs.unlink).toBeCalled(); });
it('should unlink the file and call the callback with the error on a connect err', function() { httpOperations.download('/tmp', 100, false, callback); stub(fs, 'unlink'); httpOperations.watchUpload.args[0][2](); calledOnceWith(onError, 'error'); onError.args[0][1]('An Error'); calledOnceWith(fs.unlink, '/tmp/test_file.zip'); calledOnceWith(callback, 'An Error'); fs.unlink.restore(); });
return function (request, response) { async.waterfall([ async.series.bind(async, [ Student.get.bind(Student, request.params.id), fs.readFile.bind(fs, request.files['0'].path) ]), function (results, callback) { var student = results[0], fileContents = results[1]; student[documentName] = fileContents; student.save(callback); }, fs.unlink.bind(fs, request.files['0'].path) ], response.handle()); };
beforeEach((done) => { awsClient.listS3Objects.resolves(mockS3ListResponse); awsClient.getS3Object.resolves(mockS3ObjectResponse); awsClient.getSecret.resolves(mockPgSecret); awsClient.copyS3Object.resolves(); awsClient.deleteS3Object.resolves(); cssClient.getCustomerIdsByProductCode.resolves(mockDealerResponse); pgClient.connect.resolves(); pgClient.select.resolves(mockVinResponse); pgClient._exec.resolves(); onStub.callsFake((event, cb) => { cb(); }); etl.csv_parser.callsFake(() => {}); etl.map.callsFake(() => {}); etl.postgres.upsert.callsFake(() => {}); pipeStub .onFirstCall() .returns({ on: onStub }) .onSecondCall() .returns({ pipe: pipeStub }) .onThirdCall() .callsFake(({ _fn }) => { _fn(mockVastRow); return { pipe: pipeStub }; }) .onCall(3) .returns({ on: onStub }); fs.createReadStream .onFirstCall() .returns({ pipe: pipeStub }) .onSecondCall() .returns({ pipe: pipeStub }); unzip.Extract.returns(); fs.writeFile.callsFake((dir, res, cb) => { cb(null); }); fs.readdir .callsFake((dir, cb) => { cb(null, [ 'test-data.tsv' ]); }); fs.unlink .callsFake((dir, cb) => { cb(null); }); ctrl(mockServer).then(done); });
module.exports.checkPublicDirReachable = function (app, dir, url, done) { var secret = Math.random().toString().substr(2); async.series([ fs.writeFile.bind(null, dir + secret, secret), request.bind(null, url + secret), fs.unlink.bind(null, dir + secret), ], function (err, results) { if (err) { done(err); } if (results[1].length && results[1][0].body !== secret) { app.cli.info(results[1][0].body); done("retrieved file is not expected."); } done(null); }); };