コード例 #1
0
        /// Compares a set of rules against a source path...
        /// Used for both RulesPresent and RulesNotePresent options
        /// Focus is pass/fail not detailed comparison output -see Tagdiff for more
        public TagTestCommand(TagTestCommandOptions opt)
        {
            _arg_srcPath               = opt.SourcePath;
            _arg_customRulesPath       = opt.CustomRulesPath;
            _arg_outputFile            = opt.OutputFilePath;
            _arg_consoleVerbosityLevel = opt.ConsoleVerbosityLevel ?? "medium";
            opt.TestType ??= "RulesPresent";
            _arg_logger = opt.Log;

            WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
            if (!Enum.TryParse(_arg_consoleVerbosityLevel, true, out verbosity))
            {
                throw new OpException(String.Format(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x")));
            }
            WriteOnce.Verbosity = verbosity;

            if (!Enum.TryParse(opt.TestType, true, out _arg_tagTestType))
            {
                throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, opt.TestType));
            }

            _arg_ignoreDefaultRules = opt.IgnoreDefaultRules;
            _rulesSet = new RuleSet(_arg_logger);

            if (string.IsNullOrEmpty(opt.CustomRulesPath) && _arg_ignoreDefaultRules)
            {
                throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_NORULES_SPECIFIED));
            }

            ConfigureRules();
            ConfigureOutput();
        }
コード例 #2
0
        public TagDiffCommand(TagDiffCommandOptions opt)
        {
            _arg_src1                  = opt.SourcePath1;
            _arg_src2                  = opt.SourcePath2;
            _arg_customRulesPath       = opt.CustomRulesPath;
            _arg_consoleVerbosityLevel = opt.ConsoleVerbosityLevel ?? "medium";

            _arg_ignoreDefault = opt.IgnoreDefaultRules;
            opt.TestType ??= "equality";
            _arg_outputFile = opt.OutputFilePath;
            _arg_logger     = opt.Log;


            WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
            if (!Enum.TryParse(_arg_consoleVerbosityLevel, true, out verbosity))
            {
                throw new OpException(String.Format(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x")));
            }
            WriteOnce.Verbosity = verbosity;

            if (!Enum.TryParse(opt.TestType, true, out _arg_tagTestType))
            {
                throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, opt.TestType));
            }
        }
コード例 #3
0
        private void WriteJsonResult()
        {
            //writes out json report for convenient link from report summary page(s)
            CLIAnalyzeCmdOptions jsonOptions = new CLIAnalyzeCmdOptions()
            {
                OutputFileFormat = "json",
                OutputFilePath   = "output.json"
            };

            //quiet normal write noise for json writter to just gen the file; then restore
            WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity;
            WriteOnce.Verbosity = WriteOnce.ConsoleVerbosity.None;
            AnalyzeJsonWriter jsonWriter = (AnalyzeJsonWriter)WriterFactory.GetWriter(jsonOptions);

            jsonWriter.WriteResults(_analyzeResult, jsonOptions);
            WriteOnce.Verbosity = saveVerbosity;
        }
