Skip to content

#1611 Project file updating #2265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GitVersionCore.Tests/Helpers/TestFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void WriteAllText(string file, string fileContents, Encoding encoding)
fileSystem[path] = encoding.GetBytes(fileContents);
}

public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
public IEnumerable<string> DirectoryEnumerateFiles(string directory, string searchPattern, SearchOption searchOption)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void ShouldStartSearchFromWorkingDirectory()
using var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(log, fileSystem);
assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(workingDir, false, assemblyInfoFiles.ToArray()));

fileSystem.Received().DirectoryGetFiles(Arg.Is(workingDir), Arg.Any<string>(), Arg.Any<SearchOption>());
fileSystem.Received().DirectoryEnumerateFiles(Arg.Is(workingDir), Arg.Any<string>(), Arg.Any<SearchOption>());
}

[TestCase("cs", "[assembly: AssemblyVersion(\"1.0.0.0\")]\r\n[assembly: AssemblyInformationalVersion(\"1.0.0.0\")]\r\n[assembly: AssemblyFileVersion(\"1.0.0.0\")]")]
Expand Down
318 changes: 318 additions & 0 deletions src/GitVersionCore.Tests/VersionConverters/ProjectFileUpdaterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
using System;
using System.IO;
using System.Xml.Linq;
using GitVersion;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;
using GitVersion.VersionCalculation;
using GitVersion.VersionConverters.AssemblyInfo;
using GitVersionCore.Tests.Helpers;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using NUnit.Framework;
using Shouldly;

