Use system rm -r to allow removal of files with broken char encodings

This commit is contained in:
James Allen 2014-04-02 12:53:02 +01:00
parent 841d1cb872
commit b484f08d6e
3 changed files with 58 additions and 13 deletions

View file

@ -5,7 +5,7 @@ Settings = require("settings-sharelatex")
Path = require "path" Path = require "path"
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
Metrics = require "./Metrics" Metrics = require "./Metrics"
rimraf = require "rimraf" child_process = require "child_process"
module.exports = CompileManager = module.exports = CompileManager =
doCompile: (request, callback = (error, outputFiles) ->) -> doCompile: (request, callback = (error, outputFiles) ->) ->
@ -34,6 +34,21 @@ module.exports = CompileManager =
return callback(error) if error? return callback(error) if error?
callback null, outputFiles 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) 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}"))

View file

@ -10,7 +10,6 @@
"mkdirp": "0.3.5", "mkdirp": "0.3.5",
"mysql": "2.0.0-alpha7", "mysql": "2.0.0-alpha7",
"request": "~2.21.0", "request": "~2.21.0",
"rimraf": "2.1.4",
"logger-sharelatex": "git+https://github.com/sharelatex/logger-sharelatex.git#master", "logger-sharelatex": "git+https://github.com/sharelatex/logger-sharelatex.git#master",
"settings-sharelatex": "git+https://github.com/sharelatex/settings-sharelatex.git#master", "settings-sharelatex": "git+https://github.com/sharelatex/settings-sharelatex.git#master",
"sequelize": "~2.0.0-beta.2", "sequelize": "~2.0.0-beta.2",

View file

@ -3,6 +3,7 @@ sinon = require('sinon')
require('chai').should() require('chai').should()
modulePath = require('path').join __dirname, '../../../app/js/CompileManager' modulePath = require('path').join __dirname, '../../../app/js/CompileManager'
tk = require("timekeeper") tk = require("timekeeper")
EventEmitter = require("events").EventEmitter
describe "CompileManager", -> describe "CompileManager", ->
beforeEach -> beforeEach ->
@ -12,7 +13,7 @@ describe "CompileManager", ->
"./OutputFileFinder": @OutputFileFinder = {} "./OutputFileFinder": @OutputFileFinder = {}
"settings-sharelatex": @Settings = { path: compilesDir: "/compiles/dir" } "settings-sharelatex": @Settings = { path: compilesDir: "/compiles/dir" }
"logger-sharelatex": @logger = { log: sinon.stub() } "logger-sharelatex": @logger = { log: sinon.stub() }
"rimraf": @rimraf = sinon.stub().callsArg(1) "child_process": @child_process = {}
@callback = sinon.stub() @callback = sinon.stub()
describe "doCompile", -> describe "doCompile", ->
@ -61,13 +62,43 @@ describe "CompileManager", ->
@callback.calledWith(null, @output_files).should.equal true @callback.calledWith(null, @output_files).should.equal true
describe "clearProject", -> describe "clearProject", ->
beforeEach -> describe "succesfully", ->
@Settings.compileDir = "compiles" beforeEach ->
@CompileManager.clearProject @project_id, @callback @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", -> it "should remove the project directory", ->
@rimraf.calledWith("#{@Settings.path.compilesDir}/#{@project_id}") @child_process.spawn
.should.equal true .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"])
.should.equal true
it "should call the callback", -> it "should call the callback", ->
@callback.called.should.equal true @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}"