|
| 1 | +// Copyright 2026 The kpt Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package porch |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | +) |
| 22 | + |
| 23 | +func TestIsValidSubpackageDir(t *testing.T) { |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + dir string |
| 27 | + expectValid bool |
| 28 | + }{ |
| 29 | + // Invalid cases |
| 30 | + {name: "empty string", dir: "", expectValid: false}, |
| 31 | + {name: "leading slash", dir: "/subpkg", expectValid: false}, |
| 32 | + {name: "trailing slash", dir: "subpkg/", expectValid: false}, |
| 33 | + {name: "double dots at start", dir: "../subpkg", expectValid: false}, |
| 34 | + {name: "double dots in middle", dir: "sub/../pkg", expectValid: false}, |
| 35 | + {name: "double dots at end", dir: "subpkg/..", expectValid: false}, |
| 36 | + {name: "only double dots", dir: "..", expectValid: false}, |
| 37 | + {name: "dot segment at start", dir: "./subpkg", expectValid: false}, |
| 38 | + {name: "dot segment in middle", dir: "sub/./pkg", expectValid: false}, |
| 39 | + {name: "only dot", dir: ".", expectValid: false}, |
| 40 | + {name: "leading and trailing slash", dir: "/subpkg/", expectValid: false}, |
| 41 | + {name: "spaces in path", dir: "sub pkg", expectValid: false}, |
| 42 | + {name: "special characters", dir: "sub@pkg", expectValid: false}, |
| 43 | + {name: "backslash", dir: "sub\\pkg", expectValid: false}, |
| 44 | + {name: "colon in path", dir: "sub:pkg", expectValid: false}, |
| 45 | + {name: "empty segment (double slash)", dir: "sub//pkg", expectValid: false}, |
| 46 | + {name: "with underscores (invalid DNS)", dir: "my_subpkg", expectValid: false}, |
| 47 | + {name: "mixed with underscores (invalid DNS)", dir: "my-sub_pkg.v1/nested-dir", expectValid: false}, |
| 48 | + {name: "with dots in name", dir: "my.subpkg", expectValid: false}, |
| 49 | + |
| 50 | + // Valid cases |
| 51 | + {name: "simple directory", dir: "subpkg", expectValid: true}, |
| 52 | + {name: "nested directory", dir: "path/to/subpkg", expectValid: true}, |
| 53 | + {name: "two levels", dir: "sub/pkg", expectValid: true}, |
| 54 | + {name: "with hyphens", dir: "my-subpkg", expectValid: true}, |
| 55 | + {name: "numeric name", dir: "123", expectValid: true}, |
| 56 | + {name: "deeply nested", dir: "a/b/c/d/e", expectValid: true}, |
| 57 | + {name: "single char segments", dir: "a/b/c", expectValid: true}, |
| 58 | + {name: "starts with digit", dir: "1subpackage", expectValid: true}, |
| 59 | + {name: "ends with digit", dir: "subpackage1", expectValid: true}, |
| 60 | + {name: "contains digits", dir: "1subpckage2/3subpackage4/5subpackage6", expectValid: true}, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + err := IsValidSubpackageDir(tt.dir) |
| 66 | + if tt.expectValid { |
| 67 | + assert.NoError(t, err) |
| 68 | + } else { |
| 69 | + assert.Error(t, err) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestComposeSubpkgObjName(t *testing.T) { |
| 76 | + subpackageName, err := ComposeSubpkgObjName("") |
| 77 | + assert.NotNil(t, err) |
| 78 | + assert.Equal(t, "", subpackageName) |
| 79 | + |
| 80 | + subpackageName, err = ComposeSubpkgObjName("my-subpackage") |
| 81 | + assert.Nil(t, err) |
| 82 | + assert.Equal(t, "my-subpackage", subpackageName) |
| 83 | + |
| 84 | + subpackageName, err = ComposeSubpkgObjName("level1/level2/my-subpackage") |
| 85 | + assert.Nil(t, err) |
| 86 | + assert.Equal(t, "level1.level2.my-subpackage", subpackageName) |
| 87 | + |
| 88 | + subpackageName, err = ComposeSubpkgObjName("/level1/level2/") |
| 89 | + assert.NotNil(t, err) |
| 90 | + assert.Equal(t, "", subpackageName) |
| 91 | +} |
0 commit comments