void WritePartialRuleDetails(RuleSet rules) { WriteOnce.Result("RuleId,Rulename,RuleDesc,Tags,AppliesToLanguage", true, WriteOnce.ConsoleVerbosity.High); //option to write out partial rule data foreach (Rule r in rules) { string tags = ""; string languages = ""; foreach (string tag in r.Tags) { tags += tag + ","; } tags = tags.Remove(tags.Length - 1); if (r.AppliesTo != null && r.AppliesTo.Length > 0) { foreach (string lang in r.AppliesTo) { languages += lang + ","; } languages = languages.Remove(languages.Length - 1); } else { languages = "Not-specified-so-all"; } WriteOnce.Result(string.Format("{0},{1},{2},{3},{4}", r.Id, r.Name, r.Description, tags, languages), true, WriteOnce.ConsoleVerbosity.High); } }
bool CompareTags(string[] fileTags1, string[] fileTags2) { bool found = true; //are all tags in file1 found in file2 foreach (string s1 in fileTags1) { if (!fileTags2.Contains(s1)) { found = false; WriteOnce.Result(s1, true, WriteOnce.ConsoleVerbosity.High); } } //none missing if (found) { WriteOnce.Result(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_NONE), true, WriteOnce.ConsoleVerbosity.High); } return(found); }
public int Run() { WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "Exporttags")); SortedDictionary <string, string> uniqueTags = new SortedDictionary <string, string>(); foreach (Rule r in _rules) { //builds a list of unique tags foreach (string t in r.Tags) { if (uniqueTags.ContainsKey(t)) { continue; } else { uniqueTags.Add(t, t); } } } //separate loop so results are sorted (Sorted type) foreach (string s in uniqueTags.Values) { WriteOnce.Result(s, true); } WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Exporttags")); WriteOnce.FlushAll(); if (!String.IsNullOrEmpty(_arg_outputFile)) { WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Low); } return((int)ExitCode.Success); }
public int Run() { WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "Tagdiff")); //setup output TextWriter outputWriter; if (!string.IsNullOrEmpty(_arg_outputFile)) { outputWriter = File.CreateText(_arg_outputFile); outputWriter.WriteLine(Program.GetVersionString()); WriteOnce.Writer = outputWriter; } if (_arg_src1 == _arg_src2) { throw new OpException(ErrMsg.GetString(ErrMsg.ID.TAGDIFF_SAME_FILE_ARG)); } else if (string.IsNullOrEmpty(_arg_src1) || string.IsNullOrEmpty(_arg_src2)) { throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_INVALID_ARG_VALUE)); } #region setup analyze calls //save to quiet analyze cmd WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; string tmp1 = Path.GetTempFileName(); string tmp2 = Path.GetTempFileName(); AnalyzeCommand.ExitCode result1 = AnalyzeCommand.ExitCode.CriticalError; AnalyzeCommand.ExitCode result2 = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_src1, OutputFilePath = tmp1, OutputFileFormat = "json", CustomRulesPath = _arg_rulesPath, IgnoreDefaultRules = _arg_ignoreDefault, SimpleTagsOnly = true, AllowDupTags = false, FilePathExclusions = "sample,example,test,docs,.vs,.git", ConsoleVerbosityLevel = "None" }); AnalyzeCommand cmd2 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_src2, OutputFilePath = tmp2, OutputFileFormat = "json", CustomRulesPath = _arg_rulesPath, IgnoreDefaultRules = _arg_ignoreDefault, SimpleTagsOnly = true, AllowDupTags = false, FilePathExclusions = "sample,example,test,docs,.vs,.git", ConsoleVerbosityLevel = "None" }); result1 = (AnalyzeCommand.ExitCode)cmd1.Run(); result2 = (AnalyzeCommand.ExitCode)cmd2.Run(); } catch (Exception e) { //restore WriteOnce.Verbosity = saveVerbosity; throw e; } //restore WriteOnce.Verbosity = saveVerbosity; #endregion bool successResult; bool equal1 = true; bool equal2 = true; //process results for each analyze call before comparing results if (result1 == AnalyzeCommand.ExitCode.CriticalError) { throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR, _arg_src1)); } else if (result2 == AnalyzeCommand.ExitCode.CriticalError) { throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR, _arg_src2)); } else if (result1 == AnalyzeCommand.ExitCode.NoMatches || result2 == AnalyzeCommand.ExitCode.NoMatches) { throw new OpException(ErrMsg.GetString(ErrMsg.ID.TAGDIFF_NO_TAGS_FOUND)); } else //compare tag results; assumed (result1&2 == AnalyzeCommand.ExitCode.MatchesFound) { string file1TagsJson = File.ReadAllText(tmp1); string file2TagsJson = File.ReadAllText(tmp2); var file1Tags = JsonConvert.DeserializeObject <TagsFile[]>(file1TagsJson).First(); var file2Tags = JsonConvert.DeserializeObject <TagsFile[]>(file2TagsJson).First(); //can't simply compare counts as content may differ; must compare both in directions in two passes a->b; b->a //first pass WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_GAP, Path.GetFileName(_arg_src1), Path.GetFileName(_arg_src2)), true, WriteOnce.ConsoleVerbosity.High); equal1 = CompareTags(file1Tags.Tags, file2Tags.Tags); //reverse order for second pass WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_GAP, Path.GetFileName(_arg_src2), Path.GetFileName(_arg_src1)), true, WriteOnce.ConsoleVerbosity.High); equal2 = CompareTags(file2Tags.Tags, file1Tags.Tags); //final results bool resultsDiffer = !(equal1 && equal2); if (_arg_tagTestType == TagTestType.Inequality && resultsDiffer) { successResult = true; } else if (_arg_tagTestType == TagTestType.Equality && !resultsDiffer) { successResult = true; } else { successResult = false; } WriteOnce.General(ErrMsg.GetString(ErrMsg.ID.TAGDIFF_RESULTS_DIFFER), false); WriteOnce.Result(resultsDiffer.ToString()); } //cleanup try { File.Delete(tmp1); File.Delete(tmp2); } catch { //no action needed; } WriteOnce.FlushAll(); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagdiff")); return(successResult ? (int)ExitCode.TestPassed : (int)ExitCode.TestFailed); }
public int Run() { WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "tagtest")); //init based on true or false present argument value bool testSuccess = true; //one file vs ruleset string tmp1 = Path.GetTempFileName(); WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; AnalyzeCommand.ExitCode result = AnalyzeCommand.ExitCode.CriticalError; //setup analyze call with silent option #region analyzesetup try { AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_srcPath, OutputFilePath = tmp1, OutputFileFormat = "json", CustomRulesPath = _arg_customRulesPath, IgnoreDefaultRules = _arg_ignoreDefaultRules, SimpleTagsOnly = true, AllowDupTags = false, ConsoleVerbosityLevel = "None" }); //quiet analysis commands result = (AnalyzeCommand.ExitCode)cmd1.Run(); } catch (Exception e) { WriteOnce.Verbosity = saveVerbosity; throw e; } //restore WriteOnce.Verbosity = saveVerbosity; if (result == AnalyzeCommand.ExitCode.CriticalError) { throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR)); } else if (result == AnalyzeCommand.ExitCode.NoMatches) { //results WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); if (_arg_tagTestType == TagTestType.RulesPresent) { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_FAIL), true, ConsoleColor.Red, WriteOnce.ConsoleVerbosity.Low); } else { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low); } WriteOnce.FlushAll(); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagtest")); return((int)ExitCode.CriticalError); } #endregion //assumed (result == AnalyzeCommand.ExitCode.MatchesFound) string file1TagsJson = File.ReadAllText(tmp1); var file1TagsObj = JsonConvert.DeserializeObject <TagsFile[]>(file1TagsJson); var file1Tags = file1TagsObj.First(); // here we have a single FileList object File.Delete(tmp1); foreach (Rule r in _rulesSet) { //supports both directions by generalizing string[] testList1 = _arg_tagTestType == TagTestType.RulesNotPresent ? r.Tags : file1Tags.Tags; string[] testList2 = _arg_tagTestType == TagTestType.RulesNotPresent ? file1Tags.Tags : r.Tags; foreach (string t in testList2) { if (TagTest(testList1, t)) { WriteOnce.Result(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TAGS_FOUND, t), true, WriteOnce.ConsoleVerbosity.High); } else { testSuccess = false; WriteOnce.Result(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TAGS_MISSING, t), true, WriteOnce.ConsoleVerbosity.High); } } } //results WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); if (testSuccess) { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low); } else { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_FAIL), true, ConsoleColor.Red, WriteOnce.ConsoleVerbosity.Low); } WriteOnce.FlushAll(); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagtest")); return(testSuccess ? (int)ExitCode.NoDiff : (int)ExitCode.DiffFound); }