Skip to content

Commit 82c368a

Browse files
authored
GH-2890 - made the ProjectFileUpdater accept all "Microsoft.NET.Sdk" sdks (#2891)
1 parent 36b8f8c commit 82c368a

File tree

2 files changed

+48
-113
lines changed

2 files changed

+48
-113
lines changed

src/GitVersion.Core.Tests/VersionConverters/ProjectFileUpdaterTests.cs

Lines changed: 40 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -19,109 +19,45 @@ public class ProjectFileUpdaterTests : TestBase
1919
private IVariableProvider variableProvider;
2020
private ILog log;
2121
private IFileSystem fileSystem;
22+
private IProjectFileUpdater projectFileUpdater;
23+
private List<string> logMessages;
2224

2325
[SetUp]
2426
public void Setup()
2527
{
2628
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestCaseAttribute>();
2729
var sp = ConfigureServices();
28-
this.log = Substitute.For<ILog>();
29-
this.fileSystem = sp.GetService<IFileSystem>();
30-
this.variableProvider = sp.GetService<IVariableProvider>();
31-
}
32-
33-
[TestCase(@"
34-
<Project Sdk=""Microsoft.NET.Sdk"">
35-
<PropertyGroup>
36-
<OutputType>Exe</OutputType>
37-
<TargetFramework>netcoreapp3.1</TargetFramework>
38-
</PropertyGroup>
39-
</Project>
40-
")]
41-
[Category(NoMono)]
42-
[Description(NoMonoDescription)]
43-
public void CanUpdateProjectFileWithStandardProjectFileXml(string xml)
44-
{
45-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
46-
47-
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
48-
49-
canUpdate.ShouldBe(true);
50-
}
5130

52-
[TestCase(@"
53-
<Project Sdk=""Microsoft.NET.Sdk.Worker"">
54-
<PropertyGroup>
55-
<OutputType>Exe</OutputType>
56-
<TargetFramework>netcoreapp3.1</TargetFramework>
57-
</PropertyGroup>
58-
</Project>
59-
")]
60-
[Category(NoMono)]
61-
[Description(NoMonoDescription)]
62-
public void CanUpdateProjectFileWithStandardWorkerProjectFileXml(string xml)
63-
{
64-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
31+
this.logMessages = new List<string>();
32+
this.log = new Log(new TestLogAppender(this.logMessages.Add));
6533

66-
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
67-
68-
canUpdate.ShouldBe(true);
34+
this.fileSystem = sp.GetService<IFileSystem>();
35+
this.variableProvider = sp.GetService<IVariableProvider>();
36+
this.projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem!);
6937
}
7038

71-
[TestCase(@"
72-
<Project Sdk=""Microsoft.NET.Sdk.Web"">
73-
<PropertyGroup>
74-
<OutputType>Exe</OutputType>
75-
<TargetFramework>netcoreapp3.1</TargetFramework>
76-
</PropertyGroup>
77-
</Project>
78-
")]
7939
[Category(NoMono)]
8040
[Description(NoMonoDescription)]
81-
public void CanUpdateProjectFileWithStandardWebProjectFileXml(string xml)
41+
[TestCase("Microsoft.NET.Sdk")]
42+
[TestCase("Microsoft.NET.Sdk.Worker")]
43+
[TestCase("Microsoft.NET.Sdk.Web")]
44+
[TestCase("Microsoft.NET.Sdk.WindowsDesktop")]
45+
[TestCase("Microsoft.NET.Sdk.Razor")]
46+
[TestCase("Microsoft.NET.Sdk.BlazorWebAssembly")]
47+
public void CanUpdateProjectFileWithSdkProjectFileXml(string sdk)
8248
{
83-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
84-
85-
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
86-
87-
canUpdate.ShouldBe(true);
88-
}
89-
90-
[TestCase(@"
91-
<Project Sdk=""Microsoft.NET.Sdk.WindowsDesktop"">
49+
var xml = $@"
50+
<Project Sdk=""{sdk}"">
9251
<PropertyGroup>
9352
<OutputType>Exe</OutputType>
94-
<TargetFramework>net461</TargetFramework>
95-
</PropertyGroup>
96-
</Project>
97-
")]
98-
[Category(NoMono)]
99-
[Description(NoMonoDescription)]
100-
public void CanUpdateProjectFileWithStandardDesktopProjectFileXml(string xml)
101-
{
102-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
103-
104-
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
105-
106-
canUpdate.ShouldBe(true);
107-
}
108-
109-
[TestCase(@"
110-
<Project Sdk=""Microsoft.NET.Sdk.Razor"">
111-
<PropertyGroup>
11253
<TargetFramework>netcoreapp3.1</TargetFramework>
11354
</PropertyGroup>
11455
</Project>
115-
")]
116-
[Category(NoMono)]
117-
[Description(NoMonoDescription)]
118-
public void CanUpdateProjectFileWithRazorClassLibraryProjectFileXml(string xml)
119-
{
120-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
121-
56+
";
12257
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
12358

12459
canUpdate.ShouldBe(true);
60+
logMessages.ShouldBeEmpty();
12561
}
12662

12763
[TestCase(@"
@@ -136,11 +72,13 @@ public void CanUpdateProjectFileWithRazorClassLibraryProjectFileXml(string xml)
13672
[Description(NoMonoDescription)]
13773
public void CannotUpdateProjectFileWithIncorrectProjectSdk(string xml)
13874
{
139-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
140-
14175
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
14276

14377
canUpdate.ShouldBe(false);
78+
79+
logMessages.ShouldNotBeEmpty();
80+
logMessages.Count.ShouldBe(1);
81+
logMessages.First().ShouldContain("Specified project file Sdk (SomeOtherProject.Sdk) is not supported, please ensure the project sdk starts with 'Microsoft.NET.Sdk'");
14482
}
14583

14684
[TestCase(@"
@@ -155,11 +93,13 @@ public void CannotUpdateProjectFileWithIncorrectProjectSdk(string xml)
15593
[Description(NoMonoDescription)]
15694
public void CannotUpdateProjectFileWithMissingProjectSdk(string xml)
15795
{
158-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
159-
16096
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
16197

16298
canUpdate.ShouldBe(false);
99+
100+
logMessages.ShouldNotBeEmpty();
101+
logMessages.Count.ShouldBe(1);
102+
logMessages.First().ShouldContain("Specified project file Sdk () is not supported, please ensure the project sdk starts with 'Microsoft.NET.Sdk'");
163103
}
164104

165105
[TestCase(@"
@@ -175,11 +115,13 @@ public void CannotUpdateProjectFileWithMissingProjectSdk(string xml)
175115
[Description(NoMonoDescription)]
176116
public void CannotUpdateProjectFileWithoutAssemblyInfoGeneration(string xml)
177117
{
178-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
179-
180118
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
181119

182120
canUpdate.ShouldBe(false);
121+
122+
logMessages.ShouldNotBeEmpty();
123+
logMessages.Count.ShouldBe(1);
124+
logMessages.First().ShouldContain("Project file specifies <GenerateAssemblyInfo>false</GenerateAssemblyInfo>: versions set in this project file will not affect the output artifacts");
183125
}
184126

185127
[TestCase(@"
@@ -190,11 +132,13 @@ public void CannotUpdateProjectFileWithoutAssemblyInfoGeneration(string xml)
190132
[Description(NoMonoDescription)]
191133
public void CannotUpdateProjectFileWithoutAPropertyGroup(string xml)
192134
{
193-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
194-
195135
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
196136

197137
canUpdate.ShouldBe(false);
138+
139+
logMessages.ShouldNotBeEmpty();
140+
logMessages.Count.ShouldBe(1);
141+
logMessages.First().ShouldContain("Unable to locate any <PropertyGroup> elements in specified project file. Are you sure it is in a correct format?");
198142
}
199143

200144
[TestCase(@"
@@ -209,11 +153,9 @@ public void CannotUpdateProjectFileWithoutAPropertyGroup(string xml)
209153
[Description(NoMonoDescription)]
210154
public void UpdateProjectXmlVersionElementWithStandardXmlInsertsElement(string xml)
211155
{
212-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
213-
214156
var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
215157
var xmlRoot = XElement.Parse(xml);
216-
ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);
158+
ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer!);
217159

218160
var expectedXml = XElement.Parse(@"
219161
<Project Sdk=""Microsoft.NET.Sdk"">
@@ -239,11 +181,9 @@ public void UpdateProjectXmlVersionElementWithStandardXmlInsertsElement(string x
239181
[Description(NoMonoDescription)]
240182
public void UpdateProjectXmlVersionElementWithStandardXmlModifiesElement(string xml)
241183
{
242-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
243-
244184
var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
245185
var xmlRoot = XElement.Parse(xml);
246-
ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);
186+
ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer!);
247187

248188
var expectedXml = XElement.Parse(@"
249189
<Project Sdk=""Microsoft.NET.Sdk"">
@@ -272,11 +212,9 @@ public void UpdateProjectXmlVersionElementWithStandardXmlModifiesElement(string
272212
[Description(NoMonoDescription)]
273213
public void UpdateProjectXmlVersionElementWithDuplicatePropertyGroupsModifiesLastElement(string xml)
274214
{
275-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
276-
277215
var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
278216
var xmlRoot = XElement.Parse(xml);
279-
ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);
217+
ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer!);
280218

281219
var expectedXml = XElement.Parse(@"
282220
<Project Sdk=""Microsoft.NET.Sdk"">
@@ -306,11 +244,9 @@ public void UpdateProjectXmlVersionElementWithDuplicatePropertyGroupsModifiesLas
306244
[Description(NoMonoDescription)]
307245
public void UpdateProjectXmlVersionElementWithMultipleVersionElementsLastOneIsModified(string xml)
308246
{
309-
using var projectFileUpdater = new ProjectFileUpdater(this.log, this.fileSystem);
310-
311247
var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
312248
var xmlRoot = XElement.Parse(xml);
313-
ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);
249+
ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer!);
314250

315251
var expectedXml = XElement.Parse(@"
316252
<Project Sdk=""Microsoft.NET.Sdk"">
@@ -339,8 +275,8 @@ public void UpdateProjectFileAddsVersionToFile(string xml)
339275

340276
VerifyAssemblyInfoFile(xml, fileName, AssemblyVersioningScheme.MajorMinorPatch, verify: (fs, variables) =>
341277
{
342-
using var projectFileUpdater = new ProjectFileUpdater(this.log, fs);
343-
projectFileUpdater.Execute(variables, new AssemblyInfoContext(Path.GetTempPath(), false, fileName));
278+
using var projFileUpdater = new ProjectFileUpdater(this.log, fs);
279+
projFileUpdater.Execute(variables, new AssemblyInfoContext(Path.GetTempPath(), false, fileName));
344280

345281
var expectedXml = @"
346282
<Project Sdk=""Microsoft.NET.Sdk"">

src/GitVersion.Core/VersionConverters/AssemblyInfo/ProjectFileUpdater.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ namespace GitVersion.VersionConverters.AssemblyInfo;
77

88
public interface IProjectFileUpdater : IVersionConverter<AssemblyInfoContext>
99
{
10+
bool CanUpdateProjectFile(XElement xmlRoot);
1011
}
1112

1213
public sealed class ProjectFileUpdater : IProjectFileUpdater
1314
{
1415
internal const string AssemblyVersionElement = "AssemblyVersion";
15-
internal const string FileVersionElement = "FileVersion";
16-
internal const string InformationalVersionElement = "InformationalVersion";
17-
internal const string VersionElement = "Version";
16+
private const string FileVersionElement = "FileVersion";
17+
private const string InformationalVersionElement = "InformationalVersion";
18+
private const string VersionElement = "Version";
1819

1920
private readonly List<Action> restoreBackupTasks = new();
2021
private readonly List<Action> cleanupBackupTasks = new();
@@ -100,20 +101,18 @@ public void Execute(VersionVariables variables, AssemblyInfoContext context)
100101
CommitChanges();
101102
}
102103

103-
internal bool CanUpdateProjectFile(XElement xmlRoot)
104+
public bool CanUpdateProjectFile(XElement xmlRoot)
104105
{
105106
if (xmlRoot.Name != "Project")
106107
{
107108
this.log.Warning($"Invalid project file specified, root element must be <Project>.");
108109
return false;
109110
}
110111

111-
var supportedSdks = new[] { "Microsoft.NET.Sdk", "Microsoft.NET.Sdk.Web", "Microsoft.NET.Sdk.WindowsDesktop", "Microsoft.NET.Sdk.Razor", "Microsoft.NET.Sdk.Worker" };
112112
var sdkAttribute = xmlRoot.Attribute("Sdk");
113-
if (sdkAttribute == null || !supportedSdks.Contains(sdkAttribute.Value))
113+
if (sdkAttribute == null || !sdkAttribute.Value.StartsWith("Microsoft.NET.Sdk"))
114114
{
115-
var supportedSdkString = string.Join("|", supportedSdks);
116-
this.log.Warning($"Specified project file Sdk ({sdkAttribute?.Value}) is not supported, please ensure the project sdk is of the following: {supportedSdkString}.");
115+
this.log.Warning($"Specified project file Sdk ({sdkAttribute?.Value}) is not supported, please ensure the project sdk starts with 'Microsoft.NET.Sdk'");
117116
return false;
118117
}
119118

@@ -206,7 +205,7 @@ private IEnumerable<FileInfo> GetProjectFiles(AssemblyInfoContext context)
206205
}
207206
}
208207

209-
private bool IsSupportedProjectFile(string fileName)
208+
private static bool IsSupportedProjectFile(string fileName)
210209
{
211210
if (fileName.IsNullOrEmpty())
212211
{

0 commit comments

Comments
 (0)