-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for sourcing behavior
- Loading branch information
Showing
6 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 get . | ||
- name: Test with the Go CLI | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FOO=${bar:-qux} | ||
BAR=xxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FOO=bar | ||
BAZ=qux |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
BAR=xxx | ||
FOO=${BAR} | ||
BAZ=$BAR | ||
QUX=${QUX} |