public void TestFileStream()
        {
            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 file2Path = CreateAndLeakTempStream(filePath);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Assert.IsFalse(File.Exists(file2Path), "FileStream should NOT exist");
            }
        }
Beispiel #2
0
        public void TestFileStreamContent()
        {
            string filePath;

            using (var fileStream = TempData.CreateFileStream())
            {
                filePath = fileStream.Name;
#if NET45_OR_GREATER || TARGETS_NETCOREAPP
                using (var textWriter = new StreamWriter(fileStream, Encoding.UTF8, 4096, true))
                    textWriter.Write("O La La");
#else
                var textWriter = new StreamWriter(fileStream, Encoding.UTF8, 4096);
                textWriter.Write("O La La");
                textWriter.Flush();
#endif

                string content;
                fileStream.Position = 0;
#if NET45_OR_GREATER || TARGETS_NETCOREAPP
                using (var textReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096, true))
                    content = textReader.ReadToEnd();
#else
                var textReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096);
                content = textReader.ReadToEnd();
#endif
                Assert.AreEqual(content, "O La La");
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
        }
        private static string CreateAndLeakTempStream(string filePath)
        {
            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);

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

            using (var fileStream = TempData.CreateFileStream(tempPath, fileName))
            {
                Assert.AreEqual(fileStream.Name, filePath);
                Assert.IsTrue(File.Exists(filePath), "File should exist");
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
        }
Beispiel #5
0
        private static string CreateAndLeakTempStream(string filePath)
        {
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable CA2000  // Dispose objects before losing scope
            var file2 = TempData.CreateFileStream();
#pragma warning restore CA2000  // Dispose objects before losing scope
#pragma warning restore IDE0079 // Remove unnecessary suppression
            var file2Path = file2.Name;
            Assert.AreNotEqual(filePath, file2Path, "Path should not match");
            Assert.IsTrue(File.Exists(file2.Name), "FileStream should exist");
            GC.KeepAlive(file2);

            return(file2Path);
        }
        public void TestFileStreamSpecificPathSubDirectory()
        {
            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.CreateFileStream(tempPath, fileName))
            {
                Assert.AreEqual(file.Name, filePath);
                Assert.IsTrue(File.Exists(filePath), "File should exist");
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
            Directory.Delete(tempPath);
        }
Beispiel #7
0
        public void Test08FileStreamContent()
        {
            string filePath;

            using (var fileStream = TempData.CreateFileStream())
            {
                filePath = fileStream.Name;

                using (var textWriter = new StreamWriter(fileStream, Encoding.UTF8, 4096, true))
                    textWriter.Write("O La La");

                string content;
                fileStream.Position = 0;
                using (var textReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096, true))
                    content = textReader.ReadToEnd();
                Assert.AreEqual(content, "O La La");
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
        }
Beispiel #8
0
        public void TestFileStreamContent()
        {
            string filePath;

            using (var fileStream = TempData.CreateFileStream())
            {
                filePath = fileStream.Name;
#if NET45_OR_GREATER || TARGETS_NETCOREAPP
                using (var textWriter = new StreamWriter(fileStream, Encoding.UTF8, 4096, true))
                    textWriter.Write("O La La");
#else
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable CA2000  // Dispose objects before losing scope
                var textWriter = new StreamWriter(fileStream, Encoding.UTF8, 4096);
#pragma warning restore CA2000  // Dispose objects before losing scope
#pragma warning restore IDE0079 // Remove unnecessary suppression

                textWriter.Write("O La La");
                textWriter.Flush();
#endif

                string content;
                fileStream.Position = 0;
#if NET45_OR_GREATER || TARGETS_NETCOREAPP
                using (var textReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096, true))
                    content = textReader.ReadToEnd();
#else
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable CA2000  // Dispose objects before losing scope
                var textReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096);
#pragma warning restore CA2000  // Dispose objects before losing scope
#pragma warning restore IDE0079 // Remove unnecessary suppression

                content = textReader.ReadToEnd();
#endif
                Assert.AreEqual(content, "O La La");
            }
            Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
        }
Beispiel #9
0
        public void Test07FileStream()
        {
            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");
            }
        }