コード例 #4
0
        public VerifyRulesCommand(VerifyRulesCommandOptions opt)
        {
            _arg_customRulesPath       = opt.CustomRulesPath;
            _arg_outputFile            = opt.OutputFilePath;
            _arg_consoleVerbosityLevel = opt.ConsoleVerbosityLevel ?? "medium";
            _arg_logger = opt.Log;

            WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
            if (!Enum.TryParse(_arg_consoleVerbosityLevel, true, out verbosity))
            {
                throw new OpException(String.Format(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x")));
            }
            WriteOnce.Verbosity = verbosity;

            ConfigOutput();
            ConfigRules();
        }
コード例 #5
0
        List <string> _fileExclusionList;//see exclusion list


        public AnalyzeCommand(AnalyzeCommandOptions opt)
        {
            _arg_sourcePath            = opt.SourcePath;
            _arg_outputFile            = opt.OutputFilePath;
            _arg_fileFormat            = opt.OutputFileFormat;
            _arg_outputTextFormat      = opt.TextOutputFormat;
            _arg_outputUniqueTagsOnly  = !opt.AllowDupTags;
            _arg_customRulesPath       = opt.CustomRulesPath;
            _arg_confidenceFilters     = opt.ConfidenceFilters ?? "high,medium";
            _arg_consoleVerbosityLevel = opt.ConsoleVerbosityLevel ?? "medium";
            _arg_autoBrowserOpen       = !opt.AutoBrowserOpen;
            _arg_ignoreDefaultRules    = opt.IgnoreDefaultRules;
            _arg_simpleTagsOnly        = opt.SimpleTagsOnly;
            _arg_logger = opt.Log;

            //if not called via CLI set default
            opt.FilePathExclusions = opt.FilePathExclusions ?? "sample,example,test,docs,.vs,.git";

            if (!string.IsNullOrEmpty(opt.FilePathExclusions))
            {
                _fileExclusionList = opt.FilePathExclusions.ToLower().Split(",").ToList <string>();
                if (_fileExclusionList != null && (_fileExclusionList.Contains("none") || _fileExclusionList.Contains("None")))
                {
                    _fileExclusionList.Clear();
                }
            }

            WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
            if (!Enum.TryParse(_arg_consoleVerbosityLevel, true, out verbosity))
            {
                throw new OpException(String.Format(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x")));
            }
            WriteOnce.Verbosity = verbosity;


            LastUpdated = DateTime.MinValue;
            DateScanned = DateTime.Now;

            ConfigOutput();
            ConfigSourcetoScan();
            ConfigConfidenceFilters();
            ConfigRules();

            _uniqueTagsControl = new HashSet <string>();
        }
コード例 #6
0
        public ExportTagsCommand(ExportTagsCommandOptions opt)
        {
            _rules = new RuleSet(WriteOnce.Log);
            _arg_customRulesPath       = opt.CustomRulesPath;
            _arg_outputFile            = opt.OutputFilePath;
            _arg_ignoreDefaultRules    = opt.IgnoreDefaultRules;
            _arg_consoleVerbosityLevel = opt.ConsoleVerbosityLevel ?? "medium";
            _arg_logger = opt.Log;

            WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
            if (!Enum.TryParse(_arg_consoleVerbosityLevel, true, out verbosity))
            {
                throw new OpException(String.Format(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x")));
            }
            WriteOnce.Verbosity = verbosity;

            ConfigureOutput();
            ConfigRules();
        }
コード例 #7
0
        public PackRulesCommand(PackRulesCommandOptions opt)
        {
            _path       = opt.RepackDefaultRules ? Utils.GetPath(Utils.AppPath.defaultRulesSrc) : opt.CustomRulesPath;
            _outputfile = opt.RepackDefaultRules && String.IsNullOrEmpty(opt.OutputFilePath) ? Utils.GetPath(Utils.AppPath.defaultRulesPackedFile) : opt.OutputFilePath;
            _arg_consoleVerbosityLevel = opt.ConsoleVerbosityLevel;

            if (!opt.RepackDefaultRules && string.IsNullOrEmpty(opt.CustomRulesPath) || string.IsNullOrEmpty(_outputfile))
            {
                throw new OpException(ErrMsg.FormatString(ErrMsg.ID.PACK_MISSING_OUTPUT_ARG));
            }

            _outputfile = Path.GetFullPath(_outputfile);
            _indent     = !opt.NotIndented;

            WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
            if (!Enum.TryParse(_arg_consoleVerbosityLevel, true, out verbosity))
            {
                throw new OpException(String.Format(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x")));
            }
            WriteOnce.Verbosity = verbosity;
        }
コード例 #8
0
        /// <summary>
        /// Establish console verbosity
        /// For NuGet DLL use, console is muted overriding any arguments sent
        /// </summary>
        private void ConfigureConsoleOutput()
        {
            WriteOnce.SafeLog("PackRulesCommand::ConfigureConsoleOutput", LogLevel.Trace);

            //Set console verbosity based on run context (none for DLL use) and caller arguments
            if (!Utils.CLIExecutionContext)
            {
                WriteOnce.Verbosity = WriteOnce.ConsoleVerbosity.None;
            }
            else
            {
                WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
                if (!Enum.TryParse(_options.ConsoleVerbosityLevel, true, out verbosity))
                {
                    throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_INVALID_ARG_VALUE, "-x"));
                }
                else
                {
                    WriteOnce.Verbosity = verbosity;
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// Establish console verbosity
        /// For NuGet DLL use, console is muted overriding any arguments sent
        /// </summary>
        void ConfigureConsoleOutput()
        {
            WriteOnce.SafeLog("ExportTagsCommand::ConfigureConsoleOutput", LogLevel.Trace);

            //Set console verbosity based on run context (none for DLL use) and caller arguments
            if (!Utils.CLIExecutionContext)
            {
                WriteOnce.Verbosity = WriteOnce.ConsoleVerbosity.None;
            }
            else
            {
                WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
                if (!Enum.TryParse(_arg_consoleVerbosityLevel, true, out verbosity))
                {
                    WriteOnce.Error(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x"));
                    throw new Exception(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x"));
                }
                else
                {
                    WriteOnce.Verbosity = verbosity;
                }
            }
        }
コード例 #10
0
        /// <summary>
        /// Main entry from CLI
        /// </summary>
        /// <returns></returns>
        public override 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,
                    FilePathExclusions    = "sample,example,test,docs,.vs,.git",
                    ConsoleVerbosityLevel = "None",
                    Log = _arg_logger
                });


                //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);
        }
コード例 #11
0
        /// <summary>
        /// Main entry from CLI
        /// </summary>
        /// <returns></returns>
        public override int Run()
        {
            WriteOnce.SafeLog("TagDiffCommand::Run", LogLevel.Trace);
            WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "tagdiff"));

            ExitCode exitCode = ExitCode.CriticalError;

            //save to quiet analyze cmd and restore
            WriteOnce.ConsoleVerbosity saveVerbosity     = WriteOnce.Verbosity;
            AnalyzeCommand.ExitCode    analyzeCmdResult1 = AnalyzeCommand.ExitCode.CriticalError;
            AnalyzeCommand.ExitCode    analyzeCmdResult2 = AnalyzeCommand.ExitCode.CriticalError;

            try
            {
                #region setup analyze calls

                string tmp1 = Path.GetTempFileName();
                string tmp2 = Path.GetTempFileName();

                AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeCommandOptions
                {
                    SourcePath            = _arg_src1,
                    OutputFilePath        = tmp1,
                    OutputFileFormat      = "json",
                    CustomRulesPath       = _arg_customRulesPath,
                    IgnoreDefaultRules    = _arg_ignoreDefault,
                    FilePathExclusions    = _arg_fileExclusionList,
                    SimpleTagsOnly        = true,
                    ConsoleVerbosityLevel = "none",
                    Log = _arg_logger
                });
                AnalyzeCommand cmd2 = new AnalyzeCommand(new AnalyzeCommandOptions
                {
                    SourcePath            = _arg_src2,
                    OutputFilePath        = tmp2,
                    OutputFileFormat      = "json",
                    CustomRulesPath       = _arg_customRulesPath,
                    IgnoreDefaultRules    = _arg_ignoreDefault,
                    FilePathExclusions    = _arg_fileExclusionList,
                    SimpleTagsOnly        = true,
                    ConsoleVerbosityLevel = "none",
                    Log = _arg_logger
                });


                analyzeCmdResult1 = (AnalyzeCommand.ExitCode)cmd1.Run();
                analyzeCmdResult2 = (AnalyzeCommand.ExitCode)cmd2.Run();

                ConfigureFileOutput();

                //restore
                WriteOnce.Verbosity = saveVerbosity;

                #endregion

                bool equalTagsCompare1 = true;
                bool equalTagsCompare2 = true;

                //process results for each analyze call before comparing results
                if (analyzeCmdResult1 == AnalyzeCommand.ExitCode.CriticalError)
                {
                    throw new Exception(ErrMsg.FormatString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR, _arg_src1));
                }
                else if (analyzeCmdResult2 == AnalyzeCommand.ExitCode.CriticalError)
                {
                    throw new Exception(ErrMsg.FormatString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR, _arg_src2));
                }
                else if (analyzeCmdResult1 == AnalyzeCommand.ExitCode.NoMatches || analyzeCmdResult2 == AnalyzeCommand.ExitCode.NoMatches)
                {
                    throw new Exception(ErrMsg.GetString(ErrMsg.ID.TAGDIFF_NO_TAGS_FOUND));
                }
                else //compare tag results; assumed (result1&2 == AnalyzeCommand.ExitCode.Success)
                {
                    //setup output here rather than top to avoid analyze command output in this command output
                    ConfigureFileOutput();
                    ConfigureConsoleOutput();//recheck

                    string file1TagsJson = File.ReadAllText(tmp1);
                    string file2TagsJson = File.ReadAllText(tmp2);

                    var file1Tags = JsonConvert.DeserializeObject <TagsFile>(file1TagsJson);
                    var file2Tags = JsonConvert.DeserializeObject <TagsFile>(file2TagsJson);

                    //can't simply compare counts as content may differ; must compare both in directions in two passes a->b; b->a
                    WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_GAP, Path.GetFileName(_arg_src1), Path.GetFileName(_arg_src2)),
                                      true, WriteOnce.ConsoleVerbosity.High);
                    equalTagsCompare1 = 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);
                    equalTagsCompare2 = CompareTags(file2Tags.Tags, file1Tags.Tags);

                    //final results
                    bool resultsDiffer = !(equalTagsCompare1 && equalTagsCompare2);
                    if (_arg_tagTestType == TagTestType.Inequality && !resultsDiffer)
                    {
                        exitCode = ExitCode.TestFailed;
                    }
                    else if (_arg_tagTestType == TagTestType.Equality && resultsDiffer)
                    {
                        exitCode = ExitCode.TestFailed;
                    }
                    else
                    {
                        exitCode = ExitCode.TestPassed;
                    }

                    WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_DIFFER), false);
                    WriteOnce.Result(resultsDiffer.ToString());
                    WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "tagdiff"));
                    if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext)
                    {
                        WriteOnce.Info(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, WriteOnce.ConsoleVerbosity.Low, false);
                    }

                    WriteOnce.FlushAll();
                }

                //cleanup
                try
                {
                    File.Delete(tmp1);
                    File.Delete(tmp2);
                }
                catch
                {
                    //no action needed;
                }
            }
            catch (Exception e)
            {
                WriteOnce.Verbosity = saveVerbosity;
                if (analyzeCmdResult1 == AnalyzeCommand.ExitCode.Success && analyzeCmdResult2 == AnalyzeCommand.ExitCode.Success) //error not previously logged
                {
                    WriteOnce.Error(e.Message);
                }
                else
                {
                    WriteOnce.Error(e.Message, true, WriteOnce.ConsoleVerbosity.Low, false);//console but don't log again
                }
                //exit normaly for CLI callers and throw for DLL callers
                if (Utils.CLIExecutionContext)
                {
                    return((int)ExitCode.CriticalError);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (_arg_close_log_on_exit)
                {
                    Utils.Logger  = null;
                    WriteOnce.Log = null;
                }
            }

            return((int)exitCode);
        }
