Skip to content

Commit

Permalink
fix: restore folders that don't exist locally
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Jun 28, 2020
1 parent 5215187 commit c184aad
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
3 changes: 1 addition & 2 deletions lib/sync-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
51 changes: 37 additions & 14 deletions spec/sync-settings-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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}`)
}
}
})
Expand All @@ -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}`)
}
}
})
Expand All @@ -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}`)
}
}
})
Expand All @@ -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}`)
}
}
})
Expand Down Expand Up @@ -521,7 +520,7 @@ describe('syncSettings', () => {
}
} finally {
for (const file of files) {
await tryUnlink(`${atom.getConfigDirPath()}/${file}`)
await fs.remove(`${atom.getConfigDirPath()}/${file}`)
}
}
})
Expand All @@ -541,7 +540,7 @@ describe('syncSettings', () => {
}
} finally {
for (const file of files) {
await tryUnlink(`${atom.getConfigDirPath()}/${file}`)
await fs.remove(`${atom.getConfigDirPath()}/${file}`)
}
}
})
Expand All @@ -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}`)
}
}
})
Expand All @@ -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}`)
}
}
})
Expand All @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit c184aad

Please sign in to comment.