/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public TagDiffResult GetResult() { WriteOnce.SafeLog("TagDiffCommand::Run", LogLevel.Trace); WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Tag Diff")); TagDiffResult tagDiffResult = new TagDiffResult() { AppVersion = Utils.GetVersionString() }; //save to quiet analyze cmd and restore WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; try { #region setup analyze calls AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeOptions { SourcePath = _options.SourcePath1, CustomRulesPath = _options.CustomRulesPath, IgnoreDefaultRules = _options.IgnoreDefaultRules, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log }); AnalyzeCommand cmd2 = new AnalyzeCommand(new AnalyzeOptions { SourcePath = _options.SourcePath2, CustomRulesPath = _options.CustomRulesPath, IgnoreDefaultRules = _options.IgnoreDefaultRules, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log }); AnalyzeResult analyze1 = cmd1.GetResult(); AnalyzeResult analyze2 = cmd2.GetResult(); //restore WriteOnce.Verbosity = saveVerbosity; #endregion setup analyze calls bool equalTagsCompare1 = true; bool equalTagsCompare2 = true; //process results for each analyze call before comparing results if (analyze1.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR, _options.SourcePath1)); } else if (analyze2.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR, _options.SourcePath2)); } else if (analyze1.ResultCode == AnalyzeResult.ExitCode.NoMatches || analyze2.ResultCode == AnalyzeResult.ExitCode.NoMatches) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.TAGDIFF_NO_TAGS_FOUND)); } else //compare tag results; assumed (result1&2 == AnalyzeCommand.ExitCode.Success) { int count1 = 0; int sizeTags1 = analyze1.Metadata.UniqueTags.Count; string[] file1Tags = new string[sizeTags1]; foreach (string tag in analyze1.Metadata.UniqueTags.Keys.ToList <string>()) { file1Tags[count1++] = tag; } int count2 = 0; int sizeTags2 = analyze2.Metadata.UniqueTags.Count; string[] file2Tags = new string[sizeTags2]; foreach (string tag in analyze2.Metadata.UniqueTags.Keys.ToList <string>()) { file2Tags[count2++] = tag; } //can't simply compare counts as content may differ; must compare both in directions in two passes a->b; b->a equalTagsCompare1 = CompareTags(file1Tags, file2Tags, ref tagDiffResult, TagDiff.DiffSource.Source1); //reverse order for second pass equalTagsCompare2 = CompareTags(file2Tags, file1Tags, ref tagDiffResult, TagDiff.DiffSource.Source2); //final results bool resultsDiffer = !(equalTagsCompare1 && equalTagsCompare2); if (_arg_tagTestType == TagTestType.Inequality && !resultsDiffer) { tagDiffResult.ResultCode = TagDiffResult.ExitCode.TestFailed; } else if (_arg_tagTestType == TagTestType.Equality && resultsDiffer) { tagDiffResult.ResultCode = TagDiffResult.ExitCode.TestFailed; } else { tagDiffResult.ResultCode = TagDiffResult.ExitCode.TestPassed; } } } catch (OpException e) { WriteOnce.Verbosity = saveVerbosity; WriteOnce.Error(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(tagDiffResult); }
/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public TagTestResult GetResult() { WriteOnce.SafeLog("TagTestCommand::Run", LogLevel.Trace); WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Tag Test")); TagTestResult tagTestResult = new TagTestResult() { AppVersion = Utils.GetVersionString() }; //init based on true or false present argument value WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; AnalyzeResult.ExitCode analyzeCmdResult = AnalyzeResult.ExitCode.CriticalError; try { //setup analyze call with silent option AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeOptions { SourcePath = _options.SourcePath, IgnoreDefaultRules = true, CustomRulesPath = _options.CustomRulesPath, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log }); //get and perform initial analyze on results AnalyzeResult analyze1 = cmd1.GetResult(); //restore WriteOnce.Verbosity = saveVerbosity; if (analyze1.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR)); } else if (analyzeCmdResult == AnalyzeResult.ExitCode.NoMatches) { WriteOnce.General(MsgHelp.FormatString(MsgHelp.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); tagTestResult.ResultCode = _arg_tagTestType == TagTestType.RulesPresent ? TagTestResult.ExitCode.TestFailed : TagTestResult.ExitCode.TestPassed; } else //assumed (result == AnalyzeCommand.ExitCode.MatchesFound) { tagTestResult.ResultCode = TagTestResult.ExitCode.TestPassed; int count = 0; int sizeTags = analyze1.Metadata.UniqueTags.Count; string[] tagsFound = new string[sizeTags]; foreach (string tag in analyze1.Metadata.UniqueTags.Keys.ToList <string>()) { tagsFound[count++] = tag; } foreach (Rule rule in _rulesSet) { //supports both directions by generalizing string[] testList1 = _arg_tagTestType == TagTestType.RulesNotPresent ? rule.Tags : tagsFound; string[] testList2 = _arg_tagTestType == TagTestType.RulesNotPresent ? tagsFound : rule.Tags; foreach (string t in testList2) { if (TagTest(testList1, t)) { tagTestResult.TagsStatusList.Add(new TagStatus() { Tag = t, Detected = true }); } else { tagTestResult.ResultCode = TagTestResult.ExitCode.TestFailed; tagTestResult.TagsStatusList.Add(new TagStatus() { Tag = t, Detected = false }); } } } } } catch (OpException e) { WriteOnce.Verbosity = saveVerbosity; WriteOnce.Error(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(tagTestResult); }