chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using NUnit.Framework;
using LibGit2Sharp;
using System.IO;
using System.Diagnostics;
 
namespace OpenTap.Package.UnitTests
{
    [TestFixture]
    public class GitVersionCalculatorTests
    {
        Signature me = new Signature("Me", "me@opentap.io", DateTime.Now);
        MergeOptions noFastForward = new MergeOptions { FastForwardStrategy = FastForwardStrategy.NoFastForward };
 
        void commitChangesToReadme(LibGit2Sharp.Repository repo, string newContent, string commitMessage)
        {
            File.WriteAllText(repo.Info.WorkingDirectory + "readme.md", newContent);
            Commands.Stage(repo, repo.Info.WorkingDirectory + "readme.md");
            repo.Commit(commitMessage, me, me);
        }
 
        void commitChangesToGitVersionFile(LibGit2Sharp.Repository repo, string version, string commitMessage, string filename = ".gitversion")
        {
            string filePath = Path.Combine(repo.Info.WorkingDirectory, filename);
            string dir = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            using (StreamWriter file = new StreamWriter(filePath))
            {
                file.WriteLine("version = " + version);
                file.WriteLine("beta branch = master");
            }
            Commands.Stage(repo, filePath);
            repo.Commit(commitMessage, me, me);
        }
 
        [DebuggerStepThrough]
        void verifyVersion(LibGit2Sharp.Repository repo, int major, int minor, int patch, string prerelease, string branchName = "", string dir = null)
        {
            verifyVersion(repo, repo.Head.Tip, major, minor, patch, prerelease, branchName, dir);
        }
 
        [DebuggerStepThrough]
        void verifyVersion(LibGit2Sharp.Repository repo, Commit c, int major, int minor, int patch, string prerelease, string branchName = null, string dir = null)
        {
            var metadata = c.Sha.Substring(0, 8);  // (note: hashes will differ each time this is run)
            if (!String.IsNullOrEmpty(branchName))
                metadata += "." + branchName;
            using (var calc = new GitVersionCalulator(Path.Combine(repo.Info.WorkingDirectory, dir ?? "")))
            {
                SemanticVersion tv = calc.GetVersion();
                Assert.AreEqual(major, tv.Major, "Unexpected major version number.");
                Assert.AreEqual(minor, tv.Minor, "Unexpected minor version number.");
                Assert.AreEqual(patch, tv.Patch, "Unexpected patch version number.");
                Assert.AreEqual(prerelease, tv.PreRelease, "Unexpected prerelease.");
                Assert.AreEqual(metadata, tv.BuildMetadata, "Unexpected build metadata.");
            }
        }
 
        LibGit2Sharp.Repository repo;
        [SetUp]
        public void InitTestRepo()
        {
            string repoPath = Path.Combine(Path.GetTempPath(), "getgitversiontest");
            FileSystemHelper.DeleteDirectory(repoPath);
            Directory.CreateDirectory(repoPath);
            LibGit2Sharp.Repository.Init(repoPath);
            repo = new LibGit2Sharp.Repository(repoPath);
        }
 
        [TearDown]
        public void CleanupTestRepo()
        {
            string repoPath = repo.Info.WorkingDirectory;
            if (repo != null)
                repo.Dispose();
            FileSystemHelper.DeleteDirectory(repoPath);
        }
 
