add basic implementation of modifiers

This commit is contained in:
2023-11-30 10:58:50 +01:00
parent e3b6eee84d
commit e396573785
3 changed files with 161 additions and 60 deletions

View File

@ -12,8 +12,7 @@ import (
var noopPresets = make(map[string]string)
func parseAndCompare(t *testing.T, rawEnvLine string, expectedKey string, expectedValue string) {
result, err := Unmarshal(rawEnvLine)
result, _, err := Unmarshal(rawEnvLine)
if err != nil {
t.Errorf("Expected %q to parse as %q: %q, errored %q", rawEnvLine, expectedKey, expectedValue, err)
return
@ -88,7 +87,7 @@ func TestReadPlainEnv(t *testing.T) {
"OPTION_H": "1 2",
}
envMap, err := Read(envFileName)
envMap, _, err := Read(envFileName)
if err != nil {
t.Error("Error reading file")
}
@ -105,7 +104,7 @@ func TestReadPlainEnv(t *testing.T) {
}
func TestParse(t *testing.T) {
envMap, err := Parse(bytes.NewReader([]byte("ONE=1\nTWO='2'\nTHREE = \"3\"")))
envMap, _, err := Parse(bytes.NewReader([]byte("ONE=1\nTWO='2'\nTHREE = \"3\"")))
expectedValues := map[string]string{
"ONE": "1",
"TWO": "2",
@ -268,7 +267,7 @@ func TestExpanding(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
env, err := Parse(strings.NewReader(tt.input))
env, _, err := Parse(strings.NewReader(tt.input))
if err != nil {
t.Errorf("Error: %s", err.Error())
}
@ -286,7 +285,7 @@ func TestVariableStringValueSeparator(t *testing.T) {
want := map[string]string{
"TEST_URLS": "stratum+tcp://stratum.antpool.com:3333\nstratum+tcp://stratum.antpool.com:443",
}
got, err := Parse(strings.NewReader(input))
got, _, err := Parse(strings.NewReader(input))
if err != nil {
t.Error(err)
}
@ -342,7 +341,7 @@ func TestParsing(t *testing.T) {
// parses yaml style options
parseAndCompare(t, "OPTION_A: 1", "OPTION_A", "1")
//parses yaml values with equal signs
// parses yaml values with equal signs
parseAndCompare(t, "OPTION_A: Foo=bar", "OPTION_A", "Foo=bar")
// parses non-yaml options with colons
@ -394,7 +393,7 @@ func TestParsing(t *testing.T) {
parseAndCompare(t, `FOO="ba#r"`, "FOO", "ba#r")
parseAndCompare(t, "FOO='ba#r'", "FOO", "ba#r")
//newlines and backslashes should be escaped
// newlines and backslashes should be escaped
parseAndCompare(t, `FOO="bar\n\ b\az"`, "FOO", "bar\n baz")
parseAndCompare(t, `FOO="bar\\\n\ b\az"`, "FOO", "bar\\\n baz")
parseAndCompare(t, `FOO="bar\\r\ b\az"`, "FOO", "bar\\r baz")
@ -409,7 +408,7 @@ func TestParsing(t *testing.T) {
// it 'throws an error if line format is incorrect' do
// expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
badlyFormattedLine := "lol$wut"
_, err := Unmarshal(badlyFormattedLine)
_, _, err := Unmarshal(badlyFormattedLine)
if err == nil {
t.Errorf("Expected \"%v\" to return error, but it didn't", badlyFormattedLine)
}
@ -453,7 +452,7 @@ func TestLinesToIgnore(t *testing.T) {
func TestErrorReadDirectory(t *testing.T) {
envFileName := "fixtures/"
envMap, err := Read(envFileName)
envMap, _, err := Read(envFileName)
if err == nil {
t.Errorf("Expected error, got %v", envMap)
@ -462,7 +461,7 @@ func TestErrorReadDirectory(t *testing.T) {
func TestErrorParsing(t *testing.T) {
envFileName := "fixtures/invalid1.env"
envMap, err := Read(envFileName)
envMap, _, err := Read(envFileName)
if err == nil {
t.Errorf("Expected error, got %v", envMap)
}
@ -481,20 +480,20 @@ func TestComments(t *testing.T) {
func TestWrite(t *testing.T) {
writeAndCompare := func(env string, expected string) {
envMap, _ := Unmarshal(env)
envMap, _, _ := Unmarshal(env)
actual, _ := Marshal(envMap)
if expected != actual {
t.Errorf("Expected '%v' (%v) to write as '%v', got '%v' instead.", env, envMap, expected, actual)
}
}
//just test some single lines to show the general idea
//TestRoundtrip makes most of the good assertions
// just test some single lines to show the general idea
// TestRoundtrip makes most of the good assertions
//values are always double-quoted
// values are always double-quoted
writeAndCompare(`key=value`, `key="value"`)
//double-quotes are escaped
// double-quotes are escaped
writeAndCompare(`key=va"lu"e`, `key="va\"lu\"e"`)
//but single quotes are left alone
// but single quotes are left alone
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
// newlines, backslashes, and some other special chars are escaped
writeAndCompare(`foo="\n\r\\r!"`, `foo="\n\r\\r\!"`)
@ -502,14 +501,13 @@ func TestWrite(t *testing.T) {
writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"")
// integers should not be quoted
writeAndCompare(`key="10"`, `key=10`)
}
func TestRoundtrip(t *testing.T) {
fixtures := []string{"equals.env", "exported.env", "plain.env", "quoted.env"}
for _, fixture := range fixtures {
fixtureFilename := fmt.Sprintf("fixtures/%s", fixture)
env, err := readFile(fixtureFilename)
env, _, err := readFile(fixtureFilename)
if err != nil {
t.Errorf("Expected '%s' to read without error (%v)", fixtureFilename, err)
}
@ -517,7 +515,7 @@ func TestRoundtrip(t *testing.T) {
if err != nil {
t.Errorf("Expected '%s' to Marshal (%v)", fixtureFilename, err)
}
roundtripped, err := Unmarshal(rep)
roundtripped, _, err := Unmarshal(rep)
if err != nil {
t.Errorf("Expected '%s' to Mashal and Unmarshal (%v)", fixtureFilename, err)
}
@ -563,7 +561,7 @@ func TestTrailingNewlines(t *testing.T) {
for n, c := range cases {
t.Run(n, func(t *testing.T) {
result, err := Unmarshal(c.input)
result, _, err := Unmarshal(c.input)
if err != nil {
t.Errorf("Input: %q Unexpected error:\t%q", c.input, err)
}
@ -582,49 +580,49 @@ func TestWhitespace(t *testing.T) {
}{
"Leading whitespace": {
input: " A=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading tab": {
input: "\tA=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading mixed whitespace": {
input: " \t \t\n\t \t A=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading whitespace before export": {
input: " \t\t export A=a\n",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace": {
input: "A=a \t \t\n",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace with export": {
input: "export A=a\t \t \n",
key: "A",
key: "A",
value: "a",
},
"No EOL": {
input: "A=a",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace with no EOL": {
input: "A=a ",
key: "A",
key: "A",
value: "a",
},
}
for n, c := range cases {
t.Run(n, func(t *testing.T) {
result, err := Unmarshal(c.input)
result, _, err := Unmarshal(c.input)
if err != nil {
t.Errorf("Input: %q Unexpected error:\t%q", c.input, err)
}
@ -634,3 +632,78 @@ func TestWhitespace(t *testing.T) {
})
}
}
func TestModfiers(t *testing.T) {
cases := map[string]struct {
input string
key string
value string
modifiers map[string]string
}{
"No Modifier": {
input: "A=a",
key: "A",
value: "a",
},
"With comment": {
input: "A=a # my comment",
key: "A",
value: "a",
},
"With single modifier": {
input: "A=a # foo=bar",
key: "A",
value: "a",
modifiers: map[string]string{
"foo": "bar",
},
},
"With multiple modifiers": {
input: "A=a # foo=bar length=10",
key: "A",
value: "a",
modifiers: map[string]string{
"foo": "bar",
"length": "10",
},
},
"With quoted var": {
input: "A='a' # foo=bar",
key: "A",
value: "a",
modifiers: map[string]string{
"foo": "bar",
},
},
"With quoted var 2": {
input: "A='a' # foo=bar\nB=b",
key: "A",
value: "a",
modifiers: map[string]string{
"foo": "bar",
},
},
}
for n, c := range cases {
t.Run(n, func(t *testing.T) {
values, modifiers, err := Unmarshal(c.input)
if err != nil {
t.Errorf("Input: %q Unexpected error:\t%q", c.input, err)
}
if values[c.key] != c.value {
t.Errorf("Input %q Expected:\t %q/%q\nGot:\t %q", c.input, c.key, c.value, values)
}
if modifiers[c.key] == nil && c.modifiers != nil {
t.Errorf("Input %q Expected modifiers\n Got: none", c.input)
} else {
for k, v := range c.modifiers {
if modifiers[c.key][k] != v {
t.Errorf("Input %q Expected modifier %s=%s\n Got: %s=%s", c.input, k, v, k, modifiers[c.key][k])
}
}
}
})
}
}