Beispiel #1
0
 public void CreateStream()
 {
     using (var cleaner = new TestFileCleaner(useDotNet: false))
     {
         string filePath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
         using (Stream fileStream = cleaner.FileService.CreateFileStream(filePath, FileMode.CreateNew))
         {
             fileStream.Should().NotBeNull();
         }
     }
 }
Beispiel #2
0
        public void CreateLongPathNestedDirectoryTest()
        {
            using (var cleaner = new TestFileCleaner(useDotNet: false))
            {
                string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 300);
                cleaner.FileService.CreateDirectory(longPath);

                NativeMethods.FileManagement.FileExists(longPath).Should().BeFalse();
                NativeMethods.FileManagement.DirectoryExists(longPath).Should().BeTrue();
            }
        }
Beispiel #3
0
        public void CreateDirectoryTest()
        {
            using (var cleaner = new TestFileCleaner(useDotNet: false))
            {
                string directoryPath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                cleaner.FileService.CreateDirectory(directoryPath);

                File.Exists(directoryPath).Should().BeFalse();
                Directory.Exists(directoryPath).Should().BeTrue();
            }
        }
Beispiel #4
0
 public void CreateStreamsWithDifficultNames(string fileName)
 {
     using (var cleaner = new TestFileCleaner(useDotNet: false))
     {
         string filePath = Paths.Combine(cleaner.TempFolder, fileName);
         using (Stream fileStream = cleaner.FileService.CreateFileStream(filePath, FileMode.CreateNew))
         {
             fileStream.Should().NotBeNull();
             cleaner.FileService.FileExists(filePath).Should().BeTrue();
         }
     }
 }
        public void DefaultEnumerate()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string directoryPath = cleaner.CreateTestDirectory();
                string filePath = cleaner.CreateTestFile("DefaultEnumerate", directoryPath);

                var directoryInfo = cleaner.FileService.GetPathInfo(directoryPath) as IDirectoryInformation;
                directoryInfo.Should().NotBeNull();
                var files = directoryInfo.EnumerateChildren().ToArray();
                files.Should().HaveCount(1);
                files[0].Path.Should().Be(filePath);
            }
        }
Beispiel #6
0
        public void CreateNestedDirectoryTest()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string directoryPath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                string secondDirectoryPath = Paths.Combine(directoryPath, Path.GetRandomFileName());
                FileService fileService = new FileService();
                fileService.CreateDirectory(secondDirectoryPath);

                File.Exists(directoryPath).Should().BeFalse();
                Directory.Exists(directoryPath).Should().BeTrue();
                File.Exists(secondDirectoryPath).Should().BeFalse();
                Directory.Exists(secondDirectoryPath).Should().BeTrue();
            }
        }
        public void EnumerateNestedFile()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string directoryPath = cleaner.CreateTestDirectory();
                string filePath = cleaner.CreateTestFile("DefaultEnumerate", directoryPath);
                string fileName = Paths.GetFileOrDirectoryName(filePath);

                var directoryInfo = cleaner.FileService.GetPathInfo(cleaner.TempFolder) as IDirectoryInformation;
                directoryInfo.Should().NotBeNull();
                var files = directoryInfo.EnumerateChildren(ChildType.File, fileName, SearchOption.AllDirectories).ToArray();
                files.Should().HaveCount(1);
                files[0].Path.Should().Be(filePath);
            }
        }
        public void DefaultEnumerate()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string directoryPath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                Directory.CreateDirectory(directoryPath);
                string filePath = Paths.Combine(directoryPath, Path.GetRandomFileName());
                File.WriteAllText(filePath, "DefaultEnumerate");

                FileService fileService = new FileService();
                var directoryInfo = fileService.GetPathInfo(directoryPath) as IDirectoryInformation;
                directoryInfo.Should().NotBeNull();
                var files = directoryInfo.EnumerateChildren().ToArray();
                files.Should().HaveCount(1);
                files[0].Path.Should().Be(filePath);
            }
        }
        public void EnumerateNestedFilteredFile()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string directoryPath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                var createdDirectory = Directory.CreateDirectory(directoryPath);
                string fileName = Path.GetRandomFileName();
                string filePath = Paths.Combine(directoryPath, fileName);
                File.WriteAllText(filePath, "DefaultEnumerate");
                createdDirectory.Attributes = createdDirectory.Attributes |= FileAttributes.Hidden;

                FileService fileService = new FileService();
                var directoryInfo = fileService.GetPathInfo(cleaner.TempFolder) as IDirectoryInformation;
                directoryInfo.Should().NotBeNull();
                var files = directoryInfo.EnumerateChildren(ChildType.File, fileName, SearchOption.AllDirectories).ToArray();
                files.Should().HaveCount(0);
                createdDirectory.Attributes = createdDirectory.Attributes &= ~FileAttributes.Hidden;
            }
        }
