Log a more informative message when cannot recover a file

This commit is contained in:
Antonio Scandurra 2016-05-25 14:49:21 +02:00
parent c6a87b956e
commit 1a7858c9f1
2 changed files with 13 additions and 10 deletions

View File

@ -5,9 +5,10 @@ import FileRecoveryService from '../../src/main-process/file-recovery-service'
import temp from 'temp' import temp from 'temp'
import fs from 'fs-plus' import fs from 'fs-plus'
import {Emitter} from 'event-kit' import {Emitter} from 'event-kit'
import sinon from 'sinon'
describe("FileRecoveryService", () => { describe("FileRecoveryService", () => {
let recoveryService, recoveryDirectory, windows let recoveryService, recoveryDirectory, windows, previousConsoleLog
function createWindow () { function createWindow () {
const window = new BrowserWindow({show: false}) const window = new BrowserWindow({show: false})
@ -116,24 +117,23 @@ describe("FileRecoveryService", () => {
}) })
}) })
it("emits a warning when a file can't be recovered", () => { it("emits a warning when a file can't be recovered", sinon.test(function () {
const mockWindow = createWindow() const mockWindow = createWindow()
const filePath = temp.path() const filePath = temp.path()
fs.writeFileSync(filePath, "content") fs.writeFileSync(filePath, "content")
fs.chmodSync(filePath, 0444) fs.chmodSync(filePath, 0444)
const previousConsoleLog = console.log
let logs = [] let logs = []
console.log = (message) => logs.push(message) this.stub(console, 'log', (message) => logs.push(message))
recoveryService.willSavePath({sender: mockWindow.webContents}, filePath) recoveryService.willSavePath({sender: mockWindow.webContents}, filePath)
mockWindow.webContents.emit("crashed") mockWindow.webContents.emit("crashed")
let recoveryFiles = fs.listTreeSync(recoveryDirectory) let recoveryFiles = fs.listTreeSync(recoveryDirectory)
assert.equal(recoveryFiles.length, 1) assert.equal(recoveryFiles.length, 1)
assert.deepEqual(logs, [`Cannot recover ${filePath}. A recovery file has been saved here: ${recoveryFiles[0]}.`]) assert.equal(logs.length, 1)
assert.match(logs[0], new RegExp(filePath))
console.log = previousConsoleLog assert.match(logs[0], new RegExp(recoveryFiles[0]))
}) }))
}) })
it("doesn't create a recovery file when the file that's being saved doesn't exist yet", () => { it("doesn't create a recovery file when the file that's being saved doesn't exist yet", () => {

View File

@ -1,6 +1,6 @@
'use babel' 'use babel'
import {BrowserWindow, ipcMain} from 'electron' import {BrowserWindow, dialog, ipcMain} from 'electron'
import crypto from 'crypto' import crypto from 'crypto'
import Path from 'path' import Path from 'path'
import fs from 'fs-plus' import fs from 'fs-plus'
@ -113,7 +113,10 @@ export default class FileRecoveryService {
try { try {
recoveryFile.recoverSync() recoveryFile.recoverSync()
} catch (error) { } catch (error) {
console.log(`Cannot recover ${recoveryFile.originalPath}. A recovery file has been saved here: ${recoveryFile.recoveryPath}.`) console.log(
`There was a crash while saving "${recoveryFile.originalPath}", so this file might be blank or corrupted.\n` +
`Atom couldn't recover it automatically, but a recovery file has been saved at: "${recoveryFile.recoveryPath}".`
)
} finally { } finally {
this.recoveryFilesByFilePath.delete(recoveryFile.originalPath) this.recoveryFilesByFilePath.delete(recoveryFile.originalPath)
} }