Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / System / Test / Microsoft.CSharp / CSharpCodeProviderTest.cs
1 //
2 // Microsoft.CSharp.CSharpCodeProviderTest.cs
3 //
4 // Author:
5 // Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (C) 2005 Novell
8 //
9
10 using System;
11 using System.CodeDom;
12 using System.CodeDom.Compiler;
13 using System.Collections.Specialized;
14 using System.Globalization;
15 using System.IO;
16 using System.Reflection;
17 using Microsoft.CSharp;
18 using NUnit.Framework;
19
20 namespace MonoTests.Microsoft.CSharp
21 {
22         [TestFixture]
23         public class CSharpCodeProviderTest
24         {
25                 private string _tempDir;
26                 private CodeDomProvider _codeProvider;
27
28                 private static readonly string _sourceLibrary1 = "public class Test1 {}";
29                 private static readonly string _sourceLibrary2 = "public class Test2 {}";
30                 private static readonly string _sourceExecutable = "public class Program { static void Main () { } }";
31
32                 [SetUp]
33                 public void SetUp ()
34                 {
35                         _codeProvider = new CSharpCodeProvider ();
36                         _tempDir = CreateTempDirectory ();
37                 }
38
39                 [TearDown]
40                 public void TearDown ()
41                 {
42                         RemoveDirectory (_tempDir);
43                 }
44
45                 [Test]
46                 public void FileExtension ()
47                 {
48                         Assert.AreEqual ("cs", _codeProvider.FileExtension);
49                 }
50
51                 [Test]
52                 public void LanguageOptionsTest ()
53                 {
54                         Assert.AreEqual (LanguageOptions.None, _codeProvider.LanguageOptions);
55                 }
56
57                 [Test]
58                 public void GeneratorSupports ()
59                 {
60                         ICodeGenerator codeGenerator = _codeProvider.CreateGenerator ();
61                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#1");
62                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ArraysOfArrays), "#2");
63                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.AssemblyAttributes), "#3");
64                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ChainedConstructorArguments), "#4");
65                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ComplexExpressions), "#5");
66                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareDelegates), "#6");
67                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#7");
68                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEvents), "#8");
69                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareInterfaces), "#9");
70                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareValueTypes), "#10");
71                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.EntryPointMethod), "#11");
72                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GotoStatements), "#12");
73                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultidimensionalArrays), "#13");
74                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultipleInterfaceMembers), "#14");
75                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.NestedTypes), "#15");
76                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ParameterAttributes), "#16");
77                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PublicStaticMembers), "#17");
78                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReferenceParameters), "#18");
79                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReturnTypeAttributes), "#19");
80                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.StaticConstructors), "#20");
81                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.TryCatchStatements), "#21");
82                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Win32Resources), "#22");
83 #if NET_2_0
84                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareIndexerProperties), "#23");
85                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeDeclaration), "#24");
86                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeReference), "#25");
87                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PartialTypes), "#26");
88                         Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Resources), "#27");
89 #endif
90                 }
91
92                 [Test]
93                 public void CompileFromFile_InMemory ()
94                 {
95                         // create source file
96                         string sourceFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
97                         using (FileStream f = new FileStream (sourceFile, FileMode.Create)) {
98                                 using (StreamWriter s = new StreamWriter (f)) {
99                                         s.Write (_sourceLibrary1);
100                                         s.Close ();
101                                 }
102                                 f.Close ();
103                         }
104
105                         CompilerParameters options = new CompilerParameters ();
106                         options.GenerateExecutable = false;
107                         options.GenerateInMemory = true;
108                         options.TempFiles = new TempFileCollection (_tempDir);
109 #if NET_2_0
110                         options.EmbeddedResources.Add (sourceFile);
111 #endif
112
113                         ICodeCompiler compiler = _codeProvider.CreateCompiler ();
114                         CompilerResults results = compiler.CompileAssemblyFromFile (options,
115                                 sourceFile);
116
117                         // verify compilation was successful
118                         AssertCompileResults (results, true);
119
120                         Assembly compiledAssembly = results.CompiledAssembly;
121
122                         Assert.IsNotNull (compiledAssembly, "#1");
123                         Assert.AreEqual (string.Empty, compiledAssembly.Location, "#2");
124                         Assert.IsNull (results.PathToAssembly, "#3");
125                         Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#4");
126                         
127                         // verify we don't cleanup files in temp directory too agressively
128                         string[] tempFiles = Directory.GetFiles (_tempDir);
129                         Assert.AreEqual (1, tempFiles.Length, "#5");
130                         Assert.AreEqual (sourceFile, tempFiles[0], "#6");
131                         
132 #if NET_2_0
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");
144 #endif
145                 }
146
147                 [Test]
148                 public void CompileFromFileBatch_Executable_InMemory ()
149                 {
150                         // create source file
151                         string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
152                         using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
153                                 using (StreamWriter s = new StreamWriter (f)) {
154                                         s.Write (_sourceLibrary1);
155                                         s.Close ();
156                                 }
157                                 f.Close ();
158                         }
159
160                         string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
161                         using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
162                                 using (StreamWriter s = new StreamWriter (f)) {
163                                         s.Write (_sourceExecutable);
164                                         s.Close ();
165                                 }
166                                 f.Close ();
167                         }
168
169                         CompilerParameters options = new CompilerParameters ();
170                         options.GenerateExecutable = true;
171                         options.GenerateInMemory = true;
172                         options.OutputAssembly = string.Empty;
173                         options.TempFiles = new TempFileCollection (_tempDir);
174 #if NET_2_0
175                         options.EmbeddedResources.Add (sourceFile1);
176                         options.LinkedResources.Add (sourceFile2);
177 #endif
178
179                         ICodeCompiler compiler = _codeProvider.CreateCompiler ();
180                         CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
181                                 new string [] { sourceFile1, sourceFile2 });
182
183                         // verify compilation was successful
184                         AssertCompileResults (results, true);
185
186                         Assembly compiledAssembly = results.CompiledAssembly;
187
188                         Assert.IsNotNull (compiledAssembly, "#A1");
189                         Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
190                         Assert.IsNull (results.PathToAssembly, "#A3");
191                         Assert.IsNotNull (options.OutputAssembly, "#A4");
192                         Assert.AreEqual (".exe", Path.GetExtension (options.OutputAssembly), "#A5");
193                         Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
194                         Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
195
196                         Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
197                         Assert.IsNotNull (compiledAssembly.GetType ("Program"), "#B2");
198
199                         // verify we don't cleanup files in temp directory too agressively
200                         string [] tempFiles = Directory.GetFiles (_tempDir);
201                         Assert.AreEqual (2, tempFiles.Length, "#C1");
202                         Assert.IsTrue (File.Exists (sourceFile1), "#C2");
203                         Assert.IsTrue (File.Exists (sourceFile2), "#C3");
204
205 #if NET_2_0
206                         string[] resources = compiledAssembly.GetManifestResourceNames();
207                         Assert.IsNotNull (resources, "#D1");
208                         Assert.AreEqual (2, resources.Length, "#D2");
209
210                         Assert.IsTrue (resources[0] == "file1.cs" || resources [0] == "file2.cs", "#E1");
211                         Assert.IsNull (compiledAssembly.GetFile ("file1.cs"), "#E2");
212                         Assert.IsNotNull (compiledAssembly.GetManifestResourceStream  ("file1.cs"), "#E3");
213                         ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.cs");
214                         Assert.IsNotNull (info, "#E4");
215                         Assert.IsNull (info.FileName, "#E5");
216                         Assert.IsNull (info.ReferencedAssembly, "#E6");
217                         Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
218
219                         Assert.IsTrue (resources[1] == "file1.cs" || resources [1] == "file2.cs", "#F1");
220                         try {
221                                 compiledAssembly.GetFile ("file2.cs");
222                                 Assert.Fail ("#F2");
223                         } catch (FileNotFoundException) {
224                         }
225                         try {
226                                 compiledAssembly.GetManifestResourceStream  ("file2.cs");
227                                 Assert.Fail ("#F3");
228                         } catch (FileNotFoundException) {
229                         }
230                         info = compiledAssembly.GetManifestResourceInfo ("file2.cs");
231                         Assert.IsNotNull (info, "#F4");
232                         Assert.IsNotNull (info.FileName, "#F5");
233                         Assert.AreEqual ("file2.cs", info.FileName, "#F6");
234                         Assert.IsNull (info.ReferencedAssembly, "#F7");
235                         Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
236 #endif
237                 }
238
239                 [Test]
240                 public void CompileFromFileBatch_Library_InMemory ()
241                 {
242                         // create source file
243                         string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
244                         using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
245                                 using (StreamWriter s = new StreamWriter (f)) {
246                                         s.Write (_sourceLibrary1);
247                                         s.Close ();
248                                 }
249                                 f.Close ();
250                         }
251
252                         string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
253                         using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
254                                 using (StreamWriter s = new StreamWriter (f)) {
255                                         s.Write (_sourceLibrary2);
256                                         s.Close ();
257                                 }
258                                 f.Close ();
259                         }
260
261                         CompilerParameters options = new CompilerParameters ();
262                         options.GenerateExecutable = false;
263                         options.GenerateInMemory = true;
264                         options.TempFiles = new TempFileCollection (_tempDir);
265 #if NET_2_0
266                         options.EmbeddedResources.Add (sourceFile1);
267                         options.LinkedResources.Add (sourceFile2);
268 #endif
269
270                         ICodeCompiler compiler = _codeProvider.CreateCompiler ();
271                         CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
272                                 new string [] { sourceFile1, sourceFile2 });
273
274                         // verify compilation was successful
275                         AssertCompileResults (results, true);
276
277                         Assembly compiledAssembly = results.CompiledAssembly;
278
279                         Assert.IsNotNull (compiledAssembly, "#A1");
280                         Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
281                         Assert.IsNull (results.PathToAssembly, "#A3");
282                         Assert.IsNotNull (options.OutputAssembly, "#A4");
283                         Assert.AreEqual (".dll", Path.GetExtension (options.OutputAssembly), "#A5");
284                         Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
285                         Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
286
287                         Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
288                         Assert.IsNotNull (compiledAssembly.GetType ("Test2"), "#B2");
289
290                         // verify we don't cleanup files in temp directory too agressively
291                         string [] tempFiles = Directory.GetFiles (_tempDir);
292                         Assert.AreEqual (2, tempFiles.Length, "#C1");
293                         Assert.IsTrue (File.Exists (sourceFile1), "#C2");
294                         Assert.IsTrue (File.Exists (sourceFile2), "#C3");
295
296 #if NET_2_0
297                         string[] resources = compiledAssembly.GetManifestResourceNames();
298                         Assert.IsNotNull (resources, "#D1");
299                         Assert.AreEqual (2, resources.Length, "#D2");
300
301                         Assert.IsTrue (resources[0] == "file1.cs" || resources [0] == "file2.cs", "#E1");
302                         Assert.IsNull (compiledAssembly.GetFile ("file1.cs"), "#E2");
303                         Assert.IsNotNull (compiledAssembly.GetManifestResourceStream  ("file1.cs"), "#E3");
304                         ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.cs");
305                         Assert.IsNotNull (info, "#E4");
306                         Assert.IsNull (info.FileName, "#E5");
307                         Assert.IsNull (info.ReferencedAssembly, "#E6");
308                         Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
309
310                         Assert.IsTrue (resources[1] == "file1.cs" || resources [1] == "file2.cs", "#F1");
311                         try {
312                                 compiledAssembly.GetFile ("file2.cs");
313                                 Assert.Fail ("#F2");
314                         } catch (FileNotFoundException) {
315                         }
316                         try {
317                                 compiledAssembly.GetManifestResourceStream  ("file2.cs");
318                                 Assert.Fail ("#F3");
319                         } catch (FileNotFoundException) {
320                         }
321                         info = compiledAssembly.GetManifestResourceInfo ("file2.cs");
322                         Assert.IsNotNull (info, "#F4");
323                         Assert.IsNotNull (info.FileName, "#F5");
324                         Assert.AreEqual ("file2.cs", info.FileName, "#F6");
325                         Assert.IsNull (info.ReferencedAssembly, "#F7");
326                         Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
327 #endif
328                 }
329
330                 [Test]
331                 public void CompileFromSource_InMemory ()
332                 {
333                         // create a file in temp directory to ensure that compiler is not removing
334                         // too much (temporary) files
335                         string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
336                         using (FileStream fs = File.Create (tempFile)) {
337                                 fs.Close ();
338                         }
339
340                         CompilerParameters options = new CompilerParameters ();
341                         options.GenerateExecutable = false;
342                         options.GenerateInMemory = true;
343                         options.TempFiles = new TempFileCollection (_tempDir);
344
345                         ICodeCompiler compiler = _codeProvider.CreateCompiler ();
346                         CompilerResults results = compiler.CompileAssemblyFromSource (options,
347                                 _sourceLibrary1);
348
349                         // verify compilation was successful
350                         AssertCompileResults (results, true);
351
352                         Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
353                         Assert.IsNull (results.PathToAssembly, "#2");
354                         Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");
355
356                         // verify we don't cleanup files in temp directory too agressively
357                         string[] tempFiles = Directory.GetFiles (_tempDir);
358                         Assert.AreEqual (1, tempFiles.Length, "#4");
359                         Assert.AreEqual (tempFile, tempFiles[0], "#5");
360                 }
361
362                 [Test]
363                 public void CompileFromSourceBatch_InMemory ()
364                 {
365                         // create a file in temp directory to ensure that compiler is not removing
366                         // too much (temporary) files
367                         string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
368                         using (FileStream fs = File.Create (tempFile)) {
369                                 fs.Close ();
370                         }
371
372                         string outputAssembly = Path.Combine (_tempDir, "sourcebatch.dll");
373
374                         CompilerParameters options = new CompilerParameters ();
375                         options.GenerateExecutable = false;
376                         options.GenerateInMemory = true;
377                         options.OutputAssembly = outputAssembly;
378                         options.TempFiles = new TempFileCollection (_tempDir);
379
380                         ICodeCompiler compiler = _codeProvider.CreateCompiler ();
381                         CompilerResults results = compiler.CompileAssemblyFromSourceBatch (options,
382                                 new string [] { _sourceLibrary1, _sourceLibrary2 });
383
384                         // verify compilation was successful
385                         AssertCompileResults (results, true);
386
387                         Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#A1");
388                         Assert.IsNull (results.PathToAssembly, "#A2");
389                         Assert.IsNotNull (options.OutputAssembly, "#A3");
390                         Assert.AreEqual (outputAssembly, options.OutputAssembly, "#A4");
391                         Assert.IsTrue (File.Exists (outputAssembly), "#A5");
392
393                         Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#B1");
394                         Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#B2");
395
396                         // verify we don't cleanup files in temp directory too agressively
397                         string[] tempFiles = Directory.GetFiles (_tempDir);
398                         Assert.AreEqual (2, tempFiles.Length, "#C1");
399                         Assert.AreEqual (tempFile, tempFiles[0], "#C2");
400                         Assert.AreEqual (outputAssembly, tempFiles [1], "#C3");
401                 }
402
403                 [Test]
404                 public void CompileFromDom_NotInMemory ()
405                 {
406                         // create a file in temp directory to ensure that compiler is not removing
407                         // too much (temporary) files
408                         string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
409                         using (FileStream fs = File.Create (tempFile)) {
410                                 fs.Close ();
411                         }
412
413                         // compile and verify result in separate appdomain to avoid file locks
414                         AppDomain testDomain = CreateTestDomain ();
415                         CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
416
417                         string outputAssembly = null;
418
419                         try {
420                                 outputAssembly = compileTester.CompileAssemblyFromDom (_tempDir);
421                         } finally {
422                                 AppDomain.Unload (testDomain);
423                         }
424
425                         // there should be two files in temp dir: temp file and output assembly
426                         string[] tempFiles = Directory.GetFiles (_tempDir);
427                         Assert.AreEqual (2, tempFiles.Length, "#1");
428                         Assert.IsTrue (File.Exists (outputAssembly), "#2");
429                         Assert.IsTrue (File.Exists (tempFile), "#3");
430                 }
431
432                 [Test]
433                 public void CompileFromDomBatch_NotInMemory ()
434                 {
435                         // create a file in temp directory to ensure that compiler is not removing
436                         // too much (temporary) files
437                         string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
438                         using (FileStream fs = File.Create (tempFile)) {
439                                 fs.Close ();
440                         }
441
442                         // compile and verify result in separate appdomain to avoid file locks
443                         AppDomain testDomain = CreateTestDomain ();
444                         CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
445
446                         string outputAssembly = null;
447                         try {
448                                 outputAssembly = compileTester.CompileAssemblyFromDomBatch (_tempDir);
449                         } finally {
450                                 AppDomain.Unload (testDomain);
451                         }
452
453                         // there should be two files in temp dir: temp file and output assembly
454                         string[] tempFiles = Directory.GetFiles (_tempDir);
455                         Assert.AreEqual (2, tempFiles.Length, "#1");
456                         Assert.IsTrue (File.Exists (outputAssembly), "#2");
457                         Assert.IsTrue (File.Exists (tempFile), "#3");
458                 }
459
460                 [Test]
461                 public void CompileFromDom_InMemory ()
462                 {
463                         // create a file in temp directory to ensure that compiler is not removing
464                         // too much (temporary) files
465                         string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
466                         using (FileStream fs = File.Create (tempFile)) {
467                                 fs.Close ();
468                         }
469
470                         CompilerParameters options = new CompilerParameters ();
471                         options.GenerateExecutable = false;
472                         options.GenerateInMemory = true;
473                         options.TempFiles = new TempFileCollection (_tempDir);
474
475                         ICodeCompiler compiler = _codeProvider.CreateCompiler ();
476                         CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
477
478                         // verify compilation was successful
479                         AssertCompileResults (results, true);
480
481                         Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
482                         Assert.IsNull (results.PathToAssembly, "#2");
483
484                         // verify we don't cleanup files in temp directory too agressively
485                         string[] tempFiles = Directory.GetFiles (_tempDir);
486                         Assert.AreEqual (1, tempFiles.Length, "#3");
487                         Assert.AreEqual (tempFile, tempFiles[0], "#4");
488                 }
489
490                 [Test]
491                 public void CompileFromDomBatch_InMemory ()
492                 {
493                         // create a file in temp directory to ensure that compiler is not removing
494                         // too much (temporary) files
495                         string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
496                         using (FileStream fs = File.Create (tempFile)) {
497                                 fs.Close ();
498                         }
499
500                         CompilerParameters options = new CompilerParameters ();
501                         options.GenerateExecutable = false;
502                         options.GenerateInMemory = true;
503                         options.TempFiles = new TempFileCollection (_tempDir);
504
505                         ICodeCompiler compiler = _codeProvider.CreateCompiler ();
506                         CompilerResults results = compiler.CompileAssemblyFromDomBatch (options,
507                                 new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
508
509                         // verify compilation was successful
510                         AssertCompileResults (results, true);
511
512                         Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
513                         Assert.IsNull (results.PathToAssembly, "#2");
514
515                         // verify we don't cleanup files in temp directory too agressively
516                         string[] tempFiles = Directory.GetFiles (_tempDir);
517                         Assert.AreEqual (1, tempFiles.Length, "#3");
518                         Assert.AreEqual (tempFile, tempFiles[0], "#4");
519                 }
520
521                 private static string CreateTempDirectory ()
522                 {
523                         // create a uniquely named zero-byte file
524                         string tempFile = Path.GetTempFileName ();
525                         // remove the temporary file
526                         File.Delete (tempFile);
527                         // create a directory named after the unique temporary file
528                         Directory.CreateDirectory (tempFile);
529                         // return the path to the temporary directory
530                         return tempFile;
531                 }
532
533                 private static void RemoveDirectory (string path)
534                 {
535                         try {
536                                 if (Directory.Exists (path)) {
537                                         string[] directoryNames = Directory.GetDirectories (path);
538                                         foreach (string directoryName in directoryNames) {
539                                                 RemoveDirectory (directoryName);
540                                         }
541                                         string[] fileNames = Directory.GetFiles (path);
542                                         foreach (string fileName in fileNames) {
543                                                 File.Delete (fileName);
544                                         }
545                                         Directory.Delete (path, true);
546                                 }
547                         } catch (Exception ex) {
548                                 throw new AssertionException ("Unable to cleanup '" + path + "'.", ex);
549                         }
550                 }
551
552                 private static void AssertCompileResults (CompilerResults results, bool allowWarnings)
553                 {
554                         foreach (CompilerError compilerError in results.Errors) {
555                                 if (allowWarnings && compilerError.IsWarning) {
556                                         continue;
557                                 }
558
559                                 throw new Exception (compilerError.ToString ());
560                         }
561                 }
562
563                 private static AppDomain CreateTestDomain ()
564                 {
565                         return AppDomain.CreateDomain ("CompileFromDom", AppDomain.CurrentDomain.Evidence,
566                                 AppDomain.CurrentDomain.SetupInformation);
567                 }
568
569                 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
570                 {
571                         Type testerType = typeof (CrossDomainTester);
572
573                         return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
574                                 testerType.Assembly.FullName, testerType.FullName, false,
575                                 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
576                                 CultureInfo.InvariantCulture, new object[0], domain.Evidence);
577                 }
578
579                 // do not use the Assert class as this will introduce failures if the
580                 // nunit.framework assembly is not in the GAC
581                 private class CrossDomainTester : MarshalByRefObject
582                 {
583                         public string CompileAssemblyFromDom (string tempDir)
584                         {
585                                 CompilerParameters options = new CompilerParameters ();
586                                 options.GenerateExecutable = false;
587                                 options.GenerateInMemory = false;
588                                 options.TempFiles = new TempFileCollection (tempDir);
589
590                                 CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
591                                 ICodeCompiler compiler = codeProvider.CreateCompiler ();
592                                 CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
593
594                                 // verify compilation was successful
595                                 AssertCompileResults (results, true);
596
597                                 if (results.CompiledAssembly.Location.Length == 0)
598                                         throw new Exception ("Location should not be empty string");
599                                 if (results.PathToAssembly == null)
600                                         throw new Exception ("PathToAssembly should not be null");
601
602                                 return results.PathToAssembly;
603                         }
604
605                         public string CompileAssemblyFromDomBatch (string tempDir)
606                         {
607                                 CompilerParameters options = new CompilerParameters ();
608                                 options.GenerateExecutable = false;
609                                 options.GenerateInMemory = false;
610                                 options.TempFiles = new TempFileCollection (tempDir);
611
612                                 CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
613                                 ICodeCompiler compiler = codeProvider.CreateCompiler ();
614                                 CompilerResults results = compiler.CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
615
616                                 // verify compilation was successful
617                                 AssertCompileResults (results, true);
618
619                                 if (results.CompiledAssembly.Location.Length == 0)
620                                         throw new Exception ("Location should not be empty string");
621                                 if (results.PathToAssembly == null)
622                                         throw new Exception ("PathToAssembly should not be null");
623
624                                 return results.PathToAssembly;
625                         }
626                 }
627         }
628 }