Beispiel #1
0
        public void TestFileExists()
        {
            Exception ex;
            string    filePath;

            using (var file = TempData.CreateFile())
            {
                filePath = file.Path;
                Assert.DoesNotThrow(() => IoCode.FileExists(file.Path, "arg00"));

                ex = Assert.Throws <IOException>(() => IoCode.PathIsFree(filePath));
                Assert.That(ex.Message, Does.Contain(filePath));

                ex = Assert.Throws <DirectoryNotFoundException>(() => IoCode.DirectoryExists(filePath, "arg00"));
                Assert.That(ex.Message, Does.Contain("arg00"));
                Assert.That(ex.Message, Does.Contain("is a file"));
                Assert.That(ex.Message, Does.Contain(filePath));
            }

            Assert.DoesNotThrow(() => IoCode.PathIsFree(filePath));

            ex = Assert.Throws <FileNotFoundException>(() => IoCode.FileExists(filePath, "arg00"));
            Assert.That(ex.Message, Does.Contain("arg00"));
            Assert.That(ex.Message, Does.Contain(filePath));
        }
Beispiel #2
0
        public void Test04File()
        {
            var    tempPath = Path.GetTempPath();
            string filePath;

            using (var file = TempData.CreateFile())
            {
                filePath = file.Path;
                Assert.IsTrue(File.Exists(filePath), "File should exist");
                Assert.That(tempPath, Does.StartWith(tempPath));

                var file2 = TempData.CreateFile();
                Assert.AreNotEqual(file.Path, file2.Path, "Path should not match");
                Assert.IsTrue(file2.Info.Exists, "File should exist");
                file2.Dispose();
                AssertDisposed(() => file2.Info);
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");

            // test for cleanup if leaked
            {
                var file2     = TempData.CreateFile();
                var file2Path = file2.Path;
                Assert.AreNotEqual(filePath, file2Path, "Path should not match");
                Assert.IsNotNull(file2.Info, "Info is null");
                Assert.IsTrue(file2.Info.Exists, "File should exist");
                GC.KeepAlive(file2);

                // clear GC root for a debug build
                // ReSharper disable once RedundantAssignment
                file2 = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Assert.IsFalse(File.Exists(file2Path), "File should NOT exist");
            }

            // test for SuppressDelete()
            {
                var file2     = TempData.CreateFile();
                var file2Path = file2.Path;
                try
                {
                    file2.SuppressDelete();
                    Assert.AreNotEqual(filePath, file2Path, "Path should not match");
                    Assert.IsNotNull(file2.Info, "Info is null");
                    Assert.IsTrue(file2.Info.Exists, "File should exist");
                    file2.Dispose();
                    Assert.IsTrue(File.Exists(file2Path), "File should exist");
                }
                finally
                {
                    File.Delete(file2Path);
                }
            }
        }
        private static string CreateAndLeakTempFile(string filePath)
        {
            var file2     = TempData.CreateFile();
            var file2Path = file2.Path;

            Assert.AreNotEqual(filePath, file2Path, "Path should not match");
            Assert.IsNotNull(file2.Info, "Info is null");
            Assert.IsTrue(file2.Info.Exists, "File should exist");
            GC.KeepAlive(file2);

            return(file2Path);
        }
Beispiel #4
0
        public void Test05FileSpecificPath()
        {
            var tempPath = Path.GetTempPath();
            var fileName = Guid.NewGuid() + ".tmp";
            var filePath = Path.Combine(tempPath, fileName);

            using (var file = TempData.CreateFile(tempPath, fileName))
            {
                Assert.AreEqual(file.Path, filePath);
                Assert.IsNotNull(file.Info, "Info is null");
                Assert.IsTrue(file.Info.Exists, "File should exist");
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
        }
Beispiel #5
0
        private static string CreateAndLeakTempFile(string filePath)
        {
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable CA2000  // Dispose objects before losing scope
            var file2 = TempData.CreateFile();
#pragma warning restore CA2000  // Dispose objects before losing scope
#pragma warning restore IDE0079 // Remove unnecessary suppression
            var file2Path = file2.Path;
            Assert.AreNotEqual(filePath, file2Path, "Path should not match");
            NAssert.NotNull(file2.Info, "Info is null");
            Assert.IsTrue(file2.Info.Exists, "File should exist");
            GC.KeepAlive(file2);

            return(file2Path);
        }
        public void TestFile()
        {
            var    tempPath = Path.GetTempPath();
            string filePath;

            using (var file = TempData.CreateFile())
            {
                filePath = file.Path;
                Assert.IsTrue(File.Exists(filePath), "File should exist");
                Assert.That(tempPath, Does.StartWith(tempPath));

                var file2 = TempData.CreateFile();
                Assert.AreNotEqual(file.Path, file2.Path, "Path should not match");
                Assert.IsTrue(file2.Info.Exists, "File should exist");
                file2.Dispose();
                AssertDisposed(() => file2.Info);
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");

            // test for cleanup if leaked
            {
                var file2Path = CreateAndLeakTempFile(filePath);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();

                Assert.IsFalse(File.Exists(file2Path), "File should NOT exist");
            }

            // test for SuppressDelete()
            {
                var file2     = TempData.CreateFile();
                var file2Path = file2.Path;
                try
                {
                    file2.SuppressDelete();
                    Assert.AreNotEqual(filePath, file2Path, "Path should not match");
                    Assert.IsNotNull(file2.Info, "Info is null");
                    Assert.IsTrue(file2.Info.Exists, "File should exist");
                    file2.Dispose();
                    Assert.IsTrue(File.Exists(file2Path), "File should exist");
                }
                finally
                {
                    File.Delete(file2Path);
                }
            }
        }
Beispiel #7
0
        public void Test05FileContent()
        {
            string filePath;

            using (var file = TempData.CreateFile())
            {
                filePath = file.Path;

                Assert.IsNotNull(file.Info, "Info is null");
                using (var textWriter = file.Info.AppendText())
                    textWriter.Write("O La La");

                var content = File.ReadAllText(filePath);
                Assert.AreEqual(content, "O La La");
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
        }
        public void TestFileSpecificPathSubDirectory()
        {
            var tempPath = Path.Combine(Path.GetTempPath(), TempData.GetTempName());

            Assert.IsFalse(Directory.Exists(tempPath), "Directory should NOT exist");
            var fileName = TempData.GetTempName();
            var filePath = Path.Combine(tempPath, fileName);

            using (var file = TempData.CreateFile(tempPath, fileName))
            {
                Assert.AreEqual(file.Path, filePath);
                Assert.IsNotNull(file.Info, "Info is null");
                Assert.IsTrue(file.Info.Exists, "File should exist");
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
            Directory.Delete(tempPath);
        }
Beispiel #9
0
        public void Test03DirectorySpecificPath()
        {
            var tempPath = Path.GetTempPath();
            var dirName  = TempData.GetTempName();
            var dirPath  = Path.Combine(tempPath, dirName);

            using (var dir = TempData.CreateFile(tempPath, dirName))
            {
                Assert.AreEqual(dir.Path, dirPath);
                Assert.IsNotNull(dir.Info, "Info is null");
                Assert.IsTrue(dir.Info.Exists, "Directory should exist");
            }
            Assert.IsFalse(File.Exists(dirPath), "Directory should NOT exist");

            using (var dir = TempData.CreateFile(null, dirName))
            {
                Assert.AreEqual(dir.Path, dirPath);
                Assert.IsNotNull(dir.Info, "Info is null");
                Assert.IsTrue(dir.Info.Exists, "Directory should exist");
            }
            Assert.IsFalse(File.Exists(dirPath), "Directory should NOT exist");
        }