Skip to content

Commit 11079a1

Browse files
committed
Rudimentary tests for UseAspNetSettings feature.
1 parent b1a1960 commit 11079a1

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

RoslynCodeProviderTest/CSharpProviderTest.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ public class CSharpProviderTest {
1414

1515
[ClassInitialize]
1616
public static void ClassInitialize(TestContext testContext) {
17-
string frameworkFolder = Path.GetDirectoryName(typeof(object).Assembly.Location);
18-
string compilerPath = Path.Combine(frameworkFolder, "csc.exe");
19-
var codeDomProviderType = typeof(Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider);
2017
#pragma warning disable CS0618
2118
csharpCodeProvider = new CSharpCodeProvider(compilerSettings: CompilerSettingsHelper.CSC);
2219
#pragma warning restore CS0618
@@ -116,5 +113,22 @@ public void CompileAssemblyFromDom() {
116113
public void CompileAssemblyFromFile() {
117114
commonTests.CompileAssemblyFromFile(csharpCodeProvider);
118115
}
116+
117+
[TestMethod]
118+
public void CompileAssemblyFromFile_ASPNet_Magic()
119+
{
120+
// Complete added frippery is: "/nowarn:1659;1699;1701;612;618"
121+
ProviderOptions opts = new ProviderOptions(CompilerSettingsHelper.CSC) { UseAspNetSettings = true };
122+
commonTests.CompileAssemblyFromFile_CheckArgs(new CSharpCodeProvider(opts), "/nowarn:1659;1699;1701;612;618", true);
123+
}
124+
125+
[TestMethod]
126+
public void CompileAssemblyFromFile_No_ASPNet_Magic()
127+
{
128+
// _codeProvider uses options (aka CompilerSettingsHelper.VB) created via constructor, so it should
129+
// have the ASP.Net frippery disabled.
130+
commonTests.CompileAssemblyFromFile_CheckArgs(csharpCodeProvider, "/nowarn:1659;1699;1701;612;618", false);
131+
}
132+
119133
}
120134
}

RoslynCodeProviderTest/CommonCodeDomProviderTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,19 @@ public void CompileAssemblyFromDom(CodeDomProvider provider) {
500500
}
501501

502502

503-
public void CompileAssemblyFromFile(CodeDomProvider provider) {
503+
public void CompileAssemblyFromFile(CodeDomProvider provider)
504+
{
505+
CompileAssemblyFromFile_CheckArgs(provider, null, false);
506+
}
507+
508+
public void CompileAssemblyFromFile_CheckArgs(CodeDomProvider provider, string argStringToFind, bool expected) {
504509
var sourcePath = Path.Combine(Path.GetTempPath(), "foobarSourcefile.cs");
505510
try {
506511
using (var sourceStream = File.Create(sourcePath)) {
507512
var content = "public class FooClass { public string Execute() { return \"output\";}}";
513+
// If we're checking cmd args, we actually want to fail compilation so we can examine output.
514+
if (argStringToFind != null)
515+
content = "nonsense that doesn't compile.";
508516
var bytes = Encoding.ASCII.GetBytes(content);
509517
sourceStream.Write(bytes, 0, bytes.Length);
510518
}
@@ -516,6 +524,13 @@ public void CompileAssemblyFromFile(CodeDomProvider provider) {
516524
sourcePath
517525
);
518526

527+
if (argStringToFind != null)
528+
{
529+
Assert.AreNotEqual(Success, result.NativeCompilerReturnValue);
530+
Assert.AreEqual<bool>(expected, result.Output[0].Contains(argStringToFind));
531+
return;
532+
}
533+
519534
Assert.AreEqual(Success, result.NativeCompilerReturnValue);
520535
var type = result.CompiledAssembly.GetType("FooClass");
521536
var obj = Activator.CreateInstance(type);

RoslynCodeProviderTest/ProviderOptionsTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public void FromShortConstructor()
6060
[TestMethod]
6161
public void FromICompilerSettings()
6262
{
63-
IProviderOptions opts = new ProviderOptions(CompilerSettingsHelper.CSC);
63+
#pragma warning disable CS0618
64+
IProviderOptions opts = new ProviderOptions((ICompilerSettings)(CompilerSettingsHelper.CSC));
65+
#pragma warning restore CS0618
6466
Assert.IsNotNull(opts);
6567
Assert.AreEqual<string>(CompilerSettingsHelper.CSC.CompilerFullPath, opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
6668
Assert.AreEqual<int>(CompilerSettingsHelper.CSC.CompilerServerTimeToLive, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.

RoslynCodeProviderTest/VBCodeProviderTests.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class VBCodeProviderTests {
2525

2626
[ClassInitialize]
2727
public static void ClassInitialize(TestContext context) {
28-
VBCompiler.MySupport = " ";
28+
//VBCompiler.MySupport = " "; // Don't need to do this anymore with UseAspNetSettings feature
2929
VBCompiler.VBImportsString = " ";
3030
}
3131

@@ -44,7 +44,7 @@ End Function
4444
End Class");
4545
}
4646

47-
[TestMethod]
47+
[TestMethod]
4848
public void CompileAssemblyFromSource_WarningAsError() {
4949
commonTests.CompileAssemblyFromSource_WarningAsError(_codeProvider,
5050
// the variable a is declared but not used
@@ -56,6 +56,23 @@ End Function
5656
End Class",
5757
"BC42024");
5858
}
59+
60+
[TestMethod]
61+
public void CompileAssemblyFromFile_ASPNet_Magic()
62+
{
63+
// Complete added frippery is: "/nowarn:41008,40000,40008 /define:_MYTYPE=\\\"Web\\\" /optionInfer+"
64+
// But let's just check for _MYTYPE.
65+
ProviderOptions opts = new ProviderOptions(CompilerSettingsHelper.VB) { UseAspNetSettings = true };
66+
commonTests.CompileAssemblyFromFile_CheckArgs(new VBCodeProvider(opts), "/define:_MYTYPE=\\\"Web\\\"", true);
67+
}
68+
69+
[TestMethod]
70+
public void CompileAssemblyFromFile_No_ASPNet_Magic()
71+
{
72+
// _codeProvider uses options (aka CompilerSettingsHelper.VB) created via constructor, so it should
73+
// have the ASP.Net frippery disabled.
74+
commonTests.CompileAssemblyFromFile_CheckArgs(_codeProvider, "/define:_MYTYPE=\"Web\"", false);
75+
}
5976
}
6077

6178
}

0 commit comments

Comments
 (0)