|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func createFile(name string, content string) (remove func(), err error) { |
| 12 | + f, err := os.Create(name) |
| 13 | + if err != nil { |
| 14 | + return nil, err |
| 15 | + } |
| 16 | + defer f.Close() |
| 17 | + |
| 18 | + if _, err := f.WriteString(content); err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + |
| 22 | + return func() { |
| 23 | + if err := os.Remove(name); err != nil { |
| 24 | + panic(fmt.Sprintf("failed to remove file: %s", err)) |
| 25 | + } |
| 26 | + }, nil |
| 27 | +} |
| 28 | + |
| 29 | +func setEnvVariables(vals map[string]string) func() { |
| 30 | + for k, v := range vals { |
| 31 | + os.Setenv(k, v) |
| 32 | + } |
| 33 | + return func() { |
| 34 | + for k := range vals { |
| 35 | + os.Unsetenv(k) |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestLoadEnvConfig(t *testing.T) { |
| 41 | + for label, test := range map[string]struct { |
| 42 | + fileContent string |
| 43 | + envVars map[string]string |
| 44 | + want *EnvConfig |
| 45 | + }{ |
| 46 | + "all values empty in file and env": { |
| 47 | + fileContent: `TELEGRAM_ID= |
| 48 | +TELEGRAM_TOKEN= |
| 49 | +EDIT_WAIT_SECONDS=`, |
| 50 | + want: &EnvConfig{ |
| 51 | + TelegramID: []int64{}, |
| 52 | + TelegramToken: "", |
| 53 | + EditWaitSeconds: 0, |
| 54 | + }, |
| 55 | + }, |
| 56 | + "no file, all values through env": { |
| 57 | + envVars: map[string]string{ |
| 58 | + "TELEGRAM_ID": "123,456", |
| 59 | + "TELEGRAM_TOKEN": "token", |
| 60 | + "EDIT_WAIT_SECONDS": "10", |
| 61 | + }, |
| 62 | + want: &EnvConfig{ |
| 63 | + TelegramID: []int64{123, 456}, |
| 64 | + TelegramToken: "token", |
| 65 | + EditWaitSeconds: 10, |
| 66 | + }, |
| 67 | + }, |
| 68 | + "all values provided in file, single TELEGRAM_ID": { |
| 69 | + fileContent: `TELEGRAM_ID=123 |
| 70 | +TELEGRAM_TOKEN=abc |
| 71 | +EDIT_WAIT_SECONDS=10`, |
| 72 | + want: &EnvConfig{ |
| 73 | + TelegramID: []int64{123}, |
| 74 | + TelegramToken: "abc", |
| 75 | + EditWaitSeconds: 10, |
| 76 | + }, |
| 77 | + }, |
| 78 | + "multiple TELEGRAM_IDs provided in file": { |
| 79 | + fileContent: `TELEGRAM_ID=123,456 |
| 80 | +TELEGRAM_TOKEN=abc |
| 81 | +EDIT_WAIT_SECONDS=10`, |
| 82 | + envVars: map[string]string{}, |
| 83 | + want: &EnvConfig{ |
| 84 | + TelegramID: []int64{123, 456}, |
| 85 | + TelegramToken: "abc", |
| 86 | + EditWaitSeconds: 10, |
| 87 | + }, |
| 88 | + }, |
| 89 | + "env variables should override file values": { |
| 90 | + fileContent: `TELEGRAM_ID=123 |
| 91 | +TELEGRAM_TOKEN=abc |
| 92 | +EDIT_WAIT_SECONDS=10`, |
| 93 | + envVars: map[string]string{ |
| 94 | + "TELEGRAM_ID": "456", |
| 95 | + "TELEGRAM_TOKEN": "def", |
| 96 | + "EDIT_WAIT_SECONDS": "20", |
| 97 | + }, |
| 98 | + want: &EnvConfig{ |
| 99 | + TelegramID: []int64{456}, |
| 100 | + TelegramToken: "def", |
| 101 | + EditWaitSeconds: 20, |
| 102 | + }, |
| 103 | + }, |
| 104 | + "multiple TELEGRAM_IDs provided in env": { |
| 105 | + fileContent: `TELEGRAM_ID=123 |
| 106 | +TELEGRAM_TOKEN=abc |
| 107 | +EDIT_WAIT_SECONDS=10`, |
| 108 | + envVars: map[string]string{ |
| 109 | + "TELEGRAM_ID": "456,789", |
| 110 | + }, |
| 111 | + want: &EnvConfig{ |
| 112 | + TelegramID: []int64{456, 789}, |
| 113 | + TelegramToken: "abc", |
| 114 | + EditWaitSeconds: 10, |
| 115 | + }, |
| 116 | + }, |
| 117 | + } { |
| 118 | + t.Run(label, func(t *testing.T) { |
| 119 | + unset := setEnvVariables(test.envVars) |
| 120 | + t.Cleanup(unset) |
| 121 | + |
| 122 | + if test.fileContent != "" { |
| 123 | + remove, err := createFile("test.env", test.fileContent) |
| 124 | + require.NoError(t, err) |
| 125 | + t.Cleanup(remove) |
| 126 | + } |
| 127 | + |
| 128 | + cfg, err := LoadEnvConfig("test.env") |
| 129 | + require.NoError(t, err) |
| 130 | + require.Equal(t, test.want, cfg) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments