Skip to content

Commit e8681ce

Browse files
committed
Merge pull request #716 from okb/feature/up-to-date-documentation
Tests to ensure up to date documentation
2 parents 9d3149c + 54ddaef commit e8681ce

File tree

4 files changed

+136
-8
lines changed

4 files changed

+136
-8
lines changed

docs/configuration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ branches:
6161
6262
The options in here are:
6363
64+
- **`branches:`** The header for all the individual branch configuration.
65+
6466
- **`mode:`** Same as above
6567

6668
- **`tag:`** The pre release tag to use for this branch. Use the value `use-branch-name-as-tag` to use the branch name instead.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
using GitVersion;
7+
8+
using NUnit.Framework;
9+
10+
using Shouldly;
11+
12+
using YamlDotNet.Serialization;
13+
14+
[TestFixture]
15+
public class DocumentationTests
16+
{
17+
[Test]
18+
public void ConfigurationDocumentationIsUpToDate()
19+
{
20+
var configurationDocumentationFile = ReadDocumentationFile("configuration.md");
21+
22+
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance;
23+
var configProperties = typeof(Config)
24+
.GetProperties(bindingFlags)
25+
.Union(typeof(BranchConfig).GetProperties(bindingFlags))
26+
.Select(p => p.GetCustomAttribute<YamlMemberAttribute>())
27+
.Where(a => a != null)
28+
.Select(a => a.Alias)
29+
.ToList();
30+
31+
configProperties.ShouldNotBeEmpty();
32+
33+
foreach (var configProperty in configProperties)
34+
{
35+
var formattedConfigProperty = string.Format("**`{0}:`**", configProperty);
36+
configurationDocumentationFile.ShouldContain(formattedConfigProperty,
37+
Environment.NewLine + configurationDocumentationFile);
38+
}
39+
}
40+
41+
42+
[Test]
43+
public void VariableDocumentationIsUpToDate()
44+
{
45+
var variableDocumentationFile = ReadDocumentationFile("more-info/variables.md");
46+
var variables = VersionVariables.AvailableVariables.ToList();
47+
48+
variables.ShouldNotBeEmpty();
49+
50+
foreach (var variable in variables)
51+
{
52+
variableDocumentationFile.ShouldContain(variable,
53+
Environment.NewLine + variableDocumentationFile);
54+
}
55+
}
56+
57+
58+
static string ReadDocumentationFile(string relativeDocumentationFilePath)
59+
{
60+
var currentDirectory = new DirectoryInfo(Environment.CurrentDirectory);
61+
while (currentDirectory != null)
62+
{
63+
var docsDirectory = currentDirectory
64+
.EnumerateDirectories("docs", SearchOption.TopDirectoryOnly)
65+
.FirstOrDefault();
66+
67+
if (docsDirectory != null)
68+
{
69+
currentDirectory = docsDirectory;
70+
break;
71+
}
72+
73+
currentDirectory = currentDirectory.Parent;
74+
}
75+
76+
if (currentDirectory == null || !currentDirectory.Name.Equals("docs", StringComparison.Ordinal))
77+
{
78+
throw new DirectoryNotFoundException("Couldn't find the 'docs' directory.");
79+
}
80+
81+
var documentationFilePath = Path.Combine(currentDirectory.FullName, relativeDocumentationFilePath);
82+
// Normalize path separators and such.
83+
documentationFilePath = new FileInfo(documentationFilePath).FullName;
84+
85+
if (!File.Exists(documentationFilePath))
86+
{
87+
throw new FileNotFoundException(string.Format("The documentation file '{0}' couldn't be found.", documentationFilePath), documentationFilePath);
88+
}
89+
90+
return File.ReadAllText(documentationFilePath);
91+
}
92+
}

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
<Compile Include="BuildServers\MyGetTests.cs" />
121121
<Compile Include="BuildServers\VsoAgentTests.cs" />
122122
<Compile Include="BuildServers\TeamCityTests.cs" />
123+
<Compile Include="DocumentationTests.cs" />
123124
<Compile Include="Fixtures\CommitCountingRepoFixture.cs" />
124125
<Compile Include="ConfigProviderTests.cs" />
125126
<Compile Include="Fixtures\LocalRepositoryFixture.cs" />

