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