michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: let {OS: {File, Path, Constants}} = Components.utils.import("resource://gre/modules/osfile.jsm", {}); michael@0: Components.utils.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: /** michael@0: * Remove all temporary files and back up files, including michael@0: * test_backupTo_option_with_tmpPath.tmp michael@0: * test_backupTo_option_with_tmpPath.tmp.backup michael@0: * test_backupTo_option_without_tmpPath.tmp michael@0: * test_backupTo_option_without_tmpPath.tmp.backup michael@0: * test_non_backupTo_option.tmp michael@0: * test_non_backupTo_option.tmp.backup michael@0: * test_backupTo_option_without_destination_file.tmp michael@0: * test_backupTo_option_without_destination_file.tmp.backup michael@0: * test_backupTo_option_with_backup_file.tmp michael@0: * test_backupTo_option_with_backup_file.tmp.backup michael@0: */ michael@0: function clearFiles() { michael@0: return Task.spawn(function () { michael@0: let files = ["test_backupTo_option_with_tmpPath.tmp", michael@0: "test_backupTo_option_without_tmpPath.tmp", michael@0: "test_non_backupTo_option.tmp", michael@0: "test_backupTo_option_without_destination_file.tmp", michael@0: "test_backupTo_option_with_backup_file.tmp"]; michael@0: for (let file of files) { michael@0: let path = Path.join(Constants.Path.tmpDir, file); michael@0: yield File.remove(path); michael@0: yield File.remove(path + ".backup"); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function* init() { michael@0: yield clearFiles(); michael@0: }); michael@0: michael@0: /** michael@0: * test when michael@0: * |backupTo| specified michael@0: * |tmpPath| specified michael@0: * destination file exists michael@0: * @result destination file will be backed up michael@0: */ michael@0: add_task(function* test_backupTo_option_with_tmpPath() { michael@0: let DEFAULT_CONTENTS = "default contents" + Math.random(); michael@0: let WRITE_CONTENTS = "abc" + Math.random(); michael@0: let path = Path.join(Constants.Path.tmpDir, michael@0: "test_backupTo_option_with_tmpPath.tmp"); michael@0: yield File.writeAtomic(path, DEFAULT_CONTENTS); michael@0: yield File.writeAtomic(path, WRITE_CONTENTS, { tmpPath: path + ".tmp", michael@0: backupTo: path + ".backup" }); michael@0: do_check_true((yield File.exists(path + ".backup"))); michael@0: let contents = yield File.read(path + ".backup"); michael@0: do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents)); michael@0: }); michael@0: michael@0: /** michael@0: * test when michael@0: * |backupTo| specified michael@0: * |tmpPath| not specified michael@0: * destination file exists michael@0: * @result destination file will be backed up michael@0: */ michael@0: add_task(function* test_backupTo_option_without_tmpPath() { michael@0: let DEFAULT_CONTENTS = "default contents" + Math.random(); michael@0: let WRITE_CONTENTS = "abc" + Math.random(); michael@0: let path = Path.join(Constants.Path.tmpDir, michael@0: "test_backupTo_option_without_tmpPath.tmp"); michael@0: yield File.writeAtomic(path, DEFAULT_CONTENTS); michael@0: yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" }); michael@0: do_check_true((yield File.exists(path + ".backup"))); michael@0: let contents = yield File.read(path + ".backup"); michael@0: do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents)); michael@0: }); michael@0: michael@0: /** michael@0: * test when michael@0: * |backupTo| not specified michael@0: * |tmpPath| not specified michael@0: * destination file exists michael@0: * @result destination file will not be backed up michael@0: */ michael@0: add_task(function* test_non_backupTo_option() { michael@0: let DEFAULT_CONTENTS = "default contents" + Math.random(); michael@0: let WRITE_CONTENTS = "abc" + Math.random(); michael@0: let path = Path.join(Constants.Path.tmpDir, michael@0: "test_non_backupTo_option.tmp"); michael@0: yield File.writeAtomic(path, DEFAULT_CONTENTS); michael@0: yield File.writeAtomic(path, WRITE_CONTENTS); michael@0: do_check_false((yield File.exists(path + ".backup"))); michael@0: }); michael@0: michael@0: /** michael@0: * test when michael@0: * |backupTo| specified michael@0: * |tmpPath| not specified michael@0: * destination file not exists michael@0: * @result no back up file exists michael@0: */ michael@0: add_task(function* test_backupTo_option_without_destination_file() { michael@0: let DEFAULT_CONTENTS = "default contents" + Math.random(); michael@0: let WRITE_CONTENTS = "abc" + Math.random(); michael@0: let path = Path.join(Constants.Path.tmpDir, michael@0: "test_backupTo_option_without_destination_file.tmp"); michael@0: yield File.remove(path); michael@0: yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" }); michael@0: do_check_false((yield File.exists(path + ".backup"))); michael@0: }); michael@0: michael@0: /** michael@0: * test when michael@0: * |backupTo| specified michael@0: * |tmpPath| not specified michael@0: * backup file exists michael@0: * destination file exists michael@0: * @result destination file will be backed up michael@0: */ michael@0: add_task(function* test_backupTo_option_with_backup_file() { michael@0: let DEFAULT_CONTENTS = "default contents" + Math.random(); michael@0: let WRITE_CONTENTS = "abc" + Math.random(); michael@0: let path = Path.join(Constants.Path.tmpDir, michael@0: "test_backupTo_option_with_backup_file.tmp"); michael@0: yield File.writeAtomic(path, DEFAULT_CONTENTS); michael@0: michael@0: yield File.writeAtomic(path + ".backup", new Uint8Array(1000)); michael@0: michael@0: yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" }); michael@0: do_check_true((yield File.exists(path + ".backup"))); michael@0: let contents = yield File.read(path + ".backup"); michael@0: do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents)); michael@0: }); michael@0: michael@0: add_task(function* cleanup() { michael@0: yield clearFiles(); michael@0: });