public void OptionsInterpretter_ChangesNothingWhenEnvVarsEmpty()
        {
            var mockEnvironmentVariableGetter = new Mock <IEnvironmentVariableGetter>();
            //  Don't setup any responses so they always return null

            OptionsInterpretter optionsInterpretter = new OptionsInterpretter(mockEnvironmentVariableGetter.Object);

            List <ValidateOptions> beforeAndAfter = new List <ValidateOptions>(2);

            for (int i = 0; i < beforeAndAfter.Capacity; i++)
            {
                beforeAndAfter.Add(new ValidateOptions
                {
                    DataToInsert = new List <OptionallyEmittedData> {
                        OptionallyEmittedData.Hashes, OptionallyEmittedData.EnvironmentVariables
                    },
                    DataToRemove = new List <OptionallyEmittedData> {
                        OptionallyEmittedData.VersionControlDetails
                    },
                    Kind = new List <ResultKind> {
                        ResultKind.Fail
                    },
                    Level = new List <FailureLevel> {
                        FailureLevel.Error, FailureLevel.Warning
                    }
                });
            }

            optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(beforeAndAfter[1]);

            Assert.True(beforeAndAfter[0].DataToInsert.SequenceEqual(beforeAndAfter[1].DataToInsert));
            Assert.True(beforeAndAfter[0].DataToRemove.SequenceEqual(beforeAndAfter[1].DataToRemove));
            Assert.True(beforeAndAfter[0].Kind.SequenceEqual(beforeAndAfter[1].Kind));
            Assert.True(beforeAndAfter[0].Level.SequenceEqual(beforeAndAfter[1].Level));
        }
Esempio n. 2
0
        /// <summary>The entry point for the SARIF multi utility.</summary>
        /// <param name="args">Arguments passed in from the tool's command line.</param>
        /// <returns>0 on success; nonzero on failure.</returns>
        public static int Main(string[] args)
        {
            OptionsInterpretter optionsInterpretter = new OptionsInterpretter();

            return(Parser.Default.ParseArguments <
                       // Keep this in alphabetical order
                       AbsoluteUriOptions,
#if DEBUG
                       AnalyzeTestOptions,
#endif
                       ApplyPolicyOptions,
                       ConvertOptions,
                       ExportValidationConfigurationOptions,
                       ExportValidationRulesMetadataOptions,
                       FileWorkItemsOptions,
                       ResultMatchingOptions,
                       MergeOptions,
                       PageOptions,
                       QueryOptions,
                       RebaseUriOptions,
                       RewriteOptions,
                       ValidateOptions>(args)
                   .WithParsed <AbsoluteUriOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
#if DEBUG
                   .WithParsed <AnalyzeTestOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
#endif
                   .WithParsed <ApplyPolicyOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <ConvertOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <ExportValidationConfigurationOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <ExportValidationRulesMetadataOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <FileWorkItemsOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <ResultMatchingOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <MergeOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <PageOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <QueryOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <RebaseUriOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <RewriteOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .WithParsed <ValidateOptions>(x => { optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(x); })
                   .MapResult(
                       (AbsoluteUriOptions absoluteUriOptions) => new AbsoluteUriCommand().Run(absoluteUriOptions),
#if DEBUG
                       (AnalyzeTestOptions fileWorkItemsOptions) => new AnalyzeTestCommand().Run(fileWorkItemsOptions),
#endif
                       (ApplyPolicyOptions options) => new ApplyPolicyCommand().Run(options),
                       (ConvertOptions convertOptions) => new ConvertCommand().Run(convertOptions),
                       (ExportValidationConfigurationOptions options) => new ExportValidationConfigurationCommand().Run(options),
                       (ExportValidationRulesMetadataOptions options) => new ExportValidationRulesMetadataCommand().Run(options),
                       (FileWorkItemsOptions fileWorkItemsOptions) => new FileWorkItemsCommand().Run(fileWorkItemsOptions),
                       (ResultMatchingOptions baselineOptions) => new ResultMatchingCommand().Run(baselineOptions),
                       (MergeOptions mergeOptions) => new MergeCommand().Run(mergeOptions),
                       (PageOptions pageOptions) => new PageCommand().Run(pageOptions),
                       (QueryOptions queryOptions) => new QueryCommand().Run(queryOptions),
                       (RebaseUriOptions rebaseOptions) => new RebaseUriCommand().Run(rebaseOptions),
                       (RewriteOptions rewriteOptions) => new RewriteCommand().Run(rewriteOptions),
                       (ValidateOptions validateOptions) => new ValidateCommand().Run(validateOptions),
                       _ => HandleParseError(args)));
        }
        public void OptionsInterpretter_CorrectlyAddsAdditiveEnvVars()
        {
            var mockEnvironmentVariableGetter = new Mock <IEnvironmentVariableGetter>();

            mockEnvironmentVariableGetter.Setup(x => x.GetEnvironmentVariable("SARIF_DATATOINSERT_ADDITION")).Returns("TextFiles;BinaryFiles;");
            //  Deliberately more delimeters than needed
            mockEnvironmentVariableGetter.Setup(x => x.GetEnvironmentVariable("SARIF_DATATOREMOVE_ADDITION")).Returns("ComprehensiveRegionProperties;RegionSnippets");
            mockEnvironmentVariableGetter.Setup(x => x.GetEnvironmentVariable("SARIF_KIND_ADDITION")).Returns("Informational;");
            //  Deliberately more delimeters than needed
            mockEnvironmentVariableGetter.Setup(x => x.GetEnvironmentVariable("SARIF_LEVEL_ADDITION")).Returns("Note");

            OptionsInterpretter optionsInterpretter = new OptionsInterpretter(mockEnvironmentVariableGetter.Object);

            ValidateOptions analyzeOptionsBase = new ValidateOptions
            {
                DataToInsert = new List <OptionallyEmittedData> {
                    OptionallyEmittedData.Hashes, OptionallyEmittedData.EnvironmentVariables
                },
                DataToRemove = new List <OptionallyEmittedData> {
                    OptionallyEmittedData.VersionControlDetails
                },
                Kind = new List <ResultKind> {
                    ResultKind.Fail
                },
                Level = new List <FailureLevel> {
                    FailureLevel.Error, FailureLevel.Warning
                }
            };

            optionsInterpretter.ConsumeEnvVarsAndInterpretOptions(analyzeOptionsBase);

            analyzeOptionsBase.DataToInsert.Should().Contain(OptionallyEmittedData.TextFiles);
            analyzeOptionsBase.DataToInsert.Should().Contain(OptionallyEmittedData.BinaryFiles);
            analyzeOptionsBase.DataToRemove.Should().Contain(OptionallyEmittedData.ComprehensiveRegionProperties);
            analyzeOptionsBase.DataToRemove.Should().Contain(OptionallyEmittedData.RegionSnippets);
            analyzeOptionsBase.Kind.Should().Contain(ResultKind.Informational);
            analyzeOptionsBase.Level.Should().Contain(FailureLevel.Note);
        }