github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/sonar/report_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package sonar 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/mock" 12 ) 13 14 var fileContent string 15 var fileName string 16 17 func writeToFileMock(f string, d []byte, p os.FileMode) error { 18 fileContent = string(d) 19 fileName = f 20 return nil 21 } 22 23 func TestWriteReport(t *testing.T) { 24 // init 25 const expected = `{"serverUrl":"https://sonarcloud.io","projectKey":"Piper-Validation/Golang","taskId":"mock.Anything","numberOfIssues":{"blocker":0,"critical":1,"major":2,"minor":3,"info":4},"coverage":{"coverage":13.7,"lineCoverage":37.1,"linesToCover":123,"uncoveredLines":23,"branchCoverage":42,"branchesToCover":30,"uncoveredBranches":3},"linesOfCode":{"total":327,"languageDistribution":[{"languageKey":"java","linesOfCode":327}]}}` 26 testData := ReportData{ 27 ServerURL: "https://sonarcloud.io", 28 ProjectKey: "Piper-Validation/Golang", 29 TaskID: mock.Anything, 30 NumberOfIssues: Issues{ 31 Critical: 1, 32 Major: 2, 33 Minor: 3, 34 Info: 4, 35 }, 36 Coverage: &SonarCoverage{ 37 Coverage: 13.7, 38 BranchCoverage: 42, 39 LineCoverage: 37.1, 40 LinesToCover: 123, 41 UncoveredLines: 23, 42 BranchesToCover: 30, 43 UncoveredBranches: 3, 44 }, 45 LinesOfCode: &SonarLinesOfCode{ 46 Total: 327, 47 LanguageDistribution: []SonarLanguageDistribution{{LanguageKey: "java", LinesOfCode: 327}}, 48 }, 49 } 50 // test 51 err := WriteReport(testData, "", writeToFileMock) 52 // assert 53 assert.NoError(t, err) 54 assert.Equal(t, expected, fileContent) 55 assert.Equal(t, reportFileName, fileName) 56 }