|
1 | | -namespace Nerdbank.GitVersioning.Tests |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using LibGit2Sharp; |
| 9 | +using Nerdbank.GitVersioning; |
| 10 | +using Validation; |
| 11 | +using Xunit.Abstractions; |
| 12 | + |
| 13 | +public abstract class RepoTestBase : IDisposable |
2 | 14 | { |
3 | | - using System; |
4 | | - using System.Collections.Generic; |
5 | | - using System.Diagnostics; |
6 | | - using System.IO; |
7 | | - using System.Linq; |
8 | | - using System.Text; |
9 | | - using System.Threading.Tasks; |
10 | | - using LibGit2Sharp; |
11 | | - using Validation; |
12 | | - using Xunit.Abstractions; |
13 | | - |
14 | | - public abstract class RepoTestBase : IDisposable |
15 | | - { |
16 | | - private readonly List<string> repoDirectories = new List<string>(); |
| 15 | + private readonly List<string> repoDirectories = new List<string>(); |
17 | 16 |
|
18 | | - public RepoTestBase(ITestOutputHelper logger) |
19 | | - { |
20 | | - Requires.NotNull(logger, nameof(logger)); |
| 17 | + public RepoTestBase(ITestOutputHelper logger) |
| 18 | + { |
| 19 | + Requires.NotNull(logger, nameof(logger)); |
21 | 20 |
|
22 | | - this.Logger = logger; |
23 | | - this.RepoPath = this.CreateDirectoryForNewRepo(); |
24 | | - } |
| 21 | + this.Logger = logger; |
| 22 | + this.RepoPath = this.CreateDirectoryForNewRepo(); |
| 23 | + } |
25 | 24 |
|
26 | | - protected ITestOutputHelper Logger { get; } |
| 25 | + protected ITestOutputHelper Logger { get; } |
27 | 26 |
|
28 | | - protected Repository Repo { get; set; } |
| 27 | + protected Repository Repo { get; set; } |
29 | 28 |
|
30 | | - protected string RepoPath { get; set; } |
| 29 | + protected string RepoPath { get; set; } |
31 | 30 |
|
32 | | - protected Signature Signer => new Signature("a", "[email protected]", new DateTimeOffset(2015, 8, 2, 0, 0, 0, TimeSpan.Zero)); |
| 31 | + protected Signature Signer => new Signature("a", "[email protected]", new DateTimeOffset(2015, 8, 2, 0, 0, 0, TimeSpan.Zero)); |
33 | 32 |
|
34 | | - public void Dispose() |
35 | | - { |
36 | | - this.Dispose(true); |
37 | | - GC.SuppressFinalize(this); |
38 | | - } |
| 33 | + public void Dispose() |
| 34 | + { |
| 35 | + this.Dispose(true); |
| 36 | + GC.SuppressFinalize(this); |
| 37 | + } |
39 | 38 |
|
40 | | - protected string CreateDirectoryForNewRepo() |
| 39 | + protected string CreateDirectoryForNewRepo() |
| 40 | + { |
| 41 | + string repoPath; |
| 42 | + do |
41 | 43 | { |
42 | | - string repoPath; |
43 | | - do |
44 | | - { |
45 | | - repoPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
46 | | - } while (Directory.Exists(repoPath)); |
47 | | - Directory.CreateDirectory(repoPath); |
| 44 | + repoPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 45 | + } while (Directory.Exists(repoPath)); |
| 46 | + Directory.CreateDirectory(repoPath); |
48 | 47 |
|
49 | | - this.repoDirectories.Add(repoPath); |
50 | | - return repoPath; |
51 | | - } |
| 48 | + this.repoDirectories.Add(repoPath); |
| 49 | + return repoPath; |
| 50 | + } |
52 | 51 |
|
53 | | - protected virtual void Dispose(bool disposing) |
| 52 | + protected virtual void Dispose(bool disposing) |
| 53 | + { |
| 54 | + if (disposing) |
54 | 55 | { |
55 | | - if (disposing) |
| 56 | + this.Repo?.Dispose(); |
| 57 | + foreach (string dir in this.repoDirectories) |
56 | 58 | { |
57 | | - this.Repo?.Dispose(); |
58 | | - foreach (string dir in this.repoDirectories) |
| 59 | + try |
| 60 | + { |
| 61 | + TestUtilities.DeleteDirectory(dir); |
| 62 | + } |
| 63 | + catch (IOException) |
59 | 64 | { |
60 | | - try |
61 | | - { |
62 | | - TestUtilities.DeleteDirectory(dir); |
63 | | - } |
64 | | - catch (IOException) |
65 | | - { |
66 | | - // This happens in AppVeyor a lot. |
67 | | - } |
| 65 | + // This happens in AppVeyor a lot. |
68 | 66 | } |
69 | 67 | } |
70 | 68 | } |
| 69 | + } |
71 | 70 |
|
72 | | - protected void InitializeSourceControl() |
| 71 | + protected void InitializeSourceControl() |
| 72 | + { |
| 73 | + Repository.Init(this.RepoPath); |
| 74 | + this.Repo = new Repository(this.RepoPath); |
| 75 | + foreach (var file in this.Repo.RetrieveStatus().Untracked) |
73 | 76 | { |
74 | | - Repository.Init(this.RepoPath); |
75 | | - this.Repo = new Repository(this.RepoPath); |
76 | | - foreach (var file in this.Repo.RetrieveStatus().Untracked) |
77 | | - { |
78 | | - Commands.Stage(this.Repo, file.FilePath); |
79 | | - } |
80 | | - |
81 | | - if (this.Repo.Index.Count > 0) |
82 | | - { |
83 | | - this.Repo.Commit("initial commit", this.Signer, this.Signer); |
84 | | - } |
| 77 | + Commands.Stage(this.Repo, file.FilePath); |
85 | 78 | } |
86 | 79 |
|
87 | | - protected void AddCommits(int count = 1) |
| 80 | + if (this.Repo.Index.Count > 0) |
88 | 81 | { |
89 | | - Verify.Operation(this.Repo != null, "Repo has not been created yet."); |
90 | | - for (int i = 1; i <= count; i++) |
91 | | - { |
92 | | - this.Repo.Commit($"filler commit {i}", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true }); |
93 | | - } |
| 82 | + this.Repo.Commit("initial commit", this.Signer, this.Signer); |
94 | 83 | } |
| 84 | + } |
95 | 85 |
|
96 | | - protected Commit WriteVersionTxtFile(string version = "1.2", string prerelease = "", string relativeDirectory = null) |
| 86 | + protected void AddCommits(int count = 1) |
| 87 | + { |
| 88 | + Verify.Operation(this.Repo != null, "Repo has not been created yet."); |
| 89 | + for (int i = 1; i <= count; i++) |
97 | 90 | { |
98 | | - if (relativeDirectory == null) |
99 | | - { |
100 | | - relativeDirectory = string.Empty; |
101 | | - } |
102 | | - |
103 | | - string versionFilePath = Path.Combine(this.RepoPath, relativeDirectory, "version.txt"); |
104 | | - File.WriteAllText(versionFilePath, $"{version}\r\n{prerelease}"); |
105 | | - return this.CommitVersionFile(versionFilePath, $"{version}{prerelease}"); |
| 91 | + this.Repo.Commit($"filler commit {i}", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true }); |
106 | 92 | } |
| 93 | + } |
107 | 94 |
|
108 | | - protected Commit WriteVersionFile(string version = "1.2", string prerelease = "", string relativeDirectory = null) |
| 95 | + protected Commit WriteVersionTxtFile(string version = "1.2", string prerelease = "", string relativeDirectory = null) |
| 96 | + { |
| 97 | + if (relativeDirectory == null) |
109 | 98 | { |
110 | | - var versionData = VersionOptions.FromVersion(new System.Version(version), prerelease); |
111 | | - return this.WriteVersionFile(versionData, relativeDirectory); |
| 99 | + relativeDirectory = string.Empty; |
112 | 100 | } |
113 | 101 |
|
114 | | - protected Commit WriteVersionFile(VersionOptions versionData, string relativeDirectory = null) |
115 | | - { |
116 | | - Requires.NotNull(versionData, nameof(versionData)); |
| 102 | + string versionFilePath = Path.Combine(this.RepoPath, relativeDirectory, "version.txt"); |
| 103 | + File.WriteAllText(versionFilePath, $"{version}\r\n{prerelease}"); |
| 104 | + return this.CommitVersionFile(versionFilePath, $"{version}{prerelease}"); |
| 105 | + } |
117 | 106 |
|
118 | | - if (relativeDirectory == null) |
119 | | - { |
120 | | - relativeDirectory = string.Empty; |
121 | | - } |
| 107 | + protected Commit WriteVersionFile(string version = "1.2", string prerelease = "", string relativeDirectory = null) |
| 108 | + { |
| 109 | + var versionData = VersionOptions.FromVersion(new System.Version(version), prerelease); |
| 110 | + return this.WriteVersionFile(versionData, relativeDirectory); |
| 111 | + } |
122 | 112 |
|
123 | | - string versionFilePath = VersionFile.SetVersion(Path.Combine(this.RepoPath, relativeDirectory), versionData); |
124 | | - return this.CommitVersionFile(versionFilePath, versionData.Version?.ToString()); |
125 | | - } |
| 113 | + protected Commit WriteVersionFile(VersionOptions versionData, string relativeDirectory = null) |
| 114 | + { |
| 115 | + Requires.NotNull(versionData, nameof(versionData)); |
126 | 116 |
|
127 | | - protected Commit CommitVersionFile(string versionFilePath, string version) |
| 117 | + if (relativeDirectory == null) |
128 | 118 | { |
129 | | - Requires.NotNullOrEmpty(versionFilePath, nameof(versionFilePath)); |
130 | | - Requires.NotNullOrEmpty(versionFilePath, nameof(versionFilePath)); |
| 119 | + relativeDirectory = string.Empty; |
| 120 | + } |
| 121 | + |
| 122 | + string versionFilePath = VersionFile.SetVersion(Path.Combine(this.RepoPath, relativeDirectory), versionData); |
| 123 | + return this.CommitVersionFile(versionFilePath, versionData.Version?.ToString()); |
| 124 | + } |
| 125 | + |
| 126 | + protected Commit CommitVersionFile(string versionFilePath, string version) |
| 127 | + { |
| 128 | + Requires.NotNullOrEmpty(versionFilePath, nameof(versionFilePath)); |
| 129 | + Requires.NotNullOrEmpty(versionFilePath, nameof(versionFilePath)); |
131 | 130 |
|
132 | | - if (this.Repo != null) |
| 131 | + if (this.Repo != null) |
| 132 | + { |
| 133 | + Assumes.True(versionFilePath.StartsWith(this.RepoPath, StringComparison.OrdinalIgnoreCase)); |
| 134 | + var relativeFilePath = versionFilePath.Substring(this.RepoPath.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); |
| 135 | + Commands.Stage(this.Repo, relativeFilePath); |
| 136 | + if (Path.GetExtension(relativeFilePath) == ".json") |
133 | 137 | { |
134 | | - Assumes.True(versionFilePath.StartsWith(this.RepoPath, StringComparison.OrdinalIgnoreCase)); |
135 | | - var relativeFilePath = versionFilePath.Substring(this.RepoPath.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); |
136 | | - Commands.Stage(this.Repo, relativeFilePath); |
137 | | - if (Path.GetExtension(relativeFilePath) == ".json") |
| 138 | + string txtFilePath = relativeFilePath.Substring(0, relativeFilePath.Length - 4) + "txt"; |
| 139 | + if (!File.Exists(Path.Combine(this.RepoPath, txtFilePath)) && this.Repo.Index[txtFilePath] != null) |
138 | 140 | { |
139 | | - string txtFilePath = relativeFilePath.Substring(0, relativeFilePath.Length - 4) + "txt"; |
140 | | - if (!File.Exists(Path.Combine(this.RepoPath, txtFilePath)) && this.Repo.Index[txtFilePath] != null) |
141 | | - { |
142 | | - this.Repo.Index.Remove(txtFilePath); |
143 | | - } |
| 141 | + this.Repo.Index.Remove(txtFilePath); |
144 | 142 | } |
145 | | - |
146 | | - return this.Repo.Commit($"Add/write {relativeFilePath} set to {version ?? "Inherited"}", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true }); |
147 | 143 | } |
148 | 144 |
|
149 | | - return null; |
| 145 | + return this.Repo.Commit($"Add/write {relativeFilePath} set to {version ?? "Inherited"}", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true }); |
150 | 146 | } |
| 147 | + |
| 148 | + return null; |
151 | 149 | } |
152 | 150 | } |
0 commit comments