コード例 #1
0
 /// <summary>
 /// Checks that a solution contains at least one PLC project.
 /// </summary>
 /// <returns>The project that can be run</returns>
 public Project findPLCProject()
 {
     foreach (Project project in solution.Projects)
     {
         try
         {
             ITcSysManager4 twinCatProject = (ITcSysManager4)project.Object;
             ITcSmTreeItem  plcConfig      = twinCatProject.LookupTreeItem("TIPC");
             ITcPlcProject  iecProjectRoot = (ITcPlcProject)plcConfig.Child[1]; // Assume you want to run the first PLC project
             iecProjectRoot.BootProjectAutostart = true;
             iecProjectRoot.GenerateBootProject(true);
             Console.WriteLine("Found PLC Project: " + project.Name + "." + plcConfig.Name);
             return(project);
         } catch {
             Console.WriteLine(project.Name + " is not a Twincat project");
         }
     }
     return(null);
 }
コード例 #2
0
 //public Boolean buildSolution()
 //public Boolean cleanSolution()
 public Project findPlcProject()
 {
     //Method to search through all projects in solution and find a valid PLC
     foreach (Project project in solution.Projects)
     {
         try
         {
             ITcSysManager13 twinCatProject     = (ITcSysManager13)project.Object;
             ITcSmTreeItem   plcProjectRootItem = twinCatProject.LookupTreeItem("TIPC^" + plcName);
             ITcPlcProject   iecProjectRoot     = (ITcPlcProject)plcProjectRootItem;
             iecProjectRoot.BootProjectAutostart = true;
             iecProjectRoot.GenerateBootProject(true);
             return(project);
         }
         catch
         {
             MessageBox.Show(project.Name + " has no PLC project of that name");
         }
     }
     return(null);
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: oswin02/TcUnit
        static void Main(string[] args)
        {
            bool showHelp = false;

            Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelKeyPressHandler);
            GlobalContext.Properties["LogLocation"] = AppDomain.CurrentDomain.BaseDirectory + "\\logs";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

            OptionSet options = new OptionSet()
                                .Add("v=|TcUnitVerifierPath=", "Path to TcUnit-Verifier TwinCAT solution", v => tcUnitVerifierPath = v)
                                .Add("t=|TcUnitTargetNetId=", "[OPTIONAL] Target NetId of TwinCAT runtime to run TcUnit-Verifier", t => tcUnitTargetNetId = t)
                                .Add("?|h|help", h => showHelp = h != null);

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `TcUnit-Verifier --help' for more information.");
                Environment.Exit(Constants.RETURN_ERROR);
            }

            /* Make sure the user has supplied the path for the Visual Studio solution file.
             *  Also verify that this file exists.
             */
            if (showHelp || tcUnitVerifierPath == null)
            {
                DisplayHelp(options);
                Environment.Exit(Constants.RETURN_ERROR);
            }
            if (!File.Exists(tcUnitVerifierPath))
            {
                log.Error("TcUnit-verifier solution " + tcUnitVerifierPath + " does not exist.");
                Environment.Exit(Constants.RETURN_ERROR);
            }

            MessageFilter.Register();

            log.Info("Starting TcUnit-Verifier...");
            try
            {
                vsInstance = new VisualStudioInstance(tcUnitVerifierPath);
                vsInstance.Load();
            }
            catch
            {
                log.Error("Solution load failed");  // Detailed error messages output by vsInstance.Load()
                CleanUp();
                Environment.Exit(Constants.RETURN_ERROR);
            }

            if (vsInstance.GetVisualStudioVersion() == null)
            {
                log.Error("Did not find Visual studio version in Visual studio solution file.");
                CleanUp();
                Environment.Exit(Constants.RETURN_ERROR);
            }

            log.Info("Cleaning and building TcUnit-Verifier_TwinCAT solution...");
            AutomationInterface automationInterface = new AutomationInterface(vsInstance);

            automationInterface.ITcSysManager.SetTargetNetId(tcUnitTargetNetId);
            ITcSmTreeItem plcProject = automationInterface.PlcTreeItem.Child[1];
            ITcPlcProject iecProject = (ITcPlcProject)plcProject;

            log.Info("Generating TcUnit-Verifier_TwinCAT boot project...");
            Thread.Sleep(10000);
            iecProject.GenerateBootProject(true);
            iecProject.BootProjectAutostart = true;

            log.Info("Activating TcUnit-Verifier_TwinCAT configuration...");
            automationInterface.ITcSysManager.ActivateConfiguration();

            /* Clean the solution. This is the only way to clean the error list which needs to be
             * clean prior to starting the TwinCAT runtime */
            vsInstance.CleanSolution();

            // Wait
            Thread.Sleep(1000);

            log.Info("Restarting TwinCAT...");
            automationInterface.ITcSysManager.StartRestartTwinCAT();

            // Wait until tests have been running and are finished
            bool testsFinishedRunningFirstLineFound = false;
            bool numberOfTestSuitesLineFound        = false;
            bool numberOfTestsLineFound             = false;
            bool numberOfSuccesfulTestsLineFound    = false;
            bool numberOfFailedTestsLineFound       = false;
            bool testsFinishedRunningLastLineFound  = false;
            int  numberOfFailedTests = 0;

            log.Info("Waiting for TcUnit-Verifier_TwinCAT to finish running tests...");

            ErrorList errorList = new ErrorList();

            ErrorItems errorItems;

            while (true)
            {
                Thread.Sleep(10000);

                errorItems = vsInstance.GetErrorItems();
                log.Info("... got " + errorItems.Count + " report lines so far.");
                for (int i = 1; i <= errorItems.Count; i++)
                {
                    ErrorItem error = errorItems.Item(i);
                    if (error.Description.Contains("| ==========TESTS FINISHED RUNNING=========="))
                    {
                        testsFinishedRunningFirstLineFound = true;
                    }
                    if (error.Description.Contains("| Test suites:"))
                    {
                        numberOfTestSuitesLineFound = true;
                    }
                    if (error.Description.Contains("| Tests:"))
                    {
                        numberOfTestsLineFound = true;
                    }
                    if (error.Description.Contains("| Successful tests:"))
                    {
                        numberOfSuccesfulTestsLineFound = true;
                    }
                    if (error.Description.Contains("| Failed tests:"))
                    {
                        numberOfFailedTestsLineFound = true;
                        // Grab the number of failed tests so we can validate it during the assertion phase
                        numberOfFailedTests = int.Parse(error.Description.Split().Last());
                    }
                    if (error.Description.Contains("| ======================================"))
                    {
                        testsFinishedRunningLastLineFound = true;
                    }
                }

                if (
                    testsFinishedRunningFirstLineFound &&
                    numberOfTestSuitesLineFound &&
                    numberOfTestsLineFound &&
                    numberOfSuccesfulTestsLineFound &&
                    numberOfFailedTestsLineFound &&
                    testsFinishedRunningLastLineFound
                    )
                {
                    break;
                }
            }
            var newErrors = errorList.AddNew(errorItems);

            log.Info("Asserting results...");

            if (numberOfFailedTests != expectedNumberOfFailedTests)
            {
                log.Error(
                    "The number of tests that failed (" + numberOfFailedTests + ") " +
                    "does not match expectations (" + expectedNumberOfFailedTests + ")"
                    );
            }

            List <ErrorList.Error> errors = new List <ErrorList.Error>(
                errorList.Where(e => (
                                    e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh ||
                                    e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelLow)
                                )
                );

            /* Insert the test classes here */
            new FB_PrimitiveTypes(errors);
            new FB_ExtendedTestInformation(errors);
            new FB_AssertTrueFalse(errors);
            new FB_AssertEveryFailedTestTwice(errors);
            new FB_CreateFourTestsWithSameName(errors);
            new FB_ArrayPrimitiveTypes(errors);
            new FB_CreateDisabledTest(errors);
            new FB_AnyPrimitiveTypes(errors);
            new FB_AssertEveryFailedTestTwiceArrayVersion(errors);
            new FB_AnyToUnionValue(errors);
            new FB_MultipleAssertWithSameParametersInSameCycleWithSameTest(errors);
            new FB_MultipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests(errors);
            new FB_MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest(errors);
            new FB_SkipAssertionsWhenFinished(errors);
            new FB_AdjustAssertFailureMessageToMax253CharLengthTest(errors);
            new FB_CheckIfSpecificTestIsFinished(errors);
            new FB_WriteProtectedFunctions(errors);
            new FB_TestFileControl(errors);
            new FB_TestXmlControl(errors);
            new FB_TestStreamBuffer(errors);
            new FB_TestFinishedNamed(errors);
            new FB_TestNumberOfAssertionsCalculation(errors);
            new FB_EmptyAssertionMessage(errors);

            log.Info("Done.");

            CleanUp();

            Environment.Exit(Constants.RETURN_SUCCESSFULL);
        }
コード例 #4
0
        static void Main(string[] args)
        {
            bool showHelp = false;
            bool enableDebugLoggingLevel = false;

            Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelKeyPressHandler);
            log4net.GlobalContext.Properties["LogLocation"] = AppDomain.CurrentDomain.BaseDirectory + "\\logs";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

            OptionSet options = new OptionSet()
                                .Add("v=|VisualStudioSolutionFilePath=", "The full path to the TwinCAT project (sln-file)", v => VisualStudioSolutionFilePath = v)
                                .Add("t=|TcUnitTaskName=", "[OPTIONAL] The name of the task running TcUnit defined under \"Tasks\"", t => TcUnitTaskName      = t)
                                .Add("a=|AmsNetId=", "[OPTIONAL] The AMS NetId of the device of where the project and TcUnit should run", a => AmsNetId       = a)
                                .Add("w=|TcVersion=", "[OPTIONAL] The TwinCAT version to be used to load the TwinCAT project", w => ForceToThisTwinCATVersion = w)
                                .Add("u=|Timeout=", "[OPTIONAL] Timeout the process with an error after X minutes", u => Timeout = u)
                                .Add("d|debug", "[OPTIONAL] Increase debug message verbosity", d => enableDebugLoggingLevel      = d != null)
                                .Add("?|h|help", h => showHelp = h != null);

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `TcUnit-Runner --help' for more information.");
                Environment.Exit(Constants.RETURN_ARGUMENT_ERROR);
            }


            if (showHelp)
            {
                DisplayHelp(options);
                Environment.Exit(Constants.RETURN_SUCCESSFULL);
            }

            /* Set logging level.
             * This is handled by changing the log4net.config file on the fly.
             * The following levels are defined in order of increasing priority:
             * - ALL
             * - DEBUG
             * - INFO
             * - WARN
             * - ERROR
             * - FATAL
             * - OFF
             */
            XmlDocument doc = new XmlDocument();

            doc.Load(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            XmlNode root          = doc.DocumentElement;
            XmlNode subNode1      = root.SelectSingleNode("root");
            XmlNode nodeForModify = subNode1.SelectSingleNode("level");

            if (enableDebugLoggingLevel)
            {
                nodeForModify.Attributes[0].Value = "DEBUG";
            }
            else
            {
                nodeForModify.Attributes[0].Value = "INFO";
            }
            doc.Save(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            System.Threading.Thread.Sleep(500); // A tiny sleep just to make sure that log4net manages to detect the change in the file

            /* Make sure the user has supplied the path for the Visual Studio solution file.
             * Also verify that this file exists.
             */
            if (VisualStudioSolutionFilePath == null)
            {
                log.Error("Visual studio solution path not provided!");
                Environment.Exit(Constants.RETURN_VISUAL_STUDIO_SOLUTION_PATH_NOT_PROVIDED);
            }

            if (!File.Exists(VisualStudioSolutionFilePath))
            {
                log.Error("Visual studio solution " + VisualStudioSolutionFilePath + " does not exist!");
                Environment.Exit(Constants.RETURN_VISUAL_STUDIO_SOLUTION_PATH_NOT_FOUND);
            }

            LogBasicInfo();

            /* Start a timeout for the process(es) if the user asked for it
             */
            if (Timeout != null)
            {
                log.Info("Timeout enabled - process(es) timesout after " + Timeout + " minute(s)");
                System.Timers.Timer timeout = new System.Timers.Timer(Int32.Parse(Timeout) * 1000 * 60);
                timeout.Elapsed  += KillProcess;
                timeout.AutoReset = false;
                timeout.Start();
            }

            MessageFilter.Register();

            TwinCATProjectFilePath = TcFileUtilities.FindTwinCATProjectFile(VisualStudioSolutionFilePath);
            if (String.IsNullOrEmpty(TwinCATProjectFilePath))
            {
                log.Error("Did not find TwinCAT project file in solution. Is this a TwinCAT project?");
                Environment.Exit(Constants.RETURN_TWINCAT_PROJECT_FILE_NOT_FOUND);
            }

            if (!File.Exists(TwinCATProjectFilePath))
            {
                log.Error("TwinCAT project file " + TwinCATProjectFilePath + " does not exist!");
                Environment.Exit(Constants.RETURN_TWINCAT_PROJECT_FILE_NOT_FOUND);
            }

            string tcVersion = TcFileUtilities.GetTcVersion(TwinCATProjectFilePath);

            if (String.IsNullOrEmpty(tcVersion))
            {
                log.Error("Did not find TwinCAT version in TwinCAT project file path");
                Environment.Exit(Constants.RETURN_TWINCAT_VERSION_NOT_FOUND);
            }

            try
            {
                vsInstance = new VisualStudioInstance(@VisualStudioSolutionFilePath, tcVersion, ForceToThisTwinCATVersion);
                bool isTcVersionPinned = XmlUtilities.IsTwinCATProjectPinned(TwinCATProjectFilePath);
                log.Info("Version is pinned: " + isTcVersionPinned);
                vsInstance.Load(isTcVersionPinned);
            }
            catch
            {
                log.Error("Error loading VS DTE. Is the correct version of Visual Studio and TwinCAT installed? Is the TcUnit-Runner running with administrator privileges?");
                CleanUpAndExitApplication(Constants.RETURN_ERROR_LOADING_VISUAL_STUDIO_DTE);
            }

            try
            {
                vsInstance.LoadSolution();
            }
            catch
            {
                log.Error("Error loading the solution. Try to open it manually and make sure it's possible to open and that all dependencies are working");
                CleanUpAndExitApplication(Constants.RETURN_ERROR_LOADING_VISUAL_STUDIO_SOLUTION);
            }

            if (vsInstance.GetVisualStudioVersion() == null)
            {
                log.Error("Did not find Visual Studio version in Visual Studio solution file");
                CleanUpAndExitApplication(Constants.RETURN_ERROR_FINDING_VISUAL_STUDIO_SOLUTION_VERSION);
            }


            AutomationInterface automationInterface = new AutomationInterface(vsInstance.GetProject());

            if (automationInterface.PlcTreeItem.ChildCount <= 0)
            {
                log.Error("No PLC-project exists in TwinCAT project");
                CleanUpAndExitApplication(Constants.RETURN_NO_PLC_PROJECT_IN_TWINCAT_PROJECT);
            }


            ITcSmTreeItem realTimeTasksTreeItem = automationInterface.RealTimeTasksTreeItem;

            /* Task name provided */
            if (!String.IsNullOrEmpty(TcUnitTaskName))
            {
                log.Info("Setting task '" + TcUnitTaskName + "' enable and autostart, and all other tasks (if existing) to disable and non-autostart");
                bool foundTcUnitTaskName = false;

                /* Find all tasks, and check whether the user provided TcUnit task is amongst them.
                 * Also update the task object (Update <Disabled> and <Autostart>-tag)
                 */
                foreach (ITcSmTreeItem child in realTimeTasksTreeItem)
                {
                    ITcSmTreeItem testTreeItem = realTimeTasksTreeItem.LookupChild(child.Name);
                    string        xmlString    = testTreeItem.ProduceXml();
                    string        newXmlString = "";
                    try
                    {
                        if (TcUnitTaskName.Equals(XmlUtilities.GetItemNameFromRealTimeTaskXML(xmlString)))
                        {
                            foundTcUnitTaskName = true;
                            newXmlString        = XmlUtilities.SetDisabledAndAndAutoStartOfRealTimeTaskXml(xmlString, false, true);
                        }
                        else
                        {
                            newXmlString = XmlUtilities.SetDisabledAndAndAutoStartOfRealTimeTaskXml(xmlString, true, false);
                        }
                        testTreeItem.ConsumeXml(newXmlString);
                        System.Threading.Thread.Sleep(3000);
                    }
                    catch
                    {
                        log.Error("Could not parse real time task XML data");
                        CleanUpAndExitApplication(Constants.RETURN_NOT_POSSIBLE_TO_PARSE_REAL_TIME_TASK_XML_DATA);
                    }
                }

                if (!foundTcUnitTaskName)
                {
                    log.Error("Could not find task '" + TcUnitTaskName + "' in TwinCAT project");
                    CleanUpAndExitApplication(Constants.RETURN_FAILED_FINDING_DEFINED_UNIT_TEST_TASK_IN_TWINCAT_PROJECT);
                }
            }

            /* No task name provided */
            else
            {
                log.Info("No task name provided. Assuming only one task exists");
                /* Check that only one task exists */
                if (realTimeTasksTreeItem.ChildCount.Equals(1))
                {
                    // Get task name
                    ITcSmTreeItem child        = realTimeTasksTreeItem.get_Child(1);
                    ITcSmTreeItem testTreeItem = realTimeTasksTreeItem.LookupChild(child.Name);
                    string        xmlString    = testTreeItem.ProduceXml();
                    TcUnitTaskName = XmlUtilities.GetItemNameFromRealTimeTaskXML(xmlString);
                    log.Info("Found task with name '" + TcUnitTaskName + "'");
                    string newXmlString = "";
                    newXmlString = XmlUtilities.SetDisabledAndAndAutoStartOfRealTimeTaskXml(xmlString, false, true);
                    testTreeItem.ConsumeXml(newXmlString);
                    System.Threading.Thread.Sleep(3000);
                }
                /* Less ore more than one task, which is an error */
                else
                {
                    log.Error("The number of tasks is not equal to 1 (one). Found " + realTimeTasksTreeItem.ChildCount.ToString() + " number of tasks. Please provide which task is the TcUnit task");
                    CleanUpAndExitApplication(Constants.RETURN_TASK_COUNT_NOT_EQUAL_TO_ONE);
                }
            }


            /* Build the solution and collect any eventual errors. Make sure to
             * filter out everything that is an error
             */
            vsInstance.CleanSolution();
            vsInstance.BuildSolution();

            ErrorItems errorsBuild = vsInstance.GetErrorItems();

            int tcBuildWarnings = 0;
            int tcBuildError    = 0;

            for (int i = 1; i <= errorsBuild.Count; i++)
            {
                ErrorItem item = errorsBuild.Item(i);
                if ((item.ErrorLevel != vsBuildErrorLevel.vsBuildErrorLevelLow))
                {
                    if (item.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelMedium)
                    {
                        tcBuildWarnings++;
                    }
                    else if (item.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh)
                    {
                        tcBuildError++;
                        log.Error("Description: " + item.Description);
                        log.Error("ErrorLevel: " + item.ErrorLevel);
                        log.Error("Filename: " + item.FileName);
                    }
                }
            }

            /* If we don't have any errors, activate the configuration and
             * start/restart TwinCAT */
            if (tcBuildError.Equals(0))
            {
                /* Check whether the user has provided an AMS NetId. If so, use it. Otherwise use
                 * the local AMS NetId */
                if (String.IsNullOrEmpty(AmsNetId))
                {
                    AmsNetId = Constants.LOCAL_AMS_NET_ID;
                }

                log.Info("Setting target NetId to '" + AmsNetId + "'");
                automationInterface.ITcSysManager.SetTargetNetId(AmsNetId);
                log.Info("Enabling boot project and setting BootProjectAutostart on " + automationInterface.ITcSysManager.GetTargetNetId());

                for (int i = 1; i <= automationInterface.PlcTreeItem.ChildCount; i++)
                {
                    ITcSmTreeItem plcProject = automationInterface.PlcTreeItem.Child[i];
                    ITcPlcProject iecProject = (ITcPlcProject)plcProject;
                    iecProject.BootProjectAutostart = true;

                    /* add the port that is used for this PLC to the AmsPorts list that
                     * is later used to monitory the AdsState
                     */
                    string xmlString = plcProject.ProduceXml();
                    AmsPorts.Add(XmlUtilities.AmsPort(xmlString));
                }
                System.Threading.Thread.Sleep(1000);
                automationInterface.ActivateConfiguration();

                // Wait
                System.Threading.Thread.Sleep(10000);

                /* Clean the solution. This is the only way to clean the error list which needs to be
                 * clean prior to starting the TwinCAT runtime */
                vsInstance.CleanSolution();

                // Wait
                System.Threading.Thread.Sleep(10000);

                automationInterface.StartRestartTwinCAT();
            }
            else
            {
                log.Error("Build errors in project");
                CleanUpAndExitApplication(Constants.RETURN_BUILD_ERROR);
            }

            /* Establish a connection to the ADS router
             */
            TcAdsClient tcAdsClient = new TcAdsClient();

            /* Run TcUnit until the results have been returned */
            TcUnitResultCollector tcUnitResultCollector = new TcUnitResultCollector();
            ErrorList             errorList             = new ErrorList();

            log.Info("Waiting for results from TcUnit...");

            ErrorItems errorItems;

            while (true)
            {
                System.Threading.Thread.Sleep(10000);

                /* Monitor the AdsState for each PLC that is used in the
                 * solution. If we can't connect to the Ads Router, we just
                 * carry on.
                 */
                try
                {
                    foreach (int amsPort in AmsPorts)
                    {
                        tcAdsClient.Connect(AmsNetId, amsPort);
                        AdsState adsState = tcAdsClient.ReadState().AdsState;
                        if (adsState != AdsState.Run)
                        {
                            log.Error("Invalid AdsState " + adsState + "<>" + AdsState.Run + ". This could indicate a PLC Exception, terminating ...");
                            Environment.Exit(Constants.RETURN_INVALID_ADSSTATE);
                        }
                    }
                }
                catch (Exception ex)
                { }
                finally
                {
                    tcAdsClient.Disconnect();
                }

                errorItems = vsInstance.GetErrorItems();

                var newErrors = errorList.AddNew(errorItems);
                if (log.IsDebugEnabled)
                {
                    foreach (var error in newErrors)
                    {
                        log.Debug(error.ErrorLevel + ": " + error.Description);
                    }
                }

                log.Info("... got " + errorItems.Count + " report lines so far.");
                if (tcUnitResultCollector.AreResultsAvailable(errorItems))
                {
                    log.Info("All results from TcUnit obtained");

                    /* The last test suite result can be returned after that we have received the test results, wait a few seconds
                     * and fetch again
                     */
                    System.Threading.Thread.Sleep(10000);
                    break;
                }
            }

            List <ErrorList.Error> errors       = new List <ErrorList.Error>(errorList.Where(e => (e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh || e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelLow)));
            List <ErrorList.Error> errorsSorted = errors.OrderBy(o => o.Description).ToList();

            /* Parse all events (from the error list) from Visual Studio and store the results */
            TcUnitTestResult testResult = tcUnitResultCollector.ParseResults(errorsSorted, TcUnitTaskName);

            /* Write xUnit XML report */
            if (testResult != null)
            {
                // No need to check if file (VisualStudioSolutionFilePath) exists, as this has already been done
                string VisualStudioSolutionDirectoryPath = Path.GetDirectoryName(VisualStudioSolutionFilePath);
                string XUnitReportFilePath = VisualStudioSolutionDirectoryPath + "\\" + Constants.XUNIT_RESULT_FILE_NAME;
                log.Info("Writing xUnit XML file to " + XUnitReportFilePath);
                // Overwrites all existing content (if existing)
                XunitXmlCreator.WriteXml(testResult, XUnitReportFilePath);
            }

            CleanUpAndExitApplication(Constants.RETURN_SUCCESSFULL);
        }
コード例 #5
0
        static int Main(string[] args)
        {
            bool showHelp = false;

            Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelKeyPressHandler);
            log4net.GlobalContext.Properties["LogLocation"] = AppDomain.CurrentDomain.BaseDirectory + "\\logs";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

            OptionSet options = new OptionSet()
                                .Add("v=|TcUnitVerifierPath=", "Path to TcUnit-Verifier TwinCAT solution", v => tcUnitVerifierPath = v)
                                .Add("?|h|help", h => showHelp = h != null);

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `TcUnit-Verifier --help' for more information.");
                return(Constants.RETURN_ERROR);
            }

            /* Make sure the user has supplied the path for the Visual Studio solution file.
             * Also verify that this file exists.
             */
            if (showHelp || tcUnitVerifierPath == null)
            {
                DisplayHelp(options);
                return(Constants.RETURN_ERROR);
            }
            if (!File.Exists(tcUnitVerifierPath))
            {
                log.Error("TcUnit-verifier solution " + tcUnitVerifierPath + " does not exist.");
                return(Constants.RETURN_ERROR);
            }

            MessageFilter.Register();

            log.Info("Starting TcUnit-Verifier...");
            try
            {
                vsInstance = new VisualStudioInstance(@tcUnitVerifierPath);
                vsInstance.Load();
            }
            catch
            {
                log.Error("Error loading VS DTE. Is the correct version of Visual Studio installed?");
                CleanUp();
                return(Constants.RETURN_ERROR);
            }

            if (vsInstance.GetVisualStudioVersion() == null)
            {
                log.Error("Did not find Visual studio version in Visual studio solution file.");
                CleanUp();
                return(Constants.RETURN_ERROR);
            }

            log.Info("Cleaning and building TcUnit-Verifier_TwinCAT solution...");
            AutomationInterface automationInterface = new AutomationInterface(vsInstance);

            automationInterface.ITcSysManager.SetTargetNetId("127.0.0.1.1.1");
            ITcSmTreeItem plcProject = automationInterface.PlcTreeItem.Child[1];
            ITcPlcProject iecProject = (ITcPlcProject)plcProject;

            log.Info("Generating TcUnit-Verifier_TwinCAT boot project...");
            iecProject.GenerateBootProject(true);
            iecProject.BootProjectAutostart = true;

            log.Info("Activating TcUnit-Verifier_TwinCAT configuration...");
            automationInterface.ITcSysManager.ActivateConfiguration();

            log.Info("Restaring TwinCAT...");
            automationInterface.ITcSysManager.StartRestartTwinCAT();

            // Wait until tests have been running and are finished
            bool testsFinishedRunningFirstLineFound = false;
            bool amountOfTestSuitesLineFound        = false;
            bool amountOfTestsLineFound             = false;
            bool amountOfSuccesfulTestsLineFound    = false;
            bool amountOfFailedTestsLineFound       = false;
            bool testsFinishedRunningLastLineFound  = false;

            log.Info("Waiting for TcUnit-Verifier_TwinCAT to finish running tests...");

            ErrorItems errorItems;

            while (true)
            {
                Thread.Sleep(1000);

                errorItems = vsInstance.GetErrorItems();

                for (int i = 1; i <= errorItems.Count; i++)
                {
                    ErrorItem item = errorItems.Item(i);
                    if (item.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh)
                    {
                        if (item.Description.ToUpper().Contains("| ==========TESTS FINISHED RUNNING=========="))
                        {
                            testsFinishedRunningFirstLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| TEST SUITES:"))
                        {
                            amountOfTestSuitesLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| TESTS:"))
                        {
                            amountOfTestsLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| SUCCESSFUL TESTS:"))
                        {
                            amountOfSuccesfulTestsLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| FAILED TESTS:"))
                        {
                            amountOfFailedTestsLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| ======================================"))
                        {
                            testsFinishedRunningLastLineFound = true;
                        }
                    }
                }

                if (testsFinishedRunningFirstLineFound && amountOfTestSuitesLineFound && amountOfTestsLineFound && amountOfSuccesfulTestsLineFound &&
                    amountOfFailedTestsLineFound && testsFinishedRunningLastLineFound)
                {
                    break;
                }
            }

            log.Info("Asserting results...");

            /* Insert the test classes here */
            FB_PrimitiveTypes                         primitiveTypes                         = new FB_PrimitiveTypes(errorItems, "PrimitiveTypes");
            FB_AssertTrueFalse                        assertTrueFalse                        = new FB_AssertTrueFalse(errorItems, "AssertTrueFalse");
            FB_AssertEveryFailedTestTwice             assertEveryFailedTestTwice             = new FB_AssertEveryFailedTestTwice(errorItems, "AssertEveryFailedTestTwice");
            FB_CreateFourTestsWithSameName            createFourTestsWithSameName            = new FB_CreateFourTestsWithSameName(errorItems, "CreateFourTestsWithSameName");
            FB_ArrayPrimitiveTypes                    arrayPrimitiveTypes                    = new FB_ArrayPrimitiveTypes(errorItems, "ArrayPrimitiveTypes");
            FB_CreateDisabledTest                     createDisabledTest                     = new FB_CreateDisabledTest(errorItems, "CreateDisabledTest");
            FB_AnyPrimitiveTypes                      anyPrimitiveTypes                      = new FB_AnyPrimitiveTypes(errorItems, "AnyPrimitiveTypes");
            FB_AssertEveryFailedTestTwiceArrayVersion assertEveryFailedTestTwiceArrayVersion = new FB_AssertEveryFailedTestTwiceArrayVersion(errorItems, "AssertEveryFailedTestTwiceArrayVersion");
            FB_AnyToUnionValue                        anyToUnionValue                        = new FB_AnyToUnionValue(errorItems, "AnyToUnionValue");
            FB_MultipleAssertWithSameParametersInSameCycleWithSameTest multipleAssertWithSameParametersInSameCycleWithSameTest = new FB_MultipleAssertWithSameParametersInSameCycleWithSameTest(errorItems, "MultipleAssertWithSameParametersInSameCycleWithSameTest");
            FB_MultipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests multipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests = new FB_MultipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests(errorItems, "MultipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests");
            FB_MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest         multipleAssertWithSameParametersInDifferentCyclesAndInSameTest         = new FB_MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest(errorItems, "MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest");
            FB_AdjustAssertFailureMessageToMax252CharLengthTest adjustAssertFailureMessageToMax252CharLengthTest = new FB_AdjustAssertFailureMessageToMax252CharLengthTest(errorItems, "AdjustAssertFailureMessageToMax252CharLengthTest");
            FB_CheckIfSpecificTestIsFinished checkIfSpecificTestIsFinished = new FB_CheckIfSpecificTestIsFinished(errorItems, "CheckIfSpecificTestIsFinished");

            log.Info("Done.");

            CleanUp();

            return(Constants.RETURN_SUCCESSFULL);
        }
コード例 #6
0
        /// <summary>
        ///  Durch die Hauptschnittstelle ITcSysManager können wir ein neue PLCProjekt generieren. Die Lookup-Methode ist implementiert.
        /// </summary>

        protected ITcSmTreeItem CreatePlcProject(IWorker worker)
        {
            if (worker.CancellationPending)
            {
                throw new Exception("Execution cancelled!");
            }

            OrderScriptContext context           = (OrderScriptContext)_context;
            ConfigurationInfo  configurationInfo = context.Order.ConfigurationInfo;

            string        plcProjectName = configurationInfo.PlcProjectName;
            ITcSmTreeItem plcConfig      = systemManager.LookupTreeItem("TIPC"); //Navigation im PLC struktur im TC.

            worker.ProgressStatus = string.Format("Creating empty PLC Project '{0}' ...", plcProjectName);
            ITcSmTreeItem plcProjectRoot = plcConfig.CreateChild(plcProjectName, 0, "", vsXaePlcEmptyTemplateName); //diese Method ausführen, um ein neues PLCProjekt erstellen.

            ITcPlcProject plcProjectRootIec = (ITcPlcProject)plcProjectRoot;                                        //setting the properties of the PLC Project like boot option.

            plcProjectRootIec.BootProjectAutostart = true;
            plcProjectRootIec.GenerateBootProject(true);

            ITcSmTreeItem plcProject = plcProjectRoot.LookupChild(plcProjectName + " Project"); // for example Schulung and Schulung Project. Now the ITcTreeItem pointer is pointing Schulung Project

            foreach (PlcObjectInfo plcObjectInfo in context.Order.ConfigurationInfo.PlcObjects) //Creating all the components of the PLC Project
            {
                if (worker.CancellationPending)
                {
                    throw new Exception("Execution cancelled!");
                }

                switch (plcObjectInfo.Type)  // we can have library, placeholder, POU, ITf, GVL and so on...
                {
                case PlcObjectType.DataType: // PlcObjectType defined in ScriptInfo.
                    createWorksheet((WorksheetInfo)plcObjectInfo, plcProject, worker);
                    break;

                case PlcObjectType.Library:
                    createLibrary((LibraryInfo)plcObjectInfo, plcProject, worker);
                    break;

                case PlcObjectType.Placeholder:
                    createPlaceholder((PlaceholderInfo)plcObjectInfo, plcProject, worker);
                    break;

                case PlcObjectType.POU:      // first create folder and then create POU
                    createWorksheet((WorksheetInfo)plcObjectInfo, plcProject, worker);
                    break;

                case PlcObjectType.Itf:
                    createWorksheet((WorksheetInfo)plcObjectInfo, plcProject, worker);
                    break;

                case PlcObjectType.Gvl:
                    createWorksheet((WorksheetInfo)plcObjectInfo, plcProject, worker);
                    break;

                default:
                    Debug.Fail("");
                    break;
                }
            }

            ITcSmTreeItem realtimeTasks = systemManager.LookupTreeItem("TIRT");     //creating tasks
            ITcSmTreeItem rtTask        = realtimeTasks.CreateChild("PlcTask", TreeItemType.Task.AsInt32());

            ITcSmTreeItem taskRef = null;

            worker.ProgressStatus = "Linking PLC instance with task 'PlcTask' ...";

            if (!TryLookupChild(plcProject, "PlcTask", out taskRef))
            {
                if (worker.CancellationPending)
                {
                    throw new Exception("Execution cancelled!");
                }

                taskRef = plcProject.CreateChild("PlcTask", TreeItemType.PlcTask.AsInt32(), "", "MAIN");
            }

            //foreach (ITcSmTreeItem prog in taskRef)
            //{
            //    string name = prog.Name;
            //}

            //ErrorItems errors;

            //if (worker.CancellationPending)
            //    throw new Exception("Execution cancelled!");

            //bool ok = CompileProject(worker, out errors);

            //if (!ok)
            //    throw new ApplicationException(string.Format("Plc Project compile produced '{0}' errors.  Please see Visual Studio Error Window for details.!", plcProject.Name));

            return(plcProject);
        }