github.com/cobalt77/jfrog-client-go@v0.14.5/utils/utils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestRemoveRepoFromPath(t *testing.T) {
     9  	assertRemoveRepoFromPath("repo/abc/def", "/abc/def", t)
    10  	assertRemoveRepoFromPath("repo/(*)", "/(*)", t)
    11  	assertRemoveRepoFromPath("repo/", "/", t)
    12  	assertRemoveRepoFromPath("/abc/def", "/abc/def", t)
    13  	assertRemoveRepoFromPath("aaa", "aaa", t)
    14  	assertRemoveRepoFromPath("", "", t)
    15  }
    16  
    17  func assertRemoveRepoFromPath(path, expected string, t *testing.T) {
    18  	result := removeRepoFromPath(path)
    19  	if expected != result {
    20  		t.Error("Unexpected string built by removeRepoFromPath. Expected: `" + expected + "` Got `" + result + "`")
    21  	}
    22  }
    23  
    24  func TestBuildTargetPath(t *testing.T) {
    25  	assertBuildTargetPath("1(*)234", "1hello234", "{1}", "hello", true, t)
    26  	assertBuildTargetPath("1234", "1hello234", "{1}", "{1}", true, t)
    27  	assertBuildTargetPath("1(2*5)6", "123456", "{1}", "2345", true, t)
    28  	assertBuildTargetPath("(*) something", "doing something", "{1} something else", "doing something else", true, t)
    29  	assertBuildTargetPath("(switch) (this)", "switch this", "{2} {1}", "this switch", true, t)
    30  	assertBuildTargetPath("before(*)middle(*)after", "before123middle456after", "{2}{1}{2}", "456123456", true, t)
    31  	assertBuildTargetPath("foo/before(*)middle(*)after", "foo/before123middle456after", "{2}{1}{2}", "456123456", true, t)
    32  	assertBuildTargetPath("foo/before(*)middle(*)after", "bar/before123middle456after", "{2}{1}{2}", "456123456", true, t)
    33  	assertBuildTargetPath("foo/before(*)middle(*)after", "bar/before123middle456after", "{2}{1}{2}", "{2}{1}{2}", false, t)
    34  	assertBuildTargetPath("foo/before(*)middle(*)", "bar/before123middle456after", "{2}{1}{2}", "456after123456after", true, t)
    35  	assertBuildTargetPath("f(*)oo/before(*)after", "f123oo/before456after", "{2}{1}{2}", "456123456", true, t)
    36  	assertBuildTargetPath("f(*)oo/before(*)after", "f123oo/before456after", "{2}{1}{2}", "456123456", false, t)
    37  	assertBuildTargetPath("generic-(*)-(bar)", "generic-foo-bar/after/a.in", "{1}/{2}", "foo/bar", true, t)
    38  	assertBuildTargetPath("generic-(*)-(bar)/(*)", "generic-foo-bar/after/a.in", "{1}/{2}/{3}", "foo/bar/after/a.in", true, t)
    39  	assertBuildTargetPath("generic-(*)-(bar)", "generic-foo-bar/after/a.in", "{1}/{2}/after/a.in", "foo/bar/after/a.in", true, t)
    40  	assertBuildTargetPath("", "nothing should change", "nothing should change", "nothing should change", true, t)
    41  }
    42  
    43  func assertBuildTargetPath(regexp, source, dest, expected string, ignoreRepo bool, t *testing.T) {
    44  	result, err := BuildTargetPath(regexp, source, dest, ignoreRepo)
    45  	if err != nil {
    46  		t.Error(err.Error())
    47  	}
    48  	if expected != result {
    49  		t.Error("Unexpected target string built. Expected: `" + expected + "` Got `" + result + "`")
    50  	}
    51  }
    52  
    53  func TestSplitWithEscape(t *testing.T) {
    54  	assertSplitWithEscape("", []string{""}, t)
    55  	assertSplitWithEscape("a", []string{"a"}, t)
    56  	assertSplitWithEscape("a/b", []string{"a", "b"}, t)
    57  	assertSplitWithEscape("a/b/c", []string{"a", "b", "c"}, t)
    58  	assertSplitWithEscape("a/b\\5/c", []string{"a", "b5", "c"}, t)
    59  	assertSplitWithEscape("a/b\\\\5.2/c", []string{"a", "b\\5.2", "c"}, t)
    60  	assertSplitWithEscape("a\\8/b\\5/c", []string{"a8", "b5", "c"}, t)
    61  	assertSplitWithEscape("a\\\\8/b\\\\5.2/c", []string{"a\\8", "b\\5.2", "c"}, t)
    62  	assertSplitWithEscape("a/b\\5/c\\0", []string{"a", "b5", "c0"}, t)
    63  	assertSplitWithEscape("a/b\\\\5.2/c\\\\0", []string{"a", "b\\5.2", "c\\0"}, t)
    64  }
    65  
    66  func assertSplitWithEscape(str string, expected []string, t *testing.T) {
    67  	result := SplitWithEscape(str, '/')
    68  	if !reflect.DeepEqual(result, expected) {
    69  		t.Error("Unexpected string array built. Expected: `", expected, "` Got `", result, "`")
    70  	}
    71  }
    72  
    73  func TestCleanPath(t *testing.T) {
    74  	if IsWindows() {
    75  		parameter := "\\\\foo\\\\baz\\\\..\\\\bar\\\\*"
    76  		got := cleanPath(parameter)
    77  		want := "\\\\foo\\\\bar\\\\*"
    78  		if got != want {
    79  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
    80  		}
    81  		parameter = "\\\\foo\\\\\\\\bar\\\\*"
    82  		got = cleanPath(parameter)
    83  		if got != want {
    84  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
    85  		}
    86  		parameter = "\\\\foo\\\\.\\\\bar\\\\*"
    87  		got = cleanPath(parameter)
    88  		if got != want {
    89  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
    90  		}
    91  		parameter = "\\\\foo\\\\.\\\\bar\\\\*\\\\"
    92  		want = "\\\\foo\\\\bar\\\\*\\\\"
    93  		got = cleanPath(parameter)
    94  		if got != want {
    95  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
    96  		}
    97  		parameter = "foo\\\\bar"
    98  		got = cleanPath(parameter)
    99  		want = "foo\\\\bar"
   100  		if got != want {
   101  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   102  		}
   103  		parameter = ".\\\\foo\\\\bar\\\\"
   104  		got = cleanPath(parameter)
   105  		want = "foo\\\\bar\\\\"
   106  		if got != want {
   107  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   108  		}
   109  	} else {
   110  		parameter := "/foo/bar/"
   111  		got := cleanPath(parameter)
   112  		want := "/foo/bar/"
   113  		if got != want {
   114  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   115  		}
   116  		parameter = "/foo/baz/../bar/*"
   117  		got = cleanPath(parameter)
   118  		want = "/foo/bar/*"
   119  		if got != want {
   120  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   121  		}
   122  		parameter = "/foo//bar/*"
   123  		got = cleanPath(parameter)
   124  		if got != want {
   125  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   126  		}
   127  		parameter = "/foo/./bar/*"
   128  		got = cleanPath(parameter)
   129  		if got != want {
   130  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   131  		}
   132  		parameter = "/foo/./bar/*/"
   133  		want = "/foo/bar/*/"
   134  		got = cleanPath(parameter)
   135  		if got != want {
   136  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   137  		}
   138  		parameter = "foo/bar"
   139  		got = cleanPath(parameter)
   140  		want = "foo/bar"
   141  		if got != want {
   142  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   143  		}
   144  		parameter = "./foo/bar/"
   145  		got = cleanPath(parameter)
   146  		want = "foo/bar/"
   147  		if got != want {
   148  			t.Errorf("cleanPath(%s) == %s, want %s", parameter, got, want)
   149  		}
   150  	}
   151  }
   152  func TestIsWildcardParentheses(t *testing.T) {
   153  	strA := "/tmp/cache/download/(github.com/)"
   154  	strB := "/tmp/cache/download/(github.com/*)"
   155  	parenthesesA := NewParenthesesSlice(strA, "")
   156  	parenthesesB := NewParenthesesSlice(strA, "{1}")
   157  
   158  	got := isWildcardParentheses(strA, parenthesesA)
   159  	want := false
   160  	if got != want {
   161  		t.Errorf("TestIsWildcardParentheses() == %t, want %t", got, want)
   162  	}
   163  
   164  	got = isWildcardParentheses(strB, parenthesesB)
   165  	want = true
   166  	if got != want {
   167  		t.Errorf("TestIsWildcardParentheses() == %t, want %t", got, want)
   168  	}
   169  }