|
| 1 | +/* |
| 2 | +Copyright 2021 The Skaffold 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 helm |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/GoogleContainerTools/skaffold/testutil" |
| 23 | +) |
| 24 | + |
| 25 | +func TestSanitizeFilePath(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + description string |
| 28 | + isWindowsOS bool |
| 29 | + input string |
| 30 | + output string |
| 31 | + }{ |
| 32 | + { |
| 33 | + description: "unescaped relative path on Windows", |
| 34 | + isWindowsOS: true, |
| 35 | + input: `.\foo\not.escaped.relative.yaml`, |
| 36 | + output: `.\\foo\\not.escaped.relative.yaml`, |
| 37 | + }, |
| 38 | + { |
| 39 | + description: "unescaped absolute path on Windows", |
| 40 | + isWindowsOS: true, |
| 41 | + input: `C:\Users\foo\not.escaped.abs.yaml`, |
| 42 | + output: `C:\\Users\\foo\\not.escaped.abs.yaml`, |
| 43 | + }, |
| 44 | + { |
| 45 | + description: "escaped relative path on Windows", |
| 46 | + isWindowsOS: true, |
| 47 | + input: `.\\foo\\escaped.relative.yaml`, |
| 48 | + output: `.\\foo\\escaped.relative.yaml`, |
| 49 | + }, |
| 50 | + { |
| 51 | + description: "escaped absolute path on Windows", |
| 52 | + isWindowsOS: true, |
| 53 | + input: `C:\\Users\\foo\\escaped.abs.yaml`, |
| 54 | + output: `C:\\Users\\foo\\escaped.abs.yaml`, |
| 55 | + }, |
| 56 | + { |
| 57 | + description: "escaped relative path with spaces on Windows", |
| 58 | + isWindowsOS: true, |
| 59 | + input: `.\\foo bar\\escaped.spaces.relative.yaml`, |
| 60 | + output: `".\\foo bar\\escaped.spaces.relative.yaml"`, |
| 61 | + }, |
| 62 | + { |
| 63 | + description: "escaped absolute path with spaces on Windows", |
| 64 | + isWindowsOS: true, |
| 65 | + input: `C:\\Users\\foo bar\\escaped.spaces.abs.yaml`, |
| 66 | + output: `"C:\\Users\\foo bar\\escaped.spaces.abs.yaml"`, |
| 67 | + }, |
| 68 | + { |
| 69 | + description: "unescaped relative path with spaces on Windows", |
| 70 | + isWindowsOS: true, |
| 71 | + input: `.\foo bar\not.escaped.spaces.relative.yaml`, |
| 72 | + output: `".\\foo bar\\not.escaped.spaces.relative.yaml"`, |
| 73 | + }, |
| 74 | + { |
| 75 | + description: "unescaped absolute path with spaces on Windows", |
| 76 | + isWindowsOS: true, |
| 77 | + input: `C:\Users\foo bar\not.escaped.spaces.abs.yaml`, |
| 78 | + output: `"C:\\Users\\foo bar\\not.escaped.spaces.abs.yaml"`, |
| 79 | + }, |
| 80 | + { |
| 81 | + description: "relative path on non-Windows", |
| 82 | + input: `./foo/spaces.relative.yaml`, |
| 83 | + output: `./foo/spaces.relative.yaml`, |
| 84 | + }, |
| 85 | + { |
| 86 | + description: "absolute path on non-Windows", |
| 87 | + input: `z/foo/spaces.abs.yaml`, |
| 88 | + output: `z/foo/spaces.abs.yaml`, |
| 89 | + }, |
| 90 | + { |
| 91 | + description: "relative path with spaces on non-Windows", |
| 92 | + input: `./foo bar/spaces.relative.yaml`, |
| 93 | + output: `"./foo bar/spaces.relative.yaml"`, |
| 94 | + }, |
| 95 | + { |
| 96 | + description: "absolute path with spaces on non-Windows", |
| 97 | + input: `z/foo bar/spaces.abs.yaml`, |
| 98 | + output: `"z/foo bar/spaces.abs.yaml"`, |
| 99 | + }, |
| 100 | + { |
| 101 | + description: "unescaped relative dir path on Windows", |
| 102 | + isWindowsOS: true, |
| 103 | + input: `.\foo\not_escaped_relative\`, |
| 104 | + output: `.\\foo\\not_escaped_relative\\`, |
| 105 | + }, |
| 106 | + { |
| 107 | + description: "escaped relative dir path on Windows", |
| 108 | + isWindowsOS: true, |
| 109 | + input: `.\\foo\\escaped_relative\\`, |
| 110 | + output: `.\\foo\\escaped_relative\\`, |
| 111 | + }, |
| 112 | + { |
| 113 | + description: "unescaped relative dir path on non-Windows", |
| 114 | + input: `./foo/not_escaped_relative/`, |
| 115 | + output: `./foo/not_escaped_relative/`, |
| 116 | + }, |
| 117 | + } |
| 118 | + for _, test := range tests { |
| 119 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 120 | + output := sanitizeFilePath(test.input, test.isWindowsOS) |
| 121 | + t.CheckDeepEqual(test.output, output) |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments