Improve Sln reader/writer tests (#5157)

* WIP improve sln reader/writer tests

* Complete the tests
This commit is contained in:
Justin Goshi 2016-12-28 09:40:49 -10:00 committed by GitHub
parent 05df71825d
commit 35f2192f3b
6 changed files with 259 additions and 119 deletions

View file

@ -1,12 +0,0 @@
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

View file

@ -1,22 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TestAppWithSln", "TestAppWithSln.xproj", "{0138CB8F-4AA9-4029-A21E-C07C30F425BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0138CB8F-4AA9-4029-A21E-C07C30F425BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0138CB8F-4AA9-4029-A21E-C07C30F425BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0138CB8F-4AA9-4029-A21E-C07C30F425BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0138CB8F-4AA9-4029-A21E-C07C30F425BA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View file

@ -1,18 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.23107" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.23107</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>0138cb8f-4aa9-4029-a21e-c07c30f425ba</ProjectGuid>
<RootNamespace>TestAppWithContents</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\..\artifacts\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -1,25 +0,0 @@
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": "1.0.1"
},
"frameworks": {
"netcoreapp1.0": {}
},
"runtimes": {
"win7-x64": {},
"win7-x86": {},
"osx.10.10-x64": {},
"osx.10.11-x64": {},
"ubuntu.14.04-x64": {},
"ubuntu.16.04-x64": {},
"centos.7-x64": {},
"rhel.7.2-x64": {},
"debian.8-x64": {},
"fedora.23-x64": {},
"opensuse.13.2-x64": {}
}
}

View file

