github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/utils/cliutils/utils_test.go (about)

     1  package cliutils
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"errors"
     7  	"github.com/jfrog/jfrog-client-go/utils/log"
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func TestSpecVarsStringToMap(t *testing.T) {
    13  	var actual map[string]string
    14  	actual = SpecVarsStringToMap("")
    15  	assertSpecVars(nil, actual, t)
    16  
    17  	actual = SpecVarsStringToMap("foo=bar")
    18  	assertSpecVars(map[string]string{"foo": "bar"}, actual, t)
    19  
    20  	actual = SpecVarsStringToMap("foo=bar;bar=foo")
    21  	assertSpecVars(map[string]string{"foo": "bar", "bar": "foo"}, actual, t)
    22  
    23  	actual = SpecVarsStringToMap("foo=bar\\;bar=foo")
    24  	assertSpecVars(map[string]string{"foo": "bar;bar=foo"}, actual, t)
    25  
    26  	actual = SpecVarsStringToMap("a=b;foo=foo=bar\\;bar=foo")
    27  	assertSpecVars(map[string]string{"foo": "foo=bar;bar=foo", "a": "b"}, actual, t)
    28  
    29  	actual = SpecVarsStringToMap("foo=bar;foo=bar")
    30  	assertSpecVars(map[string]string{"foo": "bar"}, actual, t)
    31  }
    32  
    33  func assertSpecVars(expected, actual map[string]string, t *testing.T) {
    34  	if !reflect.DeepEqual(expected, actual) {
    35  		expectedMap, _ := json.Marshal(expected)
    36  		actualMap, _ := json.Marshal(actual)
    37  		t.Error("Wrong matching expected: `" + string(expectedMap) + "` Got `" + string(actualMap) + "`")
    38  	}
    39  }
    40  
    41  func TestGetExitCode(t *testing.T) {
    42  	// No error
    43  	exitCode := GetExitCode(nil, 0, 0, false)
    44  	checkExitCode(t, ExitCodeNoError, exitCode)
    45  
    46  	// Empty error
    47  	exitCode = GetExitCode(errors.New(""), 0, 0, false)
    48  	checkExitCode(t, ExitCodeError, exitCode)
    49  
    50  	// Regular error
    51  	exitCode = GetExitCode(errors.New("Error"), 0, 0, false)
    52  	checkExitCode(t, ExitCodeError, exitCode)
    53  
    54  	// Fail-no-op true without success
    55  	exitCode = GetExitCode(nil, 0, 0, true)
    56  	checkExitCode(t, ExitCodeFailNoOp, exitCode)
    57  
    58  	// Fail-no-op true with success
    59  	exitCode = GetExitCode(nil, 1, 0, true)
    60  	checkExitCode(t, ExitCodeNoError, exitCode)
    61  
    62  	// Fail-no-op false
    63  	exitCode = GetExitCode(nil, 0, 0, false)
    64  	checkExitCode(t, ExitCodeNoError, exitCode)
    65  }
    66  
    67  func checkExitCode(t *testing.T, expected, actual ExitCode) {
    68  	if expected != actual {
    69  		t.Errorf("Exit code expected %v, got %v", expected, actual)
    70  	}
    71  }
    72  
    73  func TestReplaceSpecVars(t *testing.T) {
    74  	log.SetLogger(log.NewLogger(log.INFO, nil))
    75  	var actual []byte
    76  	actual = ReplaceVars([]byte("${foo}aa"), map[string]string{"a": "k", "foo": "bar"})
    77  	assertVariablesMap([]byte("baraa"), actual, t)
    78  
    79  	actual = ReplaceVars([]byte("a${foo}a"), map[string]string{"foo": "bar"})
    80  	assertVariablesMap([]byte("abara"), actual, t)
    81  
    82  	actual = ReplaceVars([]byte("aa${foo}"), map[string]string{"foo": "bar"})
    83  	assertVariablesMap([]byte("aabar"), actual, t)
    84  
    85  	actual = ReplaceVars([]byte("${foo}${foo}${foo}"), map[string]string{"foo": "bar"})
    86  	assertVariablesMap([]byte("barbarbar"), actual, t)
    87  
    88  	actual = ReplaceVars([]byte("${talk}-${broh}-${foo}"), map[string]string{"foo": "bar", "talk": "speak", "broh": "sroh"})
    89  	assertVariablesMap([]byte("speak-sroh-bar"), actual, t)
    90  
    91  	actual = ReplaceVars([]byte("a${foo}a"), map[string]string{"foo": ""})
    92  	assertVariablesMap([]byte("aa"), actual, t)
    93  
    94  	actual = ReplaceVars([]byte("a${foo}a"), map[string]string{"a": "k", "f": "a"})
    95  	assertVariablesMap([]byte("a${foo}a"), actual, t)
    96  
    97  	actual = ReplaceVars([]byte("a${foo}a"), map[string]string{})
    98  	assertVariablesMap([]byte("a${foo}a"), actual, t)
    99  
   100  	actual = ReplaceVars(nil, nil)
   101  	assertVariablesMap([]byte(""), actual, t)
   102  }
   103  
   104  func assertVariablesMap(expected, actual []byte, t *testing.T) {
   105  	if 0 != bytes.Compare(expected, actual) {
   106  		t.Error("Wrong matching expected: `" + string(expected) + "` Got `" + string(actual) + "`")
   107  	}
   108  }