|
1 |
| -import { promises as fs } from 'fs'; |
2 |
| -import path from 'path'; |
3 |
| -import { serial as anyTest, TestInterface } from 'ava'; |
4 |
| -import { Driver, createDriver } from './driver'; |
| 1 | +import { promises as fs } from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { serial as anyTest, TestInterface } from 'ava' |
| 4 | +import { Driver, createDriver } from './driver' |
5 | 5 |
|
6 |
| -const test = anyTest as TestInterface<Driver>; |
| 6 | +const test = anyTest as TestInterface<Driver> |
7 | 7 |
|
8 |
| -const BackupsDirectoryName = 'Standard Notes Backups'; |
| 8 | +const BackupsDirectoryName = 'Standard Notes Backups' |
9 | 9 |
|
10 | 10 | test.beforeEach(async (t) => {
|
11 |
| - t.context = await createDriver(); |
12 |
| - const backupsLocation = await t.context.backups.location(); |
13 |
| - await fs.rmdir(backupsLocation, { recursive: true }); |
14 |
| - await t.context.backups.copyDecryptScript(backupsLocation); |
15 |
| -}); |
| 11 | + t.context = await createDriver() |
| 12 | + const backupsLocation = await t.context.backups.location() |
| 13 | + await fs.rmdir(backupsLocation, { recursive: true }) |
| 14 | + await t.context.backups.copyDecryptScript(backupsLocation) |
| 15 | +}) |
16 | 16 | test.afterEach.always(async (t) => {
|
17 |
| - await t.context.stop(); |
18 |
| -}); |
| 17 | + await t.context.stop() |
| 18 | +}) |
19 | 19 |
|
20 | 20 | /**
|
21 | 21 | * Depending on the current system load, performing a backup
|
22 | 22 | * might take a while
|
23 | 23 | */
|
24 |
| -const timeoutDuration = 20 * 1000; /** 20s */ |
| 24 | +const timeoutDuration = 20 * 1000 /** 20s */ |
25 | 25 |
|
26 | 26 | function wait(duration = 1000) {
|
27 |
| - return new Promise((resolve) => setTimeout(resolve, duration)); |
| 27 | + return new Promise((resolve) => setTimeout(resolve, duration)) |
28 | 28 | }
|
29 | 29 |
|
30 | 30 | test('saves incoming data to the backups folder', async (t) => {
|
31 |
| - const data = 'Sample Data'; |
32 |
| - const fileName = await t.context.backups.save(data); |
33 |
| - const backupsLocation = await t.context.backups.location(); |
34 |
| - const files = await fs.readdir(backupsLocation); |
35 |
| - t.true(files.includes(fileName)); |
36 |
| - t.is(data, await fs.readFile(path.join(backupsLocation, fileName), 'utf8')); |
37 |
| -}); |
| 31 | + const data = 'Sample Data' |
| 32 | + const fileName = await t.context.backups.save(data) |
| 33 | + const backupsLocation = await t.context.backups.location() |
| 34 | + const files = await fs.readdir(backupsLocation) |
| 35 | + t.true(files.includes(fileName)) |
| 36 | + t.is(data, await fs.readFile(path.join(backupsLocation, fileName), 'utf8')) |
| 37 | +}) |
38 | 38 |
|
39 | 39 | test('saves the decrypt script to the backups folder', async (t) => {
|
40 |
| - const backupsLocation = await t.context.backups.location(); |
41 |
| - await wait(300); /** Disk might be busy */ |
42 |
| - const files = await fs.readdir(backupsLocation); |
43 |
| - t.true(files.includes('decrypt.html')); |
44 |
| -}); |
| 40 | + const backupsLocation = await t.context.backups.location() |
| 41 | + await wait(300) /** Disk might be busy */ |
| 42 | + const files = await fs.readdir(backupsLocation) |
| 43 | + t.true(files.includes('decrypt.html')) |
| 44 | +}) |
45 | 45 |
|
46 | 46 | test('performs a backup', async (t) => {
|
47 |
| - t.timeout(timeoutDuration); |
48 |
| - await wait(); |
49 |
| - await t.context.backups.perform(); |
50 |
| - const backupsLocation = await t.context.backups.location(); |
51 |
| - const files = await fs.readdir(backupsLocation); |
52 |
| - t.true(files.length >= 1); |
53 |
| -}); |
| 47 | + t.timeout(timeoutDuration) |
| 48 | + await wait() |
| 49 | + await t.context.backups.perform() |
| 50 | + const backupsLocation = await t.context.backups.location() |
| 51 | + const files = await fs.readdir(backupsLocation) |
| 52 | + t.true(files.length >= 1) |
| 53 | +}) |
54 | 54 |
|
55 | 55 | test('changes backups folder location', async (t) => {
|
56 |
| - t.timeout(timeoutDuration); |
57 |
| - await wait(); |
58 |
| - await t.context.backups.perform(); |
59 |
| - let newLocation = path.join(t.context.userDataPath, 'newLocation'); |
60 |
| - await fs.mkdir(newLocation); |
61 |
| - const currentLocation = await t.context.backups.location(); |
62 |
| - const fileNames = await fs.readdir(currentLocation); |
63 |
| - await t.context.backups.changeLocation(newLocation); |
64 |
| - newLocation = path.join(newLocation, BackupsDirectoryName); |
65 |
| - t.deepEqual(fileNames, await fs.readdir(newLocation)); |
| 56 | + t.timeout(timeoutDuration) |
| 57 | + await wait() |
| 58 | + await t.context.backups.perform() |
| 59 | + let newLocation = path.join(t.context.userDataPath, 'newLocation') |
| 60 | + await fs.mkdir(newLocation) |
| 61 | + const currentLocation = await t.context.backups.location() |
| 62 | + const fileNames = await fs.readdir(currentLocation) |
| 63 | + await t.context.backups.changeLocation(newLocation) |
| 64 | + newLocation = path.join(newLocation, BackupsDirectoryName) |
| 65 | + t.deepEqual(fileNames, await fs.readdir(newLocation)) |
66 | 66 |
|
67 | 67 | /** Assert that the setting was saved */
|
68 |
| - const data = await t.context.storage.dataOnDisk(); |
69 |
| - t.is(data.backupsLocation, newLocation); |
| 68 | + const data = await t.context.storage.dataOnDisk() |
| 69 | + t.is(data.backupsLocation, newLocation) |
70 | 70 |
|
71 | 71 | /** Perform backup and make sure there is one more file in the directory */
|
72 |
| - await t.context.backups.perform(); |
73 |
| - const newFileNames = await fs.readdir(newLocation); |
74 |
| - t.deepEqual(newFileNames.length, fileNames.length + 1); |
75 |
| -}); |
| 72 | + await t.context.backups.perform() |
| 73 | + const newFileNames = await fs.readdir(newLocation) |
| 74 | + t.deepEqual(newFileNames.length, fileNames.length + 1) |
| 75 | +}) |
76 | 76 |
|
77 | 77 | test('changes backups location to a child directory', async (t) => {
|
78 |
| - t.timeout(timeoutDuration); |
79 |
| - await wait(); |
80 |
| - await t.context.backups.perform(); |
81 |
| - const currentLocation = await t.context.backups.location(); |
82 |
| - const backups = await fs.readdir(currentLocation); |
83 |
| - t.is(backups.length, 2); /** 1 + decrypt script */ |
84 |
| - const newLocation = path.join(currentLocation, 'child_dir'); |
85 |
| - await t.context.backups.changeLocation(newLocation); |
86 |
| - t.deepEqual(await fs.readdir(path.join(newLocation, BackupsDirectoryName)), backups); |
87 |
| -}); |
| 78 | + t.timeout(timeoutDuration) |
| 79 | + await wait() |
| 80 | + await t.context.backups.perform() |
| 81 | + const currentLocation = await t.context.backups.location() |
| 82 | + const backups = await fs.readdir(currentLocation) |
| 83 | + t.is(backups.length, 2) /** 1 + decrypt script */ |
| 84 | + const newLocation = path.join(currentLocation, 'child_dir') |
| 85 | + await t.context.backups.changeLocation(newLocation) |
| 86 | + t.deepEqual(await fs.readdir(path.join(newLocation, BackupsDirectoryName)), backups) |
| 87 | +}) |
88 | 88 |
|
89 | 89 | test('changing backups location to the same directory should not do anything', async (t) => {
|
90 |
| - t.timeout(timeoutDuration); |
91 |
| - await wait(); |
92 |
| - await t.context.backups.perform(); |
93 |
| - await t.context.backups.perform(); |
94 |
| - const currentLocation = await t.context.backups.location(); |
95 |
| - let totalFiles = (await fs.readdir(currentLocation)).length; |
96 |
| - t.is(totalFiles, 3); /** 2 + decrypt script */ |
97 |
| - await t.context.backups.changeLocation(currentLocation); |
98 |
| - totalFiles = (await fs.readdir(currentLocation)).length; |
99 |
| - t.is(totalFiles, 3); |
100 |
| -}); |
| 90 | + t.timeout(timeoutDuration) |
| 91 | + await wait() |
| 92 | + await t.context.backups.perform() |
| 93 | + await t.context.backups.perform() |
| 94 | + const currentLocation = await t.context.backups.location() |
| 95 | + let totalFiles = (await fs.readdir(currentLocation)).length |
| 96 | + t.is(totalFiles, 3) /** 2 + decrypt script */ |
| 97 | + await t.context.backups.changeLocation(currentLocation) |
| 98 | + totalFiles = (await fs.readdir(currentLocation)).length |
| 99 | + t.is(totalFiles, 3) |
| 100 | +}) |
101 | 101 |
|
102 | 102 | test('backups are enabled by default', async (t) => {
|
103 |
| - t.is(await t.context.backups.enabled(), true); |
104 |
| -}); |
| 103 | + t.is(await t.context.backups.enabled(), true) |
| 104 | +}) |
105 | 105 |
|
106 | 106 | test('does not save a backup when they are disabled', async (t) => {
|
107 |
| - await t.context.backups.toggleEnabled(); |
108 |
| - await t.context.windowLoaded; |
| 107 | + await t.context.backups.toggleEnabled() |
| 108 | + await t.context.windowLoaded |
109 | 109 | /** Do not wait on this one as the backup shouldn't be triggered */
|
110 |
| - t.context.backups.perform(); |
111 |
| - await wait(); |
112 |
| - const backupsLocation = await t.context.backups.location(); |
113 |
| - const files = await fs.readdir(backupsLocation); |
114 |
| - t.deepEqual(files, ['decrypt.html']); |
115 |
| -}); |
| 110 | + t.context.backups.perform() |
| 111 | + await wait() |
| 112 | + const backupsLocation = await t.context.backups.location() |
| 113 | + const files = await fs.readdir(backupsLocation) |
| 114 | + t.deepEqual(files, ['decrypt.html']) |
| 115 | +}) |
0 commit comments