Esempio n. 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);
 }
Esempio n. 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);
 }
Esempio n. 3
0
        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);
        }
Esempio n. 4
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);
        }
        /// <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);
        }