test_BackupService_finalizeSingleFileArchive.js (3507B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 let gTestSourcePath; 7 let gTestDestPath; 8 let gMatchingRegex; 9 10 add_setup(async () => { 11 gMatchingRegex = new RegExp( 12 `^${BackupService.BACKUP_FILE_NAME}_[a-z0-9-]+_[0-9_-]+.html$` 13 ); 14 gTestSourcePath = await IOUtils.createUniqueDirectory( 15 PathUtils.tempDir, 16 "testFinalizeSingleFileArchiveSource" 17 ); 18 gTestDestPath = await IOUtils.createUniqueDirectory( 19 PathUtils.tempDir, 20 "testFinalizeSingleFileArchiveDest" 21 ); 22 23 registerCleanupFunction(async () => { 24 await IOUtils.remove(gTestSourcePath, { recursive: true }); 25 await IOUtils.remove(gTestDestPath, { recursive: true }); 26 }); 27 }); 28 29 /** 30 * Utility function that writes a pretend archive file into gTestSourcePath, 31 * and then calls finalizeSingleFileArchive for it, passing 32 * gTestDestPath as the destination, and some metadata for encoding in the 33 * filename. 34 * 35 * Once the async testFn function resolves, the gTestSourcePath and 36 * gTestDestPath are cleared. 37 * 38 * @param {object} metadata 39 * The metadata to encode in the filename. See the BackupService schema for 40 * details. 41 * @param {Function} testFn 42 * An async testing function to run after calling finalizeSingleFileArchive. 43 */ 44 async function testFinalizeSingleFileArchive(metadata, testFn) { 45 let bs = new BackupService(); 46 const TEST_FILE_PATH = PathUtils.join(gTestSourcePath, "test.txt"); 47 await IOUtils.writeUTF8(TEST_FILE_PATH, "test"); 48 let movedFilePath = await bs.finalizeSingleFileArchive( 49 TEST_FILE_PATH, 50 gTestDestPath, 51 metadata 52 ); 53 let movedFile = PathUtils.filename(movedFilePath); 54 try { 55 await testFn(movedFile); 56 } finally { 57 // Clear out any files in the source and destination paths between tests. 58 let filePathsToClear = [ 59 ...(await IOUtils.getChildren(gTestSourcePath)), 60 ...(await IOUtils.getChildren(gTestDestPath)), 61 ]; 62 for (let filePath of filePathsToClear) { 63 await IOUtils.remove(filePath); 64 } 65 } 66 } 67 68 /** 69 * Tests that a single file archive will get the expected filename when moved 70 * to the destination directory. 71 */ 72 add_task(async function test_filename() { 73 await testFinalizeSingleFileArchive(FAKE_METADATA, async movedFile => { 74 Assert.ok(movedFile.match(gMatchingRegex)); 75 }); 76 }); 77 78 /** 79 * Tests that a single file archive will remove older backup files in the 80 * same directory. 81 */ 82 add_task(async function test_remove_old_files() { 83 const OLDER_BACKUP = PathUtils.join( 84 gTestDestPath, 85 `FirefoxBackup_${FAKE_METADATA.profileName}_20200101-0000.html` 86 ); 87 await IOUtils.writeUTF8(OLDER_BACKUP, "test"); 88 89 await testFinalizeSingleFileArchive(FAKE_METADATA, async movedFile => { 90 Assert.ok(movedFile.match(gMatchingRegex)); 91 Assert.ok( 92 !(await IOUtils.exists(OLDER_BACKUP)), 93 "Older backup was deleted." 94 ); 95 }); 96 }); 97 98 /** 99 * Tests that a single file archive will not remove older backup files for 100 * other profiles. 101 */ 102 add_task(async function test_remove_old_files_other_profile() { 103 const OLDER_BACKUP = PathUtils.join( 104 gTestDestPath, 105 `FirefoxBackup_SomeOtherProfile_20200101-0000.html` 106 ); 107 await IOUtils.writeUTF8(OLDER_BACKUP, "test"); 108 109 await testFinalizeSingleFileArchive(FAKE_METADATA, async movedFile => { 110 Assert.ok(movedFile.match(gMatchingRegex)); 111 Assert.ok( 112 await IOUtils.exists(OLDER_BACKUP), 113 "Older backup from another profile was not deleted." 114 ); 115 }); 116 });