msbuild: test fixes. Implemented GlobalEngine and project unloading
authorCrestez Dan Leonard <cdleonard@mono-cvs.ximian.com>
Tue, 28 Mar 2006 22:32:30 +0000 (22:32 -0000)
committerCrestez Dan Leonard <cdleonard@mono-cvs.ximian.com>
Tue, 28 Mar 2006 22:32:30 +0000 (22:32 -0000)
svn path=/trunk/mcs/; revision=58682

mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/ChangeLog
mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Engine.cs
mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/InternalLoggerException.cs
mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/InvalidProjectFileException.cs
mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Project.cs
mcs/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ChangeLog
mcs/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/EngineTest.cs
mcs/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/ProjectTest.cs

index 53157974917c9b6d8543f7a32207965de6aa3fcc..ff4f500aa7e2939cdf8376cd2109e8434ad0d080 100644 (file)
@@ -1,3 +1,10 @@
+2006-03-29  Crestez Leonard  <cdleonard@gmail.com>
+       
+       * InternalLoggerException.cs, InvalidProjectFileException.cs: Fixed
+       to pass tests. Wrote serialization constructor.
+       * Engine.cs, Project.cs: Global engine and project unloading, test and
+       formatting fixes.
+
 2006-03-28  Marek Sieradzki  <marek.sieradzki@gmail.com>
 
        * UsingTask.cs: Formatting changes.
