| 1 | // Copyright (c) Bartłomiej Płotka @bwplotka |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | package envars |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "os" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/efficientgo/core/testutil" |
| 13 | ) |
| 14 | |
| 15 | func TestEval(t *testing.T) { |
| 16 | t.Run("simple.env", func(t *testing.T) { |
| 17 | b, err := os.ReadFile("testdata/simple.env") |
| 18 | testutil.Ok(t, err) |
| 19 | |
| 20 | e, err := EvalVariables( |
| 21 | context.TODO(), |
| 22 | bytes.NewReader(b), |
| 23 | "PATH="+os.Getenv("PATH"), // go executable has to be available. |
| 24 | "GOBIN=/home/something/bin", |
| 25 | "HOME="+os.Getenv("HOME"), |
| 26 | ) |
| 27 | testutil.Ok(t, err) |
| 28 | testutil.Equals(t, EnvSlice{ |
| 29 | "VAR1=with space 124", "VAR2=with space 124-yolo", "VAR3=with\\n\\nnewline", |
| 30 | }, e) |
| 31 | }) |
| 32 | t.Run("bingo.env", func(t *testing.T) { |
| 33 | b, err := os.ReadFile("testdata/bingo.env") |
| 34 | testutil.Ok(t, err) |
| 35 | |
| 36 | e, err := EvalVariables( |
| 37 | context.TODO(), |
| 38 | bytes.NewReader(b), |
| 39 | "PATH="+os.Getenv("PATH"), // go executable has to be available. |
| 40 | "GOBIN=/home/something/bin", |
| 41 | "HOME="+os.Getenv("HOME"), |
| 42 | ) |
| 43 | testutil.Ok(t, err) |
| 44 | testutil.Equals(t, EnvSlice{ |
| 45 | "GOBIN=/home/something/bin", |
| 46 | "COPYRIGHT=/home/something/bin/copyright-v0.0.0-20210107100701-44cf59f65a1b", |
| 47 | "EMBEDMD=/home/something/bin/embedmd-v1.0.0", |
| 48 | "FAILLINT=/home/something/bin/faillint-v1.5.0", |
| 49 | "GOIMPORTS=/home/something/bin/goimports-v0.0.0-20200519204825-e64124511800", |
| 50 | "GOLANGCI_LINT=/home/something/bin/golangci-lint-v1.26.0", |
| 51 | "MDOX=/home/something/bin/mdox-v0.1.1-0.20201227133330-19093fdd9326", |
| 52 | "MISSPELL=/home/something/bin/misspell-v0.3.4", |
| 53 | "PROXY=/home/something/bin/proxy-v0.10.0", |
| 54 | }, e) |
| 55 | }) |
| 56 | t.Run("export.env", func(t *testing.T) { |
| 57 | b, err := os.ReadFile("testdata/export.env") |
| 58 | testutil.Ok(t, err) |
| 59 | |
| 60 | e, err := EvalVariables( |
| 61 | context.TODO(), |
| 62 | bytes.NewReader(b), |
| 63 | "PATH="+os.Getenv("PATH"), // go executable has to be available. |
| 64 | "GOBIN=/home/something/bin", |
| 65 | "HOME="+os.Getenv("HOME"), |
| 66 | ) |
| 67 | testutil.Ok(t, err) |
| 68 | testutil.Equals(t, EnvSlice{"GOBIN=/home/something/bin", "PROXY=/home/something/bin/proxy-v0.10.0", "X=/home/something/bin/proxy-v0.12.0"}, e) |
| 69 | }) |
| 70 | |
| 71 | } |
| 72 | |
| 73 | func TestMergeEnvSlices(t *testing.T) { |
| 74 | t.Run("just base", func(t *testing.T) { |
| 75 | testutil.Equals(t, []string{ |
| 76 | "OPTION_A=1", |
| 77 | "OPTION_B=2", |
| 78 | "OPTION_C=", |
| 79 | "OPTION_D=\\n", |
| 80 | "OPTION_E=1", |
| 81 | "OPTION_F=2", |
| 82 | "OPTION_G=", |
| 83 | "OPTION_H=\n", |
| 84 | "OPTION_I=echo 'asd'", |
| 85 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable", |
| 86 | }, MergeEnvSlices([]string{ |
| 87 | "OPTION_A=1", |
| 88 | "OPTION_B=2", |
| 89 | "OPTION_C=", |
| 90 | "OPTION_D=\\n", |
| 91 | "OPTION_E=1", |
| 92 | "OPTION_F=2", |
| 93 | "OPTION_G=", |
| 94 | "OPTION_H=\n", |
| 95 | "OPTION_I=echo 'asd'", |
| 96 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable", |
| 97 | })) |
| 98 | }) |
| 99 | t.Run("just over", func(t *testing.T) { |
| 100 | testutil.Equals(t, []string{ |
| 101 | "OPTION_A=1", |
| 102 | "OPTION_B=2", |
| 103 | "OPTION_C=", |
| 104 | "OPTION_D=\\n", |
| 105 | "OPTION_E=1", |
| 106 | "OPTION_F=2", |
| 107 | "OPTION_G=", |
| 108 | "OPTION_H=\n", |
| 109 | "OPTION_I=echo 'asd'", |
| 110 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable", |
| 111 | }, MergeEnvSlices([]string{}, |
| 112 | "OPTION_A=1", |
| 113 | "OPTION_B=2", |
| 114 | "OPTION_C=", |
| 115 | "OPTION_D=\\n", |
| 116 | "OPTION_E=1", |
| 117 | "OPTION_F=2", |
| 118 | "OPTION_G=", |
| 119 | "OPTION_H=\n", |
| 120 | "OPTION_I=echo 'asd'", |
| 121 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable", |
| 122 | )) |
| 123 | }) |
| 124 | t.Run("same", func(t *testing.T) { |
| 125 | testutil.Equals(t, []string{ |
| 126 | "OPTION_A=1", |
| 127 | "OPTION_B=2", |
| 128 | "OPTION_C=", |
| 129 | "OPTION_D=\\n", |
| 130 | "OPTION_E=1", |
| 131 | "OPTION_F=2", |
| 132 | "OPTION_G=", |
| 133 | "OPTION_H=\n", |
| 134 | "OPTION_I=echo 'asd'", |
| 135 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable", |
| 136 | }, MergeEnvSlices([]string{ |
| 137 | "OPTION_A=1", |
| 138 | "OPTION_B=2", |
| 139 | "OPTION_C=", |
| 140 | "OPTION_D=\\n", |
| 141 | "OPTION_E=1", |
| 142 | "OPTION_F=2", |
| 143 | "OPTION_G=", |
| 144 | "OPTION_H=\n", |
| 145 | "OPTION_I=echo 'asd'", |
| 146 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable", |
| 147 | }, |
| 148 | "OPTION_A=1", |
| 149 | "OPTION_B=2", |
| 150 | "OPTION_C=", |
| 151 | "OPTION_D=\\n", |
| 152 | "OPTION_E=1", |
| 153 | "OPTION_F=2", |
| 154 | "OPTION_G=", |
| 155 | "OPTION_H=\n", |
| 156 | "OPTION_I=echo 'asd'", |
| 157 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable", |
| 158 | )) |
| 159 | }) |
| 160 | t.Run("real with dups", func(t *testing.T) { |
| 161 | testutil.Equals(t, []string{ |
| 162 | "OPTION_A=1", |
| 163 | "OPTION_B=2", |
| 164 | "OPTION_C=22", |
| 165 | "OPTION_D=\\n22", |
| 166 | "OPTION_E=122", |
| 167 | "OPTION_F=222", |
| 168 | "OPTION_G=22", |
| 169 | "OPTION_H=\n22", |
| 170 | "OPTION_I=echo 'asd'22", |
| 171 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable22", |
| 172 | }, MergeEnvSlices([]string{ |
| 173 | "OPTION_A=1", |
| 174 | "OPTION_B=2", |
| 175 | "OPTION_C=", |
| 176 | "OPTION_G=", |
| 177 | "OPTION_H=\n", |
| 178 | "OPTION_H=\n", |
| 179 | "OPTION_H=\n", |
| 180 | "OPTION_H=\n", |
| 181 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable", |
| 182 | }, |
| 183 | "OPTION_C=22", |
| 184 | "OPTION_D=\\n22", |
| 185 | "OPTION_E=122", |
| 186 | "OPTION_F=222", |
| 187 | "OPTION_G=22", |
| 188 | "OPTION_H=\n", |
| 189 | "OPTION_H=\n", |
| 190 | "OPTION_H=\n", |
| 191 | "OPTION_H=\n22", |
| 192 | "OPTION_I=echo 'asd'22", |
| 193 | "OPTION_J=postgres://localhost:5432/database?sslmode=disable22", |
| 194 | )) |
| 195 | }) |
| 196 | |
| 197 | } |