public override int Run(ExportConfigurationOptions exportOptions)
        {
            int result = FAILURE;

            try
            {
                PropertiesDictionary allOptions = new PropertiesDictionary();

                // The export command could be updated in the future to accept an arbitrary set
                // of analyzers for which to build an options XML file suitable for configuring them.
                // Currently, we perform discovery against the built-in CodeFormatter rules
                // and analyzers only.
                ImmutableArray <IOptionsProvider> providers = DriverUtilities.GetExports <IOptionsProvider>(DefaultPlugInAssemblies);
                foreach (IOptionsProvider provider in providers)
                {
                    foreach (IOption option in provider.GetOptions())
                    {
                        allOptions.SetProperty(option, option.DefaultValue, cacheDescription: true);
                    }
                }


                string extension = Path.GetExtension(exportOptions.OutputFilePath);

                if (extension.Equals(".xml", StringComparison.OrdinalIgnoreCase))
                {
                    allOptions.SaveToXml(exportOptions.OutputFilePath);
                }
                else if (extension.Equals(".json", StringComparison.OrdinalIgnoreCase))
                {
                    allOptions.SaveToJson(exportOptions.OutputFilePath);
                }
                else if (exportOptions.FileFormat == FileFormat.Xml)
                {
                    allOptions.SaveToXml(exportOptions.OutputFilePath);
                }
                else
                {
                    allOptions.SaveToJson(exportOptions.OutputFilePath);
                }

                Console.WriteLine("Configuration file saved to: " + Path.GetFullPath(exportOptions.OutputFilePath));

                result = SUCCESS;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
            }

            return(result);
        }
        public void AnalyzeCommand_FireAllRules()
        {
            PropertiesDictionary configuration = ExportConfigurationCommandBaseTests.s_defaultConfiguration;

            string path = Path.GetTempFileName() + ".xml";

            configuration.SetProperty(SimpleTestRule.Behaviors, TestRuleBehaviors.LogError);

            try
            {
                configuration.SaveToXml(path);

                string location = GetThisTestAssemblyFilePath();

                Run run = AnalyzeFile(location, configFileName: path);

                int resultCount                    = 0;
                int toolNotificationCount          = 0;
                int configurationNotificationCount = 0;

                SarifHelpers.ValidateRun(
                    run,
                    (issue) => { resultCount++; },
                    (toolNotification) => { toolNotificationCount++; },
                    (configurationNotification) => { configurationNotificationCount++; });

                // By default, the exception raising rule produces a single error.
                // The simple test rule doesn't raise anything without add'l configuration
                resultCount.Should().Be(2);
                run.Results.Where((result) => result.Level == ResultLevel.Error).Count().Should().Be(1);
                run.Results.Where((result) => result.Level == ResultLevel.Warning).Count().Should().Be(1);
                run.Results.Where((result) => result.Level == ResultLevel.NotApplicable).Count().Should().Be(0);

                toolNotificationCount.Should().Be(1);
                configurationNotificationCount.Should().Be(0);
            }
            finally
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
        }
Example #3
0
        public void AnalyzeCommandBase_FireAllRules()
        {
            PropertiesDictionary configuration = ExportConfigurationCommandBaseTests.s_defaultConfiguration;

            string path = Path.GetTempFileName() + ".xml";

            configuration.SetProperty(TestRule.Behaviors, TestRuleBehaviors.LogError);

            try
            {
                configuration.SaveToXml(path);

                string location = GetThisTestAssemblyFilePath();

                Run run = AnalyzeFile(location, configFileName: path);

                int resultCount                    = 0;
                int toolNotificationCount          = 0;
                int configurationNotificationCount = 0;

                SarifHelpers.ValidateRun(
                    run,
                    (issue) => { resultCount++; },
                    (toolNotification) => { toolNotificationCount++; },
                    (configurationNotification) => { configurationNotificationCount++; });

                // As configured by context, we should see a single error raised.
                resultCount.Should().Be(1);
                run.Results.Count((result) => result.Level == FailureLevel.Error).Should().Be(1);

                toolNotificationCount.Should().Be(0);
                configurationNotificationCount.Should().Be(0);
            }
            finally
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
        }
Example #4
0
        public override int Run(ExportConfigurationOptions exportOptions)
        {
            int result = FAILURE;

            try
            {
                PropertiesDictionary allOptions = new PropertiesDictionary();

                // The export command could be updated in the future to accept an arbitrary set
                // of analyzers for which to build an options XML file suitable for configuring them.
                ImmutableArray <IOptionsProvider> providers = DriverUtilities.GetExports <IOptionsProvider>(DefaultPlugInAssemblies);
                foreach (IOptionsProvider provider in providers)
                {
                    IOption sampleOption = null;

                    // Every analysis options provider has access to the following default configuration knobs
                    foreach (IOption option in provider.GetOptions())
                    {
                        sampleOption = sampleOption ?? option;
                        allOptions.SetProperty(option, option.DefaultValue, cacheDescription: true);
                    }
                }

                IEnumerable <IRule> rules;
                rules = DriverUtilities.GetExports <IRule>(DefaultPlugInAssemblies);

                // This code injects properties that are provided for every rule instance.
                foreach (IRule rule in rules)
                {
                    object objectResult;
                    PropertiesDictionary properties;

                    string ruleOptionsKey = rule.Id + "." + rule.Name + ".Options";

                    if (!allOptions.TryGetValue(ruleOptionsKey, out objectResult))
                    {
                        objectResult = allOptions[ruleOptionsKey] = new PropertiesDictionary();
                    }
                    properties = (PropertiesDictionary)objectResult;

                    foreach (IOption option in DefaultDriverOptions.Instance.GetOptions())
                    {
                        properties.SetProperty(option, option.DefaultValue, cacheDescription: true, persistToSettingsContainer: false);
                    }
                }

                string extension = Path.GetExtension(exportOptions.OutputFilePath);

                if (extension.Equals(".xml", StringComparison.OrdinalIgnoreCase))
                {
                    allOptions.SaveToXml(exportOptions.OutputFilePath);
                }
                else if (extension.Equals(".json", StringComparison.OrdinalIgnoreCase))
                {
                    allOptions.SaveToJson(exportOptions.OutputFilePath);
                }
                else if (exportOptions.FileFormat == FileFormat.Xml)
                {
                    allOptions.SaveToXml(exportOptions.OutputFilePath);
                }
                else
                {
                    allOptions.SaveToJson(exportOptions.OutputFilePath);
                }

                Console.WriteLine("Configuration file saved to: " + Path.GetFullPath(exportOptions.OutputFilePath));

                result = SUCCESS;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
            }

            return(result);
        }