Author: gert
Date: 2005-06-10 00:57:38 -0400 (Fri, 10 Jun 2005)
New Revision: 45732

Modified:
   trunk/mcs/class/System/Microsoft.VisualBasic/ChangeLog
   trunk/mcs/class/System/Microsoft.VisualBasic/VBCodeCompiler.cs
   trunk/mcs/class/System/Test/Microsoft.VisualBasic/ChangeLog
   trunk/mcs/class/System/Test/Microsoft.VisualBasic/VBCodeProviderTest.cs
Log:
* VBCodeCompiler.cs: 
  - ArgumentException was thrown when more than one file/source or 
    CodeCompileUnit is built.
  - Temporary files are not cleaned up after compilation.
* VBCodeProviderTest.cs:
  - Added batch of tests that check whether temporary files are cleaned up.
  - Compile multiple sources, CodeCompileUnits and files.
  - Use separate AppDomain for tests that actually load compiled assembly,
    to ensure no test output is left on the system.


Modified: trunk/mcs/class/System/Microsoft.VisualBasic/ChangeLog
===================================================================
--- trunk/mcs/class/System/Microsoft.VisualBasic/ChangeLog      2005-06-10 
04:52:21 UTC (rev 45731)
+++ trunk/mcs/class/System/Microsoft.VisualBasic/ChangeLog      2005-06-10 
04:57:38 UTC (rev 45732)
@@ -1,3 +1,9 @@
+2005-06-09  Gert Driesen <[EMAIL PROTECTED]>
+
+       * VBCodeCompiler.cs: ArgumentException was thrown when more than 
+       one file/source or CodeCompileUnit is built, temporary files were
+       not cleaned up. Fixes bug #75191.
+
 2005-05-29  Gert Driesen <[EMAIL PROTECTED]>
 
        * VBCodeCompiler.cs: Load compiled assembly from byte array if

