private void EvalCodeCover(string reportFile)
        {
            if (File.Exists(reportFile) == false)
            {
                _context.ConsoleWrite(reportFile + " NOT FOUND!");
                return;
            }

            List <string> asmblyNames  = SlnFileHelper.GetAssemblyNames(_slnFilePath);
            ReportResult  reportResult = XmlHelper.XmlDeserializeFromFile <ReportResult>(reportFile);


            if (reportResult.Assemblies != null)
            {
                foreach (var a in reportResult.Assemblies)
                {
                    if (a.Name.EndsWith(".UnitTest", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    // 只计算解决方案中包含的项目
                    if (asmblyNames.FirstOrDefault(x => x.Equals(a.Name, StringComparison.OrdinalIgnoreCase)) == null)
                    {
                        continue;
                    }

                    _codeCoverResults.Add(new UnitTestResult {
                        ProjectName = a.Name,
                        Total       = a.TotalStatements,
                        Passed      = a.CoveredStatements
                    });
                }
            }
        }
Exemple #2
0
        internal static void ShareExecute(TaskContext context, TaskAction action, Action <string> callback)
        {
            if (action.Items == null || action.Items.Length == 0)
            {
                action.Items = context.JobOption.SlnFiles;
            }

            if (action.Items == null || action.Items.Length == 0)
            {
                return;
            }

            foreach (string sln in action.Items)
            {
                List <string> projectFiles = SlnFileHelper.GetProjectFiles(sln);

                foreach (string file in projectFiles)
                {
                    string projectName = Path.GetFileNameWithoutExtension(file);
                    if (projectName.EndsWith("Test"))
                    {
                        continue;
                    }

                    // 忽略WEB项目
                    if (file.IndexOf(".Web") > 0)
                    {
                        continue;
                    }

                    callback(file);
                }
            }
        }
Exemple #3
0
        private List <Assembly> LoadAssemblies(AssemblyScanOption option)
        {
            List <string> asmNames = SlnFileHelper.GetAssemblyNames(option.Sln);

            string[] files = Directory.GetFiles(option.Bin, "*.dll", SearchOption.TopDirectoryOnly);

            List <Assembly> list = new List <Assembly>();

            foreach (string file in files)
            {
                string filename = Path.GetFileNameWithoutExtension(file);

                if (asmNames.FindIndex(x => x.EqualsIgnoreCase(filename)) < 0)
                {
                    continue;
                }

                // 过滤程序集,加快速度
                //if( filename.StartsWith("MySoft", StringComparison.OrdinalIgnoreCase) == false )
                //	continue;

                // 忽略网站项目
                if (filename.IndexOf("WebApplication", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    continue;
                }
                if (filename.IndexOf("WebSite", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    continue;
                }

                // 忽略测试项目的程序集
                if (filename.IndexOf("Test", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    continue;
                }

                try {
                    Assembly assembly = Assembly.LoadFrom(file);

                    if (assembly != null)
                    {
                        list.Add(assembly);
                    }
                }
                catch (Exception ex) {
                    _invaidAssemblyList.Add(new KeyValuePair <string, Exception>(file, ex));
                }
            }
            return(list);
        }
        private List <string> GetCodeAnalysisLogFiles(string slnFilePath)
        {
            List <SlnProjectInfo> list  = SlnFileHelper.GetProjects(slnFilePath);
            List <string>         files = new List <string>();

            foreach (SlnProjectInfo project in list)
            {
                if (string.IsNullOrEmpty(project.OutputPath) || string.IsNullOrEmpty(project.AssemblyName))
                {
                    continue;
                }

                string file = Path.Combine(project.OutputPath, project.AssemblyName + ".dll.CodeAnalysisLog.xml");
                files.Add(file);
            }

            return(files);
        }
Exemple #5
0
        public List <ProjectCheckResult> Execute(BranchSettings branch, string slnFilePath)
        {
            string slnPath = Path.GetDirectoryName(slnFilePath);
            List <ProjectCheckResult> result = new List <ProjectCheckResult>();

            //string[] files = Directory.GetFiles(slnPath, "*.csproj", SearchOption.AllDirectories);
            List <string> files = SlnFileHelper.GetProjectFiles(slnFilePath);

            foreach (string filePath in files)
            {
                // 忽略WEB项目
                if (filePath.IndexOf(".Web", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    continue;
                }

                // 忽略测试项目
                if (filePath.IndexOf("Test", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    continue;
                }


                Project project = SlnFileHelper.ParseCsprojFile(filePath);

                // 判断项目是不是【纯类库】项目
                bool isLibrary = project.IsLibrary() && project.IsWebApplication() == false;


                foreach (var group in project.Groups)
                {
                    if (string.IsNullOrEmpty(group.Condition))
                    {
                        continue;
                    }

                    Match match = Regex.Match(group.Condition, @"'(Debug|Release)\|AnyCPU'", RegexOptions.IgnoreCase);
                    if (match.Success == false)
                    {
                        continue;
                    }

                    string configuration = match.Groups[1].Value;

                    if (string.IsNullOrEmpty(group.OutputPath) ||
                        group.OutputPath.TrimEnd('\\').Equals("bin", StringComparison.OrdinalIgnoreCase) == false)
                    {
                        result.Add(new ProjectCheckResult {
                            ProjectName   = filePath.Substring(slnPath.Length + 1),
                            Configuration = configuration,
                            Reason        = "SPEC:P00001; 请将项目的【输出路径】设置为【bin】"
                        });
                    }


                    if (isLibrary && string.IsNullOrEmpty(group.DocumentationFile))
                    {
                        result.Add(new ProjectCheckResult {
                            ProjectName   = filePath.Substring(slnPath.Length + 1),
                            Configuration = configuration,
                            Reason        = "SPEC:P00002; 请将【XML文档文件】设置为选中状态"
                        });
                    }

                    if (string.IsNullOrEmpty(group.TreatWarningsAsErrors))
                    {
                        result.Add(new ProjectCheckResult {
                            ProjectName   = filePath.Substring(slnPath.Length + 1),
                            Configuration = configuration,
                            Reason        = "SPEC:P00003; 请将【警告视为错误】设置为【全部】"
                        });
                    }
                }
            }


            foreach (var x in result)
            {
                x.RuleCode = x.GetRuleCode();
            }


            return(result);
        }
        public SlnUnitTestResult Execute(BranchSettings branch, string slnFilePath)
        {
            if (File.Exists(slnFilePath) == false)
            {
                throw new FileNotFoundException("指定的文件不存在:" + slnFilePath);
            }

            _slnFilePath = slnFilePath;
            _branch      = branch;


            if (string.IsNullOrEmpty(s_nunitPath) || string.IsNullOrEmpty(s_dotCoverPath))
            {
                _context.ConsoleWrite("NUnit or ReSharper dotCover NOT INSTALL");
                return(null);
            }


            //在 sln 文件中,类型项目,单元测试项目,WebApplication项目的GUID类型是一样,没法区开,所以就用名字来识别了。
            string[] projectFiles = SlnFileHelper.GetProjectFiles(slnFilePath)
                                    .Where(x => x.EndsWithIgnoreCase(".UnitTest.csproj")).ToArray();


            List <UnitTestWorker> threads = new List <UnitTestWorker>();

            foreach (string file in projectFiles)
            {
                UnitTestWorker worker = new UnitTestWorker(_context, s_nunitPath, s_dotCoverPath, file);
                threads.Add(worker);

                // 运行每个单元测试项目
                worker.Execute();
            }


            // 等待所有线程执行
            foreach (UnitTestWorker worker in threads)
            {
                worker.Wait();

                if (worker.Output.Length > 0)
                {
                    _context.ConsoleWrite(worker.Output.ToString());
                }

                if (worker.Result != null)
                {
                    _unitTestResults.Add(worker.Result);
                }
            }



            //汇总统计单元测试代码覆盖率
            string mergeFile  = MergedSnapshots(projectFiles);
            string reportFile = CreateCoverReport(mergeFile);

            EvalCodeCover(reportFile);


            SlnUnitTestResult total = new SlnUnitTestResult();

            total.UnitTestResults  = _unitTestResults;
            total.CodeCoverResults = _codeCoverResults;
            return(total);
        }