Skip to content

Commit 87ed41b

Browse files
authored
Merge pull request #2265 from svengeance/ProjectFileUpdating
#1611 Project file updating
2 parents 08a43ed + daeb873 commit 87ed41b

File tree

15 files changed

+653
-13
lines changed

15 files changed

+653
-13
lines changed

src/GitVersionCore.Tests/Helpers/TestFileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void WriteAllText(string file, string fileContents, Encoding encoding)
7373
fileSystem[path] = encoding.GetBytes(fileContents);
7474
}
7575

76-
public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
76+
public IEnumerable<string> DirectoryEnumerateFiles(string directory, string searchPattern, SearchOption searchOption)
7777
{
7878
throw new NotImplementedException();
7979
}

src/GitVersionCore.Tests/VersionConverters/AssemblyInfoFileUpdaterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void ShouldStartSearchFromWorkingDirectory()
139139
using var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(log, fileSystem);
140140
assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(workingDir, false, assemblyInfoFiles.ToArray()));
141141

142-
fileSystem.Received().DirectoryGetFiles(Arg.Is(workingDir), Arg.Any<string>(), Arg.Any<SearchOption>());
142+
fileSystem.Received().DirectoryEnumerateFiles(Arg.Is(workingDir), Arg.Any<string>(), Arg.Any<SearchOption>());
143143
}
144144

