github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/utils/search_test.go (about)

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