src/GitVersionCore/OutputVariables/VersionVariables.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,32 @@
33
using System.Collections;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using System.Reflection;
67

78
public class VersionVariables : IEnumerable<KeyValuePair<string, string>>
89
{
9-
public VersionVariables(string major, string minor, string patch, string buildMetaData, string buildMetaDataPadded, string fullBuildMetaData, string branchName, string sha, string majorMinorPatch, string semVer, string legacySemVer, string legacySemVerPadded, string fullSemVer, string assemblySemVer, string preReleaseTag, string preReleaseTagWithDash, string informationalVersion,
10-
string commitDate, string nugetVersion, string nugetVersionV2, string commitsSinceVersionSource, string commitsSinceVersionSourcePadded)
10+
public VersionVariables(string major,
11+
string minor,
12+
string patch,
13+
string buildMetaData,
14+
string buildMetaDataPadded,
15+
string fullBuildMetaData,
16+
string branchName,
17+
string sha,
18+
string majorMinorPatch,
19+
string semVer,
20+
string legacySemVer,
21+
string legacySemVerPadded,
22+
string fullSemVer,
23+
string assemblySemVer,
24+
string preReleaseTag,
25+
string preReleaseTagWithDash,
26+
string informationalVersion,
27+
string commitDate,
28+
string nugetVersion,
29+
string nugetVersionV2,
30+
string commitsSinceVersionSource,
31+
string commitsSinceVersionSourcePadded)
1132
{
1233
Major = major;
1334
Minor = minor;
@@ -33,6 +54,7 @@ public VersionVariables(string major, string minor, string patch, string buildMe
3354
CommitsSinceVersionSourcePadded = commitsSinceVersionSourcePadded;
3455
}
3556

57+
3658
public string Major { get; private set; }
3759
public string Minor { get; private set; }
3860
public string Patch { get; private set; }
@@ -57,30 +79,40 @@ public VersionVariables(string major, string minor, string patch, string buildMe
5779

5880
public static IEnumerable<string> AvailableVariables
5981
{
60-
get { return typeof(VersionVariables).GetProperties().Select(p => p.Name).OrderBy(a => a); }
82+
get
83+
{
84+
return typeof(VersionVariables)
85+
.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance)
86+
.Select(p => p.Name)
87+
.Where(p => p != "AvailableVariables" && p != "Item")
88+
.OrderBy(a => a);
89+
}
6190
}
6291

6392
public string CommitDate { get; set; }
6493

94+
public string this[string variable]
95+
{
96+
get { return (string)typeof(VersionVariables).GetProperty(variable).GetValue(this, null); }
97+
}
98+
99+
65100
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
66101
{
67102
var type = typeof(string);
68103
return typeof(VersionVariables)
69104
.GetProperties()
70105
.Where(p => p.PropertyType == type && !p.GetIndexParameters().Any())
71-
.Select(p => new KeyValuePair<string, string>(p.Name, (string) p.GetValue(this, null)))
106+
.Select(p => new KeyValuePair<string, string>(p.Name, (string)p.GetValue(this, null)))
72107
.GetEnumerator();
73108
}
74109

110+
75111
IEnumerator IEnumerable.GetEnumerator()
76112
{
77113
return GetEnumerator();
78114
}
79115

80-
public string this [string variable]
81-
{
82-
get { return (string) typeof(VersionVariables).GetProperty(variable).GetValue(this, null); }
83-
}
84116

85117
public bool TryGetValue(string variable, out string variableValue)
86118
{
@@ -94,6 +126,7 @@ public bool TryGetValue(string variable, out string variableValue)
94126
return false;
95127
}
96128

129+
97130
public bool ContainsKey(string variable)
98131
{
99132
return typeof(VersionVariables).GetProperty(variable) != null;

0 commit comments

Comments
 (0)