-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
AssemblyInfoTests.cs
94 lines (85 loc) · 3.29 KB
/
AssemblyInfoTests.cs
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
using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using IOPath = System.IO.Path;
namespace Xamarin.Forms.MSBuild.UnitTests
{
[TestFixture]
public class AssemblyInfoTests
{
static readonly string[] references = new[]
{
"Xamarin.Forms.Core",
"Xamarin.Forms.Maps",
"Xamarin.Forms.Xaml",
"Xamarin.Forms.Build.Tasks",
"Xamarin.Forms.Platform",
};
const string s_productName = "Xamarin.Forms";
const string s_company = "Microsoft";
const string s_gitInfoFile = "GitInfo.txt";
[Test, TestCaseSource(nameof(references))]
public void AssemblyTitle(string assemblyName)
{
Assembly testAssembly = System.Reflection.Assembly.Load(assemblyName);
Assert.AreEqual(assemblyName, testAssembly.GetName().Name);
}
[Test, TestCaseSource(nameof(references))]
public void AssemblyVersion(string assemblyName)
{
Assembly testAssembly = System.Reflection.Assembly.Load(assemblyName);
Version actual = testAssembly.GetName().Version;
Assert.AreEqual(2, actual.Major, actual.ToString());
Assert.AreEqual(0, actual.Minor, actual.ToString());
Assert.AreEqual(0, actual.Build, actual.ToString());
}
[Test, TestCaseSource(nameof(references))]
public void FileVersion(string assemblyName)
{
Assembly testAssembly = System.Reflection.Assembly.Load(assemblyName);
FileVersionInfo actual = FileVersionInfo.GetVersionInfo(testAssembly.Location);
Version expected = Version.Parse(GetFileFromRoot(s_gitInfoFile));
Assert.AreEqual(expected.Major, actual.FileMajorPart, $"FileMajorPart is wrong. {actual.ToString()}");
Assert.AreEqual(expected.Minor, actual.FileMinorPart, $"FileMinorPart is wrong. {actual.ToString()}");
// Fails locally
//Assert.AreEqual(expected.Build, actual.FileBuildPart, $"FileBuildPart is wrong. {actual.ToString()}");
//We need to enable this
// Assert.AreEqual(ThisAssembly.Git.Commits, version.FilePrivatePart);
Assert.AreEqual(s_productName, actual.ProductName);
Assert.AreEqual(s_company, actual.CompanyName);
}
[Test, TestCaseSource(nameof(references))]
public void ProductAndCompany(string assemblyName)
{
Assembly testAssembly = System.Reflection.Assembly.Load(assemblyName);
FileVersionInfo actual = FileVersionInfo.GetVersionInfo(testAssembly.Location);
Assert.AreEqual(s_productName, actual.ProductName);
Assert.AreEqual(s_company, actual.CompanyName);
}
static string GetFileFromRoot(string file)
{
var gitInfoFile = IOPath.Combine(TestContext.CurrentContext.TestDirectory, "..", "..", "..", "..", file);
if (!File.Exists(gitInfoFile))
{
//NOTE: VSTS may be running tests in a staging directory, so we can use an environment variable to find the source
// https://docs.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?view=vsts&tabs=batch#buildsourcesdirectory
var sourcesDirectory = Environment.GetEnvironmentVariable("BUILD_SOURCESDIRECTORY");
if (!string.IsNullOrEmpty(sourcesDirectory))
{
gitInfoFile = IOPath.Combine(sourcesDirectory, file);
if (!File.Exists(gitInfoFile))
{
Assert.Fail($"Unable to find {file} at path: {gitInfoFile}");
}
}
else
{
Assert.Fail($"Unable to find {file} at path: {gitInfoFile}");
}
}
return File.ReadAllText(gitInfoFile);
}
}
}