Beispiel #1
0
        public void Test01Directory()
        {
            var    tempPath = Path.GetTempPath();
            string dirPath;

            using (var dir = TempData.CreateDirectory())
            {
                dirPath = dir.Path;
                Assert.IsTrue(Directory.Exists(dirPath), "Directory should exist");
                Assert.That(dirPath, Does.StartWith(tempPath));

                var dir2 = TempData.CreateDirectory();
                Assert.AreNotEqual(dir.Path, dir2.Path, "Path should not match");
                Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
                dir2.Dispose();
                AssertDisposed(() => dir2.Info);
            }
            Assert.IsFalse(Directory.Exists(dirPath), "Directory should NOT exist");

            // test for cleanup if leaked
            {
                var dir2     = TempData.CreateDirectory();
                var dir2Path = dir2.Path;
                Assert.AreNotEqual(dirPath, dir2Path, "Path should not match");
                Assert.IsNotNull(dir2.Info, "Info is null");
                Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
                GC.KeepAlive(dir2);

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

            // test for SuppressDelete()
            {
                var dir2     = TempData.CreateDirectory();
                var dir2Path = dir2.Path;
                try
                {
                    dir2.SuppressDelete();
                    Assert.AreNotEqual(dirPath, dir2Path, "Path should not match");
                    Assert.IsNotNull(dir2.Info, "Info is null");
                    Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
                    dir2.Dispose();
                    Assert.IsTrue(Directory.Exists(dir2Path), "Directory should exist");
                }
                finally
                {
                    Directory.Delete(dir2Path);
                }
            }
        }
        private static string CreateAndLeakTempDir(string s)
        {
            var dir2     = TempData.CreateDirectory();
            var dir2Path = dir2.Path;

            Assert.AreNotEqual(s, dir2Path, "Path should not match");
            Assert.IsNotNull(dir2.Info, "Info is null");
            Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
            GC.KeepAlive(dir2);
            return(dir2Path);
        }
Beispiel #3
0
        private static string CreateAndLeakTempDir(string s)
        {
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable CA2000  // Dispose objects before losing scope
            var dir2 = TempData.CreateDirectory();
#pragma warning restore CA2000  // Dispose objects before losing scope
#pragma warning restore IDE0079 // Remove unnecessary suppression
            var dir2Path = dir2.Path;
            Assert.AreNotEqual(s, dir2Path, "Path should not match");
            NAssert.NotNull(dir2.Info, "Info is null");
            Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
            GC.KeepAlive(dir2);
            return(dir2Path);
        }
        public void TestDirectory()
        {
            var    tempPath = Path.GetTempPath();
            string dirPath;

            using (var dir = TempData.CreateDirectory())
            {
                dirPath = dir.Path;
                Assert.IsTrue(Directory.Exists(dirPath), "Directory should exist");
                Assert.That(dirPath, Does.StartWith(tempPath));

                var dir2 = TempData.CreateDirectory();
                Assert.AreNotEqual(dir.Path, dir2.Path, "Path should not match");
                Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
                dir2.Dispose();
                AssertDisposed(() => dir2.Info);
            }
            Assert.IsFalse(Directory.Exists(dirPath), "Directory should NOT exist");

            // test for cleanup if leaked
            {
                var dir2Path = CreateAndLeakTempDir(dirPath);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Assert.IsFalse(Directory.Exists(dir2Path), "Directory should NOT exist");
            }

            // test for SuppressDelete()
            {
                var dir2     = TempData.CreateDirectory();
                var dir2Path = dir2.Path;
                try
                {
                    dir2.SuppressDelete();
                    Assert.AreNotEqual(dirPath, dir2Path, "Path should not match");
                    Assert.IsNotNull(dir2.Info, "Info is null");
                    Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
                    dir2.Dispose();
                    Assert.IsTrue(Directory.Exists(dir2Path), "Directory should exist");
                }
                finally
                {
                    Directory.Delete(dir2Path);
                }
            }
        }
        public void TestDirectorySpecificPathSubDirectory()
        {
            var tempPath = Path.Combine(Path.GetTempPath(), TempData.GetTempName());

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

            using (var directory = TempData.CreateDirectory(tempPath, dirName))
            {
                Assert.AreEqual(directory.Path, dirPath);
                Assert.IsNotNull(directory.Info, "Info is null");
                Assert.IsTrue(directory.Info.Exists, "Directory should exist");
            }
            Assert.IsFalse(File.Exists(dirPath), "Directory should NOT exist");
            Directory.Delete(tempPath);
        }
Beispiel #6
0
        public void Test02DirectoryNestedContent()
        {
            string dirPath;
            string nestedFile;
            string nestedDir;

            using (var dir = TempData.CreateDirectory())
            {
                dirPath = dir.Path;

                Assert.IsNotNull(dir.Info, "Info is null");
                nestedDir = dir.Info.CreateSubdirectory("test.dir").FullName;

                nestedFile = Path.Combine(dirPath, "test.tmp");
                File.WriteAllText(nestedFile, "O La La");
                var content = File.ReadAllText(nestedFile);
                Assert.AreEqual(content, "O La La");
            }
            Assert.IsFalse(Directory.Exists(dirPath), "Directory should NOT exist");
            Assert.IsFalse(Directory.Exists(nestedDir), "Directory should NOT exist");
            Assert.IsFalse(File.Exists(nestedFile), "File should NOT exist");
        }
Beispiel #7
0
        public void TestRetryPolicy()
        {
            var disposeCallCount = 0;

            try
            {
                Configuration.SetTempDataRetryCallback(
                    a =>
                {
                    Interlocked.Increment(ref disposeCallCount);
                    a();
                });

                string dirPath;
                string nestedFile;
                using (var dir = TempData.CreateDirectory())
                {
                    dirPath    = dir.Path;
                    nestedFile = Path.Combine(dirPath, "test.tmp");

                    using (File.Create(nestedFile))
                    {
                        Assert.Throws <IOException>(() => dir.Dispose());
                        Assert.AreEqual(disposeCallCount, 1);
                    }

                    Assert.DoesNotThrow(() => dir.Dispose());
                    Assert.AreEqual(disposeCallCount, 2);
                }
                Assert.AreEqual(disposeCallCount, 2);
                Assert.IsFalse(Directory.Exists(dirPath), "Directory should NOT exist");
                Assert.IsFalse(File.Exists(nestedFile), "File should NOT exist");
            }
            finally
            {
                Configuration.SetTempDataRetryCallback(null);
            }
        }
Beispiel #8
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));
        }