        [Test]
        public void GetGitVersion()
        {
            commitChangesToReadme(repo, "Hej", "Added Readme");
 
            verifyVersion(repo, 0, 0, 1, "beta.1");
 
            commitChangesToGitVersionFile(repo, "1.0.0", "Added .gitversion file with version = 1.0.0");
 
            verifyVersion(repo,1, 0, 0, "beta.1");
 
 
            var featureBranch = repo.CreateBranch("feature");
            Commands.Checkout(repo, featureBranch);
            commitChangesToReadme(repo, "Hej2", "Changed Readme on feature branch");
 
            verifyVersion(repo, 1, 0, 0, "alpha.1.1", featureBranch.FriendlyName);
 
            Commands.Checkout(repo, "master");
            repo.Merge(featureBranch, me, noFastForward);
 
            verifyVersion(repo, 1, 0, 0, "beta.3");
 
            commitChangesToReadme(repo, "Version 2", "Bumped version to 2.0");
            commitChangesToGitVersionFile(repo, "2.0.0", "Updated version to 2.0.0 in .gitversion file");
 
            verifyVersion(repo, 2, 0, 0, "beta.1");
 
 
            repo.CreateBranch("release2x");
            Commands.Checkout(repo, "release2x");
 
            verifyVersion(repo, 2, 0, 0, "rc");
 
            Commands.Checkout(repo, "master");
 
            verifyVersion(repo, 2, 0, 0, "beta.1");
 
            Commands.Checkout(repo, "release2x");
            repo.ApplyTag("v2.0.0", me, "Official 2.0.0 release");
 
            verifyVersion(repo, 2, 0, 0, null);
 
            Commands.Checkout(repo, "master");
            commitChangesToGitVersionFile(repo, "2.1.0", "Bumped version to 2.1");
            verifyVersion(repo, 2, 1, 0, "beta.1");
            featureBranch = repo.CreateBranch("feature2");
            Commands.Checkout(repo, featureBranch);
            commitChangesToReadme(repo, "Version 2.1 something", "Changed readme");
            verifyVersion(repo, 2, 1, 0, "alpha.1.1", featureBranch.FriendlyName);
            Commands.Checkout(repo, "master");
            repo.Merge(featureBranch, me, noFastForward);
            verifyVersion(repo, 2, 1, 0, "beta.3");
            Commands.Checkout(repo, "release2x");
            repo.Merge("master", me, noFastForward);
 
 
            // At this point we have created the following git history:
            // *   Merge branch 'master' into 'rc2x'
            // |\
            // | *   (master) Merge branch 'feature2'
            // | |\
            // | | * (feature2) Changed readme
            // | |/
            // | *   (tag: v2.1) Bumped version to 2.1
            // |/
            // *     (tag: v2.0, release2x) Bumped version to 2.0
            var commit210rc = repo.Head.Tip;
            verifyVersion(repo, 2, 1, 0, "rc.1");
 
            var hotfixBranch = repo.CreateBranch("hotfix");
            Commands.Checkout(repo, hotfixBranch);
            commitChangesToReadme(repo, "Version 2.1 hotfix", "Changed readme on hotfix branch.");
            Commands.Checkout(repo, "release2x");
            repo.Merge(hotfixBranch, me, noFastForward);
 
            // At this point we have created the following git history:
            // *   (rc2x) Merge branch 'hotfix' into 'rc2x'
            // |\
            // | *   (hotfix) Changed readme on hotfix branch.
            // |/
            // *   Merge branch 'master' into 'rc2x'
            // |\
            // | *   (master) Merge branch 'feature2'
            // | |\
            // | | * (feature2) Changed readme
            // | |/
            // | *   (tag: v2.1) Bumped version to 2.1
            // |/
            // *     (tag: v2.0, release2x) Bumped version to 2.0
            new GitVersionAction() { PrintLog = "10", RepoPath = repo.Info.WorkingDirectory }.Execute(new System.Threading.CancellationToken());
            verifyVersion(repo, 2, 1, 0, "rc.2");
 
            // Now we will try to go back to a previous commit (not the HEAD):
            // This is what GitLab CI does, so important for us to support
 
            //Commands.Checkout(repo, shortHash211alpha);
            //verifyVersion(2, 1, 0, "alpha.1.1", shortHash211alpha + ".hotfix");   // TODO: This fails and we should try to improve. See TODO in GitVersionCalculator 
 
            Commands.Checkout(repo, commit210rc);
            verifyVersion(repo,commit210rc, 2, 1, 0, "rc.1");
        }
 
 
        [TestCase(new[]// if the branch we are on is not the beta or release branch, the prerelease should be alpha
        {
            "version = 2.0",
            "beta branch = test",
            "release tag = NotMatching"
        }, "alpha.1")]
        [TestCase(new[]
        {
            "version = 2.0",
            "#beta branch = NotMatching",
            "release tag = NotMatching"
        }, "beta.1")]
        [TestCase(new[]
        {
            "version = 2.0.0",
            "beta branch = master",
            "release tag = NotMatching"
        }, "beta.1")]
 
