Skip to content

Commit 2153c2e

Browse files
committed
Adding rudimentary tests for ProviderOptions with a couple fixes that bubbled up along the way.
1 parent b90fef4 commit 2153c2e

4 files changed

Lines changed: 161 additions & 3 deletions

File tree

RoslynCodeProviderTest/App.config

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<system.codedom>
4+
<compilers>
5+
<compiler language="c#;cs;csharp" extension=".fakecs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701">
6+
<providerOption name="CompilerVersion" value="v6.0"/>
7+
<providerOption name="CompilerLocation" value="C:\Path\To\Nowhere\csc.exe"/>
8+
<providerOption name="CompilerServerTTL" value="42"/>
9+
<providerOption name="WarnAsError" value="true"/>
10+
<providerOption name="UseAspNetSettings" value="false"/>
11+
<providerOption name="CustomSetting" value="foo"/>
12+
<providerOption name="AnotherCoolSetting" value="bar"/>
13+
</compiler>
14+
<compiler language="vb;vbs;visualbasic;vbscript" extension=".fakevb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+">
15+
<providerOption name="CustomSetting" value="foo2"/>
16+
<providerOption name="AnotherCoolSetting" value="bar2"/>
17+
</compiler>
18+
</compilers>
19+
</system.codedom>
20+
</configuration>

