public void TestDirectory() { var fs = new PhysicalFileSystem(); var pathInfo = fs.ConvertPathFromInternal(SystemPath); var pathToCreate = pathInfo / "TestCreateDirectory"; var systemPathToCreate = fs.ConvertPathToInternal(pathToCreate); var movedDirectory = pathInfo / "TestCreateDirectoryMoved"; var systemMovedDirectory = fs.ConvertPathToInternal(movedDirectory); try { // CreateDirectory Assert.False(Directory.Exists(systemPathToCreate)); fs.CreateDirectory(pathToCreate); Assert.True(Directory.Exists(systemPathToCreate)); // DirectoryExists Assert.True(fs.DirectoryExists(pathToCreate)); Assert.False(fs.DirectoryExists(pathToCreate / "not_found")); // MoveDirectory fs.MoveDirectory(pathToCreate, movedDirectory); Assert.False(Directory.Exists(systemPathToCreate)); Assert.True(fs.DirectoryExists(movedDirectory)); // Delete the directory fs.DeleteDirectory(movedDirectory, false); Assert.False(Directory.Exists(systemMovedDirectory)); } finally { SafeDeleteDirectory(systemPathToCreate); SafeDeleteDirectory(systemMovedDirectory); } }
async Task AddProjectToSolution(string projectPath) { var process = new Process(); process.StartInfo.FileName = "dotnet"; process.StartInfo.WorkingDirectory = fs.ConvertPathToInternal(projectRoot); process.StartInfo.Arguments = $"sln add {projectPath}"; process.Start(); await process.WaitForExitAsync(); Commands.Stage(repo, $"{fs.ConvertPathToInternal(projectRoot / $"AdventOfCode.sln")}"); }
public void 列舉根路徑內的子資料夾() { using var fileSystem = new PhysicalFileSystem(); var executingAssembly = Assembly.GetExecutingAssembly(); var rootPath = Path.GetDirectoryName(executingAssembly.Location); var rootUPath = fileSystem.ConvertPathFromInternal(rootPath); var rootUPath1 = fileSystem.ConvertPathToInternal(rootUPath); var subName = "TestFolder"; var subPath = $"{rootUPath}/{subName}"; var subPath1 = $"{subPath}/1"; var subFile1 = $"{subPath}/1/1.txt"; var subPath1_1 = $"{subPath}/1/1_1"; var subFile1_1 = $"{subPath}/1/1_1/1_1.txt"; var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1"; var subPath2 = $"{subPath}/2"; var content = "This is test string"; var contentBytes = Encoding.UTF8.GetBytes(content); if (fileSystem.DirectoryExists(subPath1_1_1) == false) { fileSystem.CreateDirectory(subPath1_1_1); } if (fileSystem.DirectoryExists(subPath2) == false) { fileSystem.CreateDirectory(subPath2); } if (fileSystem.FileExists(subFile1) == false) { using var stream = fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); stream.Write(contentBytes, 0, contentBytes.Length); } if (fileSystem.FileExists(subFile1_1) == false) { using var stream = fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); stream.Write(contentBytes, 0, contentBytes.Length); } var directoryEntries = fileSystem.EnumerateDirectoryEntries(subPath); foreach (var entry in directoryEntries) { Console.WriteLine(entry.Path); } Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1)); Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1)); Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1)); Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2)); fileSystem.DeleteDirectory(subPath, true); }
public void TestEnumerate() { var fs = new PhysicalFileSystem(); var path = fs.ConvertPathFromInternal(SystemPath); var files = fs.EnumerateFiles(path).Select(p => fs.ConvertPathToInternal(p)).ToList(); var expectedfiles = Directory.EnumerateFiles(SystemPath).ToList(); Assert.Equal(expectedfiles, files); var dirs = fs.EnumerateDirectories(path / "../../..").Select(p => fs.ConvertPathToInternal(p)).ToList(); var expecteddirs = Directory.EnumerateDirectories(Path.GetFullPath(Path.Combine(SystemPath, "..\\..\\.."))).ToList(); Assert.Equal(expecteddirs, dirs); var paths = fs.EnumeratePaths(path / "../..").Select(p => fs.ConvertPathToInternal(p)).ToList(); var expectedPaths = Directory.EnumerateFileSystemEntries(Path.GetFullPath(Path.Combine(SystemPath, "..\\.."))).ToList(); Assert.Equal(expectedPaths, paths); }
public Templator(LocalDate date) { if (date.Month != 12) { throw new InvalidOperationException("Advent of code only runs in december!"); } fs = new PhysicalFileSystem(); var path = fs.ConvertPathFromInternal(AppDomain.CurrentDomain.BaseDirectory); projectRoot = path / "../../../../../"; repo = new Repository(fs.ConvertPathToInternal(projectRoot)); challengeDate = date; }
public PhysicalFileSystemWatcher(PhysicalFileSystem fileSystem, UPath path) : base(fileSystem, path) { _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); _watcher = new System.IO.FileSystemWatcher(_fileSystem.ConvertPathToInternal(path)) { Filter = "*" }; _watcher.Changed += (sender, args) => RaiseChanged(Remap(args)); _watcher.Created += (sender, args) => RaiseCreated(Remap(args)); _watcher.Deleted += (sender, args) => RaiseDeleted(Remap(args)); _watcher.Error += (sender, args) => RaiseError(Remap(args)); _watcher.Renamed += (sender, args) => RaiseRenamed(Remap(args)); }
public static string ToOsPath(this UPath path) { return(FileSystem.ConvertPathToInternal(path)); }
public void TestFileExceptions() { var fs = new PhysicalFileSystem(); var path = fs.ConvertPathFromInternal(SystemPath); var fileName = $"toto-{Guid.NewGuid()}.txt"; var filePath = path / fileName; fs.CreateFile(filePath).Dispose(); var filePathNotExist = path / "FileDoesNotExist.txt"; var systemFilePath = Path.Combine(SystemPath, fileName); // Try to create a folder on an unauthorized location try { // CreateFile Assert.Throws <UnauthorizedAccessException>(() => fs.CreateFile("/toto.txt")); // Length Assert.Throws <UnauthorizedAccessException>(() => fs.GetFileLength("/toto.txt")); // ConvertPathFromInternal / ConvertPathToInternal Assert.Throws <NotSupportedException>(() => fs.ConvertPathFromInternal(@"\\network\toto.txt")); Assert.Throws <NotSupportedException>(() => fs.ConvertPathFromInternal(@"zx:\toto.txt")); Assert.Throws <ArgumentException>(() => fs.ConvertPathToInternal(@"/toto.txt")); Assert.Throws <ArgumentException>(() => fs.ConvertPathToInternal(@"/mnt/yo/toto.txt")); // LastWriteTime, LastAccessTime, CreationTime Assert.Throws <DirectoryNotFoundException>(() => fs.GetLastWriteTime("/toto.txt")); Assert.Throws <DirectoryNotFoundException>(() => fs.GetLastAccessTime("/toto.txt")); Assert.Throws <DirectoryNotFoundException>(() => fs.GetCreationTime("/toto.txt")); Assert.Throws <UnauthorizedAccessException>(() => fs.SetLastWriteTime("/", DateTime.Now)); Assert.Throws <UnauthorizedAccessException>(() => fs.SetLastAccessTime("/", DateTime.Now)); Assert.Throws <UnauthorizedAccessException>(() => fs.SetCreationTime("/", DateTime.Now)); Assert.Throws <UnauthorizedAccessException>(() => fs.SetLastWriteTime("/mnt", DateTime.Now)); Assert.Throws <UnauthorizedAccessException>(() => fs.SetLastAccessTime("/mnt", DateTime.Now)); Assert.Throws <UnauthorizedAccessException>(() => fs.SetCreationTime("/mnt", DateTime.Now)); Assert.Throws <DirectoryNotFoundException>(() => fs.SetLastWriteTime("/toto.txt", DateTime.Now)); Assert.Throws <DirectoryNotFoundException>(() => fs.SetLastAccessTime("/toto.txt", DateTime.Now)); Assert.Throws <DirectoryNotFoundException>(() => fs.SetCreationTime("/toto.txt", DateTime.Now)); // FileAttributes Assert.Throws <DirectoryNotFoundException>(() => fs.GetAttributes("/toto.txt")); Assert.Throws <DirectoryNotFoundException>(() => fs.SetAttributes("/toto.txt", FileAttributes.ReadOnly)); Assert.Throws <UnauthorizedAccessException>(() => fs.SetAttributes("/", FileAttributes.ReadOnly)); // CopyFile Assert.Throws <UnauthorizedAccessException>(() => fs.CopyFile("/toto.txt", filePath, true)); Assert.Throws <UnauthorizedAccessException>(() => fs.CopyFile(filePath, "/toto.txt", true)); // Delete Assert.Throws <UnauthorizedAccessException>(() => fs.DeleteFile("/toto.txt")); // Move Assert.Throws <UnauthorizedAccessException>(() => fs.MoveFile("/toto.txt", filePath)); Assert.Throws <UnauthorizedAccessException>(() => fs.MoveFile(filePath, "/toto.txt")); // ReplaceFile Assert.Throws <FileNotFoundException>(() => fs.ReplaceFile("/toto.txt", filePath, filePath, true)); Assert.Throws <FileNotFoundException>(() => fs.ReplaceFile(filePath, "/toto.txt", filePath, true)); Assert.Throws <UnauthorizedAccessException>(() => fs.ReplaceFile(filePath, filePath, "/toto.txt", true)); Assert.Throws <FileNotFoundException>(() => fs.ReplaceFile(filePathNotExist, filePath, filePath, true)); Assert.Throws <FileNotFoundException>(() => fs.ReplaceFile(filePath, filePathNotExist, filePath, true)); } finally { SafeDeleteFile(systemFilePath); } }
public void TestFile() { var fs = new PhysicalFileSystem(); var path = fs.ConvertPathFromInternal(SystemPath); var fileName = $"toto-{Guid.NewGuid()}.txt"; var filePath = path / fileName; var filePathDest = path / Path.ChangeExtension(fileName, "dest"); var filePathBack = path / Path.ChangeExtension(fileName, "bak"); var systemFilePath = Path.Combine(SystemPath, fileName); var systemFilePathDest = fs.ConvertPathToInternal(filePathDest); var systemFilePathBack = fs.ConvertPathToInternal(filePathBack); try { // CreateFile / OpenFile var fileStream = fs.CreateFile(filePath); var buffer = Encoding.UTF8.GetBytes("This is a test"); fileStream.Write(buffer, 0, buffer.Length); fileStream.Dispose(); // FileLength Assert.Equal(buffer.Length, fs.GetFileLength(filePath)); // LastAccessTime // LastWriteTime // CreationTime Assert.Equal(File.GetLastWriteTime(systemFilePath), fs.GetLastWriteTime(filePath)); Assert.Equal(File.GetLastAccessTime(systemFilePath), fs.GetLastAccessTime(filePath)); Assert.Equal(File.GetCreationTime(systemFilePath), fs.GetCreationTime(filePath)); var lastWriteTime = DateTime.Now + TimeSpan.FromSeconds(10); var lastAccessTime = DateTime.Now + TimeSpan.FromSeconds(11); var creationTime = DateTime.Now + TimeSpan.FromSeconds(12); fs.SetLastWriteTime(filePath, lastWriteTime); fs.SetLastAccessTime(filePath, lastAccessTime); fs.SetCreationTime(filePath, creationTime); Assert.Equal(lastWriteTime, fs.GetLastWriteTime(filePath)); Assert.Equal(lastAccessTime, fs.GetLastAccessTime(filePath)); Assert.Equal(creationTime, fs.GetCreationTime(filePath)); // FileAttributes Assert.Equal(File.GetAttributes(systemFilePath), fs.GetAttributes(filePath)); var attributes = fs.GetAttributes(filePath); attributes |= FileAttributes.ReadOnly; fs.SetAttributes(filePath, attributes); Assert.Equal(File.GetAttributes(systemFilePath), fs.GetAttributes(filePath)); attributes &= ~FileAttributes.ReadOnly; fs.SetAttributes(filePath, attributes); Assert.Equal(File.GetAttributes(systemFilePath), fs.GetAttributes(filePath)); // FileExists Assert.True(File.Exists(systemFilePath)); Assert.True(fs.FileExists(filePath)); // CopyFile fs.CopyFile(filePath, filePathDest, true); Assert.True(File.Exists(systemFilePathDest)); Assert.True(fs.FileExists(filePathDest)); // DeleteFile fs.DeleteFile(filePath); Assert.False(File.Exists(systemFilePath)); Assert.False(fs.FileExists(filePath)); // MoveFile fs.MoveFile(filePathDest, filePath); Assert.False(File.Exists(systemFilePathDest)); Assert.False(fs.FileExists(filePathDest)); Assert.True(File.Exists(systemFilePath)); Assert.True(fs.FileExists(filePath)); // ReplaceFile // copy file to filePathDest fs.CopyFile(filePath, filePathDest, true); // Change src file var filestream2 = fs.OpenFile(filePath, FileMode.Open, FileAccess.ReadWrite); var buffer2 = Encoding.UTF8.GetBytes("This is a test 123"); filestream2.Write(buffer2, 0, buffer2.Length); filestream2.Dispose(); Assert.Equal(buffer2.Length, fs.GetFileLength(filePath)); // Perform ReplaceFile fs.ReplaceFile(filePath, filePathDest, filePathBack, true); Assert.False(fs.FileExists(filePath)); Assert.True(fs.FileExists(filePathDest)); Assert.True(fs.FileExists(filePathBack)); Assert.Equal(buffer2.Length, fs.GetFileLength(filePathDest)); Assert.Equal(buffer.Length, fs.GetFileLength(filePathBack)); // RootFileSystem fs.GetLastWriteTime("/"); fs.GetLastAccessTime("/"); fs.GetCreationTime("/"); fs.GetLastWriteTime("/mnt"); fs.GetLastAccessTime("/mnt"); fs.GetCreationTime("/mnt"); fs.GetLastWriteTime("/mnt/c"); fs.GetLastAccessTime("/mnt/c"); fs.GetCreationTime("/mnt/c"); fs.GetAttributes("/mnt/c"); var sysAttr = FileAttributes.Directory | FileAttributes.System | FileAttributes.ReadOnly; Assert.True((fs.GetAttributes("/") & (sysAttr)) == sysAttr); Assert.True((fs.GetAttributes("/mnt") & (sysAttr)) == sysAttr); } finally { SafeDeleteFile(systemFilePath); SafeDeleteFile(systemFilePathDest); SafeDeleteFile(systemFilePathBack); } }