예제 #1
0
        public void TestExceptions()
        {
            var fs = new MemoryFileSystem();

            Assert.Throws <ArgumentNullException>(() => fs.AppendAllText("/a.txt", null));
            Assert.Throws <ArgumentNullException>(() => fs.WriteAllText("/a.txt", null));
            Assert.Throws <ArgumentNullException>(() => fs.WriteAllText("/a.txt", "content", null));
            Assert.Throws <ArgumentNullException>(() => fs.WriteAllText("/a.txt", null, null));
            Assert.Throws <ArgumentNullException>(() => fs.ReadAllText("/a.txt", null));
            Assert.Throws <ArgumentNullException>(() => fs.ReadAllLines("/a.txt", null));
            Assert.Throws <ArgumentNullException>(() => fs.WriteAllBytes("/a", null));
            Assert.Throws <ArgumentNullException>(() => fs.AppendAllText("/a", null, null));
            Assert.Throws <ArgumentNullException>(() => fs.AppendAllText("/a", "content", null));
            Assert.Throws <ArgumentNullException>(() => fs.EnumeratePaths("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumeratePaths("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFiles("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFiles("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateDirectories("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateDirectories("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFileEntries("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFileEntries("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateDirectoryEntries("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateDirectoryEntries("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFileSystemEntries("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFileSystemEntries("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <FileNotFoundException>(() => fs.GetFileEntry("/a.txt"));
            Assert.Throws <DirectoryNotFoundException>(() => fs.GetDirectoryEntry("/a"));
        }
예제 #2
0
        public void 列舉根路徑內的子資料夾()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();
            var subName = "../../path";

            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 uPath = UPath.Combine(rootUPath, "..");

            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));
        }