[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"));
		}
		[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"));
		}
		[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"));
		}
		[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"));
		}
Exemplo n.º 5
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);
            }
        }