Skip to content

Commit

Permalink
Add test cases for SARIF files created by jscpd
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Jan 15, 2025
1 parent 4a127c1 commit fde9873
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Cake.Issues.Sarif.Tests/SarifIssuesProviderFixture.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
namespace Cake.Issues.Sarif.Tests;

internal class SarifIssuesProviderFixture(string fileResourceName)
: BaseConfigurableIssueProviderFixture<SarifIssuesProvider, SarifIssuesSettings>(fileResourceName)
internal class SarifIssuesProviderFixture
: BaseConfigurableIssueProviderFixture<SarifIssuesProvider, SarifIssuesSettings>
{
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;
Expand Down
54 changes: 54 additions & 0 deletions src/Cake.Issues.Sarif.Tests/SarifIssuesProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
65 changes: 65 additions & 0 deletions src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-linux.sarif
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
]
}
65 changes: 65 additions & 0 deletions src/Cake.Issues.Sarif.Tests/Testfiles/jscpd-windows.sarif
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
]
}

0 comments on commit fde9873

Please sign in to comment.