From 12dbfa41d177f7d26a8fba42e58dc8553f9ec97e Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Mon, 19 Feb 2024 20:57:40 +0100 Subject: [PATCH] Add unit tests for sourcing behavior --- .github/workflows/unit.yml | 21 ++++++++++ cmd/backup/config_provider.go | 2 + cmd/backup/config_provider_test.go | 67 ++++++++++++++++++++++++++++++ cmd/backup/testdata/braces.env | 2 + cmd/backup/testdata/default.env | 2 + cmd/backup/testdata/expansion.env | 4 ++ 6 files changed, 98 insertions(+) create mode 100644 .github/workflows/unit.yml create mode 100644 cmd/backup/config_provider_test.go create mode 100644 cmd/backup/testdata/braces.env create mode 100644 cmd/backup/testdata/default.env create mode 100644 cmd/backup/testdata/expansion.env diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml new file mode 100644 index 00000000..04b35ecc --- /dev/null +++ b/.github/workflows/unit.yml @@ -0,0 +1,21 @@ +name: Run Unit Tests + +on: + push: + branches: + - main + pull_request: + +jobs: + build: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '1.22.x' + - name: Install dependencies + run: go mod download + - name: Test with the Go CLI + run: go test -v ./... diff --git a/cmd/backup/config_provider.go b/cmd/backup/config_provider.go index 8414305f..0d33b185 100644 --- a/cmd/backup/config_provider.go +++ b/cmd/backup/config_provider.go @@ -123,6 +123,8 @@ func loadConfigsFromEnvFiles(directory string) ([]*Config, error) { return configs, nil } +// source tries to mimic the pre v2.37.0 behavior of calling +// `set +a; source $path; set -a` and returns the env vars as a map func source(path string) (map[string]string, error) { f, err := os.Open(path) if err != nil { diff --git a/cmd/backup/config_provider_test.go b/cmd/backup/config_provider_test.go new file mode 100644 index 00000000..ab033688 --- /dev/null +++ b/cmd/backup/config_provider_test.go @@ -0,0 +1,67 @@ +package main + +import ( + "os" + "reflect" + "testing" +) + +func TestSource(t *testing.T) { + tests := []struct { + name string + input string + expectError bool + expectedOutput map[string]string + }{ + { + "default", + "testdata/default.env", + false, + map[string]string{ + "FOO": "bar", + "BAZ": "qux", + }, + }, + { + "not found", + "testdata/nope.env", + true, + nil, + }, + { + "braces", + "testdata/braces.env", + false, + map[string]string{ + "FOO": "${bar:-qux}", + "BAR": "xxx", + }, + }, + { + "expansion", + "testdata/expansion.env", + false, + map[string]string{ + "BAR": "xxx", + "FOO": "xxx", + "BAZ": "xxx", + "QUX": "yyy", + }, + }, + } + + os.Setenv("QUX", "yyy") + defer os.Unsetenv("QUX") + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result, err := source(test.input) + if (err != nil) != test.expectError { + t.Errorf("Unexpected error value %v", err) + } + if !reflect.DeepEqual(test.expectedOutput, result) { + t.Errorf("Expected %v, got %v", test.expectedOutput, result) + } + }) + } +} diff --git a/cmd/backup/testdata/braces.env b/cmd/backup/testdata/braces.env new file mode 100644 index 00000000..a13a2639 --- /dev/null +++ b/cmd/backup/testdata/braces.env @@ -0,0 +1,2 @@ +FOO=${bar:-qux} +BAR=xxx diff --git a/cmd/backup/testdata/default.env b/cmd/backup/testdata/default.env new file mode 100644 index 00000000..214ea78c --- /dev/null +++ b/cmd/backup/testdata/default.env @@ -0,0 +1,2 @@ +FOO=bar +BAZ=qux diff --git a/cmd/backup/testdata/expansion.env b/cmd/backup/testdata/expansion.env new file mode 100644 index 00000000..cba0e6d0 --- /dev/null +++ b/cmd/backup/testdata/expansion.env @@ -0,0 +1,4 @@ +BAR=xxx +FOO=${BAR} +BAZ=$BAR +QUX=${QUX}