TestAssetInfo.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 System.Runtime.CompilerServices;
  4. namespace Microsoft.DotNet.TestFramework
  5. {
  6. public class TestAssetInfo
  7. {
  8. // This is needed each release after we upgrade to 9.0 but the templates haven't been upgraded yet
  9. public static readonly string currentTfm = "net9.0";
  10. private readonly string [] FilesToExclude = { ".DS_Store", ".noautobuild" };
  11. public string AssetName { get; private set; }
  12. public FileInfo DotnetExeFile => _testAssets.DotnetCsprojExe;
  13. public string ProjectFilePattern => "*.csproj";
  14. public DirectoryInfo Root { get; private set; }
  15. private TestAssets _testAssets { get; }
  16. internal TestAssetInfo(DirectoryInfo root, string assetName, TestAssets testAssets)
  17. {
  18. if (root == null)
  19. {
  20. throw new ArgumentNullException(nameof(root));
  21. }
  22. if (string.IsNullOrWhiteSpace(assetName))
  23. {
  24. throw new ArgumentException("Argument cannot be null or whitespace", nameof(assetName));
  25. }
  26. if (testAssets == null)
  27. {
  28. throw new ArgumentNullException(nameof(testAssets));
  29. }
  30. Root = root;
  31. AssetName = assetName;
  32. _testAssets = testAssets;
  33. }
  34. public TestAssetInstance CreateInstance([CallerMemberName] string callingMethod = "", string identifier = "")
  35. {
  36. var instancePath = GetTestDestinationDirectory(callingMethod, identifier);
  37. var testInstance = new TestAssetInstance(this, instancePath);
  38. return testInstance;
  39. }
  40. internal IEnumerable<FileInfo> GetSourceFiles()
  41. {
  42. ThrowIfTestAssetDoesNotExist();
  43. return Root.GetFiles("*.*", SearchOption.AllDirectories)
  44. .Where(f => !FilesToExclude.Contains(f.Name));
  45. }
  46. private DirectoryInfo GetTestDestinationDirectory(string callingMethod, string identifier) => _testAssets.CreateTestDirectory(AssetName, callingMethod, identifier);
  47. private void ThrowIfTestAssetDoesNotExist()
  48. {
  49. if (!Root.Exists)
  50. {
  51. throw new DirectoryNotFoundException($"Directory not found at '{Root.FullName}'");
  52. }
  53. }
  54. }
  55. }