Esempio n. 1
0
        public void Test06FileStream()
        {
            var    tempPath = Path.GetTempPath();
            string filePath;

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

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

            // test for cleanup if leaked
            {
                var file2     = TempData.CreateFileStream();
                var file2Path = file2.Name;
                Assert.AreNotEqual(filePath, file2Path, "Path should not match");
                Assert.IsTrue(File.Exists(file2.Name), "FileStream 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), "FileStream should NOT exist");
            }
        }
Esempio n. 2
0
        public void TestDirectoryExists()
        {
            Exception ex;
            string    dirPath;

            using (var dir = TempData.CreateDirectory())
            {
                dirPath = dir.Path;
                Assert.DoesNotThrow(() => IoCode.DirectoryExists(dir.Path, "arg00"));

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

                ex = Assert.Throws <FileNotFoundException>(() => IoCode.FileExists(dirPath, "arg00"));
                Assert.That(ex.Message, Does.Contain("arg00"));
                Assert.That(ex.Message, Does.Contain("is a directory"));
                Assert.That(ex.Message, Does.Contain(dirPath));
            }

            Assert.DoesNotThrow(() => IoCode.PathIsFree(dirPath));
            ex = Assert.Throws <DirectoryNotFoundException>(() => IoCode.DirectoryExists(dirPath, "arg00"));
            Assert.That(ex.Message, Does.Contain("arg00"));
            Assert.That(ex.Message, Does.Contain(dirPath));
        }