From 3851f5510f3f976fc12374f3e6269b66270ad692 Mon Sep 17 00:00:00 2001 From: James Allen Date: Wed, 2 Apr 2014 12:53:02 +0100 Subject: [PATCH] Use system rm -r to allow removal of files with broken char encodings --- app/coffee/CompileManager.coffee | 21 +++++++-- package.json | 1 - test/unit/coffee/CompileManagerTests.coffee | 49 +++++++++++++++++---- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/app/coffee/CompileManager.coffee b/app/coffee/CompileManager.coffee index 7871047..dce6a86 100644 --- a/app/coffee/CompileManager.coffee +++ b/app/coffee/CompileManager.coffee @@ -5,7 +5,7 @@ Settings = require("settings-sharelatex") Path = require "path" logger = require "logger-sharelatex" Metrics = require "./Metrics" -rimraf = require "rimraf" +child_process = require "child_process" module.exports = CompileManager = doCompile: (request, callback = (error, outputFiles) ->) -> @@ -34,6 +34,21 @@ module.exports = CompileManager = return callback(error) if error? callback null, outputFiles - clearProject: (project_id, callback = (error) ->) -> + clearProject: (project_id, _callback = (error) ->) -> + callback = (error) -> + _callback(error) + _callback = () -> + compileDir = Path.join(Settings.path.compilesDir, project_id) - rimraf compileDir, callback + proc = child_process.spawn "rm", ["-r", compileDir] + + proc.on "error", callback + + stderr = "" + proc.stderr.on "data", (chunk) -> stderr += chunk.toString() + + proc.on "close", (code) -> + if code == 0 + return callback(null) + else + return callback(new Error("rm -r #{compileDir} failed: #{stderr}")) diff --git a/package.json b/package.json index db76807..0fdc15a 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "mkdirp": "0.3.5", "mysql": "2.0.0-alpha7", "request": "~2.21.0", - "rimraf": "2.1.4", "logger-sharelatex": "git+https://github.com/sharelatex/logger-sharelatex.git#master", "settings-sharelatex": "git+https://github.com/sharelatex/settings-sharelatex.git#master", "sequelize": "~2.0.0-beta.2", diff --git a/test/unit/coffee/CompileManagerTests.coffee b/test/unit/coffee/CompileManagerTests.coffee index 204da11..6c75835 100644 --- a/test/unit/coffee/CompileManagerTests.coffee +++ b/test/unit/coffee/CompileManagerTests.coffee @@ -3,6 +3,7 @@ sinon = require('sinon') require('chai').should() modulePath = require('path').join __dirname, '../../../app/js/CompileManager' tk = require("timekeeper") +EventEmitter = require("events").EventEmitter describe "CompileManager", -> beforeEach -> @@ -12,7 +13,7 @@ describe "CompileManager", -> "./OutputFileFinder": @OutputFileFinder = {} "settings-sharelatex": @Settings = { path: compilesDir: "/compiles/dir" } "logger-sharelatex": @logger = { log: sinon.stub() } - "rimraf": @rimraf = sinon.stub().callsArg(1) + "child_process": @child_process = {} @callback = sinon.stub() describe "doCompile", -> @@ -61,13 +62,43 @@ describe "CompileManager", -> @callback.calledWith(null, @output_files).should.equal true describe "clearProject", -> - beforeEach -> - @Settings.compileDir = "compiles" - @CompileManager.clearProject @project_id, @callback + describe "succesfully", -> + beforeEach -> + @Settings.compileDir = "compiles" + @proc = new EventEmitter() + @proc.stdout = new EventEmitter() + @proc.stderr = new EventEmitter() + @child_process.spawn = sinon.stub().returns(@proc) + @CompileManager.clearProject @project_id, @callback + @proc.emit "close", 0 - it "should remove the project directory", -> - @rimraf.calledWith("#{@Settings.path.compilesDir}/#{@project_id}") - .should.equal true + it "should remove the project directory", -> + @child_process.spawn + .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"]) + .should.equal true - it "should call the callback", -> - @callback.called.should.equal true + it "should call the callback", -> + @callback.called.should.equal true + + describe "with a non-success status code", -> + beforeEach -> + @Settings.compileDir = "compiles" + @proc = new EventEmitter() + @proc.stdout = new EventEmitter() + @proc.stderr = new EventEmitter() + @child_process.spawn = sinon.stub().returns(@proc) + @CompileManager.clearProject @project_id, @callback + @proc.stderr.emit "data", @error = "oops" + @proc.emit "close", 1 + + it "should remove the project directory", -> + @child_process.spawn + .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"]) + .should.equal true + + it "should call the callback with an error from the stderr", -> + @callback + .calledWith(new Error()) + .should.equal true + + @callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_id} failed: #{@error}"