Skip to content

Commit 48cf530

Browse files
authored
Merge pull request #4197 from rose-a/fix-4196
Fix syntax error in generated GitVersionInformation class when using project namespace
2 parents de1638b + be4810c commit 48cf530

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
// GitVersion
5+
//
6+
// Changes to this file may cause incorrect behavior and will be lost if
7+
// the code is regenerated.
8+
// </auto-generated>
9+
//------------------------------------------------------------------------------
10+
11+
#if NET20 || NET35 || NETCOREAPP1_0 || NETCOREAPP1_1 || NETSTANDARD1_0 || NETSTANDARD1_1 || NETSTANDARD1_2 || NETSTANDARD1_3 || NETSTANDARD1_4 || NETSTANDARD1_5 || NETSTANDARD1_6
12+
namespace System.Diagnostics.CodeAnalysis
13+
{
14+
[global::System.AttributeUsage(
15+
global::System.AttributeTargets.Assembly |
16+
global::System.AttributeTargets.Class |
17+
global::System.AttributeTargets.Struct |
18+
global::System.AttributeTargets.Constructor |
19+
global::System.AttributeTargets.Method |
20+
global::System.AttributeTargets.Property |
21+
global::System.AttributeTargets.Event,
22+
Inherited = false, AllowMultiple = false)]
23+
internal sealed class ExcludeFromCodeCoverageAttribute : global::System.Attribute { }
24+
}
25+
#endif
26+
27+
namespace My.Custom.Namespace
28+
{
29+
[global::System.Runtime.CompilerServices.CompilerGenerated]
30+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
31+
static class GitVersionInformation
32+
{
33+
public const string AssemblySemFileVer = "1.2.3.0";
34+
public const string AssemblySemVer = "1.2.3.0";
35+
public const string BranchName = "feature1";
36+
public const string BuildMetaData = "5";
37+
public const string CommitDate = "2014-03-06";
38+
public const string CommitsSinceVersionSource = "5";
39+
public const string EscapedBranchName = "feature1";
40+
public const string FullBuildMetaData = "5.Branch.feature1.Sha.commitSha";
41+
public const string FullSemVer = "1.2.3-unstable.4+5";
42+
public const string InformationalVersion = "1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha";
43+
public const string Major = "1";
44+
public const string MajorMinorPatch = "1.2.3";
45+
public const string Minor = "2";
46+
public const string Patch = "3";
47+
public const string PreReleaseLabel = "unstable";
48+
public const string PreReleaseLabelWithDash = "-unstable";
49+
public const string PreReleaseNumber = "4";
50+
public const string PreReleaseTag = "unstable.4";
51+
public const string PreReleaseTagWithDash = "-unstable.4";
52+
public const string SemVer = "1.2.3-unstable.4";
53+
public const string Sha = "commitSha";
54+
public const string ShortSha = "commitShortSha";
55+
public const string UncommittedChanges = "0";
56+
public const string VersionSourceSha = "versionSourceSha";
57+
public const string WeightedPreReleaseNumber = "4";
58+
}
59+
}

src/GitVersion.Output.Tests/Output/GitVersionInfoGeneratorTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,44 @@ public void ShouldCreateFile(string fileExtension)
4949

5050
DirectoryHelper.DeleteDirectory(directory);
5151
}
52+
53+
/// <summary>
54+
/// Regression test for issue #4196 (https://github.com/GitTools/GitVersion/issues/4196)
55+
/// </summary>
56+
[TestCase]
57+
public void ShouldProperlyOutputNamespaceDeclaration()
58+
{
59+
const string fileExtension = "cs";
60+
const string targetNamespace = "My.Custom.Namespace";
61+
62+
var directory = PathHelper.Combine(PathHelper.GetTempPath(), "GitVersionInfoGeneratorTests", Guid.NewGuid().ToString());
63+
if (!Directory.Exists(directory))
64+
Directory.CreateDirectory(directory);
65+
var fileName = "GitVersionInformation.g." + fileExtension;
66+
var fullPath = PathHelper.Combine(directory, fileName);
67+
68+
var semanticVersion = new SemanticVersion
69+
{
70+
Major = 1,
71+
Minor = 2,
72+
Patch = 3,
73+
PreReleaseTag = "unstable4",
74+
BuildMetaData = new("versionSourceSha", 5,
75+
"feature1", "commitSha", "commitShortSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z"), 0)
76+
};
77+
78+
var sp = ConfigureServices();
79+
80+
var fileSystem = sp.GetRequiredService<IFileSystem>();
81+
var variableProvider = sp.GetRequiredService<IVariableProvider>();
82+
83+
var variables = variableProvider.GetVariablesFor(semanticVersion, EmptyConfigurationBuilder.New.Build(), 0);
84+
using var generator = sp.GetRequiredService<IGitVersionInfoGenerator>();
85+
86+
generator.Execute(variables, new(directory, fileName, fileExtension, targetNamespace));
87+
88+
fileSystem.ReadAllText(fullPath).ShouldMatchApproved(c => c.SubFolder(PathHelper.Combine("Approved", fileExtension)));
89+
90+
DirectoryHelper.DeleteDirectory(directory);
91+
}
5292
}

src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void Execute(GitVersionVariables variables, GitVersionInfoContext context
6161
string getTargetNamespace(string extension) => extension switch
6262
{
6363
".vb" => context.TargetNamespace ?? "Global",
64-
".cs" => context.TargetNamespace != null ? $"{PathHelper.NewLine}namespace {context.TargetNamespace};" : "",
64+
".cs" => context.TargetNamespace != null ? $"{PathHelper.NewLine}namespace {context.TargetNamespace}" : "",
6565
".fs" => context.TargetNamespace ?? "global",
6666
_ => targetNamespaceSentinelValue,
6767
};

0 commit comments

Comments
 (0)