github.com/jfrog/jfrog-client-go@v1.40.2/utils/utils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"sort"
     8  	"testing"
     9  
    10  	"github.com/jfrog/jfrog-client-go/utils/io"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestRemoveRepoFromPath(t *testing.T) {
    15  	assertRemoveRepoFromPath("repo/abc/def", "/abc/def", t)
    16  	assertRemoveRepoFromPath("repo/(*)", "/(*)", t)
    17  	assertRemoveRepoFromPath("repo/", "/", t)
    18  	assertRemoveRepoFromPath("/abc/def", "/abc/def", t)
    19  	assertRemoveRepoFromPath("aaa", "aaa", t)
    20  	assertRemoveRepoFromPath("", "", t)
    21  }
    22  
    23  func assertRemoveRepoFromPath(path, expected string, t *testing.T) {
    24  	result := removeRepoFromPath(path)
    25  	if expected != result {
    26  		t.Error("Unexpected string built by removeRepoFromPath. Expected: `" + expected + "` Got `" + result + "`")
    27  	}
    28  }
    29  
    30  func TestBuildTargetPath(t *testing.T) {
    31  	assertBuildTargetPath("1(*)234", "1hello234", "{1}", "hello", true, t)
    32  	assertBuildTargetPath("1234", "1hello234", "{1}", "{1}", true, t)
    33  	assertBuildTargetPath("1(2*5)6", "123456", "{1}", "2345", true, t)
    34  	assertBuildTargetPath("(*) something", "doing something", "{1} something else", "doing something else", true, t)
    35  	assertBuildTargetPath("(switch) (this)", "switch this", "{2} {1}", "this switch", true, t)
    36  	assertBuildTargetPath("before(*)middle(*)after", "before123middle456after", "{2}{1}{2}", "456123456", true, t)
    37  	assertBuildTargetPath("foo/before(*)middle(*)after", "foo/before123middle456after", "{2}{1}{2}", "456123456", true, t)
    38  	assertBuildTargetPath("foo/before(*)middle(*)after", "bar/before123middle456after", "{2}{1}{2}", "456123456", true, t)
    39  	assertBuildTargetPath("foo/before(*)middle(*)after", "bar/before123middle456after", "{2}{1}{2}", "{2}{1}{2}", false, t)
    40  	assertBuildTargetPath("foo/before(*)middle(*)", "bar/before123middle456after", "{2}{1}{2}", "456after123456after", true, t)
    41  	assertBuildTargetPath("f(*)oo/before(*)after", "f123oo/before456after", "{2}{1}{2}", "456123456", true, t)
    42  	assertBuildTargetPath("f(*)oo/before(*)after", "f123oo/before456after", "{2}{1}{2}", "456123456", false, t)
    43  	assertBuildTargetPath("generic-(*)-(bar)", "generic-foo-bar/after/a.in", "{1}/{2}", "foo/bar", true, t)
    44  	assertBuildTargetPath("generic-(*)-(bar)/(*)", "generic-foo-bar/after/a.in", "{1}/{2}/{3}", "foo/bar/after/a.in", true, t)
    45  	assertBuildTargetPath("generic-(*)-(bar)", "generic-foo-bar/after/a.in", "{1}/{2}/after/a.in", "foo/bar/after/a.in", true, t)
    46  	assertBuildTargetPath("", "nothing should change", "nothing should change", "nothing should change", true, t)
    47  }
    48  
    49  func assertBuildTargetPath(regexp, source, dest, expected string, ignoreRepo bool, t *testing.T) {
    50  	result, _, err := BuildTargetPath(regexp, source, dest, ignoreRepo)
    51  	assert.NoError(t, err)
    52  	assert.Equal(t, expected, result)
    53  }
    54  
    55  func TestSplitWithEscape(t *testing.T) {
    56  	assertSplitWithEscape("", []string{""}, t)
    57  	assertSplitWithEscape("a", []string{"a"}, t)
    58  	assertSplitWithEscape("a/b", []string{"a", "b"}, t)
    59  	assertSplitWithEscape("a/b/c", []string{"a", "b", "c"}, t)
    60  	assertSplitWithEscape("a/b\\5/c", []string{"a", "b5", "c"}, t)
    61  	assertSplitWithEscape("a/b\\\\5.2/c", []string{"a", "b\\5.2", "c"}, t)
    62  	assertSplitWithEscape("a\\8/b\\5/c", []string{"a8", "b5", "c"}, t)
    63  	assertSplitWithEscape("a\\\\8/b\\\\5.2/c", []string{"a\\8", "b\\5.2", "c"}, t)
    64  	assertSplitWithEscape("a/b\\5/c\\0", []string{"a", "b5", "c0"}, t)
    65  	assertSplitWithEscape("a/b\\\\5.2/c\\\\0", []string{"a", "b\\5.2", "c\\0"}, t)
    66  }
    67  
    68  func assertSplitWithEscape(str string, expected []string, t *testing.T) {
    69  	result := SplitWithEscape(str, '/')
    70  	if !reflect.DeepEqual(result, expected) {
    71  		t.Error("Unexpected string array built. Expected: `", expected, "` Got `", result, "`")
    72  	}
    73  }
    74  
    75  func TestConvertLocalPatternToRegexp(t *testing.T) {
    76  	var tests = []struct {
    77  		localPath string
    78  		expected  string
    79  	}{
    80  		{"./", "^.*$"},
    81  		{".\\\\", "^.*$"},
    82  		{".\\", "^.*$"},
    83  		{"./abc", "abc"},
    84  		{".\\\\abc", "abc"},
    85  		{".\\abc", "abc"},
    86  	}
    87  	for _, test := range tests {
    88  		assert.Equal(t, test.expected, ConvertLocalPatternToRegexp(test.localPath, RegExp))
    89  	}
    90  }
    91  func TestCleanPath(t *testing.T) {
    92  	if io.IsWindows() {
    93  		parameter := "\\\\foo\\\\baz\\\\..\\\\bar\\\\*"
    94  		got := cleanPath(parameter)
    95  		want := "\\\\foo\\\\bar\\\\*"
    96  		if got != want {
    97  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
    98  		}
    99  		parameter = "\\\\foo\\\\\\\\bar\\\\*"
   100  		got = cleanPath(parameter)
   101  		if got != want {
   102  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   103  		}
   104  		parameter = "\\\\foo\\\\.\\\\bar\\\\*"
   105  		got = cleanPath(parameter)
   106  		if got != want {
   107  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   108  		}
   109  		parameter = "\\\\foo\\\\.\\\\bar\\\\*\\\\"
   110  		want = "\\\\foo\\\\bar\\\\*\\\\"
   111  		got = cleanPath(parameter)
   112  		if got != want {
   113  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   114  		}
   115  		parameter = "foo\\\\bar"
   116  		got = cleanPath(parameter)
   117  		want = "foo\\\\bar"
   118  		if got != want {
   119  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   120  		}
   121  		parameter = ".\\\\foo\\\\bar\\\\"
   122  		got = cleanPath(parameter)
   123  		want = "foo\\\\bar\\\\"
   124  		if got != want {
   125  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   126  		}
   127  	} else {
   128  		parameter := "/foo/bar/"
   129  		got := cleanPath(parameter)
   130  		want := "/foo/bar/"
   131  		if got != want {
   132  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   133  		}
   134  		parameter = "/foo/baz/../bar/*"
   135  		got = cleanPath(parameter)
   136  		want = "/foo/bar/*"
   137  		if got != want {
   138  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   139  		}
   140  		parameter = "/foo//bar/*"
   141  		got = cleanPath(parameter)
   142  		if got != want {
   143  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   144  		}
   145  		parameter = "/foo/./bar/*"
   146  		got = cleanPath(parameter)
   147  		if got != want {
   148  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   149  		}
   150  		parameter = "/foo/./bar/*/"
   151  		want = "/foo/bar/*/"
   152  		got = cleanPath(parameter)
   153  		if got != want {
   154  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   155  		}
   156  		parameter = "foo/bar"
   157  		got = cleanPath(parameter)
   158  		want = "foo/bar"
   159  		if got != want {
   160  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   161  		}
   162  		parameter = "./foo/bar/"
   163  		got = cleanPath(parameter)
   164  		want = "foo/bar/"
   165  		if got != want {
   166  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   167  		}
   168  	}
   169  }
   170  func TestIsWildcardParentheses(t *testing.T) {
   171  	strA := "/tmp/cache/download/(github.com/)"
   172  	strB := "/tmp/cache/download/(github.com/*)"
   173  	parenthesesA := CreateParenthesesSlice(strA, "")
   174  	parenthesesB := CreateParenthesesSlice(strA, "{1}")
   175  
   176  	got := isWildcardParentheses(strA, parenthesesA)
   177  	want := false
   178  	if got != want {
   179  		t.Errorf("TestIsWildcardParentheses() == %t, want %t", got, want)
   180  	}
   181  
   182  	got = isWildcardParentheses(strB, parenthesesB)
   183  	want = true
   184  	if got != want {
   185  		t.Errorf("TestIsWildcardParentheses() == %t, want %t", got, want)
   186  	}
   187  }
   188  
   189  func equalSlicesIgnoreOrder(s1, s2 []string) bool {
   190  	if len(s1) != len(s2) {
   191  		return false
   192  	}
   193  	sort.Strings(s1)
   194  	sort.Strings(s2)
   195  	return reflect.DeepEqual(s1, s2)
   196  }
   197  
   198  func TestGetMaxPlaceholderIndex(t *testing.T) {
   199  	type args struct {
   200  		toReplace string
   201  	}
   202  	tests := []struct {
   203  		name    string
   204  		args    args
   205  		want    int
   206  		wantErr assert.ErrorAssertionFunc
   207  	}{
   208  		{"empty", args{""}, 0, nil},
   209  		{"empty", args{"{}"}, 0, nil},
   210  		{"basic", args{"{1}{5}{3}"}, 5, nil},
   211  		{"basic", args{"}5{{3}"}, 3, nil},
   212  		{"basic", args{"{1}}5}{3}"}, 3, nil},
   213  		{"basic", args{"{1}5{}}{3}"}, 3, nil},
   214  		{"special characters", args{"!@#$%^&*abc(){}}{{2}!@#$%^&*abc(){}}{{1}!@#$%^&*abc(){}}{"}, 2, nil},
   215  		{"multiple digits", args{"{2}{100}fdsff{101}d#%{99}"}, 101, nil},
   216  	}
   217  	for _, tt := range tests {
   218  		t.Run(tt.name, func(t *testing.T) {
   219  			got, err := getMaxPlaceholderIndex(tt.args.toReplace)
   220  			assert.NoError(t, err)
   221  			assert.Equalf(t, tt.want, got, "getMaxPlaceholderIndex(%v)", tt.args.toReplace)
   222  		})
   223  	}
   224  }
   225  
   226  func TestReplacePlaceHolders(t *testing.T) {
   227  	type args struct {
   228  		groups    []string
   229  		toReplace string
   230  		isRegexp  bool
   231  	}
   232  	tests := []struct {
   233  		name            string
   234  		args            args
   235  		expected        string
   236  		expectedBoolean bool
   237  	}{
   238  		// First element in the group isn't relevant cause the matching loop starts from index 1.
   239  		{"non regexp, empty group", args{[]string{}, "{1}-{2}-{3}", false}, "{1}-{2}-{3}", false},
   240  		{"non regexp, empty group", args{[]string{""}, "{1}-{2}-{3}", false}, "{1}-{2}-{3}", false},
   241  		{"regexp, empty group", args{[]string{}, "{1}-{2}-{3}", true}, "{1}-{2}-{3}", false},
   242  		{"regexp, empty group", args{[]string{""}, "{1}-{2}-{3}", true}, "{1}-{2}-{3}", false},
   243  		// Non regular expressions
   244  		{"basic", args{[]string{"", "a", "b", "c"}, "{1}-{2}-{3}", false}, "a-b-c", true},
   245  		{"opposite order", args{[]string{"", "a", "b", "c"}, "{3}-{2}-{1}-{4}", false}, "c-b-a-{4}", true},
   246  		{"double", args{[]string{"", "a", "b"}, "{2}-{2}-{1}-{1}", false}, "b-b-a-a", true},
   247  		{"skip placeholders indexes", args{[]string{"", "a", "b"}, "{4}-{1}", false}, "b-a", true},
   248  		// Regular expressions
   249  		{"basic", args{[]string{"", "a", "b", "c"}, "{1}-{2}-{3}", true}, "a-b-c", true},
   250  		{"opposite order", args{[]string{"", "a", "b", "c"}, "{4}-{3}-{2}-{5}", true}, "{4}-c-b-{5}", true},
   251  		{"double", args{[]string{"", "a", "b"}, "{2}-{2}-{1}-{1}", true}, "b-b-a-a", true},
   252  		{"skip placeholders indexes", args{[]string{"", "a", "b"}, "{3}-{1}", true}, "{3}-a", true},
   253  	}
   254  	for _, tt := range tests {
   255  		t.Run(tt.name, func(t *testing.T) {
   256  			result, replaceOccurred, err := ReplacePlaceHolders(tt.args.groups, tt.args.toReplace, tt.args.isRegexp)
   257  			assert.NoError(t, err)
   258  			assert.Equalf(t, tt.expected, result, "ReplacePlaceHolders(%v, %v, %v)", tt.args.groups, tt.args.toReplace, tt.args.isRegexp)
   259  			assert.Equalf(t, tt.expectedBoolean, replaceOccurred, "ReplacePlaceHolders(%v, %v, %v)", tt.args.groups, tt.args.toReplace, tt.args.isRegexp)
   260  		})
   261  	}
   262  }
   263  
   264  func TestValidateMinimumVersion(t *testing.T) {
   265  	minTestVersion := "6.9.0"
   266  	tests := []struct {
   267  		artifactoryVersion string
   268  		expectedResult     bool
   269  	}{
   270  		{"6.5.0", false},
   271  		{"6.2.0", false},
   272  		{"5.9.0", false},
   273  		{"6.0.0", false},
   274  		{"6.6.0", false},
   275  		{"6.9.0", true},
   276  		{Development, true},
   277  		{"6.10.2", true},
   278  		{"6.15.2", true},
   279  	}
   280  	for _, test := range tests {
   281  		t.Run(test.artifactoryVersion, func(t *testing.T) {
   282  			err := ValidateMinimumVersion(Xray, test.artifactoryVersion, minTestVersion)
   283  			if test.expectedResult {
   284  				assert.NoError(t, err)
   285  			} else {
   286  				assert.ErrorContains(t, err, fmt.Sprintf(MinimumVersionMsg, Xray, test.artifactoryVersion, minTestVersion))
   287  			}
   288  		})
   289  	}
   290  }
   291  
   292  func TestSetEnvWithResetCallback(t *testing.T) {
   293  	type args struct {
   294  		key   string
   295  		value string
   296  	}
   297  	tests := []struct {
   298  		name   string
   299  		args   args
   300  		init   func()
   301  		finish func()
   302  	}{
   303  		{
   304  			name: "existing environment variable",
   305  			args: args{key: "TEST_KEY", value: "test_value"},
   306  			init: func() {
   307  				assert.NoError(t, os.Setenv("TEST_KEY", "test-init-value"))
   308  			},
   309  			finish: func() {
   310  				assert.Equal(t, os.Getenv("TEST_KEY"), "test-init-value")
   311  			},
   312  		},
   313  		{
   314  			name: "non-existing environment variable",
   315  			args: args{key: "NEW_TEST_KEY", value: "test_value"},
   316  			init: func() {
   317  
   318  			},
   319  			finish: func() {
   320  				_, exist := os.LookupEnv("NEW_TEST_KEY")
   321  				assert.False(t, exist)
   322  			},
   323  		},
   324  	}
   325  	for _, tt := range tests {
   326  		t.Run(tt.name, func(t *testing.T) {
   327  			tt.init()
   328  			resetCallback, err := SetEnvWithResetCallback(tt.args.key, tt.args.value)
   329  			assert.NoError(t, err)
   330  			assert.Equal(t, tt.args.value, os.Getenv(tt.args.key))
   331  			assert.NoError(t, resetCallback())
   332  			tt.finish()
   333  		})
   334  	}
   335  }