Move away from MSTest to Xunit. (#136)

This commit is contained in:
Steve Molloy 2022-03-15 22:17:24 -07:00 коммит произвёл GitHub
Родитель e21e889f9e
Коммит b0a3cb8f88
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 216 добавлений и 204 удалений

Просмотреть файл

@ -18,7 +18,7 @@
<!-- Composite targets -->
<Target Name="BuildCI" DependsOnTargets="Clean;Build" />
<Target Name="Build" DependsOnTargets="BuildAssemblies;BuildTests;BuildPackages" />
<Target Name="Build" DependsOnTargets="BuildAssemblies;UnitTest;BuildPackages" />
<Target Name="Clean" DependsOnTargets="CleanPackages;CleanTests;CleanAssemblies" />
<Target Name="Rebuild" DependsOnTargets="Clean;Build" />
@ -58,5 +58,8 @@
<Exec Command=".nuget\NuGet.exe restore" />
</Target>
<Import Project="tools\RoslynCodeProvider.targets" />
<Target Name="UnitTest" DependsOnTargets="BuildTests">
<MSBuild Targets="XunitTest" Projects="@(TestProject)" />
</Target>
</Project>

Просмотреть файл

@ -1,36 +1,39 @@
using System;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
[TestClass]
public class CSharpProviderTest {
private CommonCodeDomProviderTests commonTests = new CommonCodeDomProviderTests();
private static CodeDomProvider csharpCodeProvider;
[ClassInitialize]
public static void ClassInitialize(TestContext testContext) {
static CSharpProviderTest() {
#pragma warning disable CS0618
csharpCodeProvider = new CSharpCodeProvider(compilerSettings: CompilerSettingsHelper.CSC);
#pragma warning restore CS0618
AppContext.SetSwitch("Switch.System.DisableTempFileCollectionDirectoryFeature", true);
}
[TestMethod]
[Fact]
public void AssemblyVersion()
{
commonTests.AssemblyVersion(csharpCodeProvider);
}
[Fact]
public void FileExtension() {
commonTests.FileExtension(csharpCodeProvider, "cs");
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_Parse_Error() {
commonTests.CompileAssemblyFromSource_Parse_Error(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_WarningAsError() {
commonTests.CompileAssemblyFromSource_WarningAsError(csharpCodeProvider,
// the variable a is declared but not used
@ -38,83 +41,83 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"CS0168"/*errorNumber*/);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_ReferenceAssembly_AssemblyNameOnly() {
commonTests.CompileAssemblyFromSource_ReferenceAssembly_AssemblyNameOnly(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_ReferenceAssembly_NameCannotBeResolved() {
commonTests.CompileAssemblyFromSource_ReferenceAssembly_NameCannotBeResolved(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_ReferenceAssembly_LocalReference() {
commonTests.CompileAssemblyFromSource_ReferenceAssembly_LocalReference(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_ReferenceAssembly_PathWithComma() {
commonTests.CompileAssemblyFromSource_ReferenceAssembly_PathWithComma(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_GenerateInMemory_True() {
commonTests.CompileAssemblyFromSource_GenerateInMemory_True(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_GenerateInMemory_False() {
commonTests.CompileAssemblyFromSource_GenerateInMemory_False(csharpCodeProvider,
"public class FooClass { public string Execute() { return \"output\";}}");
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_InvalidOutputPath() {
commonTests.CompileAssemblyFromSource_InvalidOutputPath(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_GenerateExecutable_True() {
commonTests.CompileAssemblyFromSource_GenerateExecutable_True(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_GenerateExecutable_True_Failed() {
commonTests.CompileAssemblyFromSource_GenerateExecutable_True_Failed(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_CreateOutputFileFailed() {
commonTests.CompileAssemblyFromSource_CreateOutputFileFailed(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_CreatePDBFileFailed() {
commonTests.CompileAssemblyFromSource_CreatePDBFileFailed(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_IncludeDebugInformation_True() {
commonTests.CompileAssemblyFromSource_IncludeDebugInformation_True(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_IncludeDebugInformation_False() {
commonTests.CompileAssemblyFromSource_IncludeDebugInformation_False(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromDom() {
commonTests.CompileAssemblyFromDom(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromFile() {
commonTests.CompileAssemblyFromFile(csharpCodeProvider);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromFile_ASPNet_Magic()
{
// Complete added frippery is: "/nowarn:1659;1699;1701;612;618"
@ -122,7 +125,7 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
commonTests.CompileAssemblyFromFile_CheckArgs(new CSharpCodeProvider(opts), "/nowarn:1659;1699;1701;612;618", true);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromFile_No_ASPNet_Magic()
{
// _codeProvider uses options (aka CompilerSettingsHelper.VB) created via constructor, so it should

Просмотреть файл

@ -5,17 +5,26 @@ using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
[TestClass]
public class CommonCodeDomProviderTests {
private const int Failed = 1;
private const int Success = 0;
public void AssemblyVersion(CodeDomProvider provider)
{
var ver = provider.GetType().Assembly.GetName().Version;
Assert.Equal(3, ver.Major);
Assert.Equal(11, ver.Minor);
}
public void FileExtension(CodeDomProvider provider, string extension) {
Assert.AreEqual(extension, provider.FileExtension);
Assert.Equal(extension, provider.FileExtension);
}
@ -26,10 +35,10 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public string Execute() { return \"output\" /*;*/ }}"
);
Assert.AreEqual(Failed, result.NativeCompilerReturnValue);
Assert.IsTrue(result.Errors.HasErrors);
Assert.AreEqual(1, result.Errors.Count);
Assert.AreEqual("CS1002", result.Errors[0].ErrorNumber);
Assert.Equal(Failed, result.NativeCompilerReturnValue);
Assert.True(result.Errors.HasErrors);
Assert.Single(result.Errors);
Assert.Equal("CS1002", result.Errors[0].ErrorNumber);
}
@ -44,9 +53,9 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
sourceCode
);
Assert.AreEqual(Failed, result.NativeCompilerReturnValue);
Assert.IsTrue(result.Errors.HasErrors);
Assert.AreEqual(errorNumber, result.Errors[0].ErrorNumber);
Assert.Equal(Failed, result.NativeCompilerReturnValue);
Assert.True(result.Errors.HasErrors);
Assert.Equal(errorNumber, result.Errors[0].ErrorNumber);
}
@ -60,12 +69,12 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public string Execute() { return \"output\"; }}"
);
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
Assert.Equal(Success, result.NativeCompilerReturnValue);
var type = result.CompiledAssembly.GetType("FooClass");
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Assert.IsNull(result.PathToAssembly);
Assert.AreEqual(@"output", output);
Assert.Null(result.PathToAssembly);
Assert.Equal(@"output", output);
}
@ -81,8 +90,8 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public string Execute() { int a; return \"output\"; }}"
);
// Assert.IsNull(result.PathToAssembly);
Assert.AreEqual(Failed, result.NativeCompilerReturnValue);
// Assert.Null(result.PathToAssembly);
Assert.Equal(Failed, result.NativeCompilerReturnValue);
bool referenceErrorInOutput = false;
foreach (var line in result.Output) {
@ -91,7 +100,7 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
}
}
Assert.IsTrue(referenceErrorInOutput);
Assert.True(referenceErrorInOutput);
}
@ -105,10 +114,10 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass1 { public static string Execute() { return \"output\";}}"
);
Assert.AreEqual(Success, result1.NativeCompilerReturnValue);
Assert.IsNotNull(result1.PathToAssembly);
Assert.Equal(Success, result1.NativeCompilerReturnValue);
Assert.NotNull(result1.PathToAssembly);
tempFiles.Add(result1.PathToAssembly);
Assert.AreEqual(".dll", Path.GetExtension(result1.PathToAssembly));
Assert.Equal(".dll", Path.GetExtension(result1.PathToAssembly));
string referenceName = Path.GetFileName(result1.PathToAssembly);
var asm1 = GetAssemblyByName(result1.PathToAssembly);
@ -116,7 +125,7 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
var obj1 = Activator.CreateInstance(type1);
var output1 = type1.GetMethod("Execute").Invoke(obj1, new object[] { });
Assert.AreEqual(@"output", output1);
Assert.Equal(@"output", output1);
var param2 = new CompilerParameters(new string[] { referenceName });
param2.GenerateExecutable = true;
@ -124,9 +133,9 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
param2,
"public class FooClass2 { public static void Main() { System.Console.Write(FooClass1.Execute());}}"
);
Assert.IsNotNull(result2.PathToAssembly);
Assert.NotNull(result2.PathToAssembly);
tempFiles.Add(result2.PathToAssembly);
Assert.AreEqual(Success, result2.NativeCompilerReturnValue);
Assert.Equal(Success, result2.NativeCompilerReturnValue);
AppDomain newAppDomain = null;
try {
newAppDomain = System.AppDomain.CreateDomain("NewApplicationDomain");
@ -157,17 +166,17 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass1 { public static string Execute() { return \"output\";}}"
);
Assert.AreEqual(Success, result1.NativeCompilerReturnValue);
Assert.IsNotNull(result1.PathToAssembly);
Assert.Equal(Success, result1.NativeCompilerReturnValue);
Assert.NotNull(result1.PathToAssembly);
tempFiles.Add(result1.PathToAssembly);
Assert.AreEqual(".dll", Path.GetExtension(result1.PathToAssembly));
Assert.Equal(".dll", Path.GetExtension(result1.PathToAssembly));
string referenceName = Path.GetFileName(result1.PathToAssembly);
var asm1 = GetAssemblyByName(result1.PathToAssembly);
var type1 = asm1.GetType("FooClass1");
var obj1 = Activator.CreateInstance(type1);
var output1 = type1.GetMethod("Execute").Invoke(obj1, new object[] { });
Assert.AreEqual(@"output", output1);
Assert.Equal(@"output", output1);
var param2 = new CompilerParameters(new string[] { referenceName });
param2.GenerateExecutable = true;
@ -176,8 +185,8 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass2 { public static void Main() { System.Console.Write(FooClass1.Execute());}}"
);
Assert.AreEqual(Success, result2.NativeCompilerReturnValue);
Assert.IsNotNull(result2.PathToAssembly);
Assert.Equal(Success, result2.NativeCompilerReturnValue);
Assert.NotNull(result2.PathToAssembly);
tempFiles.Add(result2.PathToAssembly);
AppDomain newAppDomain = null;
try {
@ -204,12 +213,12 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"using System.Runtime; public class FooClass { public string Execute() { return \"output\";}}"
);
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
Assert.Equal(Success, result.NativeCompilerReturnValue);
var type = result.CompiledAssembly.GetType("FooClass");
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Assert.IsNull(result.PathToAssembly);
Assert.AreEqual(@"output", output);
Assert.Null(result.PathToAssembly);
Assert.Equal(@"output", output);
}
public void CompileAssemblyFromSource_GenerateInMemory_False(CodeDomProvider provider, string sourceCode) {
@ -224,8 +233,8 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
sourceCode
);
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
Assert.IsNotNull(result.PathToAssembly);
Assert.Equal(Success, result.NativeCompilerReturnValue);
Assert.NotNull(result.PathToAssembly);
// Read assembly into memory:
Assembly asm = GetAssemblyByName(result.PathToAssembly);
@ -234,10 +243,10 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Assert.AreEqual(@"output", output);
Assert.AreEqual(param.OutputAssembly, result.PathToAssembly);
Assert.Equal(@"output", output);
Assert.Equal(param.OutputAssembly, result.PathToAssembly);
Assert.IsTrue(File.Exists(param.OutputAssembly));
Assert.True(File.Exists(param.OutputAssembly));
}
finally {
DeleteFiles(tempFiles);
@ -257,8 +266,8 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public string Execute() { return \"output\";}}"
);
// Assert.IsNull(result.PathToAssembly);
Assert.AreEqual(Failed, result.NativeCompilerReturnValue);
// Assert.Null(result.PathToAssembly);
Assert.Equal(Failed, result.NativeCompilerReturnValue);
}
finally {
DeleteFiles(tempFiles);
@ -280,8 +289,8 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public static void Main(){} public string Execute() { return \"output\";}}"
);
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
Assert.IsNotNull(result.PathToAssembly);
Assert.Equal(Success, result.NativeCompilerReturnValue);
Assert.NotNull(result.PathToAssembly);
tempFiles.Add(result.PathToAssembly);
Assembly asm = GetAssemblyByName(result.PathToAssembly);
@ -289,8 +298,8 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Assert.AreEqual(".exe", Path.GetExtension(result.PathToAssembly));
Assert.AreEqual(@"output", output);
Assert.Equal(".exe", Path.GetExtension(result.PathToAssembly));
Assert.Equal(@"output", output);
}
finally {
DeleteFiles(tempFiles);
@ -312,9 +321,9 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass {public string Execute() { return \"output\";}}"
);
Assert.AreEqual(Failed, result.NativeCompilerReturnValue);
// Assert.IsNull(result.PathToAssembly);
Assert.AreEqual("CS5001"/*miss main entry*/, result.Errors[0].ErrorNumber);
Assert.Equal(Failed, result.NativeCompilerReturnValue);
// Assert.Null(result.PathToAssembly);
Assert.Equal("CS5001"/*miss main entry*/, result.Errors[0].ErrorNumber);
}
@ -331,10 +340,10 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public string Execute() { return \"output\";}}"
);
Assert.AreEqual(Failed, result.NativeCompilerReturnValue);
Assert.Equal(Failed, result.NativeCompilerReturnValue);
// The InProc provider does not give error while the old provider
// does. We probably should fix the behavior of InProc provider.
// Assert.IsFalse(result.Errors.HasErrors);
// Assert.False(result.Errors.HasErrors);
bool filenameInOutput = false;
foreach (var line in result.Output) {
if (line.Contains(Path.GetFileName(param.OutputAssembly))) {
@ -342,7 +351,7 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
}
}
Assert.IsTrue(filenameInOutput);
Assert.True(filenameInOutput);
}
}
finally {
@ -367,10 +376,10 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public string Execute() { return \"output\";}}"
);
Assert.AreEqual(Failed, result.NativeCompilerReturnValue);
Assert.Equal(Failed, result.NativeCompilerReturnValue);
// The InProc provider does not give error while the old provider
// does. We probably should fix the behavior of InProc provider.
// Assert.IsFalse(result.Errors.HasErrors);
// Assert.False(result.Errors.HasErrors);
bool filenameInOutput = false;
foreach (var line in result.Output) {
if (line.Contains(Path.GetFileName(pdbFilename))) {
@ -378,7 +387,7 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
}
}
Assert.IsTrue(filenameInOutput);
Assert.True(filenameInOutput);
}
}
finally {
@ -402,11 +411,11 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public string Execute() { return \"output\";}}"
);
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
Assert.Equal(Success, result.NativeCompilerReturnValue);
// In Debug mode, visual studio would try to load the pdb file.
// Delete the file before it's held by VS.
Assert.IsTrue(File.Exists(pdbFileName));
Assert.True(File.Exists(pdbFileName));
File.Delete(pdbFileName);
// Read assembly into memory:
@ -415,9 +424,9 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Assert.AreEqual(@"output", output);
Assert.AreEqual(param.OutputAssembly, result.PathToAssembly);
Assert.IsTrue(File.Exists(param.OutputAssembly));
Assert.Equal(@"output", output);
Assert.Equal(param.OutputAssembly, result.PathToAssembly);
Assert.True(File.Exists(param.OutputAssembly));
}
finally {
DeleteFiles(tempFiles);
@ -440,7 +449,7 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
"public class FooClass { public string Execute() { return \"output\";}}"
);
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
Assert.Equal(Success, result.NativeCompilerReturnValue);
// Read assembly into memory:
Assembly asm = GetAssemblyByName(param.OutputAssembly);
@ -448,10 +457,10 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Assert.AreEqual(@"output", output);
Assert.AreEqual(param.OutputAssembly, result.PathToAssembly);
Assert.IsTrue(File.Exists(param.OutputAssembly));
Assert.IsFalse(File.Exists(pdbFileName));
Assert.Equal(@"output", output);
Assert.Equal(param.OutputAssembly, result.PathToAssembly);
Assert.True(File.Exists(param.OutputAssembly));
Assert.False(File.Exists(pdbFileName));
}
finally {
DeleteFiles(tempFiles);
@ -492,11 +501,11 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
compileUnit
);
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
Assert.Equal(Success, result.NativeCompilerReturnValue);
var type = result.CompiledAssembly.GetType(string.Format("{0}.{1}", spaceName, className));
var obj = Activator.CreateInstance(type);
var output = type.GetMethod(methodName).Invoke(obj, new object[] { });
Assert.AreEqual("output", output);
Assert.Equal("output", output);
}
@ -526,16 +535,16 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
if (argStringToFind != null)
{
Assert.AreNotEqual(Success, result.NativeCompilerReturnValue);
Assert.AreEqual<bool>(expected, result.Output[0].Contains(argStringToFind));
Assert.NotEqual(Success, result.NativeCompilerReturnValue);
Assert.Equal(expected, result.Output[0].Contains(argStringToFind));
return;
}
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
Assert.Equal(Success, result.NativeCompilerReturnValue);
var type = result.CompiledAssembly.GetType("FooClass");
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Assert.AreEqual(@"output", output);
Assert.Equal(@"output", output);
}
finally {
File.Delete(sourcePath);

Просмотреть файл

@ -28,6 +28,8 @@
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -54,11 +56,7 @@
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="CommonCodeDomProviderTests.cs" />
@ -77,6 +75,21 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="xunit">
<Version>2.4.1</Version>
</PackageReference>
<PackageReference Include="xunit.runner.msbuild">
<Version>2.4.1</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio">
<Version>2.4.3</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>

Просмотреть файл

@ -1,117 +1,105 @@
using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;
using Xunit;
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
[TestClass]
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest
{
public class ProviderOptionsTests {
private const int Failed = 1;
private const int Success = 0;
private static bool IsDev = false;
[ClassInitialize]
public static void ClassInitialize(TestContext context) {
static ProviderOptionsTests() {
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("DEV_ENVIRONMENT")) ||
!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("IN_DEBUG_MODE")) ||
CompilationUtil.IsDebuggerAttached)
IsDev = true;
}
[TestMethod]
[Fact]
public void DefaultSettings()
{
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakevb");
Assert.IsNotNull(opts);
Assert.AreEqual<string>(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"roslyn"), opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
Assert.AreEqual<int>(IsDev ? 15 * 60 : 10, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
Assert.IsTrue(opts.UseAspNetSettings); // Default is false... except through the GetProviderOptionsFor factory method we used here.
Assert.IsFalse(opts.WarnAsError);
Assert.IsNull(opts.CompilerVersion);
Assert.AreEqual<int>(2, opts.AllOptions.Count);
Assert.AreEqual<string>("foo2", opts.AllOptions["CustomSetting"]);
Assert.AreEqual<string>("bar2", opts.AllOptions["AnotherCoolSetting"]);
Assert.NotNull(opts);
Assert.Equal(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"roslyn"), opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
Assert.Equal(IsDev ? 15 * 60 : 10, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
Assert.True(opts.UseAspNetSettings); // Default is false... except through the GetProviderOptionsFor factory method we used here.
Assert.False(opts.WarnAsError);
Assert.Null(opts.CompilerVersion);
Assert.Equal(2, opts.AllOptions.Count);
Assert.Equal("foo2", opts.AllOptions["CustomSetting"]);
Assert.Equal("bar2", opts.AllOptions["AnotherCoolSetting"]);
}
[TestMethod]
[Fact]
public void FromShortConstructor()
{
IProviderOptions opts = new ProviderOptions(@"D:\My\Fun\Compiler\Path\compiles.exe", 123);
Assert.IsNotNull(opts);
Assert.AreEqual<string>(@"D:\My\Fun\Compiler\Path\compiles.exe", opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
Assert.AreEqual<int>(123, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
Assert.IsFalse(opts.UseAspNetSettings); // Default via constructor is false.
Assert.IsFalse(opts.WarnAsError);
Assert.IsNull(opts.CompilerVersion);
Assert.AreEqual<int>(0, opts.AllOptions.Count);
Assert.NotNull(opts);
Assert.Equal(@"D:\My\Fun\Compiler\Path\compiles.exe", opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
Assert.Equal(123, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
Assert.False(opts.UseAspNetSettings); // Default via constructor is false.
Assert.False(opts.WarnAsError);
Assert.Null(opts.CompilerVersion);
Assert.Equal(0, opts.AllOptions.Count);
}
[TestMethod]
[Fact]
public void FromICompilerSettings()
{
#pragma warning disable CS0618
IProviderOptions opts = new ProviderOptions((ICompilerSettings)(CompilerSettingsHelper.CSC));
#pragma warning restore CS0618
Assert.IsNotNull(opts);
Assert.AreEqual<string>(CompilerSettingsHelper.CSC.CompilerFullPath, opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
Assert.AreEqual<int>(CompilerSettingsHelper.CSC.CompilerServerTimeToLive, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
Assert.IsFalse(opts.UseAspNetSettings); // Default via constructor is false.
Assert.IsFalse(opts.WarnAsError);
Assert.IsNull(opts.CompilerVersion);
Assert.AreEqual<int>(0, opts.AllOptions.Count);
Assert.NotNull(opts);
Assert.Equal(CompilerSettingsHelper.CSC.CompilerFullPath, opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
Assert.Equal(CompilerSettingsHelper.CSC.CompilerServerTimeToLive, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
Assert.False(opts.UseAspNetSettings); // Default via constructor is false.
Assert.False(opts.WarnAsError);
Assert.Null(opts.CompilerVersion);
Assert.Equal(0, opts.AllOptions.Count);
}
// <providerOptions> override defaults
[TestMethod]
[Fact]
public void FromProviderOptions()
{
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakecs");
Assert.IsNotNull(opts);
Assert.AreEqual<string>(@"C:\Path\To\Nowhere\csc.exe", opts.CompilerFullPath);
Assert.AreEqual<int>(42, opts.CompilerServerTimeToLive);
Assert.IsFalse(opts.UseAspNetSettings);
Assert.IsTrue(opts.WarnAsError);
Assert.AreEqual<string>("v6.0", opts.CompilerVersion);
Assert.AreEqual<int>(7, opts.AllOptions.Count);
Assert.AreEqual<string>("foo", opts.AllOptions["CustomSetting"]);
Assert.AreEqual<string>("bar", opts.AllOptions["AnotherCoolSetting"]);
Assert.NotNull(opts);
Assert.Equal(@"C:\Path\To\Nowhere\csc.exe", opts.CompilerFullPath);
Assert.Equal(42, opts.CompilerServerTimeToLive);
Assert.False(opts.UseAspNetSettings);
Assert.True(opts.WarnAsError);
Assert.Equal("v6.0", opts.CompilerVersion);
Assert.Equal(7, opts.AllOptions.Count);
Assert.Equal("foo", opts.AllOptions["CustomSetting"]);
Assert.Equal("bar", opts.AllOptions["AnotherCoolSetting"]);
}
// <appSettings> override <providerOptions> for location only
// Actually, we can't do this because A) AppSettings can be added but not cleaned up after this test, and
// B) the setting has probably already been read and cached by the AppSettings utility class, so updating
// the value here wouldn't have any affect anyway.
//[TestMethod]
[Fact(Skip = "Need to fake config system first")]
public void FromAppSettings()
{
ConfigurationManager.AppSettings.Set("aspnet:RoslynCompilerLocation", @"C:\Location\for\all\from\appSettings\compiler.exe");
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakecs");
ConfigurationManager.AppSettings.Remove("aspnet:RoslynCompilerLocation");
Assert.IsNotNull(opts);
Assert.AreEqual<string>(@"C:\Location\for\all\from\appSettings\compiler.exe", opts.CompilerFullPath);
Assert.AreEqual<int>(42, opts.CompilerServerTimeToLive);
Assert.IsFalse(opts.UseAspNetSettings);
Assert.IsTrue(opts.WarnAsError);
Assert.AreEqual<string>("v6.0", opts.CompilerVersion);
Assert.AreEqual<int>(7, opts.AllOptions.Count);
Assert.AreEqual<string>("foo", opts.AllOptions["CustomSetting"]);
Assert.AreEqual<string>("bar", opts.AllOptions["AnotherCoolSetting"]);
Assert.NotNull(opts);
Assert.Equal(@"C:\Location\for\all\from\appSettings\compiler.exe", opts.CompilerFullPath);
Assert.Equal(42, opts.CompilerServerTimeToLive);
Assert.False(opts.UseAspNetSettings);
Assert.True(opts.WarnAsError);
Assert.Equal("v6.0", opts.CompilerVersion);
Assert.Equal(7, opts.AllOptions.Count);
Assert.Equal("foo", opts.AllOptions["CustomSetting"]);
Assert.Equal("bar", opts.AllOptions["AnotherCoolSetting"]);
}
// Environment overrides all for location and TTL
[TestMethod]
[Fact]
public void FromEnvironment()
{
// See note on the 'FromAppSettings' test.
@ -123,34 +111,34 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
Environment.SetEnvironmentVariable("VBCSCOMPILER_TTL", null);
//ConfigurationManager.AppSettings.Remove("aspnet:RoslynCompilerLocation");
Assert.IsNotNull(opts);
Assert.AreEqual<string>(@"C:\My\Compiler\Location\vbcsc.exe", opts.CompilerFullPath);
Assert.AreEqual<int>(98, opts.CompilerServerTimeToLive);
Assert.IsFalse(opts.UseAspNetSettings);
Assert.IsTrue(opts.WarnAsError);
Assert.AreEqual<string>("v6.0", opts.CompilerVersion);
Assert.AreEqual<int>(7, opts.AllOptions.Count);
Assert.AreEqual<string>("foo", opts.AllOptions["CustomSetting"]);
Assert.AreEqual<string>("bar", opts.AllOptions["AnotherCoolSetting"]);
Assert.NotNull(opts);
Assert.Equal(@"C:\My\Compiler\Location\vbcsc.exe", opts.CompilerFullPath);
Assert.Equal(98, opts.CompilerServerTimeToLive);
Assert.False(opts.UseAspNetSettings);
Assert.True(opts.WarnAsError);
Assert.Equal("v6.0", opts.CompilerVersion);
Assert.Equal(7, opts.AllOptions.Count);
Assert.Equal("foo", opts.AllOptions["CustomSetting"]);
Assert.Equal("bar", opts.AllOptions["AnotherCoolSetting"]);
}
// TTL must be int
[TestMethod]
[Fact]
public void TTL_MustBeInteger()
{
Environment.SetEnvironmentVariable("VBCSCOMPILER_TTL", "NotANumber");
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakevb");
Environment.SetEnvironmentVariable("VBCSCOMPILER_TTL", null);
Assert.IsNotNull(opts);
Assert.AreEqual<string>(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"roslyn"), opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
Assert.AreEqual<int>(IsDev ? 15 * 60 : 10, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
Assert.IsTrue(opts.UseAspNetSettings); // Default is false... except through the GetProviderOptionsFor factory method we used here.
Assert.IsFalse(opts.WarnAsError);
Assert.IsNull(opts.CompilerVersion);
Assert.AreEqual<int>(2, opts.AllOptions.Count);
Assert.AreEqual<string>("foo2", opts.AllOptions["CustomSetting"]);
Assert.AreEqual<string>("bar2", opts.AllOptions["AnotherCoolSetting"]);
Assert.NotNull(opts);
Assert.Equal(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"roslyn"), opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
Assert.Equal(IsDev ? 15 * 60 : 10, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
Assert.True(opts.UseAspNetSettings); // Default is false... except through the GetProviderOptionsFor factory method we used here.
Assert.False(opts.WarnAsError);
Assert.Null(opts.CompilerVersion);
Assert.Equal(2, opts.AllOptions.Count);
Assert.Equal("foo2", opts.AllOptions["CustomSetting"]);
Assert.Equal("bar2", opts.AllOptions["AnotherCoolSetting"]);
}
}
}

Просмотреть файл

@ -1,18 +1,9 @@
using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.CodeDom.Compiler;
using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;
using Xunit;
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
[TestClass]
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest
{
public class VBCodeProviderTests {
private const int Failed = 1;
@ -23,18 +14,23 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
private CodeDomProvider _codeProvider = new VBCodeProvider(CompilerSettingsHelper.VB);
#pragma warning restore CS0618
[ClassInitialize]
public static void ClassInitialize(TestContext context) {
static VBCodeProviderTests() {
//VBCompiler.MySupport = " "; // Don't need to do this anymore with UseAspNetSettings feature
VBCompiler.VBImportsString = " ";
}
[TestMethod]
[Fact]
public void AssemblyVersion()
{
commonTests.AssemblyVersion(_codeProvider);
}
[Fact]
public void FileExtension() {
commonTests.FileExtension(_codeProvider, "vb");
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_DLL_GenerateInMemory_False() {
commonTests.CompileAssemblyFromSource_GenerateInMemory_False(_codeProvider,
@"Public Class FooClass
@ -44,7 +40,7 @@ namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
End Class");
}
[TestMethod]
[Fact]
public void CompileAssemblyFromSource_WarningAsError() {
commonTests.CompileAssemblyFromSource_WarningAsError(_codeProvider,
// the variable a is declared but not used
@ -57,7 +53,7 @@ End Class",
"BC42024");
}
[TestMethod]
[Fact]
public void CompileAssemblyFromFile_ASPNet_Magic()
{
// Complete added frippery is: "/nowarn:41008,40000,40008 /define:_MYTYPE=\\\"Web\\\" /optionInfer+"
@ -66,7 +62,7 @@ End Class",
commonTests.CompileAssemblyFromFile_CheckArgs(new VBCodeProvider(opts), "/define:_MYTYPE=\\\"Web\\\"", true);
}
[TestMethod]
[Fact]
public void CompileAssemblyFromFile_No_ASPNet_Magic()
{
// _codeProvider uses options (aka CompilerSettingsHelper.VB) created via constructor, so it should