From 1a7858c9f11bdeb4096bfc76fc62d16b0f05202d Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 25 May 2016 14:49:21 +0200 Subject: [PATCH] Log a more informative message when cannot recover a file --- spec/main-process/file-recovery-service.spec.js | 16 ++++++++-------- src/main-process/file-recovery-service.js | 7 +++++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/spec/main-process/file-recovery-service.spec.js b/spec/main-process/file-recovery-service.spec.js index 1cc0062f1..e18542ea8 100644 --- a/spec/main-process/file-recovery-service.spec.js +++ b/spec/main-process/file-recovery-service.spec.js @@ -5,9 +5,10 @@ import FileRecoveryService from '../../src/main-process/file-recovery-service' import temp from 'temp' import fs from 'fs-plus' import {Emitter} from 'event-kit' +import sinon from 'sinon' describe("FileRecoveryService", () => { - let recoveryService, recoveryDirectory, windows + let recoveryService, recoveryDirectory, windows, previousConsoleLog function createWindow () { 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 filePath = temp.path() fs.writeFileSync(filePath, "content") fs.chmodSync(filePath, 0444) - const previousConsoleLog = console.log let logs = [] - console.log = (message) => logs.push(message) + this.stub(console, 'log', (message) => logs.push(message)) recoveryService.willSavePath({sender: mockWindow.webContents}, filePath) mockWindow.webContents.emit("crashed") let recoveryFiles = fs.listTreeSync(recoveryDirectory) assert.equal(recoveryFiles.length, 1) - assert.deepEqual(logs, [`Cannot recover ${filePath}. A recovery file has been saved here: ${recoveryFiles[0]}.`]) - - console.log = previousConsoleLog - }) + assert.equal(logs.length, 1) + assert.match(logs[0], new RegExp(filePath)) + 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", () => { diff --git a/src/main-process/file-recovery-service.js b/src/main-process/file-recovery-service.js index 89106a610..142e86579 100644 --- a/src/main-process/file-recovery-service.js +++ b/src/main-process/file-recovery-service.js @@ -1,6 +1,6 @@ 'use babel' -import {BrowserWindow, ipcMain} from 'electron' +import {BrowserWindow, dialog, ipcMain} from 'electron' import crypto from 'crypto' import Path from 'path' import fs from 'fs-plus' @@ -113,7 +113,10 @@ export default class FileRecoveryService { try { recoveryFile.recoverSync() } 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 { this.recoveryFilesByFilePath.delete(recoveryFile.originalPath) }