github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/utils/search_test.go (about) 1 package utils 2 3 import ( 4 "bytes" 5 corelog "github.com/jfrog/jfrog-cli-core/utils/log" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/jfrog/jfrog-client-go/utils/errorutils" 11 "github.com/jfrog/jfrog-client-go/utils/io/content" 12 "github.com/jfrog/jfrog-client-go/utils/log" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestPrintSearchResults(t *testing.T) { 17 testdataPath, err := getTestDataPath() 18 assert.NoError(t, err) 19 reader := content.NewContentReader(filepath.Join(testdataPath, "search_results.json"), content.DefaultKey) 20 21 previousLog := log.Logger 22 newLog := log.NewLogger(corelog.GetCliLogLevel(), nil) 23 // Restore previous logger when the function returns. 24 defer log.SetLogger(previousLog) 25 26 // Set new logger with output redirection to buffer. 27 buffer := &bytes.Buffer{} 28 newLog.SetOutputWriter(buffer) 29 log.SetLogger(newLog) 30 31 // Print search result. 32 err = PrintSearchResults(reader) 33 assert.NoError(t, err) 34 35 // Compare output. 36 logOutput := buffer.Bytes() 37 compareResult := bytes.Compare(logOutput, []byte(expectedLogOutput)) 38 assert.Equal(t, 0, compareResult) 39 } 40 41 func getTestDataPath() (string, error) { 42 dir, err := os.Getwd() 43 if err != nil { 44 return "", errorutils.CheckError(err) 45 } 46 return filepath.Join(dir, "testdata"), nil 47 } 48 49 const expectedLogOutput = `[ 50 { 51 "path": "jfrog-cli-tests-repo1-1595270324/a/b/c/c2.in", 52 "type": "file", 53 "size": 11, 54 "created": "2020-07-20T21:39:38.374+03:00", 55 "modified": "2020-07-20T21:39:38.332+03:00", 56 "sha1": "a4f912be11e7d1d346e34c300e6d4b90e136896e", 57 "md5": "82b6d565393a3fd1cc4778b1d53c0664", 58 "props": { 59 "c": [ 60 "3" 61 ] 62 } 63 }, 64 { 65 "path": "jfrog-cli-tests-repo1-1595270324/a/b/c/c3.in", 66 "type": "file", 67 "size": 11, 68 "created": "2020-07-20T21:39:38.392+03:00", 69 "modified": "2020-07-20T21:39:38.332+03:00", 70 "sha1": "2d6ee506188db9b816a6bfb79c5df562fc1d8658", 71 "md5": "d8020b86244956f647cf1beff5acdb90", 72 "props": { 73 "c": [ 74 "3" 75 ] 76 } 77 }, 78 { 79 "path": "jfrog-cli-tests-repo1-1595270324/a/b/b2.in", 80 "type": "file", 81 "size": 9, 82 "created": "2020-07-20T21:39:38.413+03:00", 83 "modified": "2020-07-20T21:39:38.410+03:00", 84 "sha1": "3b60b837e037568856bedc1dd4952d17b3f06972", 85 "md5": "6931271be1e5f98e36bdc7a05097407b", 86 "props": { 87 "b": [ 88 "1" 89 ], 90 "c": [ 91 "3" 92 ] 93 } 94 }, 95 { 96 "path": "jfrog-cli-tests-repo1-1595270324/a/b/b3.in", 97 "type": "file", 98 "size": 9, 99 "created": "2020-07-20T21:39:38.413+03:00", 100 "modified": "2020-07-20T21:39:38.410+03:00", 101 "sha1": "ec6420d2b5f708283619b25e68f9ddd351f555fe", 102 "md5": "305b21db102cf3a3d2d8c3f7e9584dba", 103 "props": { 104 "a": [ 105 "1" 106 ], 107 "b": [ 108 "2" 109 ], 110 "c": [ 111 "3" 112 ] 113 } 114 }, 115 { 116 "path": "jfrog-cli-tests-repo1-1595270324/a/a3.in", 117 "type": "file", 118 "size": 7, 119 "created": "2020-07-20T21:39:38.430+03:00", 120 "modified": "2020-07-20T21:39:38.428+03:00", 121 "sha1": "29d38faccfe74dee60d0142a716e8ea6fad67b49", 122 "md5": "73c046196302ff7218d47046cf3c0501", 123 "props": { 124 "a": [ 125 "1" 126 ], 127 "b": [ 128 "3" 129 ], 130 "c": [ 131 "3" 132 ] 133 } 134 } 135 ] 136 `