コード例 #12
0
        /// <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);
        }
コード例 #13
0
        public override 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(Utils.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);
        }
コード例 #14
0
        /// <summary>
        /// Main entry from CLI
        /// </summary>
        /// <returns></returns>
        public override int Run()
        {
            WriteOnce.SafeLog("TagTestCommand::Run", LogLevel.Trace);
            WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "tagtest"));

            //init based on true or false present argument value
            ExitCode exitCode = ExitCode.CriticalError;

            WriteOnce.ConsoleVerbosity saveVerbosity    = WriteOnce.Verbosity;
            AnalyzeCommand.ExitCode    analyzeCmdResult = AnalyzeCommand.ExitCode.CriticalError;

            try
            {
                //one file vs ruleset
                string tmp1 = Path.GetTempFileName();

                //setup analyze call with silent option
                AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeCommandOptions
                {
                    SourcePath            = _arg_srcPath,
                    OutputFilePath        = tmp1,
                    OutputFileFormat      = "json",
                    IgnoreDefaultRules    = true,
                    CustomRulesPath       = _arg_customRulesPath,
                    FilePathExclusions    = _arg_fileExclusionList,
                    SimpleTagsOnly        = true,
                    ConsoleVerbosityLevel = "None",
                    Log = _arg_logger
                });


                //get and perform initial analyze on results
                analyzeCmdResult = (AnalyzeCommand.ExitCode)cmd1.Run();

                //must be done here to avoid losing our handle from analyze command overwriting WriteOnce.Writer
                ConfigureFileOutput();

                //restore
                WriteOnce.Verbosity = saveVerbosity;

                if (analyzeCmdResult == AnalyzeCommand.ExitCode.CriticalError)
                {
                    throw new Exception(ErrMsg.GetString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR));
                }
                else if (analyzeCmdResult == AnalyzeCommand.ExitCode.NoMatches)
                {
                    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.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "tagtest"));

                    exitCode = _arg_tagTestType == TagTestType.RulesPresent ? ExitCode.TestFailed : ExitCode.TestPassed;
                }
                else //assumed (result == AnalyzeCommand.ExitCode.MatchesFound)
                {
                    string file1TagsJson = File.ReadAllText(tmp1);
                    var    file1Tags     = JsonConvert.DeserializeObject <TagsFile>(file1TagsJson);
                    File.Delete(tmp1);

                    exitCode = ExitCode.TestPassed;
                    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
                            {
                                exitCode = ExitCode.TestFailed;
                                WriteOnce.Result(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TAGS_MISSING, t), true, WriteOnce.ConsoleVerbosity.High);
                            }

                            if (exitCode != ExitCode.TestPassed)
                            {
                                break;
                            }
                        }

                        if (exitCode != ExitCode.TestPassed)
                        {
                            break;
                        }
                    }

                    //results
                    WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low);

                    if (exitCode == ExitCode.TestFailed)
                    {
                        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.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagtest"));
                    if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext)
                    {
                        WriteOnce.Info(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, WriteOnce.ConsoleVerbosity.Low, false);
                    }

                    WriteOnce.FlushAll();
                }
            }
            catch (Exception e)
            {
                WriteOnce.Verbosity = saveVerbosity;
                if (analyzeCmdResult == AnalyzeCommand.ExitCode.Success) //then error was not previously logged
                {
                    WriteOnce.Error(e.Message);
                }
                else
                {
                    WriteOnce.Error(e.Message, true, WriteOnce.ConsoleVerbosity.Low, false);
                }

                //exit normaly for CLI callers and throw for DLL callers
                if (Utils.CLIExecutionContext)
                {
                    return((int)ExitCode.CriticalError);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (_arg_close_log_on_exit)
                {
                    Utils.Logger  = null;
                    WriteOnce.Log = null;
                }
            }

            return((int)exitCode);
        }
