PageRenderTime 77ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Kudu.Core.Test/FileSystemHelpersTest.cs

https://github.com/moacap/kudu
C# | 277 lines | 229 code | 48 blank | 0 comment | 2 complexity | 7de3afb8542980359887cf0535a03ab8 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Abstractions;
  5. using System.Linq;
  6. using Kudu.Core.Deployment;
  7. using Kudu.Core.Infrastructure;
  8. using Moq;
  9. using Xunit;
  10. namespace Kudu.Core.Test
  11. {
  12. public class FileSystemHelpersTest
  13. {
  14. [Fact]
  15. public void EnsureDirectoryCreatesDirectoryIfNotExists()
  16. {
  17. var fileSystem = new Mock<IFileSystem>();
  18. var directory = new Mock<DirectoryBase>();
  19. fileSystem.Setup(m => m.Directory).Returns(directory.Object);
  20. directory.Setup(m => m.Exists("foo")).Returns(false);
  21. string path = FileSystemHelpers.EnsureDirectory(fileSystem.Object, "foo");
  22. Assert.Equal("foo", path);
  23. directory.Verify(m => m.CreateDirectory("foo"), Times.Once());
  24. }
  25. [Fact]
  26. public void EnsureDirectoryDoesNotCreateDirectoryIfNotExists()
  27. {
  28. var fileSystem = new Mock<IFileSystem>();
  29. var directory = new Mock<DirectoryBase>();
  30. fileSystem.Setup(m => m.Directory).Returns(directory.Object);
  31. directory.Setup(m => m.Exists("foo")).Returns(true);
  32. string path = FileSystemHelpers.EnsureDirectory(fileSystem.Object, "foo");
  33. Assert.Equal("foo", path);
  34. directory.Verify(m => m.CreateDirectory("foo"), Times.Never());
  35. }
  36. [Fact]
  37. public void SmartCopyCopiesFilesFromSourceToDestination()
  38. {
  39. DirectoryWrapper sourceDirectory = GetDirectory(@"a:\test\", filePaths: new[] { @"a.txt", "b.txt", ".bar", ".deployment" });
  40. DirectoryWrapper destinationDirectory = GetDirectory(@"b:\foo\", exists: false);
  41. DeploymentHelper.SmartCopy(@"a:\test\", @"b:\foo\", null, sourceDirectory.Directory, destinationDirectory.Directory, path => GetDirectory(path, exists: false).Directory);
  42. destinationDirectory.VerifyCreated();
  43. sourceDirectory.VerifyCopied("a.txt", @"b:\foo\a.txt");
  44. sourceDirectory.VerifyCopied("b.txt", @"b:\foo\b.txt");
  45. sourceDirectory.VerifyCopied(".bar", @"b:\foo\.bar");
  46. sourceDirectory.VerifyNotCopied(".deployment", @"b:\foo\.deployment");
  47. }
  48. [Fact]
  49. public void SmartCopyDeletesFilesThatDontExistInSourceIfNoPrevious()
  50. {
  51. DirectoryWrapper sourceDirectory = GetDirectory(@"a:\test\", filePaths: new[] { @"a.txt", "b.txt" });
  52. DirectoryWrapper destinationDirectory = GetDirectory(@"b:\foo\", filePaths: new[] { "c.txt" });
  53. DeploymentHelper.SmartCopy(@"a:\test\", @"b:\foo\", null, sourceDirectory.Directory, destinationDirectory.Directory, path => GetDirectory(path, exists: false).Directory);
  54. sourceDirectory.VerifyCopied("a.txt", @"b:\foo\a.txt");
  55. sourceDirectory.VerifyCopied("b.txt", @"b:\foo\b.txt");
  56. destinationDirectory.VerifyDeleted("c.txt");
  57. }
  58. [Fact]
  59. public void SmartCopyOnlyDeletesFilesThatDontExistInSourceIfAlsoInPrevious()
  60. {
  61. var paths = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
  62. { "c.txt" }
  63. };
  64. DirectoryWrapper sourceDirectory = GetDirectory(@"a:\test\", filePaths: new[] { @"a.txt", "b.txt" });
  65. DirectoryWrapper destinationDirectory = GetDirectory(@"b:\foo\", filePaths: new[] { "c.txt", "generated.log" });
  66. DeploymentHelper.SmartCopy(@"a:\test\", @"b:\foo\", paths.Contains, sourceDirectory.Directory, destinationDirectory.Directory, path => GetDirectory(path, exists: false).Directory);
  67. sourceDirectory.VerifyCopied("a.txt", @"b:\foo\a.txt");
  68. sourceDirectory.VerifyCopied("b.txt", @"b:\foo\b.txt");
  69. destinationDirectory.VerifyDeleted("c.txt");
  70. destinationDirectory.VerifyNotDeleted("generated.log");
  71. }
  72. [Fact]
  73. public void SmartCopyAlwaysOverwritesFiles()
  74. {
  75. var previousPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
  76. { "a.txt" }
  77. };
  78. DirectoryWrapper sourceDirectory = GetDirectory(@"a:\source\", filePaths: new[] { @"a.txt" });
  79. DirectoryWrapper destinationDirectory = GetDirectory(@"b:\target\", filePaths: new[] { "a.txt" });
  80. sourceDirectory.Files["a.txt"].Setup(m => m.LastWriteTimeUtc).Returns(new DateTime(2010, 11, 19));
  81. destinationDirectory.Files["a.txt"].Setup(m => m.LastWriteTimeUtc).Returns(new DateTime(2011, 11, 19));
  82. DeploymentHelper.SmartCopy(@"a:\source\", @"b:\target\", previousPaths.Contains, sourceDirectory.Directory, destinationDirectory.Directory, path => GetDirectory(path, exists: false).Directory);
  83. sourceDirectory.VerifyCopied(@"a.txt", @"b:\target\a.txt");
  84. }
  85. [Fact]
  86. public void SmartCopyCopiesSubDirectoriesAndFiles()
  87. {
  88. DirectoryWrapper sourceSub = GetDirectory(@"a:\source\sub1", filePaths: new[] { "b.js" });
  89. DirectoryWrapper sourceDirectory = GetDirectory(@"a:\source\",
  90. filePaths: new[] { @"a.txt" },
  91. directories: new[] { sourceSub });
  92. DirectoryWrapper destinationDirectory = GetDirectory(@"b:\target\", exists: false);
  93. DeploymentHelper.SmartCopy(@"a:\source\", @"b:\target\", null, sourceDirectory.Directory, destinationDirectory.Directory, path =>
  94. {
  95. var newDir = GetDirectory(path, exists: false);
  96. string shortName = GetShortName(destinationDirectory.Directory.FullName, path);
  97. destinationDirectory.Directories[shortName] = newDir;
  98. return newDir.Directory;
  99. });
  100. destinationDirectory.VerifyCreated();
  101. destinationDirectory.VerifyCreated("sub1");
  102. sourceDirectory.VerifyCopied("a.txt", @"b:\target\a.txt");
  103. sourceSub.VerifyCopied("b.js", @"b:\target\sub1\b.js");
  104. }
  105. [Fact]
  106. public void SmartCopyDeletesSubDirectoryIfNoPreviousAndDirectoryDoesnotExistInSource()
  107. {
  108. DirectoryWrapper sourceDirectory = GetDirectory(@"a:\source\", filePaths: new[] { "b.txt" });
  109. DirectoryWrapper destinationSub = GetDirectory(@"b:\target\sub3", filePaths: new[] { "o.js" });
  110. DirectoryWrapper destinationDirectory = GetDirectory(@"b:\target\", directories: new[] { destinationSub });
  111. DeploymentHelper.SmartCopy(@"a:\source\", @"b:\target\", null, sourceDirectory.Directory, destinationDirectory.Directory, path => GetDirectory(path).Directory);
  112. destinationSub.VerifyDeleted();
  113. sourceDirectory.VerifyCopied("b.txt", @"b:\target\b.txt");
  114. }
  115. [Fact]
  116. public void SmartCopyOnlyDeletesSubDirectoryIfExistsInPrevious()
  117. {
  118. var previousPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
  119. { @"sub" },
  120. { @"sub\b.txt" }
  121. };
  122. DirectoryWrapper sourceDirectory = GetDirectory(@"a:\source\");
  123. DirectoryWrapper destinationSub1 = GetDirectory(@"b:\target\sub", filePaths: new[] { "b.js" });
  124. DirectoryWrapper destinationSub2 = GetDirectory(@"b:\target\sub3", filePaths: new[] { "o.js" });
  125. DirectoryWrapper destinationDirectory = GetDirectory(@"b:\target\", directories: new[] { destinationSub1, destinationSub2 });
  126. DeploymentHelper.SmartCopy(@"a:\source\", @"b:\target\", previousPaths.Contains, sourceDirectory.Directory, destinationDirectory.Directory, path => GetDirectory(path).Directory);
  127. destinationSub1.VerifyDeleted();
  128. destinationSub2.VerifyNotDeleted();
  129. }
  130. private static DirectoryWrapper GetDirectory(string fullName,
  131. IEnumerable<string> filePaths = null,
  132. IEnumerable<DirectoryWrapper> directories = null,
  133. bool hidden = false,
  134. bool exists = true)
  135. {
  136. filePaths = filePaths ?? Enumerable.Empty<string>();
  137. directories = directories ?? Enumerable.Empty<DirectoryWrapper>();
  138. var files = filePaths.Select(path =>
  139. {
  140. var mock = new Mock<FileInfoBase>();
  141. bool fileExists = true;
  142. mock.Setup(m => m.Exists).Returns(() => fileExists);
  143. mock.Setup(m => m.FullName).Returns(Path.Combine(fullName, path));
  144. mock.Setup(m => m.Name).Returns(path);
  145. mock.Setup(m => m.Delete()).Callback(() => fileExists = false);
  146. return mock;
  147. })
  148. .ToDictionary(f => f.Object.Name, StringComparer.OrdinalIgnoreCase);
  149. var directoryMapping = directories.ToDictionary(d => GetShortName(fullName, d.Directory.FullName), StringComparer.OrdinalIgnoreCase);
  150. var mockDir = new Mock<DirectoryInfoBase>();
  151. mockDir.Setup(m => m.FullName).Returns(fullName);
  152. mockDir.Setup(m => m.Name).Returns(fullName.Replace(Path.GetDirectoryName(fullName) + @"\", ""));
  153. mockDir.Setup(m => m.GetFiles()).Returns(files.Values.Select(f => f.Object).ToArray());
  154. mockDir.Setup(m => m.GetDirectories()).Returns(directoryMapping.Values.Select(d => d.Directory).ToArray());
  155. mockDir.Setup(m => m.Exists).Returns(exists);
  156. if (hidden)
  157. {
  158. mockDir.Setup(m => m.Attributes).Returns(FileAttributes.Hidden);
  159. }
  160. return new DirectoryWrapper
  161. {
  162. MockDirectory = mockDir,
  163. Files = files,
  164. Directories = directoryMapping
  165. };
  166. }
  167. private static string GetShortName(string prefix, string path)
  168. {
  169. return path.Substring(prefix.Length).Trim(Path.DirectorySeparatorChar);
  170. }
  171. private class DirectoryWrapper
  172. {
  173. public DirectoryInfoBase Directory
  174. {
  175. get
  176. {
  177. return MockDirectory.Object;
  178. }
  179. }
  180. public Mock<DirectoryInfoBase> MockDirectory { get; set; }
  181. public IDictionary<string, Mock<FileInfoBase>> Files { get; set; }
  182. public IDictionary<string, DirectoryWrapper> Directories { get; set; }
  183. public void VerifyCopied(string path, string toPath)
  184. {
  185. Assert.True(Files.ContainsKey(path));
  186. Files[path].Verify(m => m.CopyTo(toPath, true), Times.Once());
  187. }
  188. public void VerifyNotCopied(string path, string toPath = null)
  189. {
  190. Assert.True(Files.ContainsKey(path));
  191. if (String.IsNullOrEmpty(toPath))
  192. {
  193. Files[path].Verify(m => m.CopyTo(It.IsAny<string>(), true), Times.Never());
  194. }
  195. else
  196. {
  197. Files[path].Verify(m => m.CopyTo(toPath, true), Times.Never());
  198. }
  199. }
  200. public void VerifyDeleted()
  201. {
  202. MockDirectory.Verify(m => m.Delete(true), Times.Once());
  203. }
  204. public void VerifyDeleted(string path)
  205. {
  206. Assert.True(Files.ContainsKey(path));
  207. Files[path].Verify(m => m.Delete(), Times.Once());
  208. }
  209. public void VerifyNotDeleted()
  210. {
  211. MockDirectory.Verify(m => m.Delete(true), Times.Never());
  212. }
  213. public void VerifyNotDeleted(string path)
  214. {
  215. Assert.True(Files.ContainsKey(path));
  216. Files[path].Verify(m => m.Delete(), Times.Never());
  217. }
  218. public void VerifyCreated()
  219. {
  220. MockDirectory.Verify(m => m.Create(), Times.Once());
  221. }
  222. public void VerifyCreated(string path)
  223. {
  224. Assert.True(Directories.ContainsKey(path));
  225. Directories[path].VerifyCreated();
  226. }
  227. }
  228. }
  229. }