github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/utils/outputwriter/testsutils.go (about) 1 package outputwriter 2 3 import ( 4 "encoding/json" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 var ( 14 // Used for tests that are outside the outputwriter package. 15 TestMessagesDir = filepath.Join("..", "testdata", "messages") 16 TestSummaryCommentDir = filepath.Join(TestMessagesDir, "summarycomment") 17 // Used for tests that are inside the outputwriter package. 18 testMessagesDir = filepath.Join("..", TestMessagesDir) 19 testReviewCommentDir = filepath.Join(testMessagesDir, "reviewcomment") 20 testSummaryCommentDir = filepath.Join(testMessagesDir, "summarycomment") 21 ) 22 23 type OutputTestCase struct { 24 name string 25 writer OutputWriter 26 expectedOutputPath string 27 expectedOutput string 28 } 29 30 type TestBodyResponse struct { 31 Body string `json:"body"` 32 } 33 34 func GetExpectedTestOutput(t *testing.T, testCase OutputTestCase) string { 35 if testCase.expectedOutputPath != "" { 36 return GetOutputFromFile(t, testCase.expectedOutputPath) 37 } 38 return testCase.expectedOutput 39 } 40 41 func GetOutputFromFile(t *testing.T, filePath string) string { 42 content, err := os.ReadFile(filePath) 43 assert.NoError(t, err) 44 return strings.ReplaceAll(string(content), "\r\n", "\n") 45 } 46 47 func GetJsonBodyOutputFromFile(t *testing.T, filePath string) []byte { 48 bodyRes := TestBodyResponse{Body: GetOutputFromFile(t, filePath)} 49 bytes, err := json.Marshal(bodyRes) 50 assert.NoError(t, err) 51 return bytes 52 } 53 54 func GetPRSummaryContentNoIssues(t *testing.T, summaryTestDir string, entitled, simplified bool) string { 55 dataPath := filepath.Join(summaryTestDir, "structure") 56 if simplified { 57 if entitled { 58 dataPath = filepath.Join(dataPath, "summary_comment_no_issues_simplified_entitled.md") 59 } else { 60 dataPath = filepath.Join(dataPath, "summary_comment_no_issues_simplified_not_entitled.md") 61 } 62 } else { 63 if entitled { 64 dataPath = filepath.Join(dataPath, "summary_comment_no_issues_pr_entitled.md") 65 } else { 66 dataPath = filepath.Join(dataPath, "summary_comment_no_issues_pr_not_entitled.md") 67 } 68 } 69 return GetOutputFromFile(t, dataPath) 70 }