RoslynCodeProviderTest/Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</PropertyGroup>
4747
<ItemGroup>
4848
<Reference Include="System" />
49+
<Reference Include="System.Configuration" />
4950
</ItemGroup>
5051
<Choose>
5152
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
@@ -64,6 +65,7 @@
6465
<Compile Include="CSharpProviderTest.cs" />
6566
<Compile Include="CompilerSettingsHelper.cs" />
6667
<Compile Include="Properties\AssemblyInfo.cs" />
68+
<Compile Include="ProviderOptionsTests.cs" />
6769
<Compile Include="VBCodeProviderTests.cs" />
6870
</ItemGroup>
6971
<ItemGroup>
@@ -72,6 +74,9 @@
7274
<Name>Microsoft.CodeDom.Providers.DotNetCompilerPlatform</Name>
7375
</ProjectReference>
7476
</ItemGroup>
77+
<ItemGroup>
78+
<None Include="App.config" />
79+
</ItemGroup>
7580
<Choose>
7681
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
7782
<ItemGroup>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.CodeDom.Compiler;
5+
using System.Collections.Generic;
6+
using System.Configuration;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Reflection;
10+
using System.Text;
11+
using System.Threading.Tasks;
12+
13+
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatformTest {
14+
15+
16+
[TestClass]
17+
public class ProviderOptionsTests {
18+
19+
private const int Failed = 1;
20+
private const int Success = 0;
21+
22+
private static bool IsDev = false;
23+
24+
[ClassInitialize]
25+
public static void ClassInitialize(TestContext context) {
26+
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("DEV_ENVIRONMENT")) ||
27+
!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("IN_DEBUG_MODE")) ||
28+
CompilationUtil.IsDebuggerAttached)
29+
IsDev = true;
30+
}
31+
32+
[TestMethod]
33+
public void DefaultSettings()
34+
{
35+
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakevb");
36+
Assert.IsNotNull(opts);
37+
Assert.AreEqual<string>(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\roslyn"), opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
38+
Assert.AreEqual<int>(IsDev ? 15 * 60 : 10, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
39+
Assert.IsTrue(opts.UseAspNetSettings); // Default is false... except through the GetProviderOptionsFor factory method we used here.
40+
Assert.IsFalse(opts.WarnAsError);
41+
Assert.IsNull(opts.CompilerVersion);
42+
Assert.AreEqual<int>(2, opts.AllOptions.Count);
43+
Assert.AreEqual<string>("foo2", opts.AllOptions["CustomSetting"]);
44+
Assert.AreEqual<string>("bar2", opts.AllOptions["AnotherCoolSetting"]);
45+
}
46+
47+
// <providerOptions> override defaults
48+
[TestMethod]
49+
public void FromProviderOptions()
50+
{
51+
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakecs");
52+
Assert.IsNotNull(opts);
53+
Assert.AreEqual<string>(@"C:\Path\To\Nowhere\csc.exe", opts.CompilerFullPath);
54+
Assert.AreEqual<int>(42, opts.CompilerServerTimeToLive);
55+
Assert.IsFalse(opts.UseAspNetSettings);
56+
Assert.IsTrue(opts.WarnAsError);
57+
Assert.AreEqual<string>("v6.0", opts.CompilerVersion);
58+
Assert.AreEqual<int>(7, opts.AllOptions.Count);
59+
Assert.AreEqual<string>("foo", opts.AllOptions["CustomSetting"]);
60+
Assert.AreEqual<string>("bar", opts.AllOptions["AnotherCoolSetting"]);
61+
}
62+
63+
// <appSettings> override <providerOptions> for location only
64+
// Actually, we can't do this because A) AppSettings can be added but not cleaned up after this test, and
65+
// B) the setting has probably already been read and cached by the AppSettings utility class, so updating
66+
// the value here wouldn't have any affect anyway.
67+
//[TestMethod]
68+
public void FromAppSettings()
69+
{
70+
ConfigurationManager.AppSettings.Set("aspnet:RoslynCompilerLocation", @"C:\Location\for\all\from\appSettings\compiler.exe");
71+
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakecs");
72+
ConfigurationManager.AppSettings.Remove("aspnet:RoslynCompilerLocation");
73+
74+
Assert.IsNotNull(opts);
75+
Assert.AreEqual<string>(@"C:\Location\for\all\from\appSettings\compiler.exe", opts.CompilerFullPath);
76+
Assert.AreEqual<int>(42, opts.CompilerServerTimeToLive);
77+
Assert.IsFalse(opts.UseAspNetSettings);
78+
Assert.IsTrue(opts.WarnAsError);
79+
Assert.AreEqual<string>("v6.0", opts.CompilerVersion);
80+
Assert.AreEqual<int>(7, opts.AllOptions.Count);
81+
Assert.AreEqual<string>("foo", opts.AllOptions["CustomSetting"]);
82+
Assert.AreEqual<string>("bar", opts.AllOptions["AnotherCoolSetting"]);
83+
}
84+
85+
// Environment overrides all for location and TTL
86+
[TestMethod]
87+
public void FromEnvironment()
88+
{
89+
// See note on the 'FromAppSettings' test.
90+
//ConfigurationManager.AppSettings.Set("aspnet:RoslynCompilerLocation", @"C:\Location\for\all\from\appSettings\compiler.exe");
91+
Environment.SetEnvironmentVariable("ROSLYN_COMPILER_LOCATION", @"C:\My\Compiler\Location\vbcsc.exe");
92+
Environment.SetEnvironmentVariable("VBCSCOMPILER_TTL", "98");
93+
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakecs");
94+
Environment.SetEnvironmentVariable("ROSLYN_COMPILER_LOCATION", null);
95+
Environment.SetEnvironmentVariable("VBCSCOMPILER_TTL", null);
96+
//ConfigurationManager.AppSettings.Remove("aspnet:RoslynCompilerLocation");
97+
98+
Assert.IsNotNull(opts);
99+
Assert.AreEqual<string>(@"C:\My\Compiler\Location\vbcsc.exe", opts.CompilerFullPath);
100+
Assert.AreEqual<int>(98, opts.CompilerServerTimeToLive);
101+
Assert.IsFalse(opts.UseAspNetSettings);
102+
Assert.IsTrue(opts.WarnAsError);
103+
Assert.AreEqual<string>("v6.0", opts.CompilerVersion);
104+
Assert.AreEqual<int>(7, opts.AllOptions.Count);
105+
Assert.AreEqual<string>("foo", opts.AllOptions["CustomSetting"]);
106+
Assert.AreEqual<string>("bar", opts.AllOptions["AnotherCoolSetting"]);
107+
}
108+
109+
// TTL must be int
110+
[TestMethod]
111+
public void TTL_MustBeInteger()
112+
{
113+
Environment.SetEnvironmentVariable("VBCSCOMPILER_TTL", "NotANumber");
114+
IProviderOptions opts = CompilationUtil.GetProviderOptionsFor(".fakevb");
115+
Environment.SetEnvironmentVariable("VBCSCOMPILER_TTL", null);
116+
117+
Assert.IsNotNull(opts);
118+
Assert.AreEqual<string>(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\roslyn"), opts.CompilerFullPath); // Would include csc.exe or vbc.exe if the extension we searched for wasn't fake.
119+
Assert.AreEqual<int>(IsDev ? 15 * 60 : 10, opts.CompilerServerTimeToLive); // 10 in Production. 900 in a "dev" environment.
120+
Assert.IsTrue(opts.UseAspNetSettings); // Default is false... except through the GetProviderOptionsFor factory method we used here.
121+
Assert.IsFalse(opts.WarnAsError);
122+
Assert.IsNull(opts.CompilerVersion);
123+
Assert.AreEqual<int>(2, opts.AllOptions.Count);
124+
Assert.AreEqual<string>("foo2", opts.AllOptions["CustomSetting"]);
125+
Assert.AreEqual<string>("bar2", opts.AllOptions["AnotherCoolSetting"]);
126+
}
127+
}
128+
}

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/Util/CompilationUtil.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ internal static class CompilationUtil {
1717

1818
static CompilationUtil()
1919
{
20-
CSC2 = GetProviderOptionsFor("cs");
21-
VBC2 = GetProviderOptionsFor("vb");
20+
CSC2 = GetProviderOptionsFor(".cs");
21+
VBC2 = GetProviderOptionsFor(".vb");
2222

2323
if (IsDebuggerAttached)
2424
{
@@ -46,7 +46,12 @@ public static IProviderOptions GetProviderOptionsFor(string fileExt)
4646
if (String.IsNullOrEmpty(compilerFullPath))
4747
options.TryGetValue("CompilerLocation", out compilerFullPath);
4848
if (String.IsNullOrEmpty(compilerFullPath))
49-
compilerFullPath = CompilerFullPath(@"bin\roslyn\csc.exe");
49+
compilerFullPath = CompilerFullPath(@"bin\roslyn");
50+
51+
if (fileExt.Equals(".cs", StringComparison.InvariantCultureIgnoreCase))
52+
compilerFullPath = Path.Combine(compilerFullPath, "csc.exe");
53+
else if (fileExt.Equals(".vb", StringComparison.InvariantCultureIgnoreCase))
54+
compilerFullPath = Path.Combine(compilerFullPath, "vbc.exe");
5055

5156

5257
//

0 commit comments

Comments
 (0)