1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_osfile_writeAtomic_backupTo_option.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,143 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +let {OS: {File, Path, Constants}} = Components.utils.import("resource://gre/modules/osfile.jsm", {}); 1.10 +Components.utils.import("resource://gre/modules/Task.jsm"); 1.11 + 1.12 +/** 1.13 + * Remove all temporary files and back up files, including 1.14 + * test_backupTo_option_with_tmpPath.tmp 1.15 + * test_backupTo_option_with_tmpPath.tmp.backup 1.16 + * test_backupTo_option_without_tmpPath.tmp 1.17 + * test_backupTo_option_without_tmpPath.tmp.backup 1.18 + * test_non_backupTo_option.tmp 1.19 + * test_non_backupTo_option.tmp.backup 1.20 + * test_backupTo_option_without_destination_file.tmp 1.21 + * test_backupTo_option_without_destination_file.tmp.backup 1.22 + * test_backupTo_option_with_backup_file.tmp 1.23 + * test_backupTo_option_with_backup_file.tmp.backup 1.24 + */ 1.25 +function clearFiles() { 1.26 + return Task.spawn(function () { 1.27 + let files = ["test_backupTo_option_with_tmpPath.tmp", 1.28 + "test_backupTo_option_without_tmpPath.tmp", 1.29 + "test_non_backupTo_option.tmp", 1.30 + "test_backupTo_option_without_destination_file.tmp", 1.31 + "test_backupTo_option_with_backup_file.tmp"]; 1.32 + for (let file of files) { 1.33 + let path = Path.join(Constants.Path.tmpDir, file); 1.34 + yield File.remove(path); 1.35 + yield File.remove(path + ".backup"); 1.36 + } 1.37 + }); 1.38 +} 1.39 + 1.40 +function run_test() { 1.41 + run_next_test(); 1.42 +} 1.43 + 1.44 +add_task(function* init() { 1.45 + yield clearFiles(); 1.46 +}); 1.47 + 1.48 +/** 1.49 + * test when 1.50 + * |backupTo| specified 1.51 + * |tmpPath| specified 1.52 + * destination file exists 1.53 + * @result destination file will be backed up 1.54 + */ 1.55 +add_task(function* test_backupTo_option_with_tmpPath() { 1.56 + let DEFAULT_CONTENTS = "default contents" + Math.random(); 1.57 + let WRITE_CONTENTS = "abc" + Math.random(); 1.58 + let path = Path.join(Constants.Path.tmpDir, 1.59 + "test_backupTo_option_with_tmpPath.tmp"); 1.60 + yield File.writeAtomic(path, DEFAULT_CONTENTS); 1.61 + yield File.writeAtomic(path, WRITE_CONTENTS, { tmpPath: path + ".tmp", 1.62 + backupTo: path + ".backup" }); 1.63 + do_check_true((yield File.exists(path + ".backup"))); 1.64 + let contents = yield File.read(path + ".backup"); 1.65 + do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents)); 1.66 +}); 1.67 + 1.68 +/** 1.69 + * test when 1.70 + * |backupTo| specified 1.71 + * |tmpPath| not specified 1.72 + * destination file exists 1.73 + * @result destination file will be backed up 1.74 + */ 1.75 +add_task(function* test_backupTo_option_without_tmpPath() { 1.76 + let DEFAULT_CONTENTS = "default contents" + Math.random(); 1.77 + let WRITE_CONTENTS = "abc" + Math.random(); 1.78 + let path = Path.join(Constants.Path.tmpDir, 1.79 + "test_backupTo_option_without_tmpPath.tmp"); 1.80 + yield File.writeAtomic(path, DEFAULT_CONTENTS); 1.81 + yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" }); 1.82 + do_check_true((yield File.exists(path + ".backup"))); 1.83 + let contents = yield File.read(path + ".backup"); 1.84 + do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents)); 1.85 +}); 1.86 + 1.87 +/** 1.88 + * test when 1.89 + * |backupTo| not specified 1.90 + * |tmpPath| not specified 1.91 + * destination file exists 1.92 + * @result destination file will not be backed up 1.93 + */ 1.94 +add_task(function* test_non_backupTo_option() { 1.95 + let DEFAULT_CONTENTS = "default contents" + Math.random(); 1.96 + let WRITE_CONTENTS = "abc" + Math.random(); 1.97 + let path = Path.join(Constants.Path.tmpDir, 1.98 + "test_non_backupTo_option.tmp"); 1.99 + yield File.writeAtomic(path, DEFAULT_CONTENTS); 1.100 + yield File.writeAtomic(path, WRITE_CONTENTS); 1.101 + do_check_false((yield File.exists(path + ".backup"))); 1.102 +}); 1.103 + 1.104 +/** 1.105 + * test when 1.106 + * |backupTo| specified 1.107 + * |tmpPath| not specified 1.108 + * destination file not exists 1.109 + * @result no back up file exists 1.110 + */ 1.111 +add_task(function* test_backupTo_option_without_destination_file() { 1.112 + let DEFAULT_CONTENTS = "default contents" + Math.random(); 1.113 + let WRITE_CONTENTS = "abc" + Math.random(); 1.114 + let path = Path.join(Constants.Path.tmpDir, 1.115 + "test_backupTo_option_without_destination_file.tmp"); 1.116 + yield File.remove(path); 1.117 + yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" }); 1.118 + do_check_false((yield File.exists(path + ".backup"))); 1.119 +}); 1.120 + 1.121 +/** 1.122 + * test when 1.123 + * |backupTo| specified 1.124 + * |tmpPath| not specified 1.125 + * backup file exists 1.126 + * destination file exists 1.127 + * @result destination file will be backed up 1.128 + */ 1.129 +add_task(function* test_backupTo_option_with_backup_file() { 1.130 + let DEFAULT_CONTENTS = "default contents" + Math.random(); 1.131 + let WRITE_CONTENTS = "abc" + Math.random(); 1.132 + let path = Path.join(Constants.Path.tmpDir, 1.133 + "test_backupTo_option_with_backup_file.tmp"); 1.134 + yield File.writeAtomic(path, DEFAULT_CONTENTS); 1.135 + 1.136 + yield File.writeAtomic(path + ".backup", new Uint8Array(1000)); 1.137 + 1.138 + yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" }); 1.139 + do_check_true((yield File.exists(path + ".backup"))); 1.140 + let contents = yield File.read(path + ".backup"); 1.141 + do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents)); 1.142 +}); 1.143 + 1.144 +add_task(function* cleanup() { 1.145 + yield clearFiles(); 1.146 +});