From fde98732ac9be312959aa77c3752343d9078b4a8 Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Tue, 14 Jan 2025 13:33:43 +0100 Subject: [PATCH] Add test cases for SARIF files created by jscpd --- .../SarifIssuesProviderFixture.cs | 16 ++++- .../SarifIssuesProviderTests.cs | 54 +++++++++++++++ .../Testfiles/jscpd-linux.sarif | 65 +++++++++++++++++++ .../Testfiles/jscpd-windows.sarif | 65 +++++++++++++++++++ 4 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-linux.sarif create mode 100644 src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-windows.sarif diff --git a/src/Cake.Issues.Sarif.Tests/SarifIssuesProviderFixture.cs b/src/Cake.Issues.Sarif.Tests/SarifIssuesProviderFixture.cs index 36fb8e540..3301006db 100644 --- a/src/Cake.Issues.Sarif.Tests/SarifIssuesProviderFixture.cs +++ b/src/Cake.Issues.Sarif.Tests/SarifIssuesProviderFixture.cs @@ -1,8 +1,20 @@ namespace Cake.Issues.Sarif.Tests; -internal class SarifIssuesProviderFixture(string fileResourceName) - : BaseConfigurableIssueProviderFixture(fileResourceName) +internal class SarifIssuesProviderFixture + : BaseConfigurableIssueProviderFixture { + public SarifIssuesProviderFixture(string fileResourceName) + : this(fileResourceName, @"c:\Source\Cake.Issues") + { + } + + public SarifIssuesProviderFixture(string fileResourceName, string repositoryRoot) + : base(fileResourceName) + { + this.ReadIssuesSettings = + new ReadIssuesSettings(repositoryRoot); + } + public bool UseToolNameAsIssueProviderName { get; set; } = true; public bool IgnoreSuppressedIssues { get; set; } = true; diff --git a/src/Cake.Issues.Sarif.Tests/SarifIssuesProviderTests.cs b/src/Cake.Issues.Sarif.Tests/SarifIssuesProviderTests.cs index 7e5433925..d95d8b027 100644 --- a/src/Cake.Issues.Sarif.Tests/SarifIssuesProviderTests.cs +++ b/src/Cake.Issues.Sarif.Tests/SarifIssuesProviderTests.cs @@ -357,5 +357,59 @@ public void Should_Read_Issue_Correct_For_File_Generated_By_InspectCode() // Then issues.Count.ShouldBe(1106); } + + [Fact] + public void Should_Read_Issue_Correct_For_File_Generated_By_jscpd_On_Windows() + { + // Given + var fixture = new SarifIssuesProviderFixture("jscpd-windows.sarif"); + + // When + var issues = fixture.ReadIssues().ToList(); + + // Then + issues.Count.ShouldBe(1); + + var issue = issues[0]; + IssueChecker.Check( + issue, + IssueBuilder.NewIssue( + "Clone detected in tsx, - C:/Source/Cake.Issues/foo.tsx[55:26 - 70:2] and C:/Source/Cake.Issues/bar.tsx[35:27 - 51:9]", + "Cake.Issues.Sarif.SarifIssuesProvider", + "jscpd") + .InFile(@"foo.tsx", 55, 70, 26, 2) + .OfRule( + "duplication", + new Uri("https://github.com/kucherenko/jscpd/")) + .WithPriority(IssuePriority.Warning) + .Create()); + } + + [Fact] + public void Should_Read_Issue_Correct_For_File_Generated_By_jscpd_On_Linux() + { + // Given + var fixture = new SarifIssuesProviderFixture("jscpd-linux.sarif", "/source/cake.issues"); + + // When + var issues = fixture.ReadIssues().ToList(); + + // Then + issues.Count.ShouldBe(1); + + var issue = issues[0]; + IssueChecker.Check( + issue, + IssueBuilder.NewIssue( + "Clone detected in tsx, - /source/cake.issues/foo.tsx[55:26 - 70:2] and /source/cake.issues/bar.tsx[35:27 - 51:9]", + "Cake.Issues.Sarif.SarifIssuesProvider", + "jscpd") + .InFile(@"foo.tsx", 55, 70, 26, 2) + .OfRule( + "duplication", + new Uri("https://github.com/kucherenko/jscpd/")) + .WithPriority(IssuePriority.Warning) + .Create()); + } } } diff --git a/src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-linux.sarif b/src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-linux.sarif new file mode 100644 index 000000000..c85ba7503 --- /dev/null +++ b/src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-linux.sarif @@ -0,0 +1,65 @@ +{ + "$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "version": "2.1.0", + "runs": [ + { + "tool": { + "driver": { + "name": "jscpd", + "rules": [ + { + "id": "duplication", + "shortDescription": { + "text": "Found code duplication" + }, + "helpUri": "https://github.com/kucherenko/jscpd/" + }, + { + "id": "duplications-threshold", + "shortDescription": { + "text": "Level of duplication is too high" + }, + "helpUri": "https://github.com/kucherenko/jscpd/" + } + ], + "version": "4.0.3", + "informationUri": "https://github.com/kucherenko/jscpd/" + } + }, + "results": [ + { + "level": "warning", + "message": { + "text": "Clone detected in tsx, - /source/cake.issues/foo.tsx[55:26 - 70:2] and /source/cake.issues/bar.tsx[35:27 - 51:9]" + }, + "ruleId": "duplication", + "locations": [ + { + "physicalLocation": { + "artifactLocation": { + "uri": "/source/cake.issues/foo.tsx", + "index": 0 + }, + "region": { + "startLine": 55, + "startColumn": 26, + "endLine": 70, + "endColumn": 2 + } + } + } + ], + "ruleIndex": 0 + } + ], + "artifacts": [ + { + "sourceLanguage": "XML", + "location": { + "uri": "/source/cake.issues/foo.tsx" + } + } + ] + } + ] +} \ No newline at end of file diff --git a/src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-windows.sarif b/src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-windows.sarif new file mode 100644 index 000000000..fc4dcab0e --- /dev/null +++ b/src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-windows.sarif @@ -0,0 +1,65 @@ +{ + "$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "version": "2.1.0", + "runs": [ + { + "tool": { + "driver": { + "name": "jscpd", + "rules": [ + { + "id": "duplication", + "shortDescription": { + "text": "Found code duplication" + }, + "helpUri": "https://github.com/kucherenko/jscpd/" + }, + { + "id": "duplications-threshold", + "shortDescription": { + "text": "Level of duplication is too high" + }, + "helpUri": "https://github.com/kucherenko/jscpd/" + } + ], + "version": "4.0.3", + "informationUri": "https://github.com/kucherenko/jscpd/" + } + }, + "results": [ + { + "level": "warning", + "message": { + "text": "Clone detected in tsx, - C:/Source/Cake.Issues/foo.tsx[55:26 - 70:2] and C:/Source/Cake.Issues/bar.tsx[35:27 - 51:9]" + }, + "ruleId": "duplication", + "locations": [ + { + "physicalLocation": { + "artifactLocation": { + "uri": "C:/Source/Cake.Issues/foo.tsx", + "index": 0 + }, + "region": { + "startLine": 55, + "startColumn": 26, + "endLine": 70, + "endColumn": 2 + } + } + } + ], + "ruleIndex": 0 + } + ], + "artifacts": [ + { + "sourceLanguage": "XML", + "location": { + "uri": "C:/Source/Cake.Issues/foo.tsx" + } + } + ] + } + ] +} \ No newline at end of file