index d3aadcc5c43de3bb761b34f5f9b367b07e5be9c4..715ca07258d3e6170402b3794824281b9510ad25 100644 (file)
@@ -44,7 +44,6 @@ namespace Microsoft.Build.BuildEngine {
                bool                    onlyLogCriticalEvents;
                IDictionary             projects;
 
-               // FIXME
                static Engine           globalEngine;
                static Version          version;
 
@@ -173,34 +172,70 @@ namespace Microsoft.Build.BuildEngine {
                        return result;
                }
 
+               internal void CheckBinPath ()
+               {
+                       if (BinPath == null) {
+                               throw new InvalidOperationException("Before a project can be instantiated, " +
+                                       "Engine.BinPath must be set to the location on disk where MSBuild " + 
+                                       "is installed. This is used to evaluate $(MSBuildBinPath).");
+                       }
+               }
+
                public Project CreateNewProject ()
                {
+                       CheckBinPath ();
                        return new Project (this);
                }
 
                public Project GetLoadedProject (string projectFullFileName)
                {
+                       if (projectFullFileName == null) {
+                               throw new ArgumentNullException ("projectFullFileName");
+                       }
                        return (Project) projects [projectFullFileName];
                }
 
-               public void RegisterLogger (ILogger logger)
+               internal void RemoveLoadedProject (Project p)
                {
-                       if (logger == null)
-                               throw new ArgumentNullException ("logger");
-                       logger.Initialize (eventSource);
-                       loggers.Add (logger);
+                       if (p.FullFileName != "") {
+                               projects.Remove (p.FullFileName);
+                       }
                }
-               
-               [MonoTODO]
-               public void UnloadAllProjects ()
+
+               internal void AddLoadedProject (Project p)
                {
+                       if (p.FullFileName != "") {
+                               projects.Add (p.FullFileName, p);
+                       }
                }
-               
-               [MonoTODO]
+       
                public void UnloadProject (Project project)
                {
+                       if (project.ParentEngine != this) {
+                               throw new InvalidOperationException("This project is not loaded in this engine");
+                       }
+                       project.CheckUnloaded ();
+                       if (project.FullFileName != "") {
+                               projects.Remove (project.FullFileName);
+                       }
+                       project.Unload ();
                }
 
+               public void UnloadAllProjects ()
+               {
+                       foreach (DictionaryEntry e in projects) {
+                               UnloadProject ((Project) e.Value);
+                       }
+               }
+
+               public void RegisterLogger (ILogger logger)
+               {
+                       if (logger == null)
+                               throw new ArgumentNullException ("logger");
+                       logger.Initialize (eventSource);
+                       loggers.Add (logger);
+               }
+               
                public void UnregisterAllLoggers ()
                {
                        // FIXME: check if build succeeded
@@ -262,7 +297,12 @@ namespace Microsoft.Build.BuildEngine {
                }
 
                public static Engine GlobalEngine {
-                       get { return globalEngine; }
+                       get {
+                               if (globalEngine == null) {
+                                       globalEngine = new Engine ();
+                               }
+                               return globalEngine;
+                       }
                }
 
                public BuildPropertyGroup GlobalProperties {
index bd3e3fe6b440b385f8209d68f930d534a5a464d2..d12cc389573875985b74333e09f5b06ffc405c91 100644 (file)
@@ -40,21 +40,31 @@ namespace Microsoft.Build.BuildEngine {
                string          helpKeyword;
                
                public InternalLoggerException ()
-                       : base ("Internal logger exception has occured.")
                {
+                       throw new System.InvalidOperationException(
+                               "An InternalLoggerException can only be thrown by the MSBuild engine. " +
+                               "The public constructors of this class cannot be used to create an " +
+                               "instance of the exception.");
                }
 
                public InternalLoggerException (string message)
-                       : base (message)
+                       : this ()
                {
                }
 
-               public InternalLoggerException (string message,
-                                               Exception innerException)
-                       : base (message, innerException)
+               public InternalLoggerException (string message, Exception innerException)
+                       : this ()
                {
                }
 
+               protected InternalLoggerException (SerializationInfo info, StreamingContext context)
+                       : base (info, context)
+               {
+                       buildEventArgs = (BuildEventArgs) info.GetValue("BuildEventArgs", typeof(BuildEventArgs));
+                       errorCode = info.GetString("ErrorCode");
+                       helpKeyword = info.GetString("HelpKeywordPrefix");
+               }
+
                public override void GetObjectData (SerializationInfo info,
                                                    StreamingContext context)
                {
@@ -84,4 +94,4 @@ namespace Microsoft.Build.BuildEngine {
        }
 }
 
-#endif
\ No newline at end of file
+#endif
index 96277a75f10768d8b4c1ad817c26ff6896f030af..ad9e6127fa7047d8d9251985fc72303f5e73e75b 100644 (file)
@@ -35,7 +35,6 @@ namespace Microsoft.Build.BuildEngine {
        [Serializable]
        public sealed class InvalidProjectFileException : Exception {
                
-               string  baseMessage;
                int     columnNumber;
                int     endColumnNumber;
                string  errorCode;
@@ -43,19 +42,16 @@ namespace Microsoft.Build.BuildEngine {
                string  helpKeyword;
                int     lineNumber;
                int     endLineNumber;
-               string  message;
                string  projectFile;
                
                public InvalidProjectFileException ()
                        : base ("Invalid project file exception has occured")
                {
-                       this.message = "Invalid project file exception has occured";
                }
 
                public InvalidProjectFileException (string message)
                        : base (message)
                {
-                       this.message = message;
                }
 
                public InvalidProjectFileException (string projectFile,
@@ -74,7 +70,6 @@ namespace Microsoft.Build.BuildEngine {
                        this.columnNumber = columnNumber;
                        this.endLineNumber = endLineNumber;
                        this.endColumnNumber = endColumnNumber;
-                       this.message = message;
                        this.errorSubcategory = errorSubcategory;
                        this.errorCode = errorCode;
                        this.helpKeyword = helpKeyword;
@@ -86,6 +81,8 @@ namespace Microsoft.Build.BuildEngine {
                {
                }
 
+               // FIXME: set line/column numbers?
+               [MonoTODO]
                public InvalidProjectFileException (XmlNode xmlNode,
                                                    string message,
                                                    string errorSubcategory,
@@ -93,17 +90,28 @@ namespace Microsoft.Build.BuildEngine {
                                                    string helpKeyword)
                        : base (message)
                {
-                       this.message = message;
                        this.errorSubcategory = errorSubcategory;
                        this.errorCode = errorCode;
                        this.helpKeyword = helpKeyword;
                }
 
+               protected InvalidProjectFileException (SerializationInfo info, StreamingContext context)
+                       : base (info, context)
+               {
+                       this.columnNumber = info.GetInt32 ("ColumnNumber");
+                       this.endColumnNumber = info.GetInt32 ("EndColumnNumber");
+                       this.errorCode = info.GetString ("ErrorCode");
+                       this.errorSubcategory = info.GetString ("ErrorSubcategory");
+                       this.helpKeyword = info.GetString ("HelpKeyword");
+                       this.lineNumber = info.GetInt32 ("LineNumber");
+                       this.endLineNumber = info.GetInt32 ("EndLineNumber");
+                       this.projectFile = info.GetString ("ProjectFile");
+               }
+
                public override void GetObjectData (SerializationInfo info,
                                                    StreamingContext context)
                {
                        base.GetObjectData (info, context);
-                       info.AddValue ("BaseMessage", baseMessage);
                        info.AddValue ("ColumnNumber", columnNumber);
                        info.AddValue ("EndColumnNumber", endColumnNumber);
                        info.AddValue ("ErrorCode", errorCode);
@@ -116,7 +124,7 @@ namespace Microsoft.Build.BuildEngine {
 
                public string BaseMessage {
                        get {
-                               return baseMessage;
+                               return base.Message;
                        }
                }
 
@@ -164,7 +172,11 @@ namespace Microsoft.Build.BuildEngine {
 
                public override string Message {
                        get {
-                               return base.Message;
+                               if (projectFile == null || projectFile == "") {
+                                       return BaseMessage;
+                               } else {
+                                       return BaseMessage + "  " + ProjectFile;
+                               }
                        }
                }
 
@@ -176,4 +188,4 @@ namespace Microsoft.Build.BuildEngine {
        }
 }
 
-#endif
\ No newline at end of file
+#endif
index bc0ebdbb1d40de48239a4b3a30fe3432e87bfd98..295eccac7288c837e1de6e365dc5b858d8db4744 100644 (file)
@@ -44,7 +44,6 @@ namespace Microsoft.Build.BuildEngine {
                bool                            buildEnabled;
                IDictionary                     conditionedProperties;
                string[]                        defaultTargets;
-               IList                           directlyImportedProjects;
                Encoding                        encoding;
                BuildPropertyGroup              environmentProperties;
                BuildItemGroup                  evaluatedItems;
@@ -58,21 +57,18 @@ namespace Microsoft.Build.BuildEngine {
                GroupingCollection              groups;
                bool                            isDirty;
                bool                            isValidated;
-               bool                            isReset;
                BuildItemGroupCollection        itemGroups;
-               IDictionary                     importedProjects;
                ImportCollection                imports;
                string                          initialTargets;
                Engine                          parentEngine;
                BuildPropertyGroupCollection    propertyGroups;
-               BuildPropertyGroup              reservedProperties;
                string                          schemaFile;
                TaskDatabase                    taskDatabase;
                TargetCollection                targets;
                DateTime                        timeOfLastDirty;
                UsingTaskCollection             usingTasks;
                XmlDocument                     xmlDocument;
-               //XmlElement                    xmlElement;
+               bool                            unloaded;
 
                public Project ()
                        : this (Engine.GlobalEngine)
@@ -91,6 +87,7 @@ namespace Microsoft.Build.BuildEngine {
                        targets = new TargetCollection (this);
                        taskDatabase = new TaskDatabase ();
                        globalProperties = new BuildPropertyGroup ();
+                       fullFileName = String.Empty;
 
                        foreach (BuildProperty bp in parentEngine.GlobalProperties) {
                                GlobalProperties.AddProperty (bp.Clone (true));
@@ -183,6 +180,7 @@ namespace Microsoft.Build.BuildEngine {
                                   BuildSettings buildFlags)
                
                {
+                       CheckUnloaded ();
                        if (targetNames.Length == 0) {
                                if (defaultTargets.Length != 0) {
                                        targetNames = defaultTargets;
@@ -235,6 +233,8 @@ namespace Microsoft.Build.BuildEngine {
                // Does the actual loading.
                private void DoLoad (TextReader textReader)
                {
+                       ParentEngine.RemoveLoadedProject (this);
+
                        XmlReaderSettings settings = new XmlReaderSettings ();
 
                        if (SchemaFile != null) {
@@ -245,7 +245,16 @@ namespace Microsoft.Build.BuildEngine {
 
                        XmlReader xmlReader = XmlReader.Create (textReader, settings);
                        xmlDocument.Load (xmlReader);
+
+                       if (xmlDocument.DocumentElement.GetAttribute("xmlns") != "http://schemas.microsoft.com/developer/msbuild/2003") {
+                               throw new InvalidProjectFileException(
+                                       @"The default XML namespace of the project must be the MSBuild XML namespace." + 
+                                       " If the project is authored in the MSBuild 2003 format, please add " +
+                                       "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" to the <Project> element. " +
+                                       "If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.  ");
+                       }
                        ProcessXml ();
+                       ParentEngine.AddLoadedProject (this);
                }
 
                public void Load (string projectFileName)
@@ -267,6 +276,18 @@ namespace Microsoft.Build.BuildEngine {
                        DoLoad (new StringReader (projectXml));
                }
 
+               internal void Unload ()
+               {
+                       unloaded = true;
+               }
+
+               internal void CheckUnloaded ()
+               {
+                       if (unloaded) {
+                               throw new InvalidOperationException("This project object is no longer valid.");
+                       }
+               }
+
                private void ProcessXml ()
                {
                        XmlElement xmlElement = xmlDocument.DocumentElement;
@@ -280,7 +301,7 @@ namespace Microsoft.Build.BuildEngine {
                        ProcessElements (xmlElement, null);
                        
                        isDirty = false;
-                       Evaluate();
+                       Evaluate ();
                }
 
                private void InitializeProperties ()
@@ -559,7 +580,7 @@ namespace Microsoft.Build.BuildEngine {
                                throw new ArgumentNullException ("xmlElement");
                        BuildItemGroup big = new BuildItemGroup (xmlElement, this);
                        ItemGroups.Add (big);
-                       big.Evaluate();
+                       big.Evaluate ();
                }
                
                private void AddPropertyGroup (XmlElement xmlElement)
@@ -568,7 +589,7 @@ namespace Microsoft.Build.BuildEngine {
                                throw new ArgumentNullException ("xmlElement");
                        BuildPropertyGroup bpg = new BuildPropertyGroup (xmlElement, this);
                        PropertyGroups.Add (bpg);
-                       bpg.Evaluate();
+                       bpg.Evaluate ();
                }
                
                private void AddChoose (XmlElement xmlElement)
index 50cb099b334f901f98e373b5ef6d3fba78081228..2b3d0011b87977ade36a87cf7530fcb2aa31d100 100644 (file)
@@ -1,3 +1,8 @@
+2006-03-29  Crestez Leonard  <cdleonard@gmail.com>
+
+       * EngineTest.cs: Added test for GlobalEngine.
+       * ProjectTest.cs: Fixed tests.
+       
 2006-03-27  Crestez Leonard  <cdleonard@gmail.com>
 
        * ProjectTest.cs, EngineTest.cs: Added new tests.
index a287fac2b3de8020aa3cd6f58c92518127808079..a1bd6255ee9ff79638f1fe2180329f631789d8c1 100644 (file)
@@ -39,7 +39,7 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                [SetUp]
                public void SetUp ()
                {
-                   binPath = "binPath";
+                       binPath = "binPath";
                }
 
                [Test]
@@ -130,5 +130,15 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                        project = new Project (engine);
                        Assert.AreEqual (4, project.GlobalProperties.Count, "A15");
                }
+
+               [Test]
+               public void TestGlobalEngine ()
+               {
+                       engine = new Engine ();
+                       Assert.IsFalse (engine == Engine.GlobalEngine, "1");
+                       Assert.IsNotNull (Engine.GlobalEngine, "2");
+                       engine = Engine.GlobalEngine;
+                       Assert.AreSame (engine, Engine.GlobalEngine, "3");
+               }
        }
 }
index 76097f24bd05d56d3bda06d87585249759f4125e..bdb93a69ffa25715456a231e0b147ea8e670b2f1 100644 (file)
@@ -84,13 +84,14 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                        
                        engine = new Engine (binPath);
                        proj = engine.CreateNewProject ();
+                       Assert.AreEqual (String.Empty, proj.FullFileName, "A1");
+
                        proj.LoadXml (documentString);
-                       
-                       Assert.AreEqual ("Build; Compile", proj.DefaultTargets, "A1");
+                       Assert.AreEqual (String.Empty, proj.FullFileName, "A2");
                        proj.DefaultTargets = "Build";
-                       Assert.AreEqual ("Build", proj.DefaultTargets, "A2");
+                       Assert.AreEqual ("Build", proj.DefaultTargets, "A3");
                        cproj = CloneProject (proj);
-                       Assert.AreEqual (proj.DefaultTargets, cproj.DefaultTargets, "A3");
+                       Assert.AreEqual (proj.DefaultTargets, cproj.DefaultTargets, "A4");
                }
 
                [Test]
@@ -135,11 +136,11 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                [Test]
                public void TestItems ()
                {
-                       Engine engine = new Engine ();
+                       Engine engine = new Engine (binPath);
                        Project proj = engine.CreateNewProject ();
 
                        string documentString = @"
-                               <Project>
+                               <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
                                        <ItemGroup>
                                                <Item0 Include=""A"" />
                                                <Item1 Include=""A;B;C"" />