DeleteNuGetArtifactsFixture.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System.Runtime.CompilerServices;
  4. namespace Microsoft.NET.Build.Tests
  5. {
  6. public class ConstantStringValues
  7. {
  8. public static string TestDirectoriesNamePrefix = "Nuget_reference_compat";
  9. public static string ReferencerDirectoryName = "Reference";
  10. public static string NuGetSharedDirectoryNamePostfix = "_NuGetDependencies";
  11. public static string NetstandardTargetFrameworkIdentifier = ".NETStandard";
  12. public static string DependencyDirectoryNamePrefix = "D_";
  13. public static string ConstructNuGetPackageReferencePath(TestProject dependencyProject, string identifier, [CallerMemberName] string callingMethod = null)
  14. {
  15. return TestAssetsManager.GetTestDestinationDirectoryPath(dependencyProject.Name, callingMethod, identifier);
  16. }
  17. }
  18. public class DeleteNuGetArtifactsFixture : IDisposable
  19. {
  20. public DeleteNuGetArtifactsFixture()
  21. {
  22. DeleteNuGetArtifacts();
  23. }
  24. public void Dispose()
  25. {
  26. DeleteNuGetArtifacts();
  27. }
  28. private void DeleteNuGetArtifacts()
  29. {
  30. try
  31. {
  32. // Delete the generated NuGet packages in the cache.
  33. foreach (string dir in Directory.EnumerateDirectories(TestContext.Current.NuGetCachePath, ConstantStringValues.DependencyDirectoryNamePrefix + "*"))
  34. {
  35. Directory.Delete(dir, true);
  36. }
  37. }
  38. catch
  39. {
  40. // No-Op; as this is a precaution - do not throw an exception.
  41. }
  42. }
  43. }
  44. }