From c184aadc2c0aea6339993b8610d470b9e206832d Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Sun, 28 Jun 2020 11:45:28 -0500 Subject: [PATCH] fix: restore folders that don't exist locally --- lib/sync-settings.js | 3 +-- spec/sync-settings-spec.js | 51 +++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/lib/sync-settings.js b/lib/sync-settings.js index 9d02a123..a0e79904 100644 --- a/lib/sync-settings.js +++ b/lib/sync-settings.js @@ -301,8 +301,7 @@ module.exports = class SyncSettings { if (backupData.files) { for (const fileName in backupData.files) { const file = backupData.files[fileName] - // TODO: create folder if path contains subfolder - await fs.writeFile(file.path, file.content) + await fs.outputFile(file.path, file.content) } } diff --git a/spec/sync-settings-spec.js b/spec/sync-settings-spec.js index c6e20711..3e58fb04 100644 --- a/spec/sync-settings-spec.js +++ b/spec/sync-settings-spec.js @@ -4,7 +4,6 @@ const gistApi = require('./gist-api-mock') const { config } = require('../lib/config') const utils = require('../lib/utils/utils') const fs = require('fs-extra') -const tryUnlink = (...args) => fs.unlink(...args).catch(() => {}) const path = require('path') function setDefaultSettings (namespace, settings) { @@ -38,10 +37,10 @@ describe('syncSettings', () => { afterEach(async () => { await backupLocation.delete() - await tryUnlink(atom.keymaps.getUserKeymapPath()) - await tryUnlink(atom.styles.getUserStyleSheetPath()) - await tryUnlink(atom.getUserInitScriptPath()) - await tryUnlink(path.join(atom.getConfigDirPath(), 'snippets.cson')) + await fs.remove(atom.keymaps.getUserKeymapPath()) + await fs.remove(atom.styles.getUserStyleSheetPath()) + await fs.remove(atom.getUserInitScriptPath()) + await fs.remove(path.join(atom.getConfigDirPath(), 'snippets.cson')) }) describe('backup', () => { @@ -236,7 +235,7 @@ describe('syncSettings', () => { expect(data.files['test2.tmp']).toBeDefined() } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) @@ -257,7 +256,7 @@ describe('syncSettings', () => { expect(data.files['test2.tmp']).toBeDefined() } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) @@ -279,7 +278,7 @@ describe('syncSettings', () => { expect(data.files['test2.tmp']).not.toBeDefined() } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) @@ -301,7 +300,7 @@ describe('syncSettings', () => { expect(data.files['test2.tmp']).not.toBeDefined() } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) @@ -521,7 +520,7 @@ describe('syncSettings', () => { } } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) @@ -541,7 +540,7 @@ describe('syncSettings', () => { } } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) @@ -560,7 +559,7 @@ describe('syncSettings', () => { expect(fs.existsSync(`${atom.getConfigDirPath()}/test2.tmp`)).toBe(false) } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) @@ -580,7 +579,7 @@ describe('syncSettings', () => { expect(fs.existsSync(`${atom.getConfigDirPath()}/test2.tmp`)).toBe(false) } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) @@ -594,6 +593,11 @@ describe('syncSettings', () => { } await syncSettings.backup() + + for (const file of files) { + await fs.unlink(path.join(atom.getConfigDirPath(), file)) + } + await syncSettings.restore() for (const file of files) { @@ -602,11 +606,30 @@ describe('syncSettings', () => { } } finally { for (const file of files) { - await tryUnlink(`${atom.getConfigDirPath()}/${file}`) + await fs.remove(`${atom.getConfigDirPath()}/${file}`) } } }) + it('restores folder in backup that does not exist locally', async () => { + const files = ['test/test.tmp'] + atom.config.set('sync-settings.extraFiles', files) + const folderPath = path.join(atom.getConfigDirPath(), 'test') + const filePath = path.join(folderPath, 'test.tmp') + try { + await fs.outputFile(filePath, 'test/test.tmp') + await syncSettings.backup() + await fs.remove(folderPath) + + await syncSettings.restore() + + expect(fs.existsSync(filePath)).toBe(true) + expect((await utils.fileContent(filePath)).toString()).toBe('test/test.tmp') + } finally { + await fs.remove(folderPath) + } + }) + it('skips the restore due to invalid json', async () => { atom.config.set('sync-settings.syncSettings', true) atom.config.set('some-dummy', false)