        [TestCase(new[]// if there is a tag on a release branch
        {
            "version = 2.0.0",
            "release branch = master",
            @"release tag = v\d+\.\d+\.\d+"
        }, null)]
        [TestCase(new[] // if there is a tag on a branch that is not the release branch
        {
            "version = 2.0.0",
            "beta branch = master",
            @"release tag = v\d+.\d+.\d+"
        }, null)]
        public void GitVersionConfig(string[] lines, string prerelease)
        {
            string filePath = Path.Combine(repo.Info.WorkingDirectory, ".gitversion");
            File.WriteAllLines(filePath, lines);
            Commands.Stage(repo, filePath);
            repo.Commit("Test", me, me);
            repo.ApplyTag("v2.0.0",me,"Annotation");
            repo.CreateBranch("test");
            verifyVersion(repo, 2, 0, 0, prerelease,(prerelease ?? "").StartsWith("alpha") ? "master": "");
        }
 
        [Test]
        public void CommitToReleaseAgainWithoutBump()
        {
            commitChangesToGitVersionFile(repo,"1.0.0", "Added .gitversion file with version = 1.0.0");
            verifyVersion(repo, 1, 0, 0, "beta.1");
            repo.CreateBranch("release2x");
            Commands.Checkout(repo, "release2x");
            commitChangesToReadme(repo, "Version 1.0 hotfix", "Changed readme on release branch.");
            repo.ApplyTag("v1.0.0", me, "Official 1.0.0 release");
            verifyVersion(repo, 1, 0, 0,null);
            commitChangesToReadme(repo, "Version 1.0 hotfix 2", "Changed readme on release branch.");
            repo.ApplyTag("v1.0.1", me, "Official 1.0.0 release");
            verifyVersion(repo, 1, 0, 0, null);  // TODO: this is a bit of a weird behavior
        }
 
        [Test]
        public void ConfigFileAddedOnAlphaBranch()
        {
            commitChangesToReadme(repo, "Hello", "Added readme.");
            verifyVersion(repo, 0, 0, 1, "beta.1");
            repo.CreateBranch("feature");
            Commands.Checkout(repo, "feature");
            commitChangesToGitVersionFile(repo, "1.0.0", "Added .gitversion file with version = 1.0.0");
            verifyVersion(repo, 1, 0, 0, "alpha.1.1","feature");
            Commands.Checkout(repo, "master");
            repo.Merge("feature", me, noFastForward);
            verifyVersion(repo, 1, 0, 0, "beta.1");
        }
 
        [Test]
        public void ConfigFileInSubFolders()
        {
            commitChangesToGitVersionFile(repo, "1.0.0", "Added .gitversion file with version = 1.0.0", ".gitversion");
            commitChangesToReadme(repo, "Hello", "Added readme.");
            //verifyVersion(repo, 1, 0, 0, "beta.2");
            commitChangesToGitVersionFile(repo, "2.0.0", "Added .gitversion file with version = 2.0.0 in subfolder", "test/.gitversion");
            verifyVersion(repo, 1, 0, 0, "beta.3");
            // in the "test" folder there is now a .gitversion file specifying 2.0.0.
            // That file was last changed one commit ago, so it becomes beta.1
            verifyVersion(repo, 2, 0, 0, "beta.1", dir: "test");
        }
    }
}