Exemplo n.º 1
0
        public void slotInterruption(object sender, System.EventArgs e)
        {
            if (isRunning())
            {
                // End process
                if (!process.HasExited)
                {
                    process.Kill();
                }
            }
            // Remove result
            if (File.Exists(resultFile))
            {
                File.Delete(resultFile);
            }

            output.WriteLine("Code coverage aborted.");

            this.process = null;

            CoverageEnvironment.emitFinishCoverage();
        }
Exemplo n.º 2
0
        private void StartImpl(string solutionFolder, string platform, string dllFolder, string dllFilename, string workingDirectory, string commandline)
        {
            try
            {
                if (Settings.UseNativeCoverageSupport)
                {
                    // Delete old coverage file
                    string resultFile    = Path.Combine(solutionFolder, "CodeCoverage.tmp.cov");
                    string defResultFile = Path.Combine(solutionFolder, "CodeCoverage.cov");
                    if (File.Exists(resultFile))
                    {
                        File.Delete(resultFile);
                    }

                    // Find the executables for Coverage.exe
                    string location = typeof(CoverageExecution).Assembly.Location;
                    string folder   = Path.GetDirectoryName(location);

                    // Create your Process
                    Process process = new Process();
                    if (platform == "x86")
                    {
                        process.StartInfo.FileName = Path.Combine(folder, "Resources\\Coverage-x86.exe");
                    }
                    else
                    {
                        process.StartInfo.FileName = Path.Combine(folder, "Resources\\Coverage-x64.exe");
                    }

                    if (!File.Exists(process.StartInfo.FileName))
                    {
                        throw new NotSupportedException("Coverage.exe instance for platform was not found. Expected: " + process.StartInfo.FileName);
                    }

                    string sourcesFilter = solutionFolder;
                    int    smidx         = sourcesFilter.IndexOf(' ');
                    if (smidx > 0)
                    {
                        sourcesFilter = sourcesFilter.Substring(0, smidx);
                        int lidx = sourcesFilter.LastIndexOf('\\');
                        if (lidx >= 0)
                        {
                            sourcesFilter = sourcesFilter.Substring(0, lidx - 1);
                        }
                    }

                    StringBuilder argumentBuilder = new StringBuilder();

                    argumentBuilder.Append("-o \"");
                    argumentBuilder.Append(resultFile);
                    argumentBuilder.Append("\" -p \"");
                    argumentBuilder.Append(solutionFolder.TrimEnd('\\', '/'));

                    if (workingDirectory != null && workingDirectory.Length > 0)
                    {
                        // When directory finish by \ : c++ read \" and arguments is badly computed !
                        workingDirectory = workingDirectory.TrimEnd('\\', '/');

                        argumentBuilder.Append("\" -w \"");
                        argumentBuilder.Append(workingDirectory);
                    }
                    else
                    {
                        argumentBuilder.Append("\"");
                    }

                    if (dllFilename.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
                    {
                        argumentBuilder.Append("\" -- \"");
                    }
                    else
                    {
                        argumentBuilder.Append("\" -- ");
                        argumentBuilder.Append(@"""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe""");
                        argumentBuilder.Append(" /Platform:" + platform + " \"");
                    }

                    argumentBuilder.Append(Path.Combine(dllFolder, dllFilename));
                    if (commandline != null && commandline.Length > 0)
                    {
                        argumentBuilder.Append("\" ");
                        argumentBuilder.Append(commandline);
                    }
                    else
                    {
                        argumentBuilder.Append("\"");
                    }

#if DEBUG
                    this.output.WriteLine("Execute coverage: {0}", argumentBuilder.ToString());
#endif

                    process.StartInfo.WorkingDirectory       = dllFolder;
                    process.StartInfo.Arguments              = argumentBuilder.ToString();
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;

                    process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                    process.ErrorDataReceived  += new DataReceivedEventHandler(OutputHandler);

                    process.Start();

                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    bool exited = false;
                    while (!exited && lastEvent.AddMinutes(15) > DateTime.UtcNow)
                    {
                        exited = process.WaitForExit(1000 * 60);
                    }

                    if (!exited)
                    {
                        // Kill process.
                        process.Kill();
                        throw new Exception("Creating code coverage timed out (more than 15min).");
                    }

                    string output = tb.ToString();

                    if (output.Contains("Usage:"))
                    {
                        throw new Exception("Incorrect command line argument passed to coverage tool");
                    }

                    if (output.Contains("Error: the test source file"))
                    {
                        throw new Exception("Cannot find test source file " + dllFilename);
                    }

                    if (File.Exists(resultFile))
                    {
                        // All fine, update file:
                        if (File.Exists(defResultFile))
                        {
                            File.Delete(defResultFile);
                        }
                        File.Move(resultFile, defResultFile);
                    }
                    else
                    {
                        this.output.WriteLine("No coverage report generated. Cannot continue.");
                    }
                }
                else
                {
                    // Delete old coverage file
                    string resultFile    = Path.Combine(solutionFolder, "CodeCoverage.tmp.xml");
                    string defResultFile = Path.Combine(solutionFolder, "CodeCoverage.xml");
                    if (File.Exists(resultFile))
                    {
                        File.Delete(resultFile);
                    }

                    // Create your Process
                    Process process = new Process();
                    if (platform == "x86")
                    {
                        process.StartInfo.FileName = @"c:\Program Files\OpenCppCoverage\x86\OpenCppCoverage.exe";

                        if (!File.Exists(process.StartInfo.FileName))
                        {
                            process.StartInfo.FileName = @"c:\Program Files (x86)\OpenCppCoverage\OpenCppCoverage.exe";
                        }
                    }
                    else
                    {
                        process.StartInfo.FileName = @"c:\Program Files\OpenCppCoverage\OpenCppCoverage.exe";
                    }

                    if (!File.Exists(process.StartInfo.FileName))
                    {
                        throw new NotSupportedException("OpenCPPCoverage was not found. Expected: " + process.StartInfo.FileName);
                    }

                    string sourcesFilter = solutionFolder;
                    int    smidx         = sourcesFilter.IndexOf(' ');
                    if (smidx > 0)
                    {
                        sourcesFilter = sourcesFilter.Substring(0, smidx);
                        int lidx = sourcesFilter.LastIndexOf('\\');
                        if (lidx >= 0)
                        {
                            sourcesFilter = sourcesFilter.Substring(0, lidx - 1);
                        }
                    }

                    StringBuilder argumentBuilder = new StringBuilder();
                    if (dllFilename.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
                    {
                        argumentBuilder.Append("--quiet --export_type cobertura:\"");
                        argumentBuilder.Append(resultFile);
                        argumentBuilder.Append("\" --continue_after_cpp_exception --cover_children ");
                        argumentBuilder.Append("--sources ");
                        argumentBuilder.Append(sourcesFilter);
                        argumentBuilder.Append(" -- \"");
                        argumentBuilder.Append(Path.Combine(dllFolder, dllFilename));
                        argumentBuilder.Append("\"");
                    }
                    else
                    {
                        argumentBuilder.Append("--quiet --export_type cobertura:\"");
                        argumentBuilder.Append(resultFile);
                        argumentBuilder.Append("\" --continue_after_cpp_exception --cover_children ");
                        argumentBuilder.Append("--sources ");
                        argumentBuilder.Append(sourcesFilter);
                        argumentBuilder.Append(" -- ");
                        argumentBuilder.Append(@"""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe""");
                        argumentBuilder.Append(" /Platform:" + platform + " \"");
                        argumentBuilder.Append(Path.Combine(dllFolder, dllFilename));
                        argumentBuilder.Append("\"");
                    }

#if DEBUG
                    this.output.WriteLine("Execute coverage: {0}", argumentBuilder.ToString());
#endif

                    process.StartInfo.WorkingDirectory       = Path.GetDirectoryName(resultFile);
                    process.StartInfo.Arguments              = argumentBuilder.ToString();
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;

                    process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                    process.ErrorDataReceived  += new DataReceivedEventHandler(OutputHandler);

                    process.Start();

                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    bool exited = false;
                    while (!exited && lastEvent.AddMinutes(15) > DateTime.UtcNow)
                    {
                        exited = process.WaitForExit(1000 * 60);
                    }

                    if (!exited)
                    {
                        // Kill process.
                        process.Kill();
                        throw new Exception("Creating code coverage timed out.");
                    }

                    string output = tb.ToString();

                    if (output.Contains("Usage:"))
                    {
                        throw new Exception("Incorrect command line argument passed to coverage tool");
                    }

                    if (output.Contains("Error: the test source file"))
                    {
                        throw new Exception("Cannot find test source file " + dllFilename);
                    }

                    if (File.Exists(resultFile))
                    {
                        // All fine, update file:
                        if (File.Exists(defResultFile))
                        {
                            File.Delete(defResultFile);
                        }
                        File.Move(resultFile, defResultFile);
                    }
                    else
                    {
                        this.output.WriteLine("No coverage report generated. Cannot continue.");
                    }
                }
            }
            catch (Exception ex)
            {
                output.WriteLine("Uncaught error during coverage execution: {0}", ex.Message);
            }
            Data.ReportManagerSingleton.Instance(dte).ResetData();
            Interlocked.Exchange(ref running, 0);
        }
        private void ProjectContextMenuItemCallback(object sender, EventArgs e)
        {
            var          dte          = this.dte.DTE;
            OutputWindow outputWindow = null;

            try
            {
                outputWindow = new OutputWindow(dte);

                OleMenuCommand menuCommand = sender as OleMenuCommand;
                if (menuCommand != null && dte != null)
                {
                    Array selectedProjects = (Array)dte.ActiveSolutionProjects;
                    //only support 1 selected project
                    if (selectedProjects.Length == 1)
                    {
                        EnvDTE.Project project = (EnvDTE.Project)selectedProjects.GetValue(0);
                        var            vcproj  = project.Object as VCProject;
                        if (vcproj != null)
                        {
                            IVCCollection   configs = (IVCCollection)vcproj.Configurations;
                            VCConfiguration cfg     = (VCConfiguration)vcproj.ActiveConfiguration;
                            VCDebugSettings debug   = (VCDebugSettings)cfg.DebugSettings;

                            string command          = null;
                            string arguments        = null;
                            string workingDirectory = null;
                            if (debug != null)
                            {
                                command          = cfg.Evaluate(debug.Command);
                                workingDirectory = cfg.Evaluate(debug.WorkingDirectory);
                                arguments        = cfg.Evaluate(debug.CommandArguments);
                            }

                            VCPlatform currentPlatform = (VCPlatform)cfg.Platform;

                            string platform = currentPlatform == null ? null : currentPlatform.Name;
                            if (platform != null)
                            {
                                platform = platform.ToLower();
                                if (platform.Contains("x64"))
                                {
                                    platform = "x64";
                                }
                                else if (platform.Contains("x86") || platform.Contains("win32"))
                                {
                                    platform = "x86";
                                }
                                else
                                {
                                    throw new NotSupportedException("Platform is not supported.");
                                }
                            }
                            else
                            {
                                cfg      = (VCConfiguration)configs.Item("Debug|x64");
                                platform = "x64";

                                if (cfg == null)
                                {
                                    throw new NotSupportedException("Cannot find x64 platform for project.");
                                }
                            }

                            if (command == null || String.IsNullOrEmpty(command))
                            {
                                command = cfg.PrimaryOutput;
                            }

                            if (command != null)
                            {
                                var solutionFolder = System.IO.Path.GetDirectoryName(dte.Solution.FileName);

                                CoverageExecution executor = new CoverageExecution(dte, outputWindow);
                                executor.Start(
                                    solutionFolder,
                                    platform,
                                    System.IO.Path.GetDirectoryName(command),
                                    System.IO.Path.GetFileName(command),
                                    workingDirectory,
                                    arguments);
                            }
                        }
                    }
                }
            }
            catch (NotSupportedException ex)
            {
                if (outputWindow != null)
                {
                    outputWindow.WriteLine("Error running coverage: {0}", ex.Message);
                }
            }
            catch (Exception ex)
            {
                if (outputWindow != null)
                {
                    outputWindow.WriteLine("Unexpected code coverage failure; error: {0}", ex.ToString());
                }
            }
        }