|
| 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 | +} |
0 commit comments