namespace GitVersionCore.Tests
{
[TestFixture]
[Parallelizable(ParallelScope.None)]
public class ProjectFileUpdaterTests : TestBase
{
private IVariableProvider variableProvider;
private ILog log;
private IFileSystem fileSystem;

[SetUp]
public void Setup()
{
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestCaseAttribute>();
var sp = ConfigureServices();
log = Substitute.For<ILog>();
fileSystem = sp.GetService<IFileSystem>();
variableProvider = sp.GetService<IVariableProvider>();
}

[TestCase(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
")]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void CanUpdateProjectFileWithStandardProjectFileXml(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));

canUpdate.ShouldBe(true);
}

[TestCase(@"
<Project Sdk=""SomeOtherProject.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
")]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void CannotUpdateProjectFileWithIncorrectProjectSdk(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));

canUpdate.ShouldBe(false);
}

[TestCase(@"
<Project>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
")]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void CannotUpdateProjectFileWithMissingProjectSdk(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));

canUpdate.ShouldBe(false);
}

[TestCase(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
</Project>
")]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void CannotUpdateProjectFileWithoutAssemblyInfoGeneration(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));

canUpdate.ShouldBe(false);
}

[TestCase(@"
<Project Sdk=""Microsoft.NET.Sdk"">
</Project>
")]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void CannotUpdateProjectFileWithoutAPropertyGroup(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var canUpdate = projectFileUpdater.CanUpdateProjectFile(XElement.Parse(xml));

canUpdate.ShouldBe(false);
}

[TestCase(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>"
)]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void UpdateProjectXmlVersionElementWithStandardXmlInsertsElement(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var variables = variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
var xmlRoot = XElement.Parse(xml);
projectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);

var expectedXml = XElement.Parse(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</PropertyGroup>
</Project>");
xmlRoot.ToString().ShouldBe(expectedXml.ToString());
}

[TestCase(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</PropertyGroup>
</Project>"
)]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void UpdateProjectXmlVersionElementWithStandardXmlModifiesElement(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var variables = variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
var xmlRoot = XElement.Parse(xml);
projectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);

var expectedXml = XElement.Parse(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</PropertyGroup>
</Project>");
xmlRoot.ToString().ShouldBe(expectedXml.ToString());
}

[TestCase(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</PropertyGroup>
<PropertyGroup>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</PropertyGroup>
</Project>"
)]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void UpdateProjectXmlVersionElementWithDuplicatePropertyGroupsModifiesLastElement(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var variables = variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
var xmlRoot = XElement.Parse(xml);
projectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);

var expectedXml = XElement.Parse(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</PropertyGroup>
<PropertyGroup>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</PropertyGroup>
</Project>");
xmlRoot.ToString().ShouldBe(expectedXml.ToString());
}

[TestCase(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</PropertyGroup>
</Project>"
)]

[Category(NoMono)]
[Description(NoMonoDescription)]
public void UpdateProjectXmlVersionElementWithMultipleVersionElementsLastOneIsModified(string xml)
{
using var projectFileUpdater = new ProjectFileUpdater(log, fileSystem);

var variables = variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", "v"), new TestEffectiveConfiguration(), false);
var xmlRoot = XElement.Parse(xml);
projectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer);

var expectedXml = XElement.Parse(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</PropertyGroup>
</Project>");
xmlRoot.ToString().ShouldBe(expectedXml.ToString());
}

[TestCase(@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>")]
[Category(NoMono)]
[Description(NoMonoDescription)]
public void UpdateProjectFileAddsVersionToFile(string xml)
{
var fileName = Path.Combine(Path.GetTempPath(), "TestProject.csproj");

VerifyAssemblyInfoFile(xml, fileName, AssemblyVersioningScheme.MajorMinorPatch, verify: (fs, variables) =>
{
using var projectFileUpdater = new ProjectFileUpdater(log, fs);
projectFileUpdater.Execute(variables, new AssemblyInfoContext(Path.GetTempPath(), false, fileName));

var expectedXml = @"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>2.3.1.0</AssemblyVersion>
<FileVersion>2.3.1.0</FileVersion>
<InformationalVersion>2.3.1+3.Branch.foo.Sha.hash</InformationalVersion>
</PropertyGroup>
</Project>";
var transformedXml = fs.ReadAllText(fileName);
transformedXml.ShouldBe(XElement.Parse(expectedXml).ToString());
});
}

private void VerifyAssemblyInfoFile(
string projectFileContent,
string fileName,
AssemblyVersioningScheme versioningScheme = AssemblyVersioningScheme.MajorMinorPatch,
Action<IFileSystem, VersionVariables> verify = null)
{
fileSystem = Substitute.For<IFileSystem>();
var version = new SemanticVersion
{
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceHash", 3, "foo", "hash", "shortHash", DateTimeOffset.Now),
Major = 2,
Minor = 3,
Patch = 1
};

fileSystem.Exists(fileName).Returns(true);
fileSystem.ReadAllText(fileName).Returns(projectFileContent);
fileSystem.When(f => f.WriteAllText(fileName, Arg.Any<string>())).Do(c =>
{
projectFileContent = c.ArgAt<string>(1);
fileSystem.ReadAllText(fileName).Returns(projectFileContent);
});

var config = new TestEffectiveConfiguration(assemblyVersioningScheme: versioningScheme);
var variables = variableProvider.GetVariablesFor(version, config, false);

verify?.Invoke(fileSystem, variables);
}
}
}
2 changes: 1 addition & 1 deletion src/GitVersionCore/Common/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IFileSystem
string ReadAllText(string path);
void WriteAllText(string file, string fileContents);
void WriteAllText(string file, string fileContents, Encoding encoding);
IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption);
IEnumerable<string> DirectoryEnumerateFiles(string directory, string searchPattern, SearchOption searchOption);
Stream OpenWrite(string path);
Stream OpenRead(string path);
void CreateDirectory(string path);
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore/Core/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void WriteAllText(string file, string fileContents, Encoding encoding)
File.WriteAllText(file, fileContents, encoding);
}

public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
public IEnumerable<string> DirectoryEnumerateFiles(string directory, string searchPattern, SearchOption searchOption)
{
return Directory.GetFiles(directory, searchPattern, searchOption);
return Directory.EnumerateFiles(directory, searchPattern, searchOption);
}

public Stream OpenWrite(string path)
Expand Down
Loading