2 // Microsoft.CSharp.CSharpCodeProviderTest.cs
5 // Gert Driesen (drieseng@users.sourceforge.net)
12 using System.CodeDom.Compiler;
13 using System.Collections.Specialized;
14 using System.Globalization;
16 using System.Reflection;
17 using Microsoft.CSharp;
18 using NUnit.Framework;
22 namespace MonoTests.Microsoft.CSharp
25 public class CSharpCodeProviderTest
27 private string _tempDir;
28 private CodeDomProvider _codeProvider;
30 private static readonly string _sourceLibrary1 = "public class Test1 {}";
31 private static readonly string _sourceLibrary2 = "public class Test2 {}";
32 private static readonly string _sourceLibrary3 =
33 @"public class Test3 { public void F() { } }
34 public class Test4 : Test3 { public void F() { } }";
35 private static readonly string _sourceExecutable = "public class Program { static void Main () { } }";
40 _codeProvider = new CSharpCodeProvider ();
41 _tempDir = CreateTempDirectory ();
45 public void TearDown ()
47 RemoveDirectory (_tempDir);
51 public void FileExtension ()
53 Assert.AreEqual ("cs", _codeProvider.FileExtension);
57 public void LanguageOptionsTest ()
59 Assert.AreEqual (LanguageOptions.None, _codeProvider.LanguageOptions);
63 public void GeneratorSupports ()
65 ICodeGenerator codeGenerator = _codeProvider.CreateGenerator ();
66 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#1");
67 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ArraysOfArrays), "#2");
68 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.AssemblyAttributes), "#3");
69 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ChainedConstructorArguments), "#4");
70 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ComplexExpressions), "#5");
71 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareDelegates), "#6");
72 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#7");
73 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEvents), "#8");
74 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareInterfaces), "#9");
75 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareValueTypes), "#10");
76 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.EntryPointMethod), "#11");
77 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GotoStatements), "#12");
78 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultidimensionalArrays), "#13");
79 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultipleInterfaceMembers), "#14");
80 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.NestedTypes), "#15");
81 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ParameterAttributes), "#16");
82 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PublicStaticMembers), "#17");
83 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReferenceParameters), "#18");
84 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReturnTypeAttributes), "#19");
85 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.StaticConstructors), "#20");
86 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.TryCatchStatements), "#21");
87 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Win32Resources), "#22");
88 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareIndexerProperties), "#23");
89 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeDeclaration), "#24");
90 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeReference), "#25");
91 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PartialTypes), "#26");
92 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Resources), "#27");
96 public void CompileFromFile_InMemory ()
99 string sourceFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
100 using (FileStream f = new FileStream (sourceFile, FileMode.Create)) {
101 using (StreamWriter s = new StreamWriter (f)) {
102 s.Write (_sourceLibrary1);
108 CompilerParameters options = new CompilerParameters ();
109 options.GenerateExecutable = false;
110 options.GenerateInMemory = true;
111 options.TempFiles = new TempFileCollection (_tempDir);
112 options.EmbeddedResources.Add (sourceFile);
114 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
115 CompilerResults results = compiler.CompileAssemblyFromFile (options,
118 // verify compilation was successful
119 AssertCompileResults (results, true);
121 Assembly compiledAssembly = results.CompiledAssembly;
123 Assert.IsNotNull (compiledAssembly, "#1");
124 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#2");
125 Assert.IsNull (results.PathToAssembly, "#3");
126 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#4");
128 // verify we don't cleanup files in temp directory too agressively
129 string[] tempFiles = Directory.GetFiles (_tempDir);
130 Assert.AreEqual (1, tempFiles.Length, "#5");
131 Assert.AreEqual (sourceFile, tempFiles[0], "#6");
133 string[] resources = compiledAssembly.GetManifestResourceNames();
134 Assert.IsNotNull (resources, "#7");
135 Assert.AreEqual (1, resources.Length, "#8");
136 Assert.AreEqual ("file.cs", resources[0], "#9");
137 Assert.IsNull (compiledAssembly.GetFile ("file.cs"), "#10");
138 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file.cs"), "#11");
139 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file.cs");
140 Assert.IsNotNull (info, "#12");
141 Assert.IsNull (info.FileName, "#13");
142 Assert.IsNull (info.ReferencedAssembly, "#14");
143 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#15");
147 public void CompileFromFileBatch_Executable_InMemory ()
149 // create source file
150 string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
151 using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
152 using (StreamWriter s = new StreamWriter (f)) {
153 s.Write (_sourceLibrary1);
159 string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
160 using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
161 using (StreamWriter s = new StreamWriter (f)) {
162 s.Write (_sourceExecutable);
168 CompilerParameters options = new CompilerParameters ();
169 options.GenerateExecutable = true;
170 options.GenerateInMemory = true;
171 options.OutputAssembly = string.Empty;
172 options.TempFiles = new TempFileCollection (_tempDir);
173 options.EmbeddedResources.Add (sourceFile1);
174 options.LinkedResources.Add (sourceFile2);
176 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
177 CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
178 new string [] { sourceFile1, sourceFile2 });
180 // verify compilation was successful
181 AssertCompileResults (results, true);
183 Assembly compiledAssembly = results.CompiledAssembly;
185 Assert.IsNotNull (compiledAssembly, "#A1");
186 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
187 Assert.IsNull (results.PathToAssembly, "#A3");
188 Assert.IsNotNull (options.OutputAssembly, "#A4");
189 Assert.AreEqual (".exe", Path.GetExtension (options.OutputAssembly), "#A5");
190 Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
191 Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
193 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
194 Assert.IsNotNull (compiledAssembly.GetType ("Program"), "#B2");
196 // verify we don't cleanup files in temp directory too agressively
197 string [] tempFiles = Directory.GetFiles (_tempDir);
198 Assert.AreEqual (2, tempFiles.Length, "#C1");
199 Assert.IsTrue (File.Exists (sourceFile1), "#C2");
200 Assert.IsTrue (File.Exists (sourceFile2), "#C3");
202 string[] resources = compiledAssembly.GetManifestResourceNames();
203 Assert.IsNotNull (resources, "#D1");
204 Assert.AreEqual (2, resources.Length, "#D2");
206 Assert.IsTrue (resources[0] == "file1.cs" || resources [0] == "file2.cs", "#E1");
207 Assert.IsNull (compiledAssembly.GetFile ("file1.cs"), "#E2");
208 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file1.cs"), "#E3");
209 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.cs");
210 Assert.IsNotNull (info, "#E4");
211 Assert.IsNull (info.FileName, "#E5");
212 Assert.IsNull (info.ReferencedAssembly, "#E6");
213 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
215 Assert.IsTrue (resources[1] == "file1.cs" || resources [1] == "file2.cs", "#F1");
217 compiledAssembly.GetFile ("file2.cs");
219 } catch (FileNotFoundException) {
222 compiledAssembly.GetManifestResourceStream ("file2.cs");
224 } catch (FileNotFoundException) {
226 info = compiledAssembly.GetManifestResourceInfo ("file2.cs");
227 Assert.IsNotNull (info, "#F4");
228 Assert.IsNotNull (info.FileName, "#F5");
229 Assert.AreEqual ("file2.cs", info.FileName, "#F6");
230 Assert.IsNull (info.ReferencedAssembly, "#F7");
231 Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
235 public void CompileFromFileBatch_Library_InMemory ()
237 // create source file
238 string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
239 using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
240 using (StreamWriter s = new StreamWriter (f)) {
241 s.Write (_sourceLibrary1);
247 string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
248 using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
249 using (StreamWriter s = new StreamWriter (f)) {
250 s.Write (_sourceLibrary2);
256 CompilerParameters options = new CompilerParameters ();
257 options.GenerateExecutable = false;
258 options.GenerateInMemory = true;
259 options.TempFiles = new TempFileCollection (_tempDir);
260 options.EmbeddedResources.Add (sourceFile1);
261 options.LinkedResources.Add (sourceFile2);
263 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
264 CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
265 new string [] { sourceFile1, sourceFile2 });
267 // verify compilation was successful
268 AssertCompileResults (results, true);
270 Assembly compiledAssembly = results.CompiledAssembly;
272 Assert.IsNotNull (compiledAssembly, "#A1");
273 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
274 Assert.IsNull (results.PathToAssembly, "#A3");
275 Assert.IsNotNull (options.OutputAssembly, "#A4");
276 Assert.AreEqual (".dll", Path.GetExtension (options.OutputAssembly), "#A5");
277 Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
278 Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
280 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
281 Assert.IsNotNull (compiledAssembly.GetType ("Test2"), "#B2");
283 // verify we don't cleanup files in temp directory too agressively
284 string [] tempFiles = Directory.GetFiles (_tempDir);
285 Assert.AreEqual (2, tempFiles.Length, "#C1");
286 Assert.IsTrue (File.Exists (sourceFile1), "#C2");
287 Assert.IsTrue (File.Exists (sourceFile2), "#C3");
289 string[] resources = compiledAssembly.GetManifestResourceNames();
290 Assert.IsNotNull (resources, "#D1");
291 Assert.AreEqual (2, resources.Length, "#D2");
293 Assert.IsTrue (resources[0] == "file1.cs" || resources [0] == "file2.cs", "#E1");
294 Assert.IsNull (compiledAssembly.GetFile ("file1.cs"), "#E2");
295 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file1.cs"), "#E3");
296 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.cs");
297 Assert.IsNotNull (info, "#E4");
298 Assert.IsNull (info.FileName, "#E5");
299 Assert.IsNull (info.ReferencedAssembly, "#E6");
300 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
302 Assert.IsTrue (resources[1] == "file1.cs" || resources [1] == "file2.cs", "#F1");
304 compiledAssembly.GetFile ("file2.cs");
306 } catch (FileNotFoundException) {
309 compiledAssembly.GetManifestResourceStream ("file2.cs");
311 } catch (FileNotFoundException) {
313 info = compiledAssembly.GetManifestResourceInfo ("file2.cs");
314 Assert.IsNotNull (info, "#F4");
315 Assert.IsNotNull (info.FileName, "#F5");
316 Assert.AreEqual ("file2.cs", info.FileName, "#F6");
317 Assert.IsNull (info.ReferencedAssembly, "#F7");
318 Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
322 public void CompileFromSource_InMemory ()
324 // create a file in temp directory to ensure that compiler is not removing
325 // too much (temporary) files
326 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
327 using (FileStream fs = File.Create (tempFile)) {
331 CompilerParameters options = new CompilerParameters ();
332 options.GenerateExecutable = false;
333 options.GenerateInMemory = true;
334 options.TempFiles = new TempFileCollection (_tempDir);
336 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
337 CompilerResults results = compiler.CompileAssemblyFromSource (options,
340 // verify compilation was successful
341 AssertCompileResults (results, true);
343 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
344 Assert.IsNull (results.PathToAssembly, "#2");
345 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");
347 // verify we don't cleanup files in temp directory too agressively
348 string[] tempFiles = Directory.GetFiles (_tempDir);
349 Assert.AreEqual (1, tempFiles.Length, "#4");
350 Assert.AreEqual (tempFile, tempFiles[0], "#5");
354 public void CompileFromSource_InMemory_Twice ()
356 CompilerParameters options = new CompilerParameters ();
357 options.GenerateExecutable = false;
358 options.GenerateInMemory = true;
360 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
362 var src_1 = "class X { ";
364 CompilerResults results_1 = compiler.CompileAssemblyFromSource (options, src_1);
365 var output_1 = options.OutputAssembly;
367 var src_2 = "class X { }";
369 CompilerResults results_2 = compiler.CompileAssemblyFromSource (options, src_2);
370 var output_2 = options.OutputAssembly;
372 // verify compilation was successful
373 AssertCompileResults (results_2, true);
375 Assert.AreEqual (output_1, output_2, "#1");
380 public void CompileFromSource_InMemory_With_Extra_Delete ()
382 CompilerParameters options = new CompilerParameters ();
383 options.GenerateExecutable = false;
384 options.GenerateInMemory = true;
386 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
388 var src_1 = "class X { ";
390 compiler.CompileAssemblyFromSource (options, src_1);
392 options.TempFiles.Delete ();
393 options.TempFiles.Delete ();
397 public void CompileFromSourceBatch_InMemory ()
399 // create a file in temp directory to ensure that compiler is not removing
400 // too much (temporary) files
401 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
402 using (FileStream fs = File.Create (tempFile)) {
406 string outputAssembly = Path.Combine (_tempDir, "sourcebatch.dll");
408 CompilerParameters options = new CompilerParameters ();
409 options.GenerateExecutable = false;
410 options.GenerateInMemory = true;
411 options.OutputAssembly = outputAssembly;
412 options.TempFiles = new TempFileCollection (_tempDir);
414 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
415 CompilerResults results = compiler.CompileAssemblyFromSourceBatch (options,
416 new string [] { _sourceLibrary1, _sourceLibrary2 });
418 // verify compilation was successful
419 AssertCompileResults (results, true);
421 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#A1");
422 Assert.IsNull (results.PathToAssembly, "#A2");
423 Assert.IsNotNull (options.OutputAssembly, "#A3");
424 Assert.AreEqual (outputAssembly, options.OutputAssembly, "#A4");
425 Assert.IsTrue (File.Exists (outputAssembly), "#A5");
427 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#B1");
428 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#B2");
430 // verify we don't cleanup files in temp directory too agressively
431 string[] tempFiles = Directory.GetFiles (_tempDir);
432 Assert.AreEqual (2, tempFiles.Length, "#C1");
433 Assert.AreEqual (tempFile, tempFiles[0], "#C2");
434 Assert.AreEqual (outputAssembly, tempFiles [1], "#C3");
438 public void CompileFromDom_NotInMemory ()
440 // create a file in temp directory to ensure that compiler is not removing
441 // too much (temporary) files
442 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
443 using (FileStream fs = File.Create (tempFile)) {
447 // compile and verify result in separate appdomain to avoid file locks
448 AppDomain testDomain = CreateTestDomain ();
449 CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
451 string outputAssembly = null;
454 outputAssembly = compileTester.CompileAssemblyFromDom (_tempDir);
456 AppDomain.Unload (testDomain);
459 // there should be two files in temp dir: temp file and output assembly
460 string[] tempFiles = Directory.GetFiles (_tempDir);
461 Assert.AreEqual (2, tempFiles.Length, "#1");
462 Assert.IsTrue (File.Exists (outputAssembly), "#2");
463 Assert.IsTrue (File.Exists (tempFile), "#3");
467 public void CompileFromDomBatch_NotInMemory ()
469 // create a file in temp directory to ensure that compiler is not removing
470 // too much (temporary) files
471 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
472 using (FileStream fs = File.Create (tempFile)) {
476 // compile and verify result in separate appdomain to avoid file locks
477 AppDomain testDomain = CreateTestDomain ();
478 CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
480 string outputAssembly = null;
482 outputAssembly = compileTester.CompileAssemblyFromDomBatch (_tempDir);
484 AppDomain.Unload (testDomain);
487 // there should be two files in temp dir: temp file and output assembly
488 string[] tempFiles = Directory.GetFiles (_tempDir);
489 Assert.AreEqual (2, tempFiles.Length, "#1");
490 Assert.IsTrue (File.Exists (outputAssembly), "#2");
491 Assert.IsTrue (File.Exists (tempFile), "#3");
495 public void CompileFromDom_InMemory ()
497 // create a file in temp directory to ensure that compiler is not removing
498 // too much (temporary) files
499 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
500 using (FileStream fs = File.Create (tempFile)) {
504 CompilerParameters options = new CompilerParameters ();
505 options.GenerateExecutable = false;
506 options.GenerateInMemory = true;
507 options.TempFiles = new TempFileCollection (_tempDir);
509 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
510 CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
512 // verify compilation was successful
513 AssertCompileResults (results, true);
515 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
516 Assert.IsNull (results.PathToAssembly, "#2");
518 // verify we don't cleanup files in temp directory too agressively
519 string[] tempFiles = Directory.GetFiles (_tempDir);
520 Assert.AreEqual (1, tempFiles.Length, "#3");
521 Assert.AreEqual (tempFile, tempFiles[0], "#4");
525 public void CompileFromDomBatch_InMemory ()
527 // create a file in temp directory to ensure that compiler is not removing
528 // too much (temporary) files
529 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
530 using (FileStream fs = File.Create (tempFile)) {
534 CompilerParameters options = new CompilerParameters ();
535 options.GenerateExecutable = false;
536 options.GenerateInMemory = true;
537 options.TempFiles = new TempFileCollection (_tempDir);
539 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
540 CompilerResults results = compiler.CompileAssemblyFromDomBatch (options,
541 new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
543 // verify compilation was successful
544 AssertCompileResults (results, true);
546 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
547 Assert.IsNull (results.PathToAssembly, "#2");
549 // verify we don't cleanup files in temp directory too agressively
550 string[] tempFiles = Directory.GetFiles (_tempDir);
551 Assert.AreEqual (1, tempFiles.Length, "#3");
552 Assert.AreEqual (tempFile, tempFiles[0], "#4");
556 public void MultiLineWarningIsReportedAsOneWarning()
558 CompilerParameters options = new CompilerParameters ();
559 options.GenerateExecutable = false;
560 options.GenerateInMemory = true;
561 options.TempFiles = new TempFileCollection (_tempDir);
563 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
564 CompilerResults results = compiler.CompileAssemblyFromSource (options,
567 // verify compilation was successful
568 AssertCompileResults (results, true);
572 public void EncodingMismatch ()
575 #warning Trigger Some Warning
576 public class MyClass {
577 public static string MyMethod () { return ""data""; }
580 var p = new CompilerParameters () {
581 GenerateInMemory = false,
582 GenerateExecutable = false,
583 IncludeDebugInformation = true,
584 TreatWarningsAsErrors = false,
585 TempFiles = new TempFileCollection (_tempDir, true),
588 var prov = new CSharpCodeProvider ();
589 CompilerResults results;
591 var prev = Console.OutputEncoding;
593 Console.OutputEncoding = Encoding.Unicode;
595 results = prov.CompileAssemblyFromSource (p, source);
597 Console.OutputEncoding = prev;
600 Assert.IsNotNull (results.Errors);
601 Assert.IsTrue (results.Output.Cast<string>().ToArray ()[1].Contains ("Trigger Some Warning"));
604 private static string CreateTempDirectory ()
606 // create a uniquely named zero-byte file
607 string tempFile = Path.GetTempFileName ();
608 // remove the temporary file
609 File.Delete (tempFile);
610 // create a directory named after the unique temporary file
611 Directory.CreateDirectory (tempFile);
612 // return the path to the temporary directory
616 private static void RemoveDirectory (string path)
619 if (Directory.Exists (path)) {
620 string[] directoryNames = Directory.GetDirectories (path);
621 foreach (string directoryName in directoryNames) {
622 RemoveDirectory (directoryName);
624 string[] fileNames = Directory.GetFiles (path);
625 foreach (string fileName in fileNames) {
626 File.Delete (fileName);
628 Directory.Delete (path, true);
630 } catch (Exception ex) {
631 throw new AssertionException ("Unable to cleanup '" + path + "'.", ex);
635 private static void AssertCompileResults (CompilerResults results, bool allowWarnings)
637 foreach (CompilerError compilerError in results.Errors) {
638 if (allowWarnings && compilerError.IsWarning) {
642 throw new Exception (compilerError.ToString ());
646 private static AppDomain CreateTestDomain ()
648 return AppDomain.CreateDomain ("CompileFromDom", AppDomain.CurrentDomain.Evidence,
649 AppDomain.CurrentDomain.SetupInformation);
652 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
654 Type testerType = typeof (CrossDomainTester);
656 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
657 testerType.Assembly.FullName, testerType.FullName, false,
658 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
659 CultureInfo.InvariantCulture, new object[0], domain.Evidence);
662 // do not use the Assert class as this will introduce failures if the
663 // nunit.framework assembly is not in the GAC
664 private class CrossDomainTester : MarshalByRefObject
666 public string CompileAssemblyFromDom (string tempDir)
668 CompilerParameters options = new CompilerParameters ();
669 options.GenerateExecutable = false;
670 options.GenerateInMemory = false;
671 options.TempFiles = new TempFileCollection (tempDir);
673 CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
674 ICodeCompiler compiler = codeProvider.CreateCompiler ();
675 CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
677 // verify compilation was successful
678 AssertCompileResults (results, true);
680 if (results.CompiledAssembly.Location.Length == 0)
681 throw new Exception ("Location should not be empty string");
682 if (results.PathToAssembly == null)
683 throw new Exception ("PathToAssembly should not be null");
685 return results.PathToAssembly;
688 public string CompileAssemblyFromDomBatch (string tempDir)
690 CompilerParameters options = new CompilerParameters ();
691 options.GenerateExecutable = false;
692 options.GenerateInMemory = false;
693 options.TempFiles = new TempFileCollection (tempDir);
695 CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
696 ICodeCompiler compiler = codeProvider.CreateCompiler ();
697 CompilerResults results = compiler.CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
699 // verify compilation was successful
700 AssertCompileResults (results, true);
702 if (results.CompiledAssembly.Location.Length == 0)
703 throw new Exception ("Location should not be empty string");
704 if (results.PathToAssembly == null)
705 throw new Exception ("PathToAssembly should not be null");
707 return results.PathToAssembly;