|
| 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 | +} |
0 commit comments