示例#1
0
        // load document
        /// <summary>
        /// Loads the test container file.
        /// Always returns a non-null value, or else an exception is thrown.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="recursively"></param>
        /// <returns></returns>
        public static XElement LoadTestContainer(string filename, bool recursively)
        {
            // Normalize the path.
            string filePath = Path.GetFullPath(filename);

            XDocument doc;

            switch (Path.GetExtension(filePath).ToLower())
            {
            case ".exe":
            case ".dll":
                doc = ReadTestContainerInSeperateProcess(filePath);
                if (doc.Root.Name == XNames.Error)
                {
                    System.Diagnostics.Trace.WriteLine(doc.Root, "Error reading test assembly");
                    throw new Exception((string)doc.Root.Element(XNames.ErrorMessage));
                }
                break;

            case ".csproj":
                if (!File.Exists(filePath))
                {
                    throw new Exception("can not load test project. \"" + filePath + "\" does not exist.");
                }

                // Create a new project entity xml with just the information we know.
                doc = new XDocument(
                    new XElement(XNames.TestProject
                                 , new XAttribute(XNames.ALocation, filePath)
                                 // Loaded after attaching to a session
                                 //, MSBuild.MSBuildProject.ParseProjectFileToXElement(filePath)
                                 ));
                break;

            default:
                if (!File.Exists(filePath))
                {
                    throw new Exception("can not load testlist. \"" + filePath + "\" does not exist.");
                }
                doc = XDocument.Load(filePath, LoadOptions.SetLineInfo);
                break;
            }

            // Validate the test container xml
            try
            {
                UnitTestingSchemaUtil.ValidateTestListXml(doc);
            }
            catch (Exception ex)
            {
                throw new Exception("Invalid test container \"" + filePath + "\"." + ex.Message);
            }


            // detach from the document
            XElement xroot = doc.Root;

            xroot.Remove();

            // For testlists that we load from the file system, allow them to be reloaded
            if (xroot.Name == XNames.Testlist)
            {
                xroot.SetAttributeValue(XNames.ALocation, filePath); // Always update just in case the file was moved from the current value
            }
            // replace @ character with directory
            string folderPath = Path.GetDirectoryName(filePath);

            foreach (XElement x in xroot.Descendants())
            {
                if (x.Name == XNames.Executable ||
                    x.Name == XNames.WorkingDirectory ||
                    x.Name == XNames.Shellline ||
                    x.Name == XNames.Arg
                    )
                {
                    string v = x.Value;
                    if (v.Contains('@'))
                    {
                        x.Value = Path.GetFullPath(v.Replace("@", folderPath));
                    }
                }
                else if (x.Name == XNames.Include ||
                         x.Name == XNames.TestProject
                         )
                {
                    XAttribute xloc = x.Attribute(XNames.ALocation);
                    string     v    = xloc.Value;
                    if (v.Contains('@'))
                    {
                        xloc.Value = Path.GetFullPath(v.Replace("@", folderPath));
                    }
                }

                foreach (XAttribute a in x.Attributes().Where(a =>
                                                              a.Name == XNames.AObservationFile ||
                                                              a.Name == XNames.AProject
                                                              ))
                {
                    string v = a.Value;
                    if (v.Contains('@'))
                    {
                        a.Value = Path.GetFullPath(v.Replace("@", folderPath));
                    }
                }
            }

            if (recursively)
            {
                // replace <include location="filename"/> with contents
                XElement[] includes = xroot.Descendants(XNames.Include).ToArray();
                foreach (XElement xinclude in includes)
                {
                    // We've already fully resolved the location attribute to it's full path above
                    string loc = (string)xinclude.Attribute(XNames.ALocation);
                    try
                    {
                        var xcontainer = LoadTestContainer(loc, recursively);

                        MergeInSettingsFromInclude(xcontainer, xinclude);

                        xinclude.ReplaceWith(xcontainer);
                    }
                    catch (Exception ex)
                    {
                        xinclude.Add(XConcurrencyNames.CreateXError("Error trying to load included test container: " + ex.Message));
                    }
                }
            }

            return(xroot);
        }