github.com/jfrog/jfrog-cli-core/v2@v2.52.0/artifactory/utils/repositoryutils_test.go (about) 1 package utils 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestFilterRepositoryNames(t *testing.T) { 10 repos := []string{ 11 "jfrog-docker-local", 12 "jfrog-npm-local", 13 "docker-local", 14 "jfrog-generic-remote", 15 "jfrog-maven-local", 16 } 17 includePatterns := []string{ 18 "jfrog-*-local", 19 "jfrog-generic-remote", 20 } 21 excludePatterns := []string{ 22 "*docker*", 23 "jfrog-maven-local", 24 } 25 expectedRepoNames := []string{ 26 "jfrog-npm-local", 27 "jfrog-generic-remote", 28 } 29 actualRepoNames, err := filterRepositoryNames(&repos, includePatterns, excludePatterns) 30 assert.NoError(t, err) 31 assert.ElementsMatch(t, expectedRepoNames, actualRepoNames) 32 } 33 34 var shouldIncludeRepositoryTestCases = []struct { 35 name string 36 repoKey string 37 includePatterns []string 38 excludePatterns []string 39 shouldInclude bool 40 }{ 41 {name: "Exact match", repoKey: "generic-local", includePatterns: []string{"generic-local"}, excludePatterns: []string{}, shouldInclude: true}, 42 {name: "Wildcard pattern", repoKey: "generic-local", includePatterns: []string{"*-local"}, excludePatterns: []string{}, shouldInclude: true}, 43 {name: "No match", repoKey: "generic-local", includePatterns: []string{"generic", "local"}, excludePatterns: []string{}, shouldInclude: false}, 44 {name: "Third match", repoKey: "generic-local", includePatterns: []string{"generic", "local", "generic-local"}, excludePatterns: []string{}, shouldInclude: true}, 45 {name: "Empty match", repoKey: "generic-local", includePatterns: []string{}, excludePatterns: []string{}, shouldInclude: true}, 46 {name: "All match", repoKey: "generic-local", includePatterns: []string{"*"}, excludePatterns: []string{}, shouldInclude: true}, 47 {name: "Exact exclude", repoKey: "generic-local", includePatterns: []string{}, excludePatterns: []string{"generic-local"}, shouldInclude: false}, 48 {name: "All exclude", repoKey: "generic-local", includePatterns: []string{}, excludePatterns: []string{"*"}, shouldInclude: false}, 49 {name: "All include and exclude", repoKey: "generic-local", includePatterns: []string{"*"}, excludePatterns: []string{"*"}, shouldInclude: false}, 50 {name: "Blacklisted", repoKey: "jfrog-logs", includePatterns: []string{}, excludePatterns: []string{}, shouldInclude: false}, 51 } 52 53 func TestShouldIncludeRepository(t *testing.T) { 54 for _, testCase := range shouldIncludeRepositoryTestCases { 55 t.Run(testCase.name, func(t *testing.T) { 56 repositoryFilter := &IncludeExcludeFilter{IncludePatterns: testCase.includePatterns, ExcludePatterns: testCase.excludePatterns} 57 actual, err := repositoryFilter.ShouldIncludeRepository(testCase.repoKey) 58 assert.NoError(t, err) 59 assert.Equal(t, testCase.shouldInclude, actual) 60 }) 61 } 62 }