@ -1139,7 +1139,7 @@ namespace Microsoft.DotNet.Cli.Sln.Internal
}
}
class InvalidSolutionFormatException : Exception
public class InvalidSolutionFormatException : Exception
{
public InvalidSolutionFormatException(int line) : base("Invalid format in line " + line)
{

View file

@ -13,65 +13,282 @@ namespace Microsoft.DotNet.Cli.Sln.Internal.Tests
{
public class GivenAnSlnFile : TestBase
{
private const string SolutionModified = @"
Microsoft Visual Studio Solution File, Format Version 14.00
# Visual Studio 16
VisualStudioVersion = 16.0.26006.2
MinimumVisualStudioVersion = 11.0.40219.1
Project(""{7072A694-548F-4CAE-A58F-12D257D5F486}"") = ""AppModified"", ""AppModified\AppModified.csproj"", ""{9A19103F-16F7-4668-BE54-9A1E7A4F7556}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|x64.ActiveCfg = Debug|x64
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|x64.Build.0 = Debug|x64
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|x86.ActiveCfg = Debug|x86
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|x86.Build.0 = Debug|x86
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|Any CPU.Build.0 = Release|Any CPU
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|x64.ActiveCfg = Release|x64
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|x64.Build.0 = Release|x64
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|x86.ActiveCfg = Release|x86
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = TRUE
EndGlobalSection
EndGlobal
";
private const string SolutionWithAppAndLibProjects = @"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26006.2
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{9A19103F-16F7-4668-BE54-9A1E7A4F7556}"") = ""App"", ""App\App.csproj"", ""{7072A694-548F-4CAE-A58F-12D257D5F486}""
EndProject
Project(""{13B669BE-BB05-4DDF-9536-439F39A36129}"") = ""Lib"", ""..\Lib\Lib.csproj"", ""{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|x64.ActiveCfg = Debug|x64
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|x64.Build.0 = Debug|x64
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|x86.ActiveCfg = Debug|x86
{7072A694-548F-4CAE-A58F-12D257D5F486}.Debug|x86.Build.0 = Debug|x86
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|Any CPU.Build.0 = Release|Any CPU
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|x64.ActiveCfg = Release|x64
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|x64.Build.0 = Release|x64
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|x86.ActiveCfg = Release|x86
{7072A694-548F-4CAE-A58F-12D257D5F486}.Release|x86.Build.0 = Release|x86
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Debug|x64.ActiveCfg = Debug|x64
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Debug|x64.Build.0 = Debug|x64
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Debug|x86.ActiveCfg = Debug|x86
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Debug|x86.Build.0 = Debug|x86
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Release|Any CPU.Build.0 = Release|Any CPU
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Release|x64.ActiveCfg = Release|x64
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Release|x64.Build.0 = Release|x64
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Release|x86.ActiveCfg = Release|x86
{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
";
[Fact]
public void WhenGivenAValidPathItReadsAnSlnFile()
public void WhenGivenAValidSlnFileItReadsAndVerifiesContents()
{
var solutionDirectory =
TestAssetsManager.CreateTestInstance("TestAppWithSln", callingMethod: "p").Path;
var tmpFile = Temp.CreateFile();
tmpFile.WriteAllText(SolutionWithAppAndLibProjects);
var solutionFullPath = Path.Combine(solutionDirectory, "TestAppWithSln.sln");
var slnFile = SlnFile.Read(solutionFullPath);
SlnFile slnFile = SlnFile.Read(tmpFile.Path);
slnFile.FormatVersion.Should().Be("12.00");
slnFile.ProductDescription.Should().Be("Visual Studio 14");
slnFile.VisualStudioVersion.Should().Be("14.0.25420.1");
slnFile.ProductDescription.Should().Be("Visual Studio 15");
slnFile.VisualStudioVersion.Should().Be("15.0.26006.2");
slnFile.MinimumVisualStudioVersion.Should().Be("10.0.40219.1");
slnFile.BaseDirectory.Should().Be(solutionDirectory);
slnFile.FullPath.Should().Be(solutionFullPath);
slnFile.BaseDirectory.Should().Be(Path.GetDirectoryName(tmpFile.Path));
slnFile.FullPath.Should().Be(tmpFile.Path);
slnFile.Projects.Count.Should().Be(1);
slnFile.Projects.Count.Should().Be(2);
var project = slnFile.Projects[0];
project.Id.Should().Be("{0138CB8F-4AA9-4029-A21E-C07C30F425BA}");
project.TypeGuid.Should().Be("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}");
project.Name.Should().Be("TestAppWithSln");
project.FilePath.Should().Be("TestAppWithSln.xproj");
project.Id.Should().Be("{7072A694-548F-4CAE-A58F-12D257D5F486}");
project.TypeGuid.Should().Be("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}");
project.Name.Should().Be("App");
project.FilePath.Should().Be(Path.Combine("App", "App.csproj"));
project = slnFile.Projects[1];
project.Id.Should().Be("{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}");
project.TypeGuid.Should().Be("{13B669BE-BB05-4DDF-9536-439F39A36129}");
project.Name.Should().Be("Lib");
project.FilePath.Should().Be(Path.Combine("..", "Lib", "Lib.csproj"));
slnFile.SolutionConfigurationsSection.Count.Should().Be(6);
slnFile.SolutionConfigurationsSection
.GetValue("Debug|Any CPU", string.Empty)
.Should().Be("Debug|Any CPU");
slnFile.SolutionConfigurationsSection
.GetValue("Debug|x64", string.Empty)
.Should().Be("Debug|x64");
slnFile.SolutionConfigurationsSection
.GetValue("Debug|x86", string.Empty)
.Should().Be("Debug|x86");
slnFile.SolutionConfigurationsSection
.GetValue("Release|Any CPU", string.Empty)
.Should().Be("Release|Any CPU");
slnFile.SolutionConfigurationsSection
.GetValue("Release|x64", string.Empty)
.Should().Be("Release|x64");
slnFile.SolutionConfigurationsSection
.GetValue("Release|x86", string.Empty)
.Should().Be("Release|x86");
slnFile.ProjectConfigurationsSection.Count.Should().Be(2);
var projectConfigSection = slnFile
.ProjectConfigurationsSection
.GetPropertySet("{7072A694-548F-4CAE-A58F-12D257D5F486}");
projectConfigSection.Count.Should().Be(12);
projectConfigSection
.GetValue("Debug|Any CPU.ActiveCfg", string.Empty)
.Should().Be("Debug|Any CPU");
projectConfigSection
.GetValue("Debug|Any CPU.Build.0", string.Empty)
.Should().Be("Debug|Any CPU");
projectConfigSection
.GetValue("Debug|x64.ActiveCfg", string.Empty)
.Should().Be("Debug|x64");
projectConfigSection
.GetValue("Debug|x64.Build.0", string.Empty)
.Should().Be("Debug|x64");
projectConfigSection
.GetValue("Debug|x86.ActiveCfg", string.Empty)
.Should().Be("Debug|x86");
projectConfigSection
.GetValue("Debug|x86.Build.0", string.Empty)
.Should().Be("Debug|x86");
projectConfigSection
.GetValue("Release|Any CPU.ActiveCfg", string.Empty)
.Should().Be("Release|Any CPU");
projectConfigSection
.GetValue("Release|Any CPU.Build.0", string.Empty)
.Should().Be("Release|Any CPU");
projectConfigSection
.GetValue("Release|x64.ActiveCfg", string.Empty)
.Should().Be("Release|x64");
projectConfigSection
.GetValue("Release|x64.Build.0", string.Empty)
.Should().Be("Release|x64");
projectConfigSection
.GetValue("Release|x86.ActiveCfg", string.Empty)
.Should().Be("Release|x86");
projectConfigSection
.GetValue("Release|x86.Build.0", string.Empty)
.Should().Be("Release|x86");
projectConfigSection = slnFile
.ProjectConfigurationsSection
.GetPropertySet("{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}");
projectConfigSection.Count.Should().Be(12);
projectConfigSection
.GetValue("Debug|Any CPU.ActiveCfg", string.Empty)
.Should().Be("Debug|Any CPU");
projectConfigSection
.GetValue("Debug|Any CPU.Build.0", string.Empty)
.Should().Be("Debug|Any CPU");
projectConfigSection
.GetValue("Debug|x64.ActiveCfg", string.Empty)
.Should().Be("Debug|x64");
projectConfigSection
.GetValue("Debug|x64.Build.0", string.Empty)
.Should().Be("Debug|x64");
projectConfigSection
.GetValue("Debug|x86.ActiveCfg", string.Empty)
.Should().Be("Debug|x86");
projectConfigSection
.GetValue("Debug|x86.Build.0", string.Empty)
.Should().Be("Debug|x86");
projectConfigSection
.GetValue("Release|Any CPU.ActiveCfg", string.Empty)
.Should().Be("Release|Any CPU");
projectConfigSection
.GetValue("Release|Any CPU.Build.0", string.Empty)
.Should().Be("Release|Any CPU");
projectConfigSection
.GetValue("Release|x64.ActiveCfg", string.Empty)
.Should().Be("Release|x64");
projectConfigSection
.GetValue("Release|x64.Build.0", string.Empty)
.Should().Be("Release|x64");
projectConfigSection
.GetValue("Release|x86.ActiveCfg", string.Empty)
.Should().Be("Release|x86");
projectConfigSection
.GetValue("Release|x86.Build.0", string.Empty)
.Should().Be("Release|x86");
slnFile.Sections.Count.Should().Be(3);
var solutionPropertiesSection = slnFile.Sections.GetSection("SolutionProperties");
solutionPropertiesSection.Properties.Count.Should().Be(1);
solutionPropertiesSection.Properties
.GetValue("HideSolutionNode", string.Empty)
.Should().Be("FALSE");
}
[Fact]
public void WhenGivenAValidPathItReadsModifiesThenWritesAnSln()
public void WhenGivenAValidSlnFileItModifiesSavesAndVerifiesContents()
{
var solutionDirectory =
TestAssetsManager.CreateTestInstance("TestAppWithSln", callingMethod: "p").Path;
var tmpFile = Temp.CreateFile();
tmpFile.WriteAllText(SolutionWithAppAndLibProjects);
var solutionFullPath = Path.Combine(solutionDirectory, "TestAppWithSln.sln");
SlnFile slnFile = SlnFile.Read(tmpFile.Path);
var slnFile = SlnFile.Read(solutionFullPath);
slnFile.FullPath.Should().Be(solutionFullPath);
slnFile.FormatVersion = "14.00";
slnFile.ProductDescription = "Visual Studio 16";
slnFile.VisualStudioVersion = "16.0.26006.2";
slnFile.MinimumVisualStudioVersion = "11.0.40219.1";
slnFile.Projects.Count.Should().Be(1);
slnFile.Projects.Count.Should().Be(2);
var project = slnFile.Projects[0];
project.Name.Should().Be("TestAppWithSln");
project.Name = "New Project Name";
project.FilePath.Should().Be("TestAppWithSln.xproj");
project.FilePath = "New File Path";
project.Id = "{9A19103F-16F7-4668-BE54-9A1E7A4F7556}";
project.TypeGuid = "{7072A694-548F-4CAE-A58F-12D257D5F486}";
project.Name = "AppModified";
project.FilePath = Path.Combine("AppModified", "AppModified.csproj");
slnFile.Projects.Remove(slnFile.Projects[1]);
var newSolutionFullPath = Path.Combine(solutionDirectory, "TestAppWithSln_modified.sln");
slnFile.Write(newSolutionFullPath);
slnFile.SolutionConfigurationsSection.Count.Should().Be(6);
slnFile.SolutionConfigurationsSection.Remove("Release|Any CPU");
slnFile.SolutionConfigurationsSection.Remove("Release|x64");
slnFile.SolutionConfigurationsSection.Remove("Release|x86");
slnFile = SlnFile.Read(newSolutionFullPath);
slnFile.FormatVersion.Should().Be("12.00");
slnFile.ProductDescription.Should().Be("Visual Studio 14");
slnFile.VisualStudioVersion.Should().Be("14.0.25420.1");
slnFile.MinimumVisualStudioVersion.Should().Be("10.0.40219.1");
slnFile.BaseDirectory.Should().Be(solutionDirectory);
slnFile.FullPath.Should().Be(newSolutionFullPath);
slnFile.Projects.Count.Should().Be(1);
project = slnFile.Projects[0];
project.Id.Should().Be("{0138CB8F-4AA9-4029-A21E-C07C30F425BA}");
project.TypeGuid.Should().Be("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}");
project.Name.Should().Be("New Project Name");
project.FilePath.Should().Be("New File Path");
slnFile.ProjectConfigurationsSection.Count.Should().Be(2);
var projectConfigSection = slnFile
.ProjectConfigurationsSection
.GetPropertySet("{21D9159F-60E6-4F65-BC6B-D01B71B15FFC}");
slnFile.ProjectConfigurationsSection.Remove(projectConfigSection);
slnFile.Sections.Count.Should().Be(3);
var solutionPropertiesSection = slnFile.Sections.GetSection("SolutionProperties");
solutionPropertiesSection.Properties.Count.Should().Be(1);
solutionPropertiesSection.Properties.SetValue("HideSolutionNode", "TRUE");
slnFile.Write();
File.ReadAllText(tmpFile.Path)
.Should().Be(SolutionModified);
}
[Fact]
public void WhenGivenAnSolutionWithMissingHeaderItThrows()
{
var tmpFile = Temp.CreateFile();
tmpFile.WriteAllText("Invalid Solution");
Action action = () =>
{
SlnFile.Read(tmpFile.Path);
};
action.ShouldThrow<InvalidSolutionFormatException>()
.WithMessage("Invalid format in line 1: File header is missing");
}
}
}