PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Mercurial.Net.Tests/RepositoryTestsBase.cs

#
C# | 74 lines | 68 code | 6 blank | 0 comment | 4 complexity | 9c29be96b8dd17001ca839a1ea20c102 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using NUnit.Framework;
  8. namespace Mercurial.Tests
  9. {
  10. public class RepositoryTestsBase
  11. {
  12. private readonly List<string> _Repositories = new List<string>();
  13. private readonly List<string> _TempFiles = new List<string>();
  14. protected Repository GetRepository()
  15. {
  16. string repoPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString().Replace("-", string.Empty).ToLowerInvariant());
  17. Directory.CreateDirectory(repoPath);
  18. _Repositories.Add(repoPath);
  19. if (Environment.GetEnvironmentVariable("PERSISTENTCLIENT") == "1")
  20. return new Repository(repoPath, new AutoSwitchingClientFactory());
  21. return new Repository(repoPath, new NonPersistentClientFactory());
  22. }
  23. protected string GetTempFileName()
  24. {
  25. string result = Path.GetTempFileName();
  26. _TempFiles.Add(result);
  27. return result;
  28. }
  29. [TearDown]
  30. public virtual void TearDown()
  31. {
  32. foreach (string repo in _Repositories)
  33. DeleteTempDirectory(repo);
  34. foreach (string path in _TempFiles.Where(File.Exists))
  35. File.Delete(path);
  36. }
  37. private static void DeleteTempDirectory(string path)
  38. {
  39. for (int index = 1; index < 5; index++)
  40. {
  41. try
  42. {
  43. if (Directory.Exists(path))
  44. Directory.Delete(path, true);
  45. break;
  46. }
  47. catch (DirectoryNotFoundException)
  48. {
  49. }
  50. catch (Exception ex)
  51. {
  52. Debug.WriteLine("exception while cleaning up repository directory: " + ex.GetType().Name + ": " + ex.Message);
  53. Thread.Sleep(1000);
  54. }
  55. }
  56. }
  57. protected static void WriteTextFileAndCommit(Repository repo, string fileName, string contents, string commitMessage, bool addRemove)
  58. {
  59. File.WriteAllText(Path.Combine(repo.Path, fileName), contents);
  60. repo.Commit(
  61. new CommitCommand
  62. {
  63. Message = commitMessage,
  64. AddRemove = addRemove,
  65. });
  66. }
  67. }
  68. }