Skip to content

Commit 2c4e15b

Browse files
author
Kevin Johnson
committed
Adds unit tests for the /version flag
1 parent e69f11c commit 2c4e15b

File tree

5 files changed

+84
-29
lines changed

5 files changed

+84
-29
lines changed

src/GitVersionExe.Tests/GitVersionExe.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Compile Include="MsBuildProjectArgTest.cs" />
129129
<Compile Include="PullRequestInTeamCityTest.cs" />
130130
<Compile Include="AssemblyInfoFileUpdateTests.cs" />
131+
<Compile Include="VersionWriterTests.cs" />
131132
</ItemGroup>
132133
<ItemGroup>
133134
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace GitVersionExe.Tests
2+
{
3+
using System;
4+
using System.CodeDom.Compiler;
5+
using System.Reflection;
6+
using GitVersion;
7+
using NUnit.Framework;
8+
9+
public class VersionWriterTests
10+
{
11+
[Test]
12+
public void WriteVersion_ShouldWriteFileVersion_WithNoPrereleaseTag()
13+
{
14+
var asm = GenerateAssembly(new Version(1, 0, 0), "");
15+
16+
string version = null;
17+
VersionWriter.WriteTo(asm, v => version = v);
18+
19+
Assert.IsNotNull(asm);
20+
Assert.AreEqual("1.0.0", version);
21+
}
22+
23+
[Test]
24+
public void WriteVersion_ShouldWriteFileVersion_WithPrereleaseTag()
25+
{
26+
var asm = GenerateAssembly(new Version(1, 0, 0), "-beta0004");
27+
28+
string version = null;
29+
VersionWriter.WriteTo(asm, v => version = v);
30+
31+
Assert.IsNotNull(asm);
32+
Assert.AreEqual("1.0.0-beta0004", version);
33+
}
34+
35+
private Assembly GenerateAssembly(Version fileVersion, string prereleaseInfo)
36+
{
37+
var source = string.Format(@"
38+
using System.Reflection;
39+
using System.Runtime.CompilerServices;
40+
[assembly: AssemblyTitle(""GitVersion.DynamicUnitTests"")]
41+
[assembly: AssemblyProduct(""GitVersion"")]
42+
[assembly: AssemblyVersion(""{0}"")]
43+
[assembly: AssemblyFileVersion(""{0}"")]
44+
[assembly: AssemblyInformationalVersion(""{0}{1}"")]
45+
46+
public class B
47+
{{
48+
public static int k=7;
49+
}}
50+
", fileVersion, prereleaseInfo);
51+
52+
CompilerParameters parameters = new CompilerParameters
53+
{
54+
GenerateInMemory = true,
55+
GenerateExecutable = false,
56+
OutputAssembly = "GitVersion.DynamicUnitTests.dll"
57+
};
58+
59+
var r = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(parameters, source);
60+
return r.CompiledAssembly;
61+
}
62+
}
63+
}

src/GitVersionExe/HelpWriter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace GitVersion
22
{
33
using System;
4+
using System.Reflection;
45

56
class HelpWriter
67
{
@@ -12,7 +13,8 @@ public static void Write()
1213
public static void WriteTo(Action<string> writeAction)
1314
{
1415
string version = string.Empty;
15-
VersionWriter.WriteTo(v => version = v);
16+
Assembly assembly = Assembly.GetExecutingAssembly();
17+
VersionWriter.WriteTo(assembly, v => version = v);
1618

1719
string message = "GitVersion " + version + @"
1820
Use convention to derive a SemVer product version from a GitFlow or GitHub based repository.

src/GitVersionExe/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace GitVersion
77
using System.Diagnostics;
88
using System.IO;
99
using System.Linq;
10+
using System.Reflection;
1011
using System.Text;
1112

1213
class Program
@@ -59,7 +60,8 @@ static int VerifyArgumentsAndRun()
5960

6061
if (arguments.IsVersion)
6162
{
62-
VersionWriter.Write();
63+
var assembly = Assembly.GetExecutingAssembly();
64+
VersionWriter.Write(assembly);
6365
return 0;
6466
}
6567

src/GitVersionExe/VersionWriter.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,29 @@
66

77
class VersionWriter
88
{
9-
/// <summary>
10-
/// Gets the xss version
11-
/// </summary>
12-
private static Version Version
9+
public static void Write(Assembly assembly)
1310
{
14-
get { return Assembly.GetExecutingAssembly().GetName().Version; }
11+
WriteTo(assembly, Console.WriteLine);
1512
}
1613

17-
/// <summary>
18-
/// Gets the AssemblyInformationalVersion
19-
/// </summary>
20-
private static string InformationalVersion
14+
public static void WriteTo(Assembly assembly, Action<string> writeAction)
2115
{
22-
get
23-
{
24-
var attribute = Assembly.GetExecutingAssembly()
16+
var version = GetAssemblyVersion(assembly);
17+
writeAction(version);
18+
}
19+
20+
private static string GetAssemblyVersion(Assembly assembly)
21+
{
22+
var attribute = assembly
2523
.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)
2624
.FirstOrDefault() as AssemblyInformationalVersionAttribute;
2725

28-
if (attribute != null)
29-
{
30-
return attribute.InformationalVersion;
31-
}
32-
33-
return Version.ToString();
26+
if (attribute != null)
27+
{
28+
return attribute.InformationalVersion;
3429
}
35-
}
3630

37-
public static void Write()
38-
{
39-
WriteTo(Console.WriteLine);
40-
}
41-
42-
public static void WriteTo(Action<string> writeAction)
43-
{
44-
writeAction(InformationalVersion);
31+
return assembly.GetName().Version.ToString();
4532
}
4633
}
4734
}

0 commit comments

Comments
 (0)