Modified: trunk/mcs/class/System/Microsoft.VisualBasic/VBCodeCompiler.cs
===================================================================
--- trunk/mcs/class/System/Microsoft.VisualBasic/VBCodeCompiler.cs      
2005-06-10 04:52:21 UTC (rev 45731)
+++ trunk/mcs/class/System/Microsoft.VisualBasic/VBCodeCompiler.cs      
2005-06-10 04:57:38 UTC (rev 45732)
@@ -32,20 +32,20 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+using System;
+using System.CodeDom;
+using System.CodeDom.Compiler;
+using System.IO;
+using System.Text;
+using System.Reflection;
+using System.Collections;
+using System.Collections.Specialized;
+using System.Diagnostics;
+using System.Text.RegularExpressions;
+
 namespace Microsoft.VisualBasic
 {
-       using System;
-       using System.CodeDom;
-       using System.CodeDom.Compiler;
-       using System.IO;
-       using System.Text;
-       using System.Reflection;
-       using System.Collections;
-       using System.Collections.Specialized;
-       using System.Diagnostics;
-       using System.Text.RegularExpressions;
-
-       internal class VBCodeCompiler: VBCodeGenerator, ICodeCompiler
+       internal class VBCodeCompiler : VBCodeGenerator, ICodeCompiler
        {
                static string windowsMonoPath;
                static string windowsMbasPath;
@@ -54,7 +54,7 @@
                        if (Path.DirectorySeparatorChar == '\\') {
                                // FIXME: right now we use "fixed" version 1.0
                                // mcs at any time.
-                               PropertyInfo gac = typeof 
(Environment).GetProperty ("GacPath", 
BindingFlags.Static|BindingFlags.NonPublic);
+                               PropertyInfo gac = typeof 
(Environment).GetProperty ("GacPath", BindingFlags.Static | 
BindingFlags.NonPublic);
                                MethodInfo get_gac = gac.GetGetMethod (true);
                                string p = Path.GetDirectoryName (
                                        (string) get_gac.Invoke (null, null));
@@ -72,152 +72,68 @@
                        }
                }
 
-               //
-               // Constructors
-               //
-               public VBCodeCompiler()
+               public CompilerResults CompileAssemblyFromDom 
(CompilerParameters options, CodeCompileUnit e)
                {
+                       return CompileAssemblyFromDomBatch (options, new 
CodeCompileUnit[] { e });
                }
 
-               //
-               // Methods
-               //
-               [MonoTODO]
-               public CompilerResults CompileAssemblyFromDom 
(CompilerParameters options,CodeCompileUnit e)
+               public CompilerResults CompileAssemblyFromDomBatch 
(CompilerParameters options, CodeCompileUnit[] ea)
                {
-                       return CompileAssemblyFromDomBatch (options, new 
CodeCompileUnit []{e});
-               }
+                       if (options == null) {
+                               throw new ArgumentNullException ("options");
+                       }
 
-               public CompilerResults CompileAssemblyFromDomBatch 
(CompilerParameters options,
-                                                                   
CodeCompileUnit [] ea)
-               {
-                       string [] fileNames = new string [ea.Length];
-                       int i = 0;
-                       if (options == null)
-                       options = new CompilerParameters ();
-
-                       StringCollection assemblies = 
options.ReferencedAssemblies;
-
-                       foreach (CodeCompileUnit e in ea) {
-                               fileNames [i] = GetTempFileNameWithExtension 
(options.TempFiles, "vb");
-                               FileStream f = new FileStream (fileNames [i], 
FileMode.OpenOrCreate);
-                               StreamWriter s = new StreamWriter (f);
-                               if (e.ReferencedAssemblies != null) {
-                                       foreach (string str in 
e.ReferencedAssemblies) {
-                                               if (!assemblies.Contains (str))
-                                                       assemblies.Add (str);
-                                       }
-                               }
-
-                               
((ICodeGenerator)this).GenerateCodeFromCompileUnit (e, s, new 
CodeGeneratorOptions());
-                               s.Close();
-                               f.Close();
-                               i++;
+                       try {
+                               return CompileFromDomBatch (options, ea);
+                       } finally {
+                               options.TempFiles.Delete ();
                        }
-                       return CompileAssemblyFromFileBatch (options, 
fileNames);
                }
 
                public CompilerResults CompileAssemblyFromFile 
(CompilerParameters options, string fileName)
                {
-                       return CompileAssemblyFromFileBatch (options, new 
string []{fileName});
+                       return CompileAssemblyFromFileBatch (options, new 
string[] { fileName });
                }
 
-               public CompilerResults CompileAssemblyFromFileBatch 
(CompilerParameters options,
-                                                                    string [] 
fileNames)
+               public CompilerResults CompileAssemblyFromFileBatch 
(CompilerParameters options, string[] fileNames)
                {
-                       if (null == options)
+                       if (options == null) {
                                throw new ArgumentNullException ("options");
+                       }
 
-                       if (null == fileNames)
-                               throw new ArgumentNullException ("fileNames");
-
-                       CompilerResults results = new CompilerResults 
(options.TempFiles);
-                       Process mbas = new Process ();
-
-                       string mbas_output;
-                       string [] mbas_output_lines;
-                       // FIXME: these lines had better be platform 
independent.
-                       if (Path.DirectorySeparatorChar == '\\') {
-                               mbas.StartInfo.FileName = windowsMonoPath;
-                               mbas.StartInfo.Arguments = windowsMbasPath + ' 
' + BuildArgs (options, fileNames);
-                       }
-                       else {
-                               mbas.StartInfo.FileName = "mbas";
-                               mbas.StartInfo.Arguments = BuildArgs 
(options,fileNames);
-                       }
-                       mbas.StartInfo.CreateNoWindow = true;
-                       mbas.StartInfo.UseShellExecute = false;
-                       mbas.StartInfo.RedirectStandardOutput = true;
                        try {
-                               mbas.Start();
-                               mbas_output = mbas.StandardOutput.ReadToEnd ();
-                               mbas.WaitForExit();
+                               return CompileFromFileBatch (options, 
fileNames);
                        } finally {
-                               results.NativeCompilerReturnValue = 
mbas.ExitCode;
-                               mbas.Close ();
+                               options.TempFiles.Delete ();
                        }
-
-                       mbas_output_lines = 
mbas_output.Split(Environment.NewLine.ToCharArray());
-                       bool loadIt=true;
-                       foreach (string error_line in mbas_output_lines) {
-                               CompilerError error = CreateErrorFromString 
(error_line);
-                               if (null != error) {
-                                       results.Errors.Add (error);
-                                       if (!error.IsWarning)
-                                               loadIt = false;
-                               }
-                       }
-
-                       if (loadIt) {
-                               if (options.GenerateInMemory) {
-                                       using (FileStream fs = 
File.OpenRead(options.OutputAssembly)) {
-                                               byte[] buffer = new 
byte[fs.Length];
-                                               fs.Read(buffer, 0, 
buffer.Length);
-                                               results.CompiledAssembly = 
Assembly.Load(buffer, null, options.Evidence);
-                                               fs.Close();
-                                       }
-                               } else {
-                                       results.CompiledAssembly = 
Assembly.LoadFrom(options.OutputAssembly);
-                                       results.PathToAssembly = 
options.OutputAssembly;
-                               }
-                       } else {
-                               results.CompiledAssembly = null;
-                       }
-  
-                       return results;
                }
 
-               public CompilerResults CompileAssemblyFromSource 
(CompilerParameters options,
-                                                                 string source)
+               public CompilerResults CompileAssemblyFromSource 
(CompilerParameters options, string source)
                {
-                       return CompileAssemblyFromSourceBatch (options, new 
string [] {source});
+                       return CompileAssemblyFromSourceBatch (options, new 
string[] { source });
                }
 
-               public CompilerResults CompileAssemblyFromSourceBatch 
(CompilerParameters options,
-                                                                       string 
[] sources)
+               public CompilerResults CompileAssemblyFromSourceBatch 
(CompilerParameters options, string[] sources)
                {
-                       string [] fileNames = new string [sources.Length];
-                       int i = 0;
-                       foreach (string source in sources) {
-                               fileNames [i] = GetTempFileNameWithExtension 
(options.TempFiles, "vb");
-                               FileStream f = new FileStream (fileNames [i], 
FileMode.OpenOrCreate);
-                               StreamWriter s = new StreamWriter (f);
-                               s.Write (source);
-                               s.Close ();
-                               f.Close ();
-                               i++;
+                       if (options == null) {
+                               throw new ArgumentNullException ("options");
                        }
-                       return CompileAssemblyFromFileBatch(options,fileNames);
+
+                       try {
+                               return CompileFromSourceBatch (options, 
sources);
+                       } finally {
+                               options.TempFiles.Delete ();
+                       }
                }
 
-               static string BuildArgs (CompilerParameters options, string [] 
fileNames)
+               static string BuildArgs (CompilerParameters options, string[] 
fileNames)
                {
                        StringBuilder args = new StringBuilder ();
                        args.AppendFormat ("/quiet ");
                        if (options.GenerateExecutable)
-                               args.AppendFormat("/target:exe ");
+                               args.AppendFormat ("/target:exe ");
                        else
-                               args.AppendFormat("/target:library ");
+                               args.AppendFormat ("/target:library ");
 
                        /* Disabled. It causes problems now. -- Gonzalo
                        if (options.IncludeDebugInformation)
@@ -232,32 +148,30 @@
 
                        if (options.OutputAssembly == null) {
                                string ext = (options.GenerateExecutable ? 
"exe" : "dll");
-                               options.OutputAssembly = 
GetTempFileNameWithExtension (options.TempFiles, ext);
+                               options.OutputAssembly = 
GetTempFileNameWithExtension (options.TempFiles, ext, 
!options.GenerateInMemory);
                        }
 
                        args.AppendFormat ("/out:\"{0}\" ", 
options.OutputAssembly);
 
                        bool Reference2MSVBFound;
                        Reference2MSVBFound = false;
-                       if (null != options.ReferencedAssemblies) 
-                       {
-                               foreach (string import in 
options.ReferencedAssemblies)
-                               {
+                       if (null != options.ReferencedAssemblies) {
+                               foreach (string import in 
options.ReferencedAssemblies) {
                                        if (string.Compare (import, 
"Microsoft.VisualBasic", true, 
System.Globalization.CultureInfo.InvariantCulture) == 0)
                                                Reference2MSVBFound = true;
                                        args.AppendFormat ("/r:\"{0}\" ", 
import);
                                }
                        }
                        // add standard import to Microsoft.VisualBasic if 
missing
-                       if (Reference2MSVBFound == false)
+                       if (!Reference2MSVBFound)
                                args.AppendFormat ("/r:\"{0}\" ", 
"Microsoft.VisualBasic");
 
-                       args.AppendFormat(" -- "); // makes mbas not try to 
process filenames as options
+                       args.AppendFormat (" -- "); // makes mbas not try to 
process filenames as options
 
                        foreach (string source in fileNames)
-                               args.AppendFormat("\"{0}\" ",source);
+                               args.AppendFormat ("\"{0}\" ", source);
 
-                       return args.ToString();
+                       return args.ToString ();
                }
 
                static CompilerError CreateErrorFromString (string error_string)
@@ -275,16 +189,16 @@
                        if (!match.Success)
                                return null;
 
-                       if (String.Empty != match.Result("${file}"))
+                       if (String.Empty != match.Result ("${file}"))
                                error.FileName = match.Result ("${file}");
 
                        if (String.Empty != match.Result ("${line}"))
                                error.Line = Int32.Parse (match.Result 
("${line}"));
 
-                       if (String.Empty != match.Result( "${column}"))
+                       if (String.Empty != match.Result ("${column}"))
                                error.Column = Int32.Parse (match.Result 
("${column}"));
 
-                       if (match.Result ("${level}") =="warning")
+                       if (match.Result ("${level}") == "warning")
                                error.IsWarning = true;
 
                        error.ErrorNumber = match.Result ("${number}");
@@ -292,10 +206,136 @@
                        return error;
                }
 
-               static string GetTempFileNameWithExtension (TempFileCollection 
temp_files, string extension)
+               private static string GetTempFileNameWithExtension 
(TempFileCollection temp_files, string extension, bool keepFile)
                {
+                       return temp_files.AddExtension (extension, keepFile);
+               }
+
+               private static string GetTempFileNameWithExtension 
(TempFileCollection temp_files, string extension)
+               {
                        return temp_files.AddExtension (extension);
                }
+
+               private CompilerResults CompileFromFileBatch 
(CompilerParameters options, string[] fileNames)
+               {
+                       if (options == null) {
+                               throw new ArgumentNullException ("options");
+                       }
+
+                       if (fileNames == null) {
+                               throw new ArgumentNullException ("fileNames");
+                       }
+
+                       CompilerResults results = new CompilerResults 
(options.TempFiles);
+                       Process mbas = new Process ();
+
+                       string mbas_output;
+                       string[] mbas_output_lines;
+                       // FIXME: these lines had better be platform 
independent.
+                       if (Path.DirectorySeparatorChar == '\\') {
+                               mbas.StartInfo.FileName = windowsMonoPath;
+                               mbas.StartInfo.Arguments = windowsMbasPath + ' 
' + BuildArgs (options, fileNames);
+                       } else {
+                               mbas.StartInfo.FileName = "mbas";
+                               mbas.StartInfo.Arguments = BuildArgs (options, 
fileNames);
+                       }
+                       mbas.StartInfo.CreateNoWindow = true;
+                       mbas.StartInfo.UseShellExecute = false;
+                       mbas.StartInfo.RedirectStandardOutput = true;
+                       try {
+                               mbas.Start ();
+                               mbas_output = mbas.StandardOutput.ReadToEnd ();
+                               mbas.WaitForExit ();
+                       } finally {
+                               results.NativeCompilerReturnValue = 
mbas.ExitCode;
+                               mbas.Close ();
+                       }
+
+                       mbas_output_lines = mbas_output.Split 
(Environment.NewLine.ToCharArray ());
+                       bool loadIt = true;
+                       foreach (string error_line in mbas_output_lines) {
+                               CompilerError error = CreateErrorFromString 
(error_line);
+                               if (null != error) {
+                                       results.Errors.Add (error);
+                                       if (!error.IsWarning)
+                                               loadIt = false;
+                               }
+                       }
+
+                       if (loadIt) {
+                               if (options.GenerateInMemory) {
+                                       using (FileStream fs = File.OpenRead 
(options.OutputAssembly)) {
+                                               byte[] buffer = new 
byte[fs.Length];
+                                               fs.Read (buffer, 0, 
buffer.Length);
+                                               results.CompiledAssembly = 
Assembly.Load (buffer, null, options.Evidence);
+                                               fs.Close ();
+                                       }
+                               } else {
+                                       results.CompiledAssembly = 
Assembly.LoadFrom (options.OutputAssembly);
+                                       results.PathToAssembly = 
options.OutputAssembly;
+                               }
+                       } else {
+                               results.CompiledAssembly = null;
+                       }
+
+                       return results;
+               }
+
+               private CompilerResults CompileFromDomBatch (CompilerParameters 
options, CodeCompileUnit[] ea)
+               {
+                       if (options == null) {
+                               throw new ArgumentNullException ("options");
+                       }
+
+                       if (ea == null) {
+                               throw new ArgumentNullException ("ea");
+                       }
+
+                       string[] fileNames = new string[ea.Length];
+                       StringCollection assemblies = 
options.ReferencedAssemblies;
+
+                       for (int i = 0; i < ea.Length; i++) {
+                               CodeCompileUnit compileUnit = ea[i];
+                               fileNames[i] = GetTempFileNameWithExtension 
(options.TempFiles, i + "vb");
+                               FileStream f = new FileStream (fileNames[i], 
FileMode.OpenOrCreate);
+                               StreamWriter s = new StreamWriter (f);
+                               if (compileUnit.ReferencedAssemblies != null) {
+                                       foreach (string str in 
compileUnit.ReferencedAssemblies) {
+                                               if (!assemblies.Contains (str))
+                                                       assemblies.Add (str);
+                                       }
+                               }
+
+                               ((ICodeGenerator) 
this).GenerateCodeFromCompileUnit (compileUnit, s, new CodeGeneratorOptions ());
+                               s.Close ();
+                               f.Close ();
+                       }
+                       return CompileAssemblyFromFileBatch (options, 
fileNames);
+               }
+
+               private CompilerResults CompileFromSourceBatch 
(CompilerParameters options, string[] sources)
+               {
+                       if (options == null) {
+                               throw new ArgumentNullException ("options");
+                       }
+
+                       if (sources == null) {
+                               throw new ArgumentNullException ("sources");
+                       }
+
+                       string[] fileNames = new string[sources.Length];
+
+                       for (int i = 0; i < sources.Length; i++) {
+                               fileNames[i] = GetTempFileNameWithExtension 
(options.TempFiles, i + "vb");
+                               FileStream f = new FileStream (fileNames[i], 
FileMode.OpenOrCreate);
+                               using (StreamWriter s = new StreamWriter (f)) {
+                                       s.Write (sources[i]);
+                                       s.Close ();
+                               }
+                               f.Close ();
+                       }
+                       return CompileFromFileBatch (options, fileNames);
+               }
        }
 }
 

Modified: trunk/mcs/class/System/Test/Microsoft.VisualBasic/ChangeLog
===================================================================
--- trunk/mcs/class/System/Test/Microsoft.VisualBasic/ChangeLog 2005-06-10 
04:52:21 UTC (rev 45731)
+++ trunk/mcs/class/System/Test/Microsoft.VisualBasic/ChangeLog 2005-06-10 
04:57:38 UTC (rev 45732)
@@ -1,3 +1,10 @@
+2005-06-09  Gert Driesen <[EMAIL PROTECTED]>
+
+       * VBCodeProviderTest.cs: added batch of tests that check whether
+       temporary files are cleaned up, compile multiple sources/compile units
+       and files. Use separate AppDomain for tests that actually loaded
+       the compiled assembly, to ensure no test output is left on the system.
+
 2005-06-05  Gert Driesen <[EMAIL PROTECTED]>
 
        * VBCodeProviderTest.cs: moved to using NUnit 2.x Assert, improve

Modified: 
trunk/mcs/class/System/Test/Microsoft.VisualBasic/VBCodeProviderTest.cs
===================================================================
--- trunk/mcs/class/System/Test/Microsoft.VisualBasic/VBCodeProviderTest.cs     
2005-06-10 04:52:21 UTC (rev 45731)
+++ trunk/mcs/class/System/Test/Microsoft.VisualBasic/VBCodeProviderTest.cs     
2005-06-10 04:57:38 UTC (rev 45732)
@@ -2,7 +2,7 @@
 // Microsoft.VisualBasic.VBCodeProvider.cs
 //
 // Author:
-//   Jochen Wezel ([EMAIL PROTECTED]) //
+//   Jochen Wezel ([EMAIL PROTECTED])
 //
 // (C) 2003 Jochen Wezel (CompuMaster GmbH)
 //
@@ -10,35 +10,43 @@
 // 2003-12-10 JW: publishing of this file
 //
 
-using NUnit.Framework;
 using System;
-using Microsoft.VisualBasic;
 using System.CodeDom;
 using System.CodeDom.Compiler;
 using System.ComponentModel;
 using System.Collections.Specialized;
+using System.Globalization;
 using System.Reflection;
 using System.Diagnostics;
 using System.IO;
+using Microsoft.VisualBasic;
+using NUnit.Framework;
 
 namespace MonoTests.Microsoft.VisualBasic
 {
-
-       enum OsType {
+       enum OsType
+       {
                Windows,
                Unix,
                Mac
        }
 
        [TestFixture]
-       public class VBCodeProviderTest {
-       
-               CodeDomProvider MyVBCodeProvider;
-               static OsType OS;
-               static char DSC = Path.DirectorySeparatorChar;
+       public class VBCodeProviderTest
+       {
+               private string _tempDir;
+               private CodeDomProvider _codeProvider;
+               private static OsType OS;
+               private static char DSC = Path.DirectorySeparatorChar;
 
+               private static readonly string _sourceTest1 = "Public Class 
Test1" +
+                       Environment.NewLine + "End Class";
+               private static readonly string _sourceTest2 = "Public Class 
Test2" +
+                       Environment.NewLine + "End Class";
+
                [SetUp]
-               public void GetReady() { 
+               public void GetReady ()
+               {
                        if ('/' == DSC) {
                                OS = OsType.Unix;
                        } else if ('\\' == DSC) {
@@ -47,124 +55,367 @@
                                OS = OsType.Mac;
                        }
 
-                       MyVBCodeProvider = new VBCodeProvider(); 
+                       _codeProvider = new VBCodeProvider ();
+                       _tempDir = CreateTempDirectory ();
                }
 
+               [TearDown]
+               public void TearDown ()
+               {
+                       RemoveDirectory (_tempDir);
+               }
+
                [Test]
                public void FileExtension ()
                {
-                       Assert.AreEqual("vb", MyVBCodeProvider.FileExtension, 
"#JW10");
+                       Assert.AreEqual ("vb", _codeProvider.FileExtension, 
"#JW10");
                }
 
                [Test]
                public void LanguageOptionsTest ()
                {
-                       Assert.AreEqual(LanguageOptions.CaseInsensitive, 
MyVBCodeProvider.LanguageOptions, "#JW20");
+                       Assert.AreEqual (LanguageOptions.CaseInsensitive, 
_codeProvider.LanguageOptions, "#JW20");
                }
 
                [Test]
-               public void CreateCompiler()
+               public void CreateCompiler ()
                {
-                       // prepare the compilation
-                       ICodeCompiler MyVBCodeCompiler = 
MyVBCodeProvider.CreateCompiler();
-                       Assert.IsNotNull(MyVBCodeCompiler, "#JW30 - 
CreateCompiler");
-                       
-                       CompilerParameters options = new CompilerParameters();
+                       // Prepare the compilation
+                       ICodeCompiler codeCompiler = 
_codeProvider.CreateCompiler ();
+                       Assert.IsNotNull (codeCompiler, "#JW30 - 
CreateCompiler");
+
+                       CompilerParameters options = new CompilerParameters ();
                        options.GenerateExecutable = true;
                        options.IncludeDebugInformation = true;
                        options.TreatWarningsAsErrors = true;
-                       
+
                        // process compilation
-                       CompilerResults MyVBCodeCompilerResults = 
MyVBCodeCompiler.CompileAssemblyFromSource(options,
-                               "public class TestModule" + Environment.NewLine 
+ "public shared sub Main()" 
-                               + Environment.NewLine + 
"System.Console.Write(\"Hello world!\")" 
+                       CompilerResults compilerResults = 
codeCompiler.CompileAssemblyFromSource (options,
+                               "public class TestModule" + Environment.NewLine 
+ "public shared sub Main()"
+                               + Environment.NewLine + 
"System.Console.Write(\"Hello world!\")"
                                + Environment.NewLine + "End Sub" + 
Environment.NewLine + "End Class");
 
-                       // verify outcome of compilation
-                       if (MyVBCodeCompilerResults.Errors.Count > 0) {
-                               Assert.Fail("Hello World compilation failed: " 
+ MyVBCodeCompilerResults.Errors[0].ToString());
+                       // Analyse the compilation success/messages
+                       StringCollection MyOutput = compilerResults.Output;
+                       string MyOutStr = "";
+                       foreach (string MyStr in MyOutput) {
+                               MyOutStr += MyStr + Environment.NewLine + 
Environment.NewLine;
                        }
 
-                       try
-                       {
-                               Assembly MyAss = 
MyVBCodeCompilerResults.CompiledAssembly;
+                       if (compilerResults.Errors.Count != 0) {
+                               Assert.Fail ("#JW31 - Hello world compilation: 
" + MyOutStr);
                        }
-                       catch (Exception ex)
-                       {
-                               Assert.Fail("#JW32 - 
MyVBCodeCompilerResults.CompiledAssembly hasn't been an expected object" + 
+
+                       try {
+                               Assembly MyAss = 
compilerResults.CompiledAssembly;
+                       } catch (Exception ex) {
+                               Assert.Fail ("#JW32 - 
compilerResults.CompiledAssembly hasn't been an expected object" +
                                                Environment.NewLine + 
ex.Message + Environment.NewLine + ex.StackTrace);
                        }
 
                        // Execute the test app
-                       ProcessStartInfo NewProcInfo = new ProcessStartInfo();
+                       ProcessStartInfo NewProcInfo = new ProcessStartInfo ();
                        if (Windows) {
-                               NewProcInfo.FileName = 
MyVBCodeCompilerResults.CompiledAssembly.Location;
-                       }
-                       else {
+                               NewProcInfo.FileName = 
compilerResults.CompiledAssembly.Location;
+                       } else {
                                NewProcInfo.FileName = "mono";
-                               NewProcInfo.Arguments = 
MyVBCodeCompilerResults.CompiledAssembly.Location;
+                               NewProcInfo.Arguments = 
compilerResults.CompiledAssembly.Location;
                        }
                        NewProcInfo.RedirectStandardOutput = true;
                        NewProcInfo.UseShellExecute = false;
                        NewProcInfo.CreateNoWindow = true;
                        string TestAppOutput = "";
-
-                       try
-                       {
-                               Process MyProc = Process.Start(NewProcInfo);
-                               MyProc.WaitForExit();
-                               TestAppOutput = 
MyProc.StandardOutput.ReadToEnd();
-                               MyProc.Close();
-                               MyProc.Dispose();
+                       try {
+                               Process MyProc = Process.Start (NewProcInfo);
+                               MyProc.WaitForExit ();
+                               TestAppOutput = MyProc.StandardOutput.ReadToEnd 
();
+                               MyProc.Close ();
+                               MyProc.Dispose ();
+                       } catch (Exception ex) {
+                               Assert.Fail ("#JW34 - " + ex.Message + 
Environment.NewLine + ex.StackTrace);
                        }
-                       catch (Exception ex)
-                       {
-                               Assert.Fail("#JW34 - " + ex.Message + 
Environment.NewLine + ex.StackTrace);
-                       }
-                       
-                       Assert.AreEqual("Hello world!", TestAppOutput, "#JW33 - 
Application output");
+                       Assert.AreEqual ("Hello world!", TestAppOutput, "#JW33 
- Application output");
 
                        // Clean up
-                       try
-                       {
+                       try {
                                File.Delete (NewProcInfo.FileName);
+                       } catch { }
+               }
+
+               [Test]
+               public void CreateGenerator ()
+               {
+                       ICodeGenerator MyVBCodeGen;
+                       MyVBCodeGen = _codeProvider.CreateGenerator ();
+                       Assert.IsNotNull (MyVBCodeGen, "#JW40 - 
CreateGenerator");
+                       Assert.IsTrue (MyVBCodeGen.Supports 
(GeneratorSupport.DeclareEnums), "#JW41");
+               }
+
+               [Test]
+               public void CompileFromFile_InMemory ()
+               {
+                       // create vb source file
+                       string sourceFile = Path.Combine (_tempDir, "file." + 
_codeProvider.FileExtension);
+                       using (FileStream f = new FileStream (sourceFile, 
FileMode.Create)) {
+                               using (StreamWriter s = new StreamWriter (f)) {
+                                       s.Write (_sourceTest1);
+                                       s.Close ();
+                               }
+                               f.Close ();
                        }
-                       catch {}
+
+                       CompilerParameters options = new CompilerParameters ();
+                       options.GenerateExecutable = false;
+                       options.GenerateInMemory = true;
+                       options.TempFiles = new TempFileCollection (_tempDir);
+
+                       ICodeCompiler compiler = _codeProvider.CreateCompiler 
();
+                       CompilerResults results = 
compiler.CompileAssemblyFromFile (options,
+                               sourceFile);
+
+                       Assert.AreEqual (string.Empty, 
results.CompiledAssembly.Location, "#1");
+                       Assert.IsNull (results.PathToAssembly, "#2");
+                       Assert.IsNotNull (results.CompiledAssembly.GetType 
("Test1"), "#3");
+
+                       // verify we don't cleanup files in temp directory too 
agressively
+                       string[] tempFiles = Directory.GetFiles (_tempDir);
+
+                       foreach (string file in tempFiles) {
+                               if (file != sourceFile) {
+                                       Assert.Fail ("TEMPFILE: " + file);
+                               }
+                       }
+
+                       Assert.AreEqual (1, tempFiles.Length, "#4");
+                       Assert.AreEqual (sourceFile, tempFiles[0], "#5");
+
                }
 
-               // NOTE: This test does not clean-up the generated assemblies
                [Test]
-               public void CompileAssembly_InMemory ()
+               public void CompileFromFileBatch_InMemory ()
                {
-                       // NOT in memory
-                       CompilerResults results = CompileAssembly (false);
-                       Assert.IsTrue(results.CompiledAssembly.Location.Length 
!= 0, "#1");
-                       Assert.IsNotNull(results.PathToAssembly, "#2");
+                       // create vb source file
+                       string sourceFile1 = Path.Combine (_tempDir, "file." + 
_codeProvider.FileExtension);
+                       using (FileStream f = new FileStream (sourceFile1, 
FileMode.Create)) {
+                               using (StreamWriter s = new StreamWriter (f)) {
+                                       s.Write (_sourceTest1);
+                                       s.Close ();
+                               }
+                               f.Close ();
+                       }
 
-                       // in memory
-                       results = CompileAssembly (true);
-                       Assert.AreEqual(string.Empty, 
results.CompiledAssembly.Location, "#3");
-                       Assert.IsNull(results.PathToAssembly, "#4");
+                       string sourceFile2 = Path.Combine (_tempDir, "file2." + 
_codeProvider.FileExtension);
+                       using (FileStream f = new FileStream (sourceFile2, 
FileMode.Create)) {
+                               using (StreamWriter s = new StreamWriter (f)) {
+                                       s.Write (_sourceTest2);
+                                       s.Close ();
+                               }
+                               f.Close ();
+                       }
+
+                       CompilerParameters options = new CompilerParameters ();
+                       options.GenerateExecutable = false;
+                       options.GenerateInMemory = true;
+                       options.TempFiles = new TempFileCollection (_tempDir);
+
+                       ICodeCompiler compiler = _codeProvider.CreateCompiler 
();
+                       CompilerResults results = 
compiler.CompileAssemblyFromFileBatch (options,
+                               new string[] { sourceFile1, sourceFile2 });
+
+                       Assert.AreEqual (string.Empty, 
results.CompiledAssembly.Location, "#1");
+                       Assert.IsNull (results.PathToAssembly, "#2");
+
+                       Assert.IsNotNull (results.CompiledAssembly.GetType 
("Test1"), "#3");
+                       Assert.IsNotNull (results.CompiledAssembly.GetType 
("Test2"), "#4");
+
+                       // verify we don't cleanup files in temp directory too 
agressively
+                       string[] tempFiles = Directory.GetFiles (_tempDir);
+                       Assert.AreEqual (2, tempFiles.Length, "#5");
+                       Assert.IsTrue (File.Exists (sourceFile1), "#6");
+                       Assert.IsTrue (File.Exists (sourceFile2), "#7");
                }
 
                [Test]
-               public void CreateGenerator()
+               public void CompileFromSource_InMemory ()
                {
-                       ICodeGenerator MyVBCodeGen;
-                       MyVBCodeGen = MyVBCodeProvider.CreateGenerator();
-                       Assert.IsNotNull(MyVBCodeGen, "#JW40 - 
CreateGenerator");
-                       
Assert.IsTrue(MyVBCodeGen.Supports(GeneratorSupport.DeclareEnums), "#JW41");
+                       // create a file in temp directory to ensure that 
compiler is not removing
+                       // too much (temporary) files
+                       string tempFile = Path.Combine (_tempDir, "file." + 
_codeProvider.FileExtension);
+                       using (FileStream fs = File.Create (tempFile)) {
+                               fs.Close ();
+                       }
+
+                       CompilerParameters options = new CompilerParameters ();
+                       options.GenerateExecutable = false;
+                       options.GenerateInMemory = true;
+                       options.TempFiles = new TempFileCollection (_tempDir);
+
+                       ICodeCompiler compiler = _codeProvider.CreateCompiler 
();
+                       CompilerResults results = 
compiler.CompileAssemblyFromSource (options,
+                               _sourceTest1);
+
+                       Assert.AreEqual (string.Empty, 
results.CompiledAssembly.Location, "#1");
+                       Assert.IsNull (results.PathToAssembly, "#2");
+                       Assert.IsNotNull (results.CompiledAssembly.GetType 
("Test1"), "#3");
+
+                       // verify we don't cleanup files in temp directory too 
agressively
+                       string[] tempFiles = Directory.GetFiles (_tempDir);
+                       Assert.AreEqual (1, tempFiles.Length, "#4");
+                       Assert.AreEqual (tempFile, tempFiles[0], "#5");
                }
 
-               
+               [Test]
+               public void CompileFromSourceBatch_InMemory ()
+               {
+                       // create a file in temp directory to ensure that 
compiler is not removing
+                       // too much (temporary) files
+                       string tempFile = Path.Combine (_tempDir, "file." + 
_codeProvider.FileExtension);
+                       using (FileStream fs = File.Create (tempFile)) {
+                               fs.Close ();
+                       }
+
+                       CompilerParameters options = new CompilerParameters ();
+                       options.GenerateExecutable = false;
+                       options.GenerateInMemory = true;
+                       options.TempFiles = new TempFileCollection (_tempDir);
+
+                       ICodeCompiler compiler = _codeProvider.CreateCompiler 
();
+                       CompilerResults results = 
compiler.CompileAssemblyFromSourceBatch (options,
+                               new string[] { _sourceTest1, _sourceTest2 });
+
+                       Assert.AreEqual (string.Empty, 
results.CompiledAssembly.Location, "#1");
+                       Assert.IsNull (results.PathToAssembly, "#2");
+
+                       Assert.IsNotNull (results.CompiledAssembly.GetType 
("Test1"), "#3");
+                       Assert.IsNotNull (results.CompiledAssembly.GetType 
("Test2"), "#4");
+
+                       // verify we don't cleanup files in temp directory too 
agressively
+                       string[] tempFiles = Directory.GetFiles (_tempDir);
+                       Assert.AreEqual (1, tempFiles.Length, "#5");
+                       Assert.AreEqual (tempFile, tempFiles[0], "#6");
+               }
+
+               [Test]
+               public void CompileFromDom_NotInMemory ()
+               {
+                       // create a file in temp directory to ensure that 
compiler is not removing
+                       // too much (temporary) files
+                       string tempFile = Path.Combine (_tempDir, "file." + 
_codeProvider.FileExtension);
+                       using (FileStream fs = File.Create (tempFile)) {
+                               fs.Close ();
+                       }
+
+                       // compile and verify result in separate appdomain to 
avoid file locks
+                       AppDomain testDomain = CreateTestDomain ();
+                       CrossDomainTester compileTester = 
CreateCrossDomainTester (testDomain);
+
+                       string outputAssembly = null;
+
+                       try {
+                               outputAssembly = 
compileTester.CompileAssemblyFromDom (_tempDir);
+                       } finally {
+                               AppDomain.Unload (testDomain);
+                       }
+
+                       // there should be two files in temp dir: temp file and 
output assembly
+                       string[] tempFiles = Directory.GetFiles (_tempDir);
+                       Assert.AreEqual (2, tempFiles.Length, "#1");
+                       Assert.IsTrue (File.Exists (outputAssembly), "#2");
+                       Assert.IsTrue (File.Exists (tempFile), "#3");
+               }
+
+               [Test]
+               public void CompileFromDomBatch_NotInMemory ()
+               {
+                       // create a file in temp directory to ensure that 
compiler is not removing
+                       // too much (temporary) files
+                       string tempFile = Path.Combine (_tempDir, "file." + 
_codeProvider.FileExtension);
+                       using (FileStream fs = File.Create (tempFile)) {
+                               fs.Close ();
+                       }
+
+                       // compile and verify result in separate appdomain to 
avoid file locks
+                       AppDomain testDomain = CreateTestDomain ();
+                       CrossDomainTester compileTester = 
CreateCrossDomainTester (testDomain);
+
+                       string outputAssembly = null;
+
+                       try {
+                               outputAssembly = 
compileTester.CompileAssemblyFromDomBatch (_tempDir);
+                       } finally {
+                               AppDomain.Unload (testDomain);
+                       }
+
+                       // there should be two files in temp dir: temp file and 
output assembly
+                       string[] tempFiles = Directory.GetFiles (_tempDir);
+                       Assert.AreEqual (2, tempFiles.Length, "#1");
+                       Assert.IsTrue (File.Exists (outputAssembly), "#2");
+                       Assert.IsTrue (File.Exists (tempFile), "#3");
+               }
+
+               [Test]
+               public void CompileFromDom_InMemory ()
+               {
+                       // create a file in temp directory to ensure that 
compiler is not removing
+                       // too much (temporary) files
+                       string tempFile = Path.Combine (_tempDir, "file." + 
_codeProvider.FileExtension);
+                       using (FileStream fs = File.Create (tempFile)) {
+                               fs.Close ();
+                       }
+
+                       CompilerParameters options = new CompilerParameters ();
+                       options.GenerateExecutable = false;
+                       options.GenerateInMemory = true;
+                       options.TempFiles = new TempFileCollection (_tempDir);
+
+                       ICodeCompiler compiler = _codeProvider.CreateCompiler 
();
+                       CompilerResults results = 
compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
+
+                       Assert.AreEqual (string.Empty, 
results.CompiledAssembly.Location, "#1");
+                       Assert.IsNull (results.PathToAssembly, "#2");
+
+                       // verify we don't cleanup files in temp directory too 
agressively
+                       string[] tempFiles = Directory.GetFiles (_tempDir);
+                       Assert.AreEqual (1, tempFiles.Length, "#3");
+                       Assert.AreEqual (tempFile, tempFiles[0], "#4");
+               }
+
+               [Test]
+               public void CompileFromDomBatch_InMemory ()
+               {
+                       // create a file in temp directory to ensure that 
compiler is not removing
+                       // too much (temporary) files
+                       string tempFile = Path.Combine (_tempDir, "file." + 
_codeProvider.FileExtension);
+                       using (FileStream fs = File.Create (tempFile)) {
+                               fs.Close ();
+                       }
+
+                       CompilerParameters options = new CompilerParameters ();
+                       options.GenerateExecutable = false;
+                       options.GenerateInMemory = true;
+                       options.TempFiles = new TempFileCollection (_tempDir);
+
+                       ICodeCompiler compiler = _codeProvider.CreateCompiler 
();
+                       CompilerResults results = 
compiler.CompileAssemblyFromDomBatch (options,
+                               new CodeCompileUnit[] { new CodeCompileUnit (), 
new CodeCompileUnit () });
+
+                       Assert.AreEqual (string.Empty, 
results.CompiledAssembly.Location, "#1");
+                       Assert.IsNull (results.PathToAssembly, "#2");
+
+                       // verify we don't cleanup files in temp directory too 
agressively
+                       string[] tempFiles = Directory.GetFiles (_tempDir);
+                       Assert.AreEqual (1, tempFiles.Length, "#3");
+                       Assert.AreEqual (tempFile, tempFiles[0], "#4");
+               }
+
                //TODO: [Test]
-               public void     CreateParser()
+               public void CreateParser ()
                {
                        //System.CodeDom.Compiler.ICodeParser CreateParser()
                }
 
                //TODO: [Test]
-               public void     CreateObjRef()
+               public void CreateObjRef ()
                {
                        //System.Runtime.Remoting.ObjRef 
CreateObjRef(System.Type requestedType)
                }
@@ -190,15 +441,90 @@
                        }
                }
 
-               private CompilerResults CompileAssembly(bool inMemory) {
-                       CompilerParameters options = new CompilerParameters();
-                       options.GenerateExecutable = false;
-                       options.GenerateInMemory = inMemory;
+               private static string CreateTempDirectory ()
+               {
+                       // create a uniquely named zero-byte file
+                       string tempFile = Path.GetTempFileName ();
+                       // remove the temporary file
+                       File.Delete (tempFile);
+                       // create a directory named after the unique temporary 
file
+                       Directory.CreateDirectory (tempFile);
+                       // return the path to the temporary directory
+                       return tempFile;
+               }
 
-                       VBCodeProvider codeProvider = new VBCodeProvider();
-                       ICodeCompiler compiler = codeProvider.CreateCompiler();
-                       return compiler.CompileAssemblyFromDom(options,
-                                                               new 
CodeCompileUnit());
+               private static void RemoveDirectory (string path)
+               {
+                       try {
+                               if (Directory.Exists (path)) {
+                                       string[] directoryNames = 
Directory.GetDirectories (path);
+                                       foreach (string directoryName in 
directoryNames) {
+                                               RemoveDirectory (directoryName);
+                                       }
+                                       string[] fileNames = Directory.GetFiles 
(path);
+                                       foreach (string fileName in fileNames) {
+                                               File.Delete (fileName);
+                                       }
+                                       Directory.Delete (path, true);
+                               }
+                       } catch (Exception ex) {
+                               throw new AssertionException ("Unable to 
cleanup '" + path + "'.", ex);
+                       }
                }
+
+               private static AppDomain CreateTestDomain ()
+               {
+                       return AppDomain.CreateDomain ("CompileFromDom", 
AppDomain.CurrentDomain.Evidence,
+                               AppDomain.CurrentDomain.SetupInformation);
+               }
+
+               private static CrossDomainTester CreateCrossDomainTester 
(AppDomain domain)
+               {
+                       Type testerType = typeof (CrossDomainTester);
+
+                       return (CrossDomainTester) 
domain.CreateInstanceAndUnwrap (
+                               testerType.Assembly.FullName, 
testerType.FullName, false,
+                               BindingFlags.Public | BindingFlags.Instance, 
null, new object[0],
+                               CultureInfo.InvariantCulture, new object[0], 
domain.Evidence);
+               }
+
+               private class CrossDomainTester : MarshalByRefObject
+               {
+                       public string CompileAssemblyFromDom (string tempDir)
+                       {
+                               CompilerParameters options = new 
CompilerParameters ();
+                               options.GenerateExecutable = false;
+                               options.GenerateInMemory = false;
+                               options.TempFiles = new TempFileCollection 
(tempDir);
+
+                               VBCodeProvider codeProvider = new 
VBCodeProvider ();
+                               ICodeCompiler compiler = 
codeProvider.CreateCompiler ();
+                               CompilerResults results = 
compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
+
+                               Assert.IsTrue 
(results.CompiledAssembly.Location.Length != 0,
+                                       "Location should not be empty string");
+                               Assert.IsNotNull (results.PathToAssembly, 
"PathToAssembly should not be null");
+
+                               return results.PathToAssembly;
+                       }
+
+                       public string CompileAssemblyFromDomBatch (string 
tempDir)
+                       {
+                               CompilerParameters options = new 
CompilerParameters ();
+                               options.GenerateExecutable = false;
+                               options.GenerateInMemory = false;
+                               options.TempFiles = new TempFileCollection 
(tempDir);
+
+                               VBCodeProvider codeProvider = new 
VBCodeProvider ();
+                               ICodeCompiler compiler = 
codeProvider.CreateCompiler ();
+                               CompilerResults results = 
compiler.CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { new 
CodeCompileUnit (), new CodeCompileUnit () });
+
+                               Assert.IsTrue 
(results.CompiledAssembly.Location.Length != 0,
+                                       "Location should not be empty string");
+                               Assert.IsNotNull (results.PathToAssembly, 
"PathToAssembly should not be null");
+
+                               return results.PathToAssembly;
+                       }
+               }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to