github.com/jfrog/frogbot/v2@v2.21.0/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  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  var (
    15  	// Used for tests that are outside the outputwriter package.
    16  	TestMessagesDir       = filepath.Join("..", "testdata", "messages")
    17  	TestSummaryCommentDir = filepath.Join(TestMessagesDir, "summarycomment")
    18  	// Used for tests that are inside the outputwriter package.
    19  	testMessagesDir       = filepath.Join("..", TestMessagesDir)
    20  	testReviewCommentDir  = filepath.Join(testMessagesDir, "reviewcomment")
    21  	testSummaryCommentDir = filepath.Join(testMessagesDir, "summarycomment")
    22  )
    23  
    24  type OutputTestCase struct {
    25  	name               string
    26  	writer             OutputWriter
    27  	expectedOutputPath []string
    28  	expectedOutput     []string
    29  }
    30  
    31  type TestBodyResponse struct {
    32  	Body string `json:"body"`
    33  }
    34  
    35  func GetExpectedTestCaseOutput(t *testing.T, testCase OutputTestCase) []string {
    36  	if len(testCase.expectedOutputPath) > 0 {
    37  		content := make([]string, len(testCase.expectedOutputPath))
    38  		for i, path := range testCase.expectedOutputPath {
    39  			content[i] = GetOutputFromFile(t, path)
    40  		}
    41  		return content
    42  	}
    43  	return testCase.expectedOutput
    44  }
    45  
    46  func GetExpectedTestOutput(t *testing.T, testCase OutputTestCase) string {
    47  	out := GetExpectedTestCaseOutput(t, testCase)
    48  	require.Len(t, out, 1)
    49  	return out[0]
    50  }
    51  
    52  func GetOutputFromFile(t *testing.T, filePath string) string {
    53  	content, err := os.ReadFile(filePath)
    54  	assert.NoError(t, err)
    55  	return strings.ReplaceAll(string(content), "\r\n", "\n")
    56  }
    57  
    58  func GetJsonBodyOutputFromFile(t *testing.T, filePath string) []byte {
    59  	bodyRes := TestBodyResponse{Body: GetOutputFromFile(t, filePath)}
    60  	bytes, err := json.Marshal(bodyRes)
    61  	assert.NoError(t, err)
    62  	return bytes
    63  }
    64  
    65  func GetPRSummaryContentNoIssues(t *testing.T, summaryTestDir string, entitled, simplified bool) string {
    66  	dataPath := filepath.Join(summaryTestDir, "structure")
    67  	if simplified {
    68  		if entitled {
    69  			dataPath = filepath.Join(dataPath, "summary_comment_no_issues_simplified_entitled.md")
    70  		} else {
    71  			dataPath = filepath.Join(dataPath, "summary_comment_no_issues_simplified_not_entitled.md")
    72  		}
    73  	} else {
    74  		if entitled {
    75  			dataPath = filepath.Join(dataPath, "summary_comment_no_issues_pr_entitled.md")
    76  		} else {
    77  			dataPath = filepath.Join(dataPath, "summary_comment_no_issues_pr_not_entitled.md")
    78  		}
    79  	}
    80  	return GetOutputFromFile(t, dataPath)
    81  }