コード例 #1
0
        public void DirectoryPathCanBeParsed()
        {
            var path = "a/b/c";
            var root = new MemoryDirectory("root", null);
            var dir = root.CreateDirectory(path);

            Assert.Equal("c", dir.Name);
            Assert.Equal("b", dir.Parent.Name);
            Assert.Equal("a", dir.Parent.Parent.Name);
            Assert.Equal(root, dir.Parent.Parent.Parent);
        }
コード例 #2
0
        public void FilePathWithDirectoryCanBeParsed()
        {
            var path = "a/b/c.txt";
            var expected = "test";
            var dir = new MemoryDirectory("root", null);
            var file = dir.CreateFileFromText(path, expected);
            string actual;
            using (StreamReader reader = new StreamReader(file.StreamProvider.OpenRead()))
            {
                actual = reader.ReadToEnd();
            }

            Assert.Equal(expected, actual);
            Assert.Equal("b", file.Directory.Name);
            Assert.Equal("a", file.Directory.Parent.Name);
            Assert.Equal(dir, file.Directory.Parent.Parent);
        }
コード例 #3
0
        public IDirectory CreateDirectory(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            name = name.Trim('/');
            var    index = name.IndexOf('/');
            string dirName, subPath = null;

            if (index > 0)
            {
                dirName = name.Substring(0, index);
                subPath = name.Substring(index + 1);
            }
            else
            {
                dirName = name;
            }

            IDirectory dir;

            if (DirectoryExists(dirName))
            {
                dir = _subDirs.First(d => string.Equals(d.Name, dirName, StringComparison.OrdinalIgnoreCase));
            }
            else
            {
                dir = new MemoryDirectory(dirName, this);
                _subDirs.Add(dir);
            }

            if (string.IsNullOrEmpty(subPath))
            {
                return(dir);
            }
            else
            {
                return(dir.CreateDirectory(subPath));
            }
        }
コード例 #4
0
        public IDirectory CreateDirectory(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            name = name.Trim('/');
            var index = name.IndexOf('/');
            string dirName, subPath = null;
            if (index > 0)
            {
                dirName = name.Substring(0, index);
                subPath = name.Substring(index + 1);
            }
            else
            {
                dirName = name;
            }

            IDirectory dir;
            if (DirectoryExists(dirName))
            {
                dir = _subDirs.First(d => string.Equals(d.Name, dirName, StringComparison.OrdinalIgnoreCase));
            }
            else
            {
                dir = new MemoryDirectory(dirName, this);
                _subDirs.Add(dir);
            }

            if (string.IsNullOrEmpty(subPath))
            {
                return dir;
            }
            else
            {
                return dir.CreateDirectory(subPath);
            }
        }
コード例 #5
0
        public void DiskFileCanBeRead()
        {
            string expected = "test";
            var dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            var subDir = dir.CreateSubdirectory(expected);
            using (var writer = File.CreateText(Path.Combine(subDir.FullName, "test.html")))
            {
                writer.Write(expected);
            }

            var root = new MemoryDirectory("root", null);
            var file = root.CreateFileFromDisk("test.html", subDir.GetFiles().First());
            string actual;
            using (StreamReader reader = new StreamReader(file.StreamProvider.OpenRead()))
            {
                actual = reader.ReadToEnd();
            }

            Assert.Equal(expected, actual);

            subDir.Delete(true);
        }
コード例 #6
0
        // TODO: convert this class to extension methods for IDirectory
        /// <summary>
        /// This method is used to convert legacy code to IDirectory source.
        /// </summary>
        /// <returns></returns>
        public IDirectory ToDirectory()
        {
            var root = new MemoryDirectory("root", null);
            var bin = root.CreateDirectory("bin");

            // generated all text based files. they are mostly configuration files.                
            if (TextFiles != null)
            {
                foreach (var textFileName in TextFiles.Keys)
                {
                    root.CreateFileFromText(textFileName, TextFiles[textFileName]);
                }
            }

            var currentAssemblyLocation = Assembly.GetExecutingAssembly().Location;
            if (!BinaryFiles.Contains(currentAssemblyLocation))
            {
                BinaryFiles.Add(currentAssemblyLocation);
            }

            // copy all required binary files
            if (BinaryFiles != null)
            {
                foreach (var file in BinaryFiles)
                {
                    var fileInfo = new FileInfo(file);
                    if (fileInfo.Exists)
                    {
                        bin.CreateFileFromDisk(fileInfo.Name, fileInfo);
                    }
                }
            }

            return root;
        }