145145
[TestCase("cs", "[assembly: AssemblyVersion(\"1.0.0.0\")]\r\n[assembly: AssemblyInformationalVersion(\"1.0.0.0\")]\r\n[assembly: AssemblyFileVersion(\"1.0.0.0\")]")]
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
using System;
2+
using System.IO;
3+
using System.Xml.Linq;
4+
using GitVersion;
5+
using GitVersion.Extensions;
6+
using GitVersion.Logging;
7+
using GitVersion.OutputVariables;
8+
using GitVersion.VersionCalculation;
9+
using GitVersion.VersionConverters.AssemblyInfo;
10+
using GitVersionCore.Tests.Helpers;
11+
using Microsoft.Extensions.DependencyInjection;
12+
using NSubstitute;
13+
using NUnit.Framework;
14+
using Shouldly;
15+
16+
namespace GitVersionCore.Tests
17+
{
18+
[TestFixture]
19+
[Parallelizable(ParallelScope.None)]
20+
public class ProjectFileUpdaterTests : TestBase
21+
{
22+
private IVariableProvider variableProvider;
23+
private ILog log;
24+
private IFileSystem fileSystem;
25+
26+
[SetUp]
27+
public void Setup()
28+
{
29+
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestCaseAttribute>();
30+
var sp = ConfigureServices();
31+
log = Substitute.For<ILog>();
32+
fileSystem = sp.GetService<IFileSystem>();
33+
variableProvider = sp.GetService<IVariableProvider>();
34+
}
35+
36+
[TestCase(@"
37+
<Project Sdk=""Microsoft.NET.Sdk"">
38+
<PropertyGroup>
39+
<OutputType>Exe</OutputType>
40+
<TargetFramework>netcoreapp3.1</TargetFramework>
41+
</PropertyGroup>
42+
</Project>
43+
")]
44+
[Category(NoMono)]
45+
[Description(NoMonoDescription)]
46+
public void CanUpdateProjectFileWithStandardProjectFileXml(string xml)
47+
{
48+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
49+
50+
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
51+
52+
canUpdate.ShouldBe(true);
53+
}
54+
55+
[TestCase(@"
56+
<Project Sdk=""SomeOtherProject.Sdk"">
57+
<PropertyGroup>
58+
<OutputType>Exe</OutputType>
59+
<TargetFramework>netcoreapp3.1</TargetFramework>
60+
</PropertyGroup>
61+
</Project>
62+
")]
63+
[Category(NoMono)]
64+
[Description(NoMonoDescription)]
65+
public void CannotUpdateProjectFileWithIncorrectProjectSdk(string xml)
66+
{
67+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
68+
69+
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
70+
71+
canUpdate.ShouldBe(false);
72+
}
73+
74+
[TestCase(@"
75+
<Project>
76+
<PropertyGroup>
77+
<OutputType>Exe</OutputType>
78+
<TargetFramework>netcoreapp3.1</TargetFramework>
79+
</PropertyGroup>
80+
</Project>
81+
")]
82+
[Category(NoMono)]
83+
[Description(NoMonoDescription)]
84+
public void CannotUpdateProjectFileWithMissingProjectSdk(string xml)
85+
{
86+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
87+
88+
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
89+
90+
canUpdate.ShouldBe(false);
91+
}
92+
93+
[TestCase(@"
94+
<Project Sdk=""Microsoft.NET.Sdk"">
95+
<PropertyGroup>
96+
<OutputType>Exe</OutputType>
97+
<TargetFramework>netcoreapp3.1</TargetFramework>
98+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
99+
</PropertyGroup>
100+
</Project>
101+
")]
102+
[Category(NoMono)]
103+
[Description(NoMonoDescription)]
104+
public void CannotUpdateProjectFileWithoutAssemblyInfoGeneration(string xml)
105+
{
106+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
107+
108+
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
109+
110+
canUpdate.ShouldBe(false);
111+
}
112+
113+
[TestCase(@"
114+
<Project Sdk=""Microsoft.NET.Sdk"">
115+
</Project>
116+
")]
117+
[Category(NoMono)]
118+
[Description(NoMonoDescription)]
119+
public void CannotUpdateProjectFileWithoutAPropertyGroup(string xml)
120+
{
121+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
122+
123+
var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));
124+
125+
canUpdate.ShouldBe(false);
126+
}
127+
128+
[TestCase(@"
129+
<Project Sdk=""Microsoft.NET.Sdk"">
130+
<PropertyGroup>
131+
<OutputType>Exe</OutputType>
132+
<TargetFramework>netcoreapp3.1</TargetFramework>
133+
</PropertyGroup>
134+
</Project>"
135+
)]
136+
[Category(NoMono)]
137+
[Description(NoMonoDescription)]
138+
public void UpdateProjectXmlVersionElementWithStandardXmlInsertsElement(string xml)
139+
{
140+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
141+
142+
var variables = variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
143+
var xmlRoot = XElement.Parse(xml);
144+
projectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);
145+
146+
var expectedXml = XElement.Parse(@"
147+
<Project Sdk=""Microsoft.NET.Sdk"">
148+
<PropertyGroup>
149+
<OutputType>Exe</OutputType>
150+
<TargetFramework>netcoreapp3.1</TargetFramework>
151+
<AssemblyVersion>2.0.0.0</AssemblyVersion>
152+
</PropertyGroup>
153+
</Project>");
154+
xmlRoot.ToString().ShouldBe(expectedXml.ToString());
155+
}
156+
157+
[TestCase(@"
158+
<Project Sdk=""Microsoft.NET.Sdk"">
159+
<PropertyGroup>
160+
<OutputType>Exe</OutputType>
161+
<TargetFramework>netcoreapp3.1</TargetFramework>
162+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
163+
</PropertyGroup>
164+
</Project>"
165+
)]
166+
[Category(NoMono)]
167+
[Description(NoMonoDescription)]
168+
public void UpdateProjectXmlVersionElementWithStandardXmlModifiesElement(string xml)
169+
{
170+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
171+
172+
var variables = variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
173+
var xmlRoot = XElement.Parse(xml);
174+
projectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);
175+
176+
var expectedXml = XElement.Parse(@"
177+
<Project Sdk=""Microsoft.NET.Sdk"">
178+
<PropertyGroup>
179+
<OutputType>Exe</OutputType>
180+
<TargetFramework>netcoreapp3.1</TargetFramework>
181+
<AssemblyVersion>2.0.0.0</AssemblyVersion>
182+
</PropertyGroup>
183+
</Project>");
184+
xmlRoot.ToString().ShouldBe(expectedXml.ToString());
185+
}
186+
187+
[TestCase(@"
188+
<Project Sdk=""Microsoft.NET.Sdk"">
189+
<PropertyGroup>
190+
<OutputType>Exe</OutputType>
191+
<TargetFramework>netcoreapp3.1</TargetFramework>
192+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
193+
</PropertyGroup>
194+
<PropertyGroup>
195+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
196+
</PropertyGroup>
197+
</Project>"
198+
)]
199+
[Category(NoMono)]
200+
[Description(NoMonoDescription)]
201+
public void UpdateProjectXmlVersionElementWithDuplicatePropertyGroupsModifiesLastElement(string xml)
202+
{
203+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
204+
205+
var variables = variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
206+
var xmlRoot = XElement.Parse(xml);
207+
projectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);
208+
209+
var expectedXml = XElement.Parse(@"
210+
<Project Sdk=""Microsoft.NET.Sdk"">
211+
<PropertyGroup>
212+
<OutputType>Exe</OutputType>
213+
<TargetFramework>netcoreapp3.1</TargetFramework>
214+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
215+
</PropertyGroup>
216+
<PropertyGroup>
217+
<AssemblyVersion>2.0.0.0</AssemblyVersion>
218+
</PropertyGroup>
219+
</Project>");
220+
xmlRoot.ToString().ShouldBe(expectedXml.ToString());
221+
}
222+
223+
[TestCase(@"
224+
<Project Sdk=""Microsoft.NET.Sdk"">
225+
<PropertyGroup>
226+
<OutputType>Exe</OutputType>
227+
<TargetFramework>netcoreapp3.1</TargetFramework>
228+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
229+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
230+
</PropertyGroup>
231+
</Project>"
232+
)]
233+
234+
[Category(NoMono)]
235+
[Description(NoMonoDescription)]
236+
public void UpdateProjectXmlVersionElementWithMultipleVersionElementsLastOneIsModified(string xml)
237+
{
238+
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);
239+
240+
var variables = variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
241+
var xmlRoot = XElement.Parse(xml);
242+
projectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);
243+
244+
var expectedXml = XElement.Parse(@"
245+
<Project Sdk=""Microsoft.NET.Sdk"">
246+
<PropertyGroup>
247+
<OutputType>Exe</OutputType>
248+
<TargetFramework>netcoreapp3.1</TargetFramework>
249+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
250+
<AssemblyVersion>2.0.0.0</AssemblyVersion>
251+
</PropertyGroup>
252+
</Project>");
253+
xmlRoot.ToString().ShouldBe(expectedXml.ToString());
254+
}
255+
256+
[TestCase(@"
257+
<Project Sdk=""Microsoft.NET.Sdk"">
258+
<PropertyGroup>
259+
<OutputType>Exe</OutputType>
260+
<TargetFramework>netcoreapp3.1</TargetFramework>
261+
</PropertyGroup>
262+
</Project>")]
263+
[Category(NoMono)]
264+
[Description(NoMonoDescription)]
265+
public void UpdateProjectFileAddsVersionToFile(string xml)
266+
{
267+
var fileName = Path.Combine(Path.GetTempPath(), "TestProject.csproj");
268+
269+
VerifyAssemblyInfoFile(xml, fileName, AssemblyVersioningScheme.MajorMinorPatch, verify: (fs, variables) =>
270+
{
271+
using var projectFileUpdater = new ProjectFileUpdater(log, fs);
272+
projectFileUpdater.Execute(variables, new AssemblyInfoContext(Path.GetTempPath(), false, fileName));
273+
274+
var expectedXml = @"
275+
<Project Sdk=""Microsoft.NET.Sdk"">
276+
<PropertyGroup>
277+
<OutputType>Exe</OutputType>
278+
<TargetFramework>netcoreapp3.1</TargetFramework>
279+
<AssemblyVersion>2.3.1.0</AssemblyVersion>
280+
<FileVersion>2.3.1.0</FileVersion>
281+
<InformationalVersion>2.3.1+3.Branch.foo.Sha.hash</InformationalVersion>
282+
</PropertyGroup>
283+
</Project>";
284+
var transformedXml = fs.ReadAllText(fileName);
285+
transformedXml.ShouldBe(XElement.Parse(expectedXml).ToString());
286+
});
287+
}
288+
289+
private void VerifyAssemblyInfoFile(
290+
string projectFileContent,
291+
string fileName,
292+
AssemblyVersioningScheme versioningScheme = AssemblyVersioningScheme.MajorMinorPatch,
293+
Action<IFileSystem, VersionVariables> verify = null)
294+
{
295+
fileSystem = Substitute.For<IFileSystem>();
296+
var version = new SemanticVersion
297+
{
298+
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceHash", 3, "foo", "hash", "shortHash", DateTimeOffset.Now),
299+
Major = 2,
300+
Minor = 3,
301+
Patch = 1
302+
};
303+
304+
fileSystem.Exists(fileName).Returns(true);
305+
fileSystem.ReadAllText(fileName).Returns(projectFileContent);
306+
fileSystem.When(f => f.WriteAllText(fileName, Arg.Any<string>())).Do(c =>
307+
{
308+
projectFileContent = c.ArgAt<string>(1);
309+
fileSystem.ReadAllText(fileName).Returns(projectFileContent);
310+
});
311+
312+
var config = new TestEffectiveConfiguration(assemblyVersioningScheme: versioningScheme);
313+
var variables = variableProvider.GetVariablesFor(version, config, false);
314+
315+
verify?.Invoke(fileSystem, variables);
316+
}
317+
}
318+
}

src/GitVersionCore/Common/IFileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IFileSystem
1313
string ReadAllText(string path);
1414
void WriteAllText(string file, string fileContents);
1515
void WriteAllText(string file, string fileContents, Encoding encoding);
16-
IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption);
16+
IEnumerable<string> DirectoryEnumerateFiles(string directory, string searchPattern, SearchOption searchOption);
1717
Stream OpenWrite(string path);
1818
Stream OpenRead(string path);
1919
void CreateDirectory(string path);

src/GitVersionCore/Core/FileSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public void WriteAllText(string file, string fileContents, Encoding encoding)
4646
File.WriteAllText(file, fileContents, encoding);
4747
}
4848

49-
public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
49+
public IEnumerable<string> DirectoryEnumerateFiles(string directory, string searchPattern, SearchOption searchOption)
5050
{
51-
return Directory.GetFiles(directory, searchPattern, searchOption);
51+
return Directory.EnumerateFiles(directory, searchPattern, searchOption);
5252
}
5353

5454
public Stream OpenWrite(string path)

0 commit comments

Comments
 (0)