|
| 1 | +/* |
| 2 | +Copyright 2020 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 gci |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/google/go-cmp/cmp" |
| 27 | +) |
| 28 | + |
| 29 | +func TestAppendOrReplacePrefix(t *testing.T) { |
| 30 | + testCases := []struct { |
| 31 | + desc string |
| 32 | + prefix string |
| 33 | + suffix string |
| 34 | + initialFileContents string |
| 35 | + want string |
| 36 | + }{ |
| 37 | + { |
| 38 | + desc: "simple string and empty file", |
| 39 | + prefix: "hello", |
| 40 | + suffix: "world", |
| 41 | + want: `helloworld |
| 42 | +`, |
| 43 | + }, |
| 44 | + { |
| 45 | + desc: "simple string and non empty file", |
| 46 | + prefix: "hello", |
| 47 | + suffix: "world", |
| 48 | + initialFileContents: `jelloworld |
| 49 | +chelloworld |
| 50 | +`, |
| 51 | + want: `jelloworld |
| 52 | +chelloworld |
| 53 | +helloworld |
| 54 | +`, |
| 55 | + }, |
| 56 | + { |
| 57 | + desc: "simple string and file already contains prefix", |
| 58 | + prefix: "hello", |
| 59 | + suffix: "world", |
| 60 | + initialFileContents: `helloworld |
| 61 | +helloworld |
| 62 | +jelloworld |
| 63 | +chelloworld |
| 64 | +`, |
| 65 | + want: `jelloworld |
| 66 | +chelloworld |
| 67 | +helloworld |
| 68 | +`, |
| 69 | + }, |
| 70 | + { |
| 71 | + desc: "simple string and file already contains prefix with content between the prefix and suffix", |
| 72 | + prefix: "hello", |
| 73 | + suffix: "world", |
| 74 | + initialFileContents: `hellocontentsworld |
| 75 | +jelloworld |
| 76 | +chelloworld |
| 77 | +`, |
| 78 | + want: `jelloworld |
| 79 | +chelloworld |
| 80 | +helloworld |
| 81 | +`, |
| 82 | + }, |
| 83 | + { |
| 84 | + desc: "simple string and file already contains prefix with prefix == suffix", |
| 85 | + prefix: "hello", |
| 86 | + suffix: "world", |
| 87 | + initialFileContents: `hellohello |
| 88 | +jelloworld |
| 89 | +chelloworld |
| 90 | +`, |
| 91 | + want: `jelloworld |
| 92 | +chelloworld |
| 93 | +helloworld |
| 94 | +`, |
| 95 | + }, |
| 96 | + { |
| 97 | + desc: "string with quotes and = and empty file", |
| 98 | + prefix: `'"$argon2id$v=19"'`, |
| 99 | + suffix: "admin", |
| 100 | + want: `"$argon2id$v=19"admin |
| 101 | +`, |
| 102 | + }, |
| 103 | + { |
| 104 | + desc: "string with quotes and = and non empty file", |
| 105 | + prefix: `'"$argon2id$v=19"'`, |
| 106 | + suffix: "admin", |
| 107 | + initialFileContents: `jelloworld |
| 108 | +chelloworld |
| 109 | +`, |
| 110 | + want: `jelloworld |
| 111 | +chelloworld |
| 112 | +"$argon2id$v=19"admin |
| 113 | +`, |
| 114 | + }, |
| 115 | + { |
| 116 | + desc: "string with quotes and = and file already contains prefix", |
| 117 | + prefix: `'"$argon2id$v=19"'`, |
| 118 | + suffix: "admin", |
| 119 | + initialFileContents: `"$argon2id$v=19"admin |
| 120 | +"$argon2id$v=19"admin |
| 121 | +helloworld |
| 122 | +jelloworld |
| 123 | +`, |
| 124 | + want: `helloworld |
| 125 | +jelloworld |
| 126 | +"$argon2id$v=19"admin |
| 127 | +`, |
| 128 | + }, |
| 129 | + { |
| 130 | + desc: "string with quotes and = and file already contains prefix with content between the prefix and suffix", |
| 131 | + prefix: `'"$argon2id$v=19"'`, |
| 132 | + suffix: "admin", |
| 133 | + initialFileContents: `"$argon2id$v=19"contentsadmin |
| 134 | +helloworld |
| 135 | +jelloworld |
| 136 | +`, |
| 137 | + want: `helloworld |
| 138 | +jelloworld |
| 139 | +"$argon2id$v=19"admin |
| 140 | +`, |
| 141 | + }, |
| 142 | + { |
| 143 | + desc: "string with quotes and = and file already contains prefix with prefix == suffix", |
| 144 | + prefix: `'"$argon2id$v=19"'`, |
| 145 | + suffix: "admin", |
| 146 | + initialFileContents: `"$argon2id$v=19""$argon2id$v=19" |
| 147 | +helloworld |
| 148 | +jelloworld |
| 149 | +`, |
| 150 | + want: `helloworld |
| 151 | +jelloworld |
| 152 | +"$argon2id$v=19"admin |
| 153 | +`, |
| 154 | + }, |
| 155 | + } |
| 156 | + for _, tc := range testCases { |
| 157 | + t.Run(tc.desc, func(t *testing.T) { |
| 158 | + f, err := ioutil.TempFile("", "append_or_replace_test") |
| 159 | + if err != nil { |
| 160 | + t.Fatalf("Failed to create temp file: %v", err) |
| 161 | + } |
| 162 | + defer os.Remove(f.Name()) |
| 163 | + if _, err := f.WriteString(tc.initialFileContents); err != nil { |
| 164 | + t.Fatalf("Failed to write to file: %v", err) |
| 165 | + } |
| 166 | + f.Close() |
| 167 | + args := fmt.Sprintf("source configure-helper.sh; append_or_replace_prefixed_line %s %s %s", f.Name(), tc.prefix, tc.suffix) |
| 168 | + cmd := exec.Command("bash", "-c", args) |
| 169 | + stderr, err := cmd.CombinedOutput() |
| 170 | + if err != nil { |
| 171 | + t.Fatalf("Failed to run command: %v: %s", err, stderr) |
| 172 | + } |
| 173 | + got, err := ioutil.ReadFile(f.Name()) |
| 174 | + if err != nil { |
| 175 | + t.Fatalf("Failed to read file contents: %v", err) |
| 176 | + } |
| 177 | + if diff := cmp.Diff(string(got), tc.want); diff != "" { |
| 178 | + t.Errorf("File contents: got=%s, want=%s, diff=%s", got, tc.want, diff) |
| 179 | + } |
| 180 | + }) |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments