/src/Tests/Microsoft.NET.TestFramework/Assertions/DirectoryInfoAssertions.cs

https://gitlab.com/dotnetfoundation/sdk · C# · 186 lines · 146 code · 37 blank · 3 comment · 7 complexity · 460c41564c781a6a2bff8f0276838c21 MD5 · raw file

  1. // Copyright (c) .NET Foundation and contributors. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. using FluentAssertions;
  4. using FluentAssertions.Execution;
  5. using Microsoft.DotNet.Cli.Utils;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace Microsoft.NET.TestFramework.Assertions
  12. {
  13. public class DirectoryInfoAssertions
  14. {
  15. private DirectoryInfo _dirInfo;
  16. public DirectoryInfoAssertions(DirectoryInfo dir)
  17. {
  18. _dirInfo = dir;
  19. }
  20. public DirectoryInfo DirectoryInfo => _dirInfo;
  21. public AndConstraint<DirectoryInfoAssertions> Exist()
  22. {
  23. Execute.Assertion.ForCondition(_dirInfo.Exists)
  24. .FailWith("Expected directory {0} does not exist.", _dirInfo.FullName);
  25. return new AndConstraint<DirectoryInfoAssertions>(this);
  26. }
  27. public AndConstraint<DirectoryInfoAssertions> NotExist()
  28. {
  29. Execute.Assertion.ForCondition(!_dirInfo.Exists)
  30. .FailWith("Expected directory {0} not to exist.", _dirInfo.FullName);
  31. return new AndConstraint<DirectoryInfoAssertions>(this);
  32. }
  33. public AndConstraint<DirectoryInfoAssertions> HaveFile(string expectedFile)
  34. {
  35. var file = _dirInfo.EnumerateFiles(expectedFile, SearchOption.TopDirectoryOnly).SingleOrDefault();
  36. Execute.Assertion.ForCondition(file != null)
  37. .FailWith("Expected File {0} cannot be found in directory {1}.", expectedFile, _dirInfo.FullName);
  38. return new AndConstraint<DirectoryInfoAssertions>(this);
  39. }
  40. public AndConstraint<DirectoryInfoAssertions> NotHaveFile(string expectedFile)
  41. {
  42. var file = _dirInfo.EnumerateFiles(expectedFile, SearchOption.TopDirectoryOnly).SingleOrDefault();
  43. Execute.Assertion.ForCondition(file == null)
  44. .FailWith("File {0} should not be found in directory {1}.", expectedFile, _dirInfo.FullName);
  45. return new AndConstraint<DirectoryInfoAssertions>(this);
  46. }
  47. public AndConstraint<DirectoryInfoAssertions> HaveFiles(IEnumerable<string> expectedFiles)
  48. {
  49. foreach (var expectedFile in expectedFiles)
  50. {
  51. HaveFile(expectedFile);
  52. }
  53. return new AndConstraint<DirectoryInfoAssertions>(this);
  54. }
  55. public AndConstraint<DirectoryInfoAssertions> HaveFilesMatching(
  56. string expectedFilesSearchPattern,
  57. SearchOption searchOption,
  58. string because = "",
  59. params object[] reasonArgs)
  60. {
  61. var matchingFileExists = _dirInfo.EnumerateFiles(expectedFilesSearchPattern, searchOption).Any();
  62. Execute.Assertion
  63. .ForCondition(matchingFileExists == true)
  64. .BecauseOf(because, reasonArgs)
  65. .FailWith("Expected directory {0} to contain files matching {1}, but no matching file exists.",
  66. _dirInfo.FullName, expectedFilesSearchPattern);
  67. return new AndConstraint<DirectoryInfoAssertions>(this);
  68. }
  69. public AndConstraint<DirectoryInfoAssertions> NotHaveFiles(IEnumerable<string> expectedFiles)
  70. {
  71. foreach (var expectedFile in expectedFiles)
  72. {
  73. NotHaveFile(expectedFile);
  74. }
  75. return new AndConstraint<DirectoryInfoAssertions>(this);
  76. }
  77. public AndConstraint<DirectoryInfoAssertions> NotHaveFilesMatching(string expectedFilesSearchPattern, SearchOption searchOption)
  78. {
  79. var matchingFileCount = _dirInfo.EnumerateFiles(expectedFilesSearchPattern, searchOption).Count();
  80. Execute.Assertion.ForCondition(matchingFileCount == 0)
  81. .FailWith("Found {0} files that should not exist in directory {1}. No file matching {2} should exist.",
  82. matchingFileCount, _dirInfo.FullName, expectedFilesSearchPattern);
  83. return new AndConstraint<DirectoryInfoAssertions>(this);
  84. }
  85. public AndConstraint<DirectoryInfoAssertions> HaveDirectory(string expectedDir)
  86. {
  87. var dir = _dirInfo.EnumerateDirectories(expectedDir, SearchOption.TopDirectoryOnly).SingleOrDefault();
  88. Execute.Assertion.ForCondition(dir != null)
  89. .FailWith("Expected directory {0} cannot be found inside directory {1}.", expectedDir, _dirInfo.FullName);
  90. return new AndConstraint<DirectoryInfoAssertions>(new DirectoryInfoAssertions(dir));
  91. }
  92. public AndConstraint<DirectoryInfoAssertions> OnlyHaveFiles(IEnumerable<string> expectedFiles, SearchOption searchOption = SearchOption.AllDirectories)
  93. {
  94. var actualFiles = _dirInfo.EnumerateFiles("*", searchOption)
  95. .Select(f => f.FullName.Substring(_dirInfo.FullName.Length + 1) // make relative to _dirInfo
  96. .Replace("\\", "/")); // normalize separator
  97. var missingFiles = Enumerable.Except(expectedFiles, actualFiles);
  98. var extraFiles = Enumerable.Except(actualFiles, expectedFiles);
  99. var nl = Environment.NewLine;
  100. Execute.Assertion.ForCondition(!missingFiles.Any())
  101. .FailWith($"Following files cannot be found inside directory {_dirInfo.FullName} {nl} {string.Join(nl, missingFiles)}");
  102. Execute.Assertion.ForCondition(!extraFiles.Any())
  103. .FailWith($"Following extra files are found inside directory {_dirInfo.FullName} {nl} {string.Join(nl, extraFiles)}");
  104. return new AndConstraint<DirectoryInfoAssertions>(this);
  105. }
  106. public AndConstraint<DirectoryInfoAssertions> BeEmpty()
  107. {
  108. Execute.Assertion.ForCondition(!_dirInfo.EnumerateFileSystemInfos().Any())
  109. .FailWith($"The directory {_dirInfo.FullName} is not empty.");
  110. return new AndConstraint<DirectoryInfoAssertions>(this);
  111. }
  112. public AndConstraint<DirectoryInfoAssertions> NotBeEmpty()
  113. {
  114. Execute.Assertion.ForCondition(_dirInfo.EnumerateFileSystemInfos().Any())
  115. .FailWith($"The directory {_dirInfo.FullName} is empty.");
  116. return new AndConstraint<DirectoryInfoAssertions>(this);
  117. }
  118. public AndConstraint<DirectoryInfoAssertions> NotExist(string because = "", params object[] reasonArgs)
  119. {
  120. Execute.Assertion
  121. .ForCondition(_dirInfo.Exists == false)
  122. .BecauseOf(because, reasonArgs)
  123. .FailWith($"Expected directory {_dirInfo.FullName} to not exist, but it does.");
  124. return new AndConstraint<DirectoryInfoAssertions>(this);
  125. }
  126. public AndConstraint<DirectoryInfoAssertions> NotHaveSubDirectories(params string[] notExpectedSubdirectories)
  127. {
  128. notExpectedSubdirectories = notExpectedSubdirectories ?? Array.Empty<string>();
  129. var subDirectories = _dirInfo.EnumerateDirectories();
  130. if (!notExpectedSubdirectories.Any())
  131. {
  132. // If no subdirectories were passed in, it means there should be no subdirectories at all
  133. Execute.Assertion.ForCondition(!subDirectories.Any())
  134. .FailWith("Directory {0} should not have any sub directories.", _dirInfo.FullName);
  135. }
  136. else
  137. {
  138. var actualSubDirectories = subDirectories
  139. .Select(f => f.FullName.Substring(_dirInfo.FullName.Length + 1) // make relative to _dirInfo
  140. .Replace("\\", "/")); // normalize separator
  141. var errorSubDirectories = notExpectedSubdirectories.Intersect(actualSubDirectories);
  142. var nl = Environment.NewLine;
  143. Execute.Assertion.ForCondition(!errorSubDirectories.Any())
  144. .FailWith($"The following subdirectories should not be found inside directory {_dirInfo.FullName} {nl} {string.Join(nl, errorSubDirectories)}");
  145. }
  146. return new AndConstraint<DirectoryInfoAssertions>(this);
  147. }
  148. }
  149. }