test_BackupService_resolveArchiveDestFolderPath.js (3704B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const HOME_KEY = "Home"; 7 let gTestRoot; 8 let gFakeHomePath; 9 let gFakeHomeFile; 10 11 add_setup(async () => { 12 gTestRoot = await IOUtils.createUniqueDirectory( 13 PathUtils.tempDir, 14 "testResolveArchiveDestFolderPath" 15 ); 16 gFakeHomePath = PathUtils.join(gTestRoot, "FakeHome"); 17 await IOUtils.makeDirectory(gFakeHomePath); 18 19 gFakeHomeFile = await IOUtils.getFile(gFakeHomePath); 20 21 let dirsvc = Services.dirsvc.QueryInterface(Ci.nsIProperties); 22 let originalFile; 23 try { 24 originalFile = dirsvc.get(HOME_KEY, Ci.nsIFile); 25 dirsvc.undefine(HOME_KEY); 26 } catch (e) { 27 // dirsvc.get will throw if nothing provides for the key and dirsvc.undefine 28 // will throw if it's not a persistent entry, in either case we don't want 29 // to set the original file in cleanup. 30 originalFile = undefined; 31 } 32 33 dirsvc.set(HOME_KEY, gFakeHomeFile); 34 registerCleanupFunction(() => { 35 dirsvc.undefine(HOME_KEY); 36 if (originalFile) { 37 dirsvc.set(HOME_KEY, originalFile); 38 } 39 }); 40 }); 41 42 /** 43 * Tests that we create the destination folder if the parent folder exists 44 * and the destination folder does not. 45 */ 46 add_task(async function test_create_folder() { 47 const PARENT_FOLDER = PathUtils.join(gTestRoot, "TestFolder"); 48 await IOUtils.makeDirectory(PARENT_FOLDER); 49 let bs = new BackupService(); 50 51 const DESTINATION_PATH = PathUtils.join( 52 PARENT_FOLDER, 53 BackupService.BACKUP_DIR_NAME 54 ); 55 let path = await bs.resolveArchiveDestFolderPath(DESTINATION_PATH); 56 57 Assert.equal(path, DESTINATION_PATH, "Got back the expected folder path."); 58 Assert.ok(await IOUtils.exists(path), "The destination folder was created."); 59 Assert.equal( 60 (await IOUtils.getChildren(path)).length, 61 0, 62 "Destination folder should be empty." 63 ); 64 await IOUtils.remove(PARENT_FOLDER, { recursive: true }); 65 }); 66 67 /** 68 * Tests that we will recreate the configured destination folder if the parent 69 * folder does not exist. This recreates the entire configured folder 70 * hierarchy. 71 */ 72 add_task(async function test_create_parent_folder_hierarchy() { 73 const MISSING_PARENT_FOLDER = PathUtils.join(gTestRoot, "DoesNotExistYet"); 74 Assert.ok( 75 !(await IOUtils.exists(MISSING_PARENT_FOLDER)), 76 "Folder should not exist yet." 77 ); 78 let bs = new BackupService(); 79 80 const CONFIGURED_DESTINATION_PATH = PathUtils.join( 81 MISSING_PARENT_FOLDER, 82 BackupService.BACKUP_DIR_NAME 83 ); 84 let path = await bs.resolveArchiveDestFolderPath(CONFIGURED_DESTINATION_PATH); 85 Assert.equal( 86 path, 87 CONFIGURED_DESTINATION_PATH, 88 "Got back the expected folder path." 89 ); 90 Assert.ok(await IOUtils.exists(path), "The destination folder was created."); 91 92 await IOUtils.remove(MISSING_PARENT_FOLDER, { recursive: true }); 93 }); 94 95 /** 96 * Tests that we return the destination folder if the parent folder exists 97 * along with the destination folder. 98 */ 99 add_task(async function test_find_folder() { 100 const PARENT_FOLDER = PathUtils.join(gTestRoot, "TestFolder"); 101 const DESTINATION_PATH = PathUtils.join( 102 PARENT_FOLDER, 103 BackupService.BACKUP_DIR_NAME 104 ); 105 await IOUtils.makeDirectory(DESTINATION_PATH, { createAncestors: true }); 106 107 let bs = new BackupService(); 108 let path = await bs.resolveArchiveDestFolderPath(DESTINATION_PATH); 109 110 Assert.equal(path, DESTINATION_PATH, "Got back the expected folder path."); 111 Assert.ok(await IOUtils.exists(path), "The destination folder exists."); 112 Assert.equal( 113 (await IOUtils.getChildren(path)).length, 114 0, 115 "Destination folder should be empty." 116 ); 117 await IOUtils.remove(PARENT_FOLDER, { recursive: true }); 118 });