コード例 #1
0
        private void Save()
        {
            // We're checking this because the deserialization routine calls the property setters which triggers a save. We don't
            // want to save while loading.
            if (_loading)
            {
                return;
            }
            var prefs = JsonConvert.SerializeObject(this);

            Debug.Assert(!string.IsNullOrWhiteSpace(prefs));

            try
            {
                if (!string.IsNullOrWhiteSpace(prefs))
                {
                    var temp = new TempFileForSafeWriting(_filePath);
                    RobustFile.WriteAllText(temp.TempFilePath, prefs);
                    temp.WriteWasSuccessful();
                }
            }
            catch (Exception error)
            {
                //For https://silbloom.myjetbrains.com/youtrack/issue/BL-3222  we did a real fix for 3.6.
                //But this will cover us for future errors here, which are not worth stopping the user from doing work.
                NonFatalProblem.Report(ModalIf.Alpha, PassiveIf.All, "Problem saving book preferences", "book.userprefs could not be saved to " + _filePath, error);
            }
        }
コード例 #2
0
        [TestCase(true)]         // Simulate situation on the JAARS network in 2016, .net's built-in File.Replace fails on the network drives.
        public void WriteWasSuccessful_TargetDidNotExist_TargetHasContents(bool simulateVolumeThatCannotHandleFileReplace)
        {
            var targetPath = Path.GetTempFileName();
            var f          = new TempFileForSafeWriting(targetPath);

            f.SimulateVolumeThatCannotHandleFileReplace = simulateVolumeThatCannotHandleFileReplace;
            File.WriteAllText(f.TempFilePath, "hello");
            f.WriteWasSuccessful();
            Assert.That(File.ReadAllText(targetPath), Is.EqualTo("hello"));
        }
コード例 #3
0
        private static string MakeFakeBloomdFile(Bloom.Book.Book book)
        {
            var srcFile      = Path.Combine(book.FolderPath, "book.htm");
            var filePath     = new TempFileForSafeWriting(Path.Combine(book.FolderPath, "book.bloomd")).TempFilePath;
            var fileContents = File.ReadAllLines(srcFile);

            for (var i = 0; i < 100; i++)
            {
                File.AppendAllLines(filePath, fileContents);
            }

            return(filePath);
        }
コード例 #4
0
        [TestCase(true)]         // Simulate situation on the JAARS network in 2016, .net's built-in File.Replace fails on the network drives.
        public void WriteWasSuccessful_BackupDidNotExist_HasContentsOfReplacedFile(bool simulateVolumeThatCannotHandleFileReplace)
        {
            var targetPath = Path.GetTempFileName();
            var backup     = targetPath + ".bak";

            File.WriteAllText(targetPath, "old");
            var f = new TempFileForSafeWriting(targetPath);

            f.SimulateVolumeThatCannotHandleFileReplace = simulateVolumeThatCannotHandleFileReplace;
            File.WriteAllText(f.TempFilePath, "new");
            f.WriteWasSuccessful();
            Assert.That(File.ReadAllText(backup), Is.EqualTo("old"));
            Assert.That(File.ReadAllText(targetPath), Is.EqualTo("new"));
        }
コード例 #5
0
        [TestCase(true)]         // Simulate situation on the JAARS network in 2016, .net's built-in File.Replace fails on the network drives.
        public void WriteWasSuccessful_TargetAndBakcupDidNotExist_BackupFileStillDoesNotExist(bool simulateVolumeThatCannotHandleFileReplace)
        {
            var targetPath = Path.GetTempFileName();

            File.Delete(targetPath);
            var backup = targetPath + ".bak";
            var f      = new TempFileForSafeWriting(targetPath);

            f.SimulateVolumeThatCannotHandleFileReplace = simulateVolumeThatCannotHandleFileReplace;
            File.WriteAllText(f.TempFilePath, "new");
            f.WriteWasSuccessful();
            Assert.That(File.Exists(backup), Is.False);
            Assert.That(File.ReadAllText(targetPath), Is.EqualTo("new"));
        }
コード例 #6
0
        [TestCase(true)]         // Simulate situation on the JAARS network in 2016, .net's built-in File.Replace fails on the network drives.
        public void WriteWasSuccessful_BackupExistedButTargetDidNot_BackupLeftAlone(bool simulateVolumeThatCannotHandleFileReplace)
        {
            var targetPath = Path.GetTempFileName();

            File.Delete(targetPath);
            var backup = targetPath + ".bak";

            File.WriteAllText(backup, "old bak");
            var f = new TempFileForSafeWriting(targetPath);

            f.SimulateVolumeThatCannotHandleFileReplace = simulateVolumeThatCannotHandleFileReplace;
            File.WriteAllText(f.TempFilePath, "new");
            f.WriteWasSuccessful();
            Assert.That(File.ReadAllText(backup), Is.EqualTo("old bak"));
            Assert.That(File.ReadAllText(targetPath), Is.EqualTo("new"));
        }