|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.IO; |
| 8 | +using System.Threading; |
| 9 | +using Microsoft.Build.Construction; |
| 10 | +using Microsoft.Build.Exceptions; |
| 11 | +using Microsoft.Build.Shared; |
| 12 | +using Microsoft.VisualStudio.SolutionPersistence; |
| 13 | +using Microsoft.VisualStudio.SolutionPersistence.Model; |
| 14 | +using Microsoft.VisualStudio.SolutionPersistence.Serializer; |
| 15 | +using Shouldly; |
| 16 | +using Xunit; |
| 17 | +using Xunit.Abstractions; |
| 18 | + |
| 19 | +#nullable disable |
| 20 | + |
| 21 | +namespace Microsoft.Build.UnitTests.Construction |
| 22 | +{ |
| 23 | + public class SolutionFile_NewParser_Tests |
| 24 | + { |
| 25 | + public ITestOutputHelper TestOutputHelper { get; } |
| 26 | + |
| 27 | + public SolutionFile_NewParser_Tests(ITestOutputHelper testOutputHelper) |
| 28 | + { |
| 29 | + TestOutputHelper = testOutputHelper; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Tests to see that all the data/properties are correctly parsed out of a Venus |
| 34 | + /// project in a .SLN. This can be checked only here because of AspNetConfigurations protection level. |
| 35 | + /// </summary> |
| 36 | + [Theory] |
| 37 | + [InlineData(false)] |
| 38 | + [InlineData(true)] |
| 39 | + public void ProjectWithWebsiteProperties(bool convertToSlnx) |
| 40 | + { |
| 41 | + string solutionFileContents = |
| 42 | + """ |
| 43 | + Microsoft Visual Studio Solution File, Format Version 9.00 |
| 44 | + # Visual Studio 2005 |
| 45 | + Project(`{E24C65DC-7377-472B-9ABA-BC803B73C61A}`) = `C:\WebSites\WebApplication3\`, `C:\WebSites\WebApplication3\`, `{464FD0B9-E335-4677-BE1E-6B2F982F4D86}` |
| 46 | + ProjectSection(WebsiteProperties) = preProject |
| 47 | + ProjectReferences = `{FD705688-88D1-4C22-9BFF-86235D89C2FC}|CSCla;ssLibra;ry1.dll;{F0726D09-042B-4A7A-8A01-6BED2422BD5D}|VCClassLibrary1.dll;` |
| 48 | + Frontpage = false |
| 49 | + Debug.AspNetCompiler.VirtualPath = `/publishfirst` |
| 50 | + Debug.AspNetCompiler.PhysicalPath = `..\rajeev\temp\websites\myfirstwebsite\` |
| 51 | + Debug.AspNetCompiler.TargetPath = `..\rajeev\temp\publishfirst\` |
| 52 | + Debug.AspNetCompiler.ForceOverwrite = `true` |
| 53 | + Debug.AspNetCompiler.Updateable = `false` |
| 54 | + Debug.AspNetCompiler.Debug = `true` |
| 55 | + Debug.AspNetCompiler.KeyFile = `debugkeyfile.snk` |
| 56 | + Debug.AspNetCompiler.KeyContainer = `12345.container` |
| 57 | + Debug.AspNetCompiler.DelaySign = `true` |
| 58 | + Debug.AspNetCompiler.AllowPartiallyTrustedCallers = `false` |
| 59 | + Debug.AspNetCompiler.FixedNames = `debugfixednames` |
| 60 | + Release.AspNetCompiler.VirtualPath = `/publishfirst_release` |
| 61 | + Release.AspNetCompiler.PhysicalPath = `..\rajeev\temp\websites\myfirstwebsite_release\` |
| 62 | + Release.AspNetCompiler.TargetPath = `..\rajeev\temp\publishfirst_release\` |
| 63 | + Release.AspNetCompiler.ForceOverwrite = `true` |
| 64 | + Release.AspNetCompiler.Updateable = `true` |
| 65 | + Release.AspNetCompiler.Debug = `false` |
| 66 | + VWDPort = 63496 |
| 67 | + EndProjectSection |
| 68 | + EndProject |
| 69 | + Global |
| 70 | + GlobalSection(SolutionConfigurationPlatforms) = preSolution |
| 71 | + Debug|.NET = Debug|.NET |
| 72 | + EndGlobalSection |
| 73 | + GlobalSection(ProjectConfigurationPlatforms) = postSolution |
| 74 | + {464FD0B9-E335-4677-BE1E-6B2F982F4D86}.Debug|.NET.ActiveCfg = Debug|.NET |
| 75 | + {464FD0B9-E335-4677-BE1E-6B2F982F4D86}.Debug|.NET.Build.0 = Debug|.NET |
| 76 | + EndGlobalSection |
| 77 | + GlobalSection(SolutionProperties) = preSolution |
| 78 | + HideSolutionNode = FALSE |
| 79 | + EndGlobalSection |
| 80 | + EndGlobal |
| 81 | + """; |
| 82 | + |
| 83 | + SolutionFile solution = ParseSolutionHelper(solutionFileContents.Replace('`', '"'), convertToSlnx); |
| 84 | + |
| 85 | + solution.ProjectsInOrder.ShouldHaveSingleItem(); |
| 86 | + |
| 87 | + solution.ProjectsInOrder[0].ProjectType.ShouldBe(SolutionProjectType.WebProject); |
| 88 | + solution.ProjectsInOrder[0].ProjectName.ShouldBe(@"C:\WebSites\WebApplication3\"); |
| 89 | + solution.ProjectsInOrder[0].RelativePath.ShouldBe(ConvertToUnixPathIfNeeded(@"C:\WebSites\WebApplication3\")); |
| 90 | + solution.ProjectsInOrder[0].Dependencies.Count.ShouldBe(2); |
| 91 | + solution.ProjectsInOrder[0].ParentProjectGuid.ShouldBeNull(); |
| 92 | + solution.ProjectsInOrder[0].GetUniqueProjectName().ShouldBe(@"C:\WebSites\WebApplication3\"); |
| 93 | + |
| 94 | + Hashtable aspNetCompilerParameters = solution.ProjectsInOrder[0].AspNetConfigurations; |
| 95 | + AspNetCompilerParameters debugAspNetCompilerParameters = (AspNetCompilerParameters)aspNetCompilerParameters["Debug"]; |
| 96 | + AspNetCompilerParameters releaseAspNetCompilerParameters = (AspNetCompilerParameters)aspNetCompilerParameters["Release"]; |
| 97 | + |
| 98 | + debugAspNetCompilerParameters.aspNetVirtualPath.ShouldBe(@"/publishfirst"); |
| 99 | + debugAspNetCompilerParameters.aspNetPhysicalPath.ShouldBe(@"..\rajeev\temp\websites\myfirstwebsite\"); |
| 100 | + debugAspNetCompilerParameters.aspNetTargetPath.ShouldBe(@"..\rajeev\temp\publishfirst\"); |
| 101 | + debugAspNetCompilerParameters.aspNetForce.ShouldBe(@"true"); |
| 102 | + debugAspNetCompilerParameters.aspNetUpdateable.ShouldBe(@"false"); |
| 103 | + debugAspNetCompilerParameters.aspNetDebug.ShouldBe(@"true"); |
| 104 | + debugAspNetCompilerParameters.aspNetKeyFile.ShouldBe(@"debugkeyfile.snk"); |
| 105 | + debugAspNetCompilerParameters.aspNetKeyContainer.ShouldBe(@"12345.container"); |
| 106 | + debugAspNetCompilerParameters.aspNetDelaySign.ShouldBe(@"true"); |
| 107 | + debugAspNetCompilerParameters.aspNetAPTCA.ShouldBe(@"false"); |
| 108 | + debugAspNetCompilerParameters.aspNetFixedNames.ShouldBe(@"debugfixednames"); |
| 109 | + |
| 110 | + releaseAspNetCompilerParameters.aspNetVirtualPath.ShouldBe(@"/publishfirst_release"); |
| 111 | + releaseAspNetCompilerParameters.aspNetPhysicalPath.ShouldBe(@"..\rajeev\temp\websites\myfirstwebsite_release\"); |
| 112 | + releaseAspNetCompilerParameters.aspNetTargetPath.ShouldBe(@"..\rajeev\temp\publishfirst_release\"); |
| 113 | + releaseAspNetCompilerParameters.aspNetForce.ShouldBe(@"true"); |
| 114 | + releaseAspNetCompilerParameters.aspNetUpdateable.ShouldBe(@"true"); |
| 115 | + releaseAspNetCompilerParameters.aspNetDebug.ShouldBe(@"false"); |
| 116 | + releaseAspNetCompilerParameters.aspNetKeyFile.ShouldBe(""); |
| 117 | + releaseAspNetCompilerParameters.aspNetKeyContainer.ShouldBe(""); |
| 118 | + releaseAspNetCompilerParameters.aspNetDelaySign.ShouldBe(""); |
| 119 | + releaseAspNetCompilerParameters.aspNetAPTCA.ShouldBe(""); |
| 120 | + releaseAspNetCompilerParameters.aspNetFixedNames.ShouldBe(""); |
| 121 | + |
| 122 | + List<string> aspNetProjectReferences = solution.ProjectsInOrder[0].ProjectReferences; |
| 123 | + aspNetProjectReferences.Count.ShouldBe(2); |
| 124 | + aspNetProjectReferences[0].ShouldBe("{FD705688-88D1-4C22-9BFF-86235D89C2FC}"); |
| 125 | + aspNetProjectReferences[1].ShouldBe("{F0726D09-042B-4A7A-8A01-6BED2422BD5D}"); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Helper method to create a SolutionFile object, and call it to parse the SLN file |
| 130 | + /// represented by the string contents passed in. Optionally can convert the SLN to SLNX and then parse the solution. |
| 131 | + /// </summary> |
| 132 | + internal static SolutionFile ParseSolutionHelper(string solutionFileContents, bool convertToSlnx = false) |
| 133 | + { |
| 134 | + solutionFileContents = solutionFileContents.Replace('\'', '"'); |
| 135 | + |
| 136 | + using (TestEnvironment testEnvironment = TestEnvironment.Create()) |
| 137 | + { |
| 138 | + TransientTestFile sln = testEnvironment.CreateFile(FileUtilities.GetTemporaryFileName(".sln"), solutionFileContents); |
| 139 | + |
| 140 | + string solutionPath = convertToSlnx ? ConvertToSlnx(sln.Path) : sln.Path; |
| 141 | + |
| 142 | + SolutionFile solutionFile = new SolutionFile { FullPath = solutionPath }; |
| 143 | + solutionFile.ParseUsingNewParser(); |
| 144 | + return solutionFile; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static string ConvertToSlnx(string slnPath) |
| 149 | + { |
| 150 | + string slnxPath = slnPath + "x"; |
| 151 | + ISolutionSerializer serializer = SolutionSerializers.GetSerializerByMoniker(slnPath).ShouldNotBeNull(); |
| 152 | + SolutionModel solutionModel = serializer.OpenAsync(slnPath, CancellationToken.None).Result; |
| 153 | + SolutionSerializers.SlnXml.SaveAsync(slnxPath, solutionModel, CancellationToken.None).Wait(); |
| 154 | + return slnxPath; |
| 155 | + } |
| 156 | + |
| 157 | + private static string ConvertToUnixPathIfNeeded(string path) |
| 158 | + { |
| 159 | + // In the new parser, ProjectModel.FilePath is converted to Unix-style. |
| 160 | + return !NativeMethodsShared.IsWindows ? path.Replace('\\', '/') : path; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments