Inheritance: SarifRewritingVisitor
Exemplo n.º 1
0
        public void ResultDiffingVisitor_DetectsAbsentAndNewResults()
        {
            var result1 = new Result {
                RuleId = "TST0001"
            };
            var result2 = new Result {
                RuleId = "TST0002"
            };
            var result3 = new Result {
                RuleId = "TST0003"
            };

            var sarifLog = new SarifLog
            {
                Runs = new[]
                {
                    new Run
                    {
                        Results = new List <Result>
                        {
                            result1,
                            result2
                        }
                    }
                }
            };

            var visitor = new ResultDiffingVisitor(sarifLog);

            result2 = new Result {
                RuleId = "TST0002"
            };
            result3 = new Result {
                RuleId = "TST0003"
            };


            var newResults = new Result[]
            {
                result2,
                result3
            };

            visitor.Diff(newResults);

            Assert.Single(visitor.AbsentResults);
            Assert.Equal(visitor.AbsentResults.First().RuleId, result1.RuleId);

            Assert.Single(visitor.NewResults);
            Assert.Equal(visitor.NewResults.First().RuleId, result3.RuleId);
        }
        public void ResultDiffingVisitor_DetectsAbsentAndNewResults()
        {
            var result1 = new Result { RuleId = "TST0001" };
            var result2 = new Result { RuleId = "TST0002" };
            var result3 = new Result { RuleId = "TST0003" };

            var sarifLog = new SarifLog
            {
                Runs = new[]
                {
                    new Run
                    {
                        Results = new List<Result>
                        {
                            result1,
                            result2
                        }
                    }
                }
            };

            var visitor = new ResultDiffingVisitor(sarifLog);

            result2 = new Result { RuleId = "TST0002" };
            result3 = new Result { RuleId = "TST0003" };

            var newResults = new Result[]
            {
                result2,
                result3
            };

            visitor.Diff(newResults);

            Assert.AreEqual(visitor.AbsentResults.Count, 1);
            Assert.AreEqual(visitor.AbsentResults.First().RuleId, result1.RuleId);

            Assert.AreEqual(visitor.NewResults.Count, 1);
            Assert.AreEqual(visitor.NewResults.First().RuleId, result3.RuleId);
        }
Exemplo n.º 3
0
        private void RunRules(StringBuilder sb, string inputFileName)
        {
            string fileName = Path.GetFileName(inputFileName);
            string actualDirectory = Path.Combine(Path.GetDirectoryName(inputFileName), "Actual");
            string expectedDirectory = Path.Combine(Path.GetDirectoryName(inputFileName), "Expected");

            if (!Directory.Exists(actualDirectory))
            {
                Directory.CreateDirectory(actualDirectory);
            }

            string expectedFileName = Path.Combine(expectedDirectory, fileName + ".sarif");
            string actualFileName = Path.Combine(actualDirectory, fileName + ".sarif");

            AnalyzeCommand command = new AnalyzeCommand();
            AnalyzeOptions options = new AnalyzeOptions();

            options.TargetFileSpecifiers = new string[] { inputFileName };
            options.OutputFilePath = actualFileName;
            options.Verbose = true;
            options.Recurse = false;
            options.ConfigurationFilePath = "default";

            int result = command.Run(options);

            // Note that we don't ensure a success code. That is because we
            // are running end-to-end tests for valid and invalid files

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                ContractResolver = SarifContractResolver.Instance,
                Formatting = Formatting.Indented
            };

            string expectedText = File.ReadAllText(expectedFileName);
            string actualText = File.ReadAllText(actualFileName);

            // Make sure we can successfully deserialize what was just generated
            ResultLog expectedLog = JsonConvert.DeserializeObject<ResultLog>(expectedText, settings);
            ResultLog actualLog = JsonConvert.DeserializeObject<ResultLog>(actualText, settings);

            var visitor = new ResultDiffingVisitor(expectedLog);

            if (!visitor.Diff(actualLog.RunLogs[0].Results))
            {
                string errorMessage = "The output of the tool did not match for input {0}.";
                sb.AppendLine(String.Format(CultureInfo.CurrentCulture, errorMessage, inputFileName));
                sb.AppendLine("Check differences with:");
                sb.AppendLine(GenerateDiffCommand(expectedFileName, actualFileName));
            }
        }
Exemplo n.º 4
0
        private void RunRules(StringBuilder sb, string inputFileName)
        {
            string fileName = Path.GetFileName(inputFileName);
            string actualDirectory = Path.Combine(Path.GetDirectoryName(inputFileName), "Actual");
            string expectedDirectory = Path.Combine(Path.GetDirectoryName(inputFileName), "Expected");

            if (!Directory.Exists(actualDirectory))
            {
                Directory.CreateDirectory(actualDirectory);
            }

            string expectedFileName = Path.Combine(expectedDirectory, fileName + ".sarif");
            string actualFileName = Path.Combine(actualDirectory, fileName + ".sarif");

            AnalyzeCommand command = new AnalyzeCommand();
            AnalyzeOptions options = new AnalyzeOptions();

            options.TargetFileSpecifiers = new string[] { inputFileName };
            options.OutputFilePath = actualFileName;
            options.Verbose = true;
            options.Recurse = false;
            options.ComputeTargetsHash = true;
            options.ConfigurationFilePath = "default";

            int result = command.Run(options);

            // Note that we don't ensure a success code. That is because we
            // are running end-to-end tests for valid and invalid files

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                ContractResolver = SarifContractResolver.Instance,
                Formatting = Formatting.Indented
            };

            string expectedText = File.ReadAllText(expectedFileName);
            string actualText = File.ReadAllText(actualFileName);

            // Replace repository root absolute path with Z:\ for machine and enlistment independence
            string repoRoot = Path.GetFullPath(Path.Combine(actualDirectory, "..", "..", "..", ".."));
            actualText = actualText.Replace(repoRoot.Replace(@"\", @"\\"), @"Z:");
            actualText = actualText.Replace(repoRoot.Replace(@"\", @"/"), @"Z:");

            // Remove stack traces as they can change due to inlining differences by configuration and runtime.
            actualText = Regex.Replace(actualText, @"\\r\\n   at [^""]+", "");

            // Write back the normalized actual text so that the diff command given on failure shows what was actually compared.
            File.WriteAllText(actualFileName, actualText);

            // Make sure we can successfully deserialize what was just generated
            SarifLog expectedLog = JsonConvert.DeserializeObject<SarifLog>(expectedText, settings);
            SarifLog actualLog = JsonConvert.DeserializeObject<SarifLog>(actualText, settings);

            var visitor = new ResultDiffingVisitor(expectedLog);

            if (!visitor.Diff(actualLog.Runs[0].Results))
            {
                string errorMessage = "The output of the tool did not match for input {0}.";
                sb.AppendLine(String.Format(CultureInfo.CurrentCulture, errorMessage, inputFileName));
                sb.AppendLine("Check differences with:");
                sb.AppendLine(GenerateDiffCommand(expectedFileName, actualFileName));
            }
        }