|
| 1 | +// Copyright 2022-2026 Salesforce, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package slackdotenv |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/spf13/afero" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func Test_Read(t *testing.T) { |
| 25 | + tests := map[string]struct { |
| 26 | + fs afero.Fs |
| 27 | + dotenv string |
| 28 | + writeDotenv bool |
| 29 | + expected map[string]string |
| 30 | + expectErr bool |
| 31 | + }{ |
| 32 | + "returns nil when fs is nil": { |
| 33 | + fs: nil, |
| 34 | + expected: nil, |
| 35 | + }, |
| 36 | + "returns nil when .env file does not exist": { |
| 37 | + fs: afero.NewMemMapFs(), |
| 38 | + expected: nil, |
| 39 | + }, |
| 40 | + "returns empty map for empty .env file": { |
| 41 | + fs: afero.NewMemMapFs(), |
| 42 | + dotenv: "", |
| 43 | + writeDotenv: true, |
| 44 | + expected: map[string]string{}, |
| 45 | + }, |
| 46 | + "parses single variable": { |
| 47 | + fs: afero.NewMemMapFs(), |
| 48 | + dotenv: "FOO=bar\n", |
| 49 | + writeDotenv: true, |
| 50 | + expected: map[string]string{"FOO": "bar"}, |
| 51 | + }, |
| 52 | + "parses multiple variables": { |
| 53 | + fs: afero.NewMemMapFs(), |
| 54 | + dotenv: "FOO=bar\nBAZ=qux\n", |
| 55 | + writeDotenv: true, |
| 56 | + expected: map[string]string{"FOO": "bar", "BAZ": "qux"}, |
| 57 | + }, |
| 58 | + "parses quoted values": { |
| 59 | + fs: afero.NewMemMapFs(), |
| 60 | + dotenv: `TOKEN="my secret token"` + "\n", |
| 61 | + writeDotenv: true, |
| 62 | + expected: map[string]string{"TOKEN": "my secret token"}, |
| 63 | + }, |
| 64 | + "skips comment lines": { |
| 65 | + fs: afero.NewMemMapFs(), |
| 66 | + dotenv: "# this is a comment\nFOO=bar\n", |
| 67 | + writeDotenv: true, |
| 68 | + expected: map[string]string{"FOO": "bar"}, |
| 69 | + }, |
| 70 | + "handles values with equals signs": { |
| 71 | + fs: afero.NewMemMapFs(), |
| 72 | + dotenv: "URL=https://example.com?foo=bar&baz=qux\n", |
| 73 | + writeDotenv: true, |
| 74 | + expected: map[string]string{"URL": "https://example.com?foo=bar&baz=qux"}, |
| 75 | + }, |
| 76 | + "handles empty values": { |
| 77 | + fs: afero.NewMemMapFs(), |
| 78 | + dotenv: "EMPTY=\n", |
| 79 | + writeDotenv: true, |
| 80 | + expected: map[string]string{"EMPTY": ""}, |
| 81 | + }, |
| 82 | + } |
| 83 | + for name, tc := range tests { |
| 84 | + t.Run(name, func(t *testing.T) { |
| 85 | + if tc.writeDotenv && tc.fs != nil { |
| 86 | + _ = afero.WriteFile(tc.fs, ".env", []byte(tc.dotenv), 0644) |
| 87 | + } |
| 88 | + result, err := Read(tc.fs) |
| 89 | + if tc.expectErr { |
| 90 | + assert.Error(t, err) |
| 91 | + } else { |
| 92 | + assert.NoError(t, err) |
| 93 | + } |
| 94 | + assert.Equal(t, tc.expected, result) |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
0 commit comments