Beispiel #10
0
 public void OpenNonExistantStream(string appendix)
 {
     using (var cleaner = new TestFileCleaner())
     {
         FileService fileService = new FileService();
         string filePath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName() + appendix);
         Action action = () => fileService.CreateFileStream(filePath, FileMode.Open);
         action.ShouldThrow<FileNotFoundException>();
     }
 }
Beispiel #11
0
 public void WriteAndReadLongPathStream()
 {
     using (var cleaner = new TestFileCleaner(false))
     {
         string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 300);
         FileService fileService = new FileService();
         fileService.CreateDirectory(longPath);
         string filePath = Paths.Combine(longPath, Path.GetRandomFileName());
         using (Stream fileStream = fileService.CreateFileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
         {
             fileStream.Should().NotBeNull();
             StreamWriter writer = new StreamWriter(fileStream);
             writer.WriteLine("This is a test string.");
             writer.Flush();
             fileStream.Position = 0;
             StreamReader reader = new StreamReader(fileStream);
             string readLine = reader.ReadLine();
             readLine.Should().Be("This is a test string.");
         }
     }
 }
Beispiel #12
0
 public void WriteAndReadtStream(string appendix)
 {
     using (var cleaner = new TestFileCleaner())
     {
         FileService fileService = new FileService();
         string filePath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName() + appendix);
         using (Stream fileStream = fileService.CreateFileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
         {
             fileStream.Should().NotBeNull();
             StreamWriter writer = new StreamWriter(fileStream);
             writer.WriteLine("This is a test string.");
             writer.Flush();
             fileStream.Position = 0;
             StreamReader reader = new StreamReader(fileStream);
             string readLine = reader.ReadLine();
             readLine.Should().Be("This is a test string.");
         }
     }
 }
Beispiel #13
0
        public void WriteAndReadAlternateStreams()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string directoryPath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                Directory.CreateDirectory(directoryPath);

                FileService fileService = new FileService();
                string filePath = Paths.Combine(directoryPath, Path.GetRandomFileName());
                using (Stream fileStream = fileService.CreateFileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite)) { }

                for (int i = 0; i < 3; i++)
                {
                    string streamPath = $"{filePath}:Stream{i}:$DATA";
                    using (Stream fileStream = fileService.CreateFileStream(streamPath, FileMode.CreateNew, FileAccess.ReadWrite))
                    {
                        string testString = $"This is test string {i}.";
                        fileStream.Should().NotBeNull();
                        StreamWriter writer = new StreamWriter(fileStream);
                        writer.WriteLine(testString);
                        writer.Flush();
                        fileStream.Position = 0;
                        StreamReader reader = new StreamReader(fileStream);
                        string readLine = reader.ReadLine();
                        readLine.Should().Be(testString);
                    }
                }

                var directoryInfo = fileService.GetPathInfo(directoryPath) as IDirectoryInformation;
                directoryInfo.Should().NotBeNull();
                directoryInfo.EnumerateChildren().Should().HaveCount(1);
            }
        }
Beispiel #14
0
 public void CreateLongPathStream()
 {
     using (var cleaner = new TestFileCleaner(false))
     {
         string longPath = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 300);
         FileService fileService = new FileService();
         fileService.CreateDirectory(longPath);
         string filePath = Paths.Combine(longPath, Path.GetRandomFileName());
         using (Stream fileStream = fileService.CreateFileStream(filePath, FileMode.CreateNew))
         {
             fileStream.Should().NotBeNull();
         }
     }
 }
Beispiel #15
0
 public void FileInfoHasCanonicalPaths()
 {
     using (var cleaner = new TestFileCleaner(useDotNet: false))
     {
         string filePath = cleaner.GetTestPath() + "UPPER";
         cleaner.FileService.WriteAllText(filePath, "FileInfoHasCanonicalPaths");
         var info = cleaner.FileService.GetPathInfo(filePath.ToLowerInvariant());
         info.Path.Should().Be(filePath);
     }
 }
Beispiel #16
0
 public void OpenNonExistantStream(string appendix)
 {
     using (var cleaner = new TestFileCleaner(useDotNet: false))
     {
         string filePath = cleaner.GetTestPath() + appendix;
         Action action = () => cleaner.FileService.CreateFileStream(filePath, FileMode.Open);
         action.ShouldThrow<FileNotFoundException>();
     }
 }