|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package utils |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | +) |
| 24 | + |
| 25 | +func TestShortenFileName(t *testing.T) { |
| 26 | + const hashLength = 10 |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + filename string |
| 30 | + expected string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "Shorter than max length", |
| 34 | + filename: "short file name", |
| 35 | + expected: "short file name", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Longer than max length, truncated", |
| 39 | + filename: "a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters..", |
| 40 | + expected: "a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 ch-ad31f675", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "Exactly max length, not truncated", |
| 44 | + filename: "a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters.", |
| 45 | + expected: "a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters.", |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + for _, tt := range tests { |
| 50 | + t.Run(tt.name, func(t *testing.T) { |
| 51 | + result := ShortenFileName(tt.filename) |
| 52 | + assert.Equal(t, tt.expected, result) |
| 53 | + assert.LessOrEqual(t, len(result), maxFileNameLength) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
0 commit comments