コード例 #7
0
        private IDirectory GetDiskSourceDirectory(WebHostContext context)
        {
            var descriptor = context.Deployment;
            if (string.IsNullOrEmpty(descriptor.ScopePath))
            {
                throw new InvalidOperationException("Path can't be null or empty for directory deployment");
            }

            var solutionRootPath = GetSolutionDir();
            if (string.IsNullOrEmpty(solutionRootPath))
            {
                throw new InvalidOperationException(
                    string.Format(
                        "Configuration Setting:{0} can't be null or empty for directory deployment",
                        SolutionDirAppSettingName));
            }

            var sourceDirInfo = new DirectoryInfo(Path.Combine(solutionRootPath, descriptor.ScopePath));
            if (!sourceDirInfo.Exists)
            {
                throw new InvalidOperationException(
                    string.Format(
                        "Web app path:'{0}' doesn't exist",
                        sourceDirInfo.FullName));
            }

            var directory = new MemoryDirectory("root", null);
            directory.CopyFromDisk(sourceDirInfo);

            return directory;
        }
コード例 #8
0
        public void DiskDirectoryCanBeCopied()
        {
            string expected = "test";
            var dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            var subDir = dir.CreateSubdirectory(expected);
            using (var writer = File.CreateText(Path.Combine(subDir.FullName, "test.html")))
            {
                writer.Write(expected);
            }
            var subSubDir = subDir.CreateSubdirectory(expected);
            using (var writer = File.CreateText(Path.Combine(subSubDir.FullName, "test.html")))
            {
                writer.Write(expected);
            }

            var root = new MemoryDirectory("root", null);
            root.CopyFromDisk(subDir);

            Assert.True(root.DirectoryExists(expected));

            var file = root.FindFile("test.html");
            Assert.Equal(expected, file.ReadAsString());

            file = root.FindDirectory(expected).FindFile("test.html");
            Assert.Equal(expected, file.ReadAsString());

            subDir.Delete(true);
        }
コード例 #9
0
        public void DirectoryCanBeCopiedToDisk()
        {
            string expected = "test";
            string expectedFileName = "test.html";
            var root = new MemoryDirectory("root", null);
            root.CreateFileFromText(expectedFileName, expected);
            var dir = root.CreateDirectory(expected);
            dir.CreateFileFromText(expectedFileName, expected);

            var diskDir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "root"));
            root.CopyToDisk(diskDir);

            Assert.True(Directory.Exists(Path.Combine(diskDir.FullName, expected)));
            Assert.True(File.Exists(Path.Combine(diskDir.FullName, expectedFileName)));
            Assert.True(File.Exists(Path.Combine(diskDir.FullName, expected, expectedFileName)));

            string content;
            using (var reader = new StreamReader(Path.Combine(diskDir.FullName, expectedFileName)))
            {
                content = reader.ReadToEnd();
            }
            Assert.Equal(expected, content);

            using (var reader = new StreamReader(Path.Combine(diskDir.FullName, expected, expectedFileName)))
            {
                content = reader.ReadToEnd();
            }
            Assert.Equal(expected, content);
            diskDir.Delete(true);
        }
コード例 #10
0
ファイル: FtpTests.cs プロジェクト: ZhaoYngTest01/WebApi
        public async Task UploadMemoryDirectoryRecursivelyShouldWork()
        {
            var dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            var subDir = dir.CreateSubdirectory("test");
            using (var writer = File.CreateText(Path.Combine(subDir.FullName, "test.html")))
            {
                writer.Write("Test");
            }
            var subSubDir = subDir.CreateSubdirectory("test");
            using (var writer = File.CreateText(Path.Combine(subSubDir.FullName, "test.html")))
            {
                writer.Write("Test");
            }

            var directory = new MemoryDirectory("root", null);
            directory.CopyFromDisk(subDir);
            directory.CreateDirectory("memory").CreateFileFromText("memory.txt", "memory");

            var ftp = CreateFtp();
            var path = "site/wwwroot/";
            var folderName = "test/";
            ftp.ChangeDirectory(path);
            await ftp.MakeDirectoryAsync(folderName);
            Assert.True(await ftp.DirectoryExistsAsync(folderName));
            await ftp.UploadDirectoryRecursivelyAsync(folderName, directory);
            Assert.True(await ftp.FileExistsAsync("test/test/test.html"));
            Assert.True(await ftp.FileExistsAsync("test/memory/memory.txt"));
            await ftp.RemoveDirectoryRecursivelyAsync(folderName);
            Assert.False(await ftp.DirectoryExistsAsync(folderName));

            subDir.Delete(true);
        }