LightUnit.cs :  » Installers-Generators » WiX » Microsoft » Tools » WindowsInstallerXml » Unit » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Installers Generators » WiX 
WiX » Microsoft » Tools » WindowsInstallerXml » Unit » LightUnit.cs
//-------------------------------------------------------------------------------------------------
// <copyright file="LightUnit.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
//    
//    The use and distribution terms for this software are covered by the
//    Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
//    which can be found in the file CPL.TXT at the root of this distribution.
//    By using this software in any fashion, you are agreeing to be bound by
//    the terms of this license.
//    
//    You must not remove this notice, or any other, from this software.
// </copyright>
// 
// <summary>
// The Windows Installer XML Light unit tester.
// </summary>
//-------------------------------------------------------------------------------------------------

namespace Microsoft.Tools.WindowsInstallerXml.Unit{
    using System;
    using System.Collections;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Xml;

    using Microsoft.Tools.WindowsInstallerXml;

    /// <summary>
    /// The Windows Installer XML Light unit tester.
    /// </summary>
    internal sealed class LightUnit
    {
        /// <summary>
        /// Private constructor to prevent instantiation of static class.
        /// </summary>
        private LightUnit()
        {
        }

        /// <summary>
        /// Run a Light unit test.
        /// </summary>
        /// <param name="element">The unit test element.</param>
        /// <param name="previousUnitResults">The previous unit test results.</param>
        /// <param name="update">Indicates whether to give the user the option to fix a failing test.</param>
        /// <param name="args">The command arguments passed to WixUnit.</param>
        public static void RunUnitTest(XmlElement element, UnitResults previousUnitResults, bool update, ICommandArgs args)
        {
            string arguments = element.GetAttribute("Arguments");
            string expectedErrors = element.GetAttribute("ExpectedErrors");
            string expectedResult = element.GetAttribute("ExpectedResult");
            string expectedWarnings = element.GetAttribute("ExpectedWarnings");
            string extensions = element.GetAttribute("Extensions");
            string outputFile = element.GetAttribute("OutputFile");
            string intermediateOutputType = element.GetAttribute("IntermediateOutputType");
            string suppressExtensions = element.GetAttribute("SuppressExtensions");
            string tempDirectory = element.GetAttribute("TempDirectory");
            string testName = element.ParentNode.Attributes["Name"].Value;
            string toolsDirectory = element.GetAttribute("ToolsDirectory");
            bool usePreviousOutput = ("true" == element.GetAttribute("UsePreviousOutput"));

            if (expectedErrors.Length == 0 && expectedResult.Length == 0 && intermediateOutputType.Length == 0 && outputFile.Length == 0)
            {
                throw new WixException(WixErrors.ExpectedAttributes(null, element.Name, "ExpectedErrors", "ExpectedResult", "IntermediateOutputType", "OutputFile", null));
            }

            if (expectedErrors.Length > 0 && (expectedResult.Length > 0 || intermediateOutputType.Length > 0 || outputFile.Length > 0))
            {
                throw new WixException(WixErrors.IllegalAttributeWithOtherAttributes(null, element.Name, "ExpectedErrors", "ExpectedResult", "IntermediateOutputType", "OutputFile", null));
            }
            else if (expectedResult.Length > 0 && (intermediateOutputType.Length > 0 || outputFile.Length > 0))
            {
                throw new WixException(WixErrors.IllegalAttributeWithOtherAttributes(null, element.Name, "ExpectedResult", "IntermediateOutputType", "OutputFile"));
            }
            else if (intermediateOutputType.Length > 0 && outputFile.Length > 0)
            {
                throw new WixException(WixErrors.IllegalAttributeWithOtherAttributes(null, element.Name, "IntermediateOutputType", "OutputFile", null));
            }

            string toolFile = Path.Combine(toolsDirectory, "light.exe");
            StringBuilder commandLine = new StringBuilder(arguments);
            commandLine.Append(" -b \"%WIX%\\examples\\data\"");

            // handle wixunit arguments
            if (args.NoTidy)
            {
                commandLine.Append(" -notidy");
            }

            // handle extensions
            if (!String.IsNullOrEmpty(extensions))
            {
                string[] suppressedExtensionArray = suppressExtensions.Split(';');
                foreach (string extension in extensions.Split(';'))
                {
                    if (0 > Array.BinarySearch(suppressedExtensionArray, extension))
                    {
                        commandLine.AppendFormat(" -ext \"{0}\"", extension);
                    }
                }
            }

            // handle any previous outputs
            if (usePreviousOutput)
            {
                foreach (string inputFile in previousUnitResults.OutputFiles)
                {
                    commandLine.AppendFormat(" \"{0}\"", inputFile);
                }
            }
            previousUnitResults.OutputFiles.Clear();

            // handle child elements
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    switch (node.LocalName)
                    {
                        case "LibraryFile":
                            string libraryFile = node.InnerText.Trim();

                            commandLine.AppendFormat(" \"{0}\"", libraryFile);
                            break;
                        case "LocalizationFile":
                            string localizationFile = node.InnerText.Trim();

                            commandLine.AppendFormat(" -loc \"{0}\"", localizationFile);
                            break;
                    }
                }
            }

            if (outputFile.Length > 0)
            {
                // outputFile has been explicitly set
            }
            else if (expectedResult.Length > 0)
            {
                outputFile = Path.Combine(tempDirectory, Path.GetFileName(expectedResult));
            }
            else if (intermediateOutputType.Length > 0)
            {
                string intermediateFile = String.Concat("intermediate.", intermediateOutputType);

                outputFile = Path.Combine(tempDirectory, intermediateFile);
            }
            else
            {
                outputFile = Path.Combine(tempDirectory, "ShouldNotBeCreated.msi");
            }
            commandLine.AppendFormat("{0} -out \"{1}\"", (Path.GetExtension(outputFile) == ".wixout" ? " -xo" : String.Empty), outputFile);
            previousUnitResults.OutputFiles.Add(outputFile);

            // run the tool
            ArrayList output = ToolUtility.RunTool(toolFile, commandLine.ToString());
            previousUnitResults.Errors.AddRange(ToolUtility.GetErrors(output, expectedErrors, expectedWarnings));
            previousUnitResults.Output.AddRange(output);

            // check the output file
            if (previousUnitResults.Errors.Count == 0)
            {
                if (expectedResult.Length > 0)
                {
                    ArrayList differences = CompareUnit.CompareResults(expectedResult, outputFile, testName, update);

                    previousUnitResults.Errors.AddRange(differences);
                    previousUnitResults.Output.AddRange(differences);
                }
                else if (expectedErrors.Length > 0 && File.Exists(outputFile)) // ensure the output doesn't exist
                {
                    string error = String.Format(CultureInfo.InvariantCulture, "Expected failure, but the unit test created output file \"{0}\".", outputFile);

                    previousUnitResults.Errors.Add(error);
                    previousUnitResults.Output.Add(error);
                }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.