|
| 1 | +/* |
| 2 | +Copyright 2020 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 logfile |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/GoogleContainerTools/skaffold/testutil" |
| 25 | +) |
| 26 | + |
| 27 | +func TestCreate(t *testing.T) { |
| 28 | + var tests = []struct { |
| 29 | + description string |
| 30 | + name string |
| 31 | + expectedName string |
| 32 | + }{ |
| 33 | + { |
| 34 | + description: "create file", |
| 35 | + name: "logs.txt", |
| 36 | + expectedName: "logs.txt", |
| 37 | + }, |
| 38 | + { |
| 39 | + description: "escape name", |
| 40 | + name: "a/name.txt", |
| 41 | + expectedName: "a-name.txt", |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, test := range tests { |
| 46 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 47 | + file, err := Create(test.name) |
| 48 | + defer func() { |
| 49 | + file.Close() |
| 50 | + os.Remove(file.Name()) |
| 51 | + }() |
| 52 | + |
| 53 | + t.CheckNoError(err) |
| 54 | + t.CheckDeepEqual(filepath.Join(os.TempDir(), "skaffold", test.expectedName), file.Name()) |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestEscape(t *testing.T) { |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + expected string |
| 63 | + }{ |
| 64 | + {name: "img", expected: "img"}, |
| 65 | + {name: "log.txt", expected: "log.txt"}, |
| 66 | + {name: "project/img", expected: "project-img"}, |
| 67 | + {name: "gcr.io/project/img", expected: "gcr.io-project-img"}, |
| 68 | + } |
| 69 | + for _, test := range tests { |
| 70 | + testutil.Run(t, test.name, func(t *testutil.T) { |
| 71 | + escaped := escape(test.name) |
| 72 | + |
| 73 | + t.CheckDeepEqual(test.expected, escaped) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments