From 8b877d3b5a323e6025ba76cd0f3a562164bcfd17 Mon Sep 17 00:00:00 2001 From: Peter Schraffl Date: Wed, 6 May 2020 09:52:05 +0200 Subject: [PATCH 1/2] fix: Ensure Valid Wix Version File --- .../VersionConverters/WixUpdater/WixVersionFileUpdater.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/GitVersionCore/VersionConverters/WixUpdater/WixVersionFileUpdater.cs b/src/GitVersionCore/VersionConverters/WixUpdater/WixVersionFileUpdater.cs index ca2808a7e9..1371196f17 100644 --- a/src/GitVersionCore/VersionConverters/WixUpdater/WixVersionFileUpdater.cs +++ b/src/GitVersionCore/VersionConverters/WixUpdater/WixVersionFileUpdater.cs @@ -12,14 +12,12 @@ public interface IWixVersionFileUpdater : IVersionConverter } public class WixVersionFileUpdater : IWixVersionFileUpdater { - private readonly IFileSystem fileSystem; private readonly ILog log; private string wixVersionFile; public const string WixVersionFileName = "GitVersion_WixVersion.wxi"; - public WixVersionFileUpdater(IFileSystem fileSystem, ILog log) + public WixVersionFileUpdater(ILog log) { - this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); this.log = log ?? throw new ArgumentNullException(nameof(log)); } @@ -35,8 +33,7 @@ public void Execute(VersionVariables variables, WixVersionContext context) var root = doc.DocumentElement; doc.InsertBefore(xmlDecl, root); - using var fs = fileSystem.OpenWrite(wixVersionFile); - doc.Save(fs); + doc.Save(wixVersionFile); } private static string GetWixFormatFromVersionVariables(VersionVariables variables) From 8f36a7bb9fad63f9419ed6ab8b9d81719458b4ac Mon Sep 17 00:00:00 2001 From: Peter Schraffl Date: Wed, 6 May 2020 10:33:24 +0200 Subject: [PATCH 2/2] fix: Make Unit Tests Succeeding --- ...sionFileWhenFileAlreadyExists.approved.txt | 34 +++++++++++++ .../VersionConverters/WixFileTests.cs | 50 +++++++++++++++++++ .../WixUpdater/WixVersionFileUpdater.cs | 8 ++- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/GitVersionCore.Tests/VersionConverters/Approved/WixFileTests.UpdateWixVersionFileWhenFileAlreadyExists.approved.txt diff --git a/src/GitVersionCore.Tests/VersionConverters/Approved/WixFileTests.UpdateWixVersionFileWhenFileAlreadyExists.approved.txt b/src/GitVersionCore.Tests/VersionConverters/Approved/WixFileTests.UpdateWixVersionFileWhenFileAlreadyExists.approved.txt new file mode 100644 index 0000000000..effba7df8e --- /dev/null +++ b/src/GitVersionCore.Tests/VersionConverters/Approved/WixFileTests.UpdateWixVersionFileWhenFileAlreadyExists.approved.txt @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/GitVersionCore.Tests/VersionConverters/WixFileTests.cs b/src/GitVersionCore.Tests/VersionConverters/WixFileTests.cs index 82e4f394ac..551dd6ed63 100644 --- a/src/GitVersionCore.Tests/VersionConverters/WixFileTests.cs +++ b/src/GitVersionCore.Tests/VersionConverters/WixFileTests.cs @@ -67,5 +67,55 @@ public void UpdateWixVersionFile() .ReadAllText(file) .ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved"))); } + + [Test] + [Category(NoMono)] + [Description(NoMonoDescription)] + public void UpdateWixVersionFileWhenFileAlreadyExists() + { + var workingDir = Path.GetTempPath(); + var semVer = new SemanticVersion + { + Major = 1, + Minor = 2, + Patch = 3, + BuildMetaData = "5.Branch.develop" + }; + + semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; + semVer.BuildMetaData.Sha = "commitSha"; + semVer.BuildMetaData.ShortSha = "commitShortSha"; + semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2019-02-20 23:59:59Z"); + + var config = new TestEffectiveConfiguration(buildMetaDataPadding: 2, legacySemVerPadding: 5); + + var stringBuilder = new StringBuilder(); + void Action(string s) => stringBuilder.AppendLine(s); + + var logAppender = new TestLogAppender(Action); + var log = new Log(logAppender); + + var sp = ConfigureServices(service => + { + service.AddSingleton(log); + }); + + var fileSystem = sp.GetService(); + var variableProvider = sp.GetService(); + var versionVariables = variableProvider.GetVariablesFor(semVer, config, false); + + using var wixVersionFileUpdater = sp.GetService(); + + // fake an already existing file + var file = Path.Combine(workingDir, WixVersionFileUpdater.WixVersionFileName); + fileSystem.WriteAllText(file, new string('x', 1024 * 1024)); + + wixVersionFileUpdater.Execute(versionVariables, new WixVersionContext(workingDir)); + + fileSystem + .ReadAllText(file) + .ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved"))); + } + } } diff --git a/src/GitVersionCore/VersionConverters/WixUpdater/WixVersionFileUpdater.cs b/src/GitVersionCore/VersionConverters/WixUpdater/WixVersionFileUpdater.cs index 1371196f17..238779f084 100644 --- a/src/GitVersionCore/VersionConverters/WixUpdater/WixVersionFileUpdater.cs +++ b/src/GitVersionCore/VersionConverters/WixUpdater/WixVersionFileUpdater.cs @@ -12,12 +12,14 @@ public interface IWixVersionFileUpdater : IVersionConverter } public class WixVersionFileUpdater : IWixVersionFileUpdater { + private readonly IFileSystem fileSystem; private readonly ILog log; private string wixVersionFile; public const string WixVersionFileName = "GitVersion_WixVersion.wxi"; - public WixVersionFileUpdater(ILog log) + public WixVersionFileUpdater(IFileSystem fileSystem, ILog log) { + this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); this.log = log ?? throw new ArgumentNullException(nameof(log)); } @@ -33,7 +35,9 @@ public void Execute(VersionVariables variables, WixVersionContext context) var root = doc.DocumentElement; doc.InsertBefore(xmlDecl, root); - doc.Save(wixVersionFile); + fileSystem.Delete(wixVersionFile); + using var fs = fileSystem.OpenWrite(wixVersionFile); + doc.Save(fs); } private static string GetWixFormatFromVersionVariables(VersionVariables variables)