コード例 #15
0
        /// <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() { AppVersion = Common.Utils.GetVersionString() };

            //save to quiet analyze cmd and restore
            WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity;

            try
            {
                if (_options is null)
                {
                    throw new ArgumentNullException("_options");
                }
                AnalyzeCommand cmd1 = new(new AnalyzeOptions()
                {
                    SourcePath = _options.SourcePath1,
                    CustomRulesPath = _options.CustomRulesPath,
                    IgnoreDefaultRules = _options.IgnoreDefaultRules,
                    FilePathExclusions = _options.FilePathExclusions,
                    ConsoleVerbosityLevel = "none",
                    Log = _options.Log,
                    TagsOnly = true,
                    ConfidenceFilters = _options.ConfidenceFilters,
                    FileTimeOut = _options.FileTimeOut,
                    ProcessingTimeOut = _options.ProcessingTimeOut,
                    NoFileMetadata = true,
                    NoShowProgress = true,
                    ScanUnknownTypes = _options.ScanUnknownTypes,
                    SingleThread = _options.SingleThread,
                });
                AnalyzeCommand cmd2 = new(new AnalyzeOptions()
                {
                    SourcePath = _options.SourcePath2,
                    CustomRulesPath = _options.CustomRulesPath,
                    IgnoreDefaultRules = _options.IgnoreDefaultRules,
                    FilePathExclusions = _options.FilePathExclusions,
                    ConsoleVerbosityLevel = "none",
                    Log = _options.Log,
                    TagsOnly = true,
                    ConfidenceFilters = _options.ConfidenceFilters,
                    FileTimeOut = _options.FileTimeOut,
                    ProcessingTimeOut = _options.ProcessingTimeOut,
                    NoFileMetadata = true,
                    NoShowProgress = true,
                    ScanUnknownTypes = _options.ScanUnknownTypes,
                    SingleThread = _options.SingleThread,
                });

                AnalyzeResult analyze1 = cmd1.GetResult();
                AnalyzeResult analyze2 = cmd2.GetResult();

                //restore
                WriteOnce.Verbosity = saveVerbosity;

                //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, string.Join(',', _options.SourcePath1)));
                }
                else if (analyze2.ResultCode == AnalyzeResult.ExitCode.CriticalError)
                {
                    throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR, string.Join(',', _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)
                {
                    var list1 = analyze1.Metadata.UniqueTags ?? new List <string>();
                    var list2 = analyze2.Metadata.UniqueTags ?? new List <string>();

                    var removed = list1.Except(list2);
                    var added   = list2.Except(list1);

                    foreach (var add in added)
                    {
                        tagDiffResult.TagDiffList.Add(new TagDiff()
                        {
                            Source = TagDiff.DiffSource.Source2,
                            Tag    = add
                        });
                    }
                    foreach (var remove in removed)
                    {
                        tagDiffResult.TagDiffList.Add(new TagDiff()
                        {
                            Source = TagDiff.DiffSource.Source1,
                            Tag    = remove
                        });
                    }


                    if (tagDiffResult.TagDiffList.Count > 0)
                    {
                        tagDiffResult.ResultCode = _arg_tagTestType == TagTestType.Inequality ? TagDiffResult.ExitCode.TestPassed : TagDiffResult.ExitCode.TestFailed;
                    }
                    else
                    {
                        tagDiffResult.ResultCode = _arg_tagTestType == TagTestType.Inequality ? TagDiffResult.ExitCode.TestFailed : TagDiffResult.ExitCode.TestPassed;
                    }

                    return(tagDiffResult);
                }
            }
            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;
            }
        }
    }
コード例 #16
0
        /// <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);
        }