forked from coop-cloud-mirrors/godotenv
Compare commits
6 Commits
v1.5.1
...
v1.6.0-pre
Author | SHA1 | Date | |
---|---|---|---|
a7f6c4c583 | |||
32e64fa834 | |||
7765d9d198 | |||
383d64cb7e | |||
e3b6eee84d | |||
193c9aba29 |
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -14,7 +14,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- name: Setup go
|
- name: Setup go
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: ${{ matrix.go }}
|
go-version: ${{ matrix.go }}
|
||||||
- run: go test
|
- run: go test
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
@ -3,3 +3,4 @@ OPTION_B=${OPTION_A}
|
|||||||
OPTION_C=$OPTION_B
|
OPTION_C=$OPTION_B
|
||||||
OPTION_D=${OPTION_A}${OPTION_B}
|
OPTION_D=${OPTION_A}${OPTION_B}
|
||||||
OPTION_E=${OPTION_NOT_DEFINED}
|
OPTION_E=${OPTION_NOT_DEFINED}
|
||||||
|
OPTION_F=${GLOBAL_OPTION}
|
||||||
|
@ -207,15 +207,21 @@ func TestLoadQuotedEnv(t *testing.T) {
|
|||||||
|
|
||||||
func TestSubstitutions(t *testing.T) {
|
func TestSubstitutions(t *testing.T) {
|
||||||
envFileName := "fixtures/substitutions.env"
|
envFileName := "fixtures/substitutions.env"
|
||||||
|
|
||||||
|
presets := map[string]string{
|
||||||
|
"GLOBAL_OPTION": "global",
|
||||||
|
}
|
||||||
|
|
||||||
expectedValues := map[string]string{
|
expectedValues := map[string]string{
|
||||||
"OPTION_A": "1",
|
"OPTION_A": "1",
|
||||||
"OPTION_B": "1",
|
"OPTION_B": "1",
|
||||||
"OPTION_C": "1",
|
"OPTION_C": "1",
|
||||||
"OPTION_D": "11",
|
"OPTION_D": "11",
|
||||||
"OPTION_E": "",
|
"OPTION_E": "",
|
||||||
|
"OPTION_F": "global",
|
||||||
}
|
}
|
||||||
|
|
||||||
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
|
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpanding(t *testing.T) {
|
func TestExpanding(t *testing.T) {
|
||||||
@ -573,3 +579,64 @@ func TestTrailingNewlines(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWhitespace(t *testing.T) {
|
||||||
|
cases := map[string]struct {
|
||||||
|
input string
|
||||||
|
key string
|
||||||
|
value string
|
||||||
|
}{
|
||||||
|
"Leading whitespace": {
|
||||||
|
input: " A=a\n",
|
||||||
|
key: "A",
|
||||||
|
value: "a",
|
||||||
|
},
|
||||||
|
"Leading tab": {
|
||||||
|
input: "\tA=a\n",
|
||||||
|
key: "A",
|
||||||
|
value: "a",
|
||||||
|
},
|
||||||
|
"Leading mixed whitespace": {
|
||||||
|
input: " \t \t\n\t \t A=a\n",
|
||||||
|
key: "A",
|
||||||
|
value: "a",
|
||||||
|
},
|
||||||
|
"Leading whitespace before export": {
|
||||||
|
input: " \t\t export A=a\n",
|
||||||
|
key: "A",
|
||||||
|
value: "a",
|
||||||
|
},
|
||||||
|
"Trailing whitespace": {
|
||||||
|
input: "A=a \t \t\n",
|
||||||
|
key: "A",
|
||||||
|
value: "a",
|
||||||
|
},
|
||||||
|
"Trailing whitespace with export": {
|
||||||
|
input: "export A=a\t \t \n",
|
||||||
|
key: "A",
|
||||||
|
value: "a",
|
||||||
|
},
|
||||||
|
"No EOL": {
|
||||||
|
input: "A=a",
|
||||||
|
key: "A",
|
||||||
|
value: "a",
|
||||||
|
},
|
||||||
|
"Trailing whitespace with no EOL": {
|
||||||
|
input: "A=a ",
|
||||||
|
key: "A",
|
||||||
|
value: "a",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for n, c := range cases {
|
||||||
|
t.Run(n, func(t *testing.T) {
|
||||||
|
result, err := Unmarshal(c.input)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Input: %q Unexpected error:\t%q", c.input, err)
|
||||||
|
}
|
||||||
|
if result[c.key] != c.value {
|
||||||
|
t.Errorf("Input %q Expected:\t %q/%q\nGot:\t %q", c.input, c.key, c.value, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
@ -264,6 +265,12 @@ func expandVariables(v string, m map[string]string) string {
|
|||||||
if submatch[1] == "\\" || submatch[2] == "(" {
|
if submatch[1] == "\\" || submatch[2] == "(" {
|
||||||
return submatch[0][1:]
|
return submatch[0][1:]
|
||||||
} else if submatch[4] != "" {
|
} else if submatch[4] != "" {
|
||||||
|
if val, ok := m[submatch[4]]; ok {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
if val, ok := os.LookupEnv(submatch[4]); ok {
|
||||||
|
return val
|
||||||
|
}
|
||||||
return m[submatch[4]]
|
return m[submatch[4]]
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
|
Reference in New Issue
Block a user