2017-06-05 18:23:21 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2024-10-03 08:08:22 -04:00
|
|
|
"path/filepath"
|
2017-06-05 18:23:21 -04:00
|
|
|
"testing"
|
2024-10-03 08:08:22 -04:00
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-06-05 18:23:21 -04:00
|
|
|
)
|
|
|
|
|
2022-02-25 09:35:13 -05:00
|
|
|
func tmpFileWithContent(t *testing.T, content string) string {
|
|
|
|
t.Helper()
|
2024-10-03 08:08:22 -04:00
|
|
|
fileName := filepath.Join(t.TempDir(), "envfile")
|
|
|
|
err := os.WriteFile(fileName, []byte(content), 0o644)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return fileName
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test ParseEnvFile for a non existent file
|
|
|
|
func TestParseEnvFileNonExistentFile(t *testing.T) {
|
2024-10-03 08:08:22 -04:00
|
|
|
_, err := ParseEnvFile("no_such_file")
|
|
|
|
assert.Check(t, is.ErrorType(err, os.IsNotExist))
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|
|
|
|
|
2018-07-02 01:33:44 -04:00
|
|
|
// ParseEnvFile with environment variable import definitions
|
|
|
|
func TestParseEnvVariableDefinitionsFile(t *testing.T) {
|
|
|
|
content := `# comment=
|
|
|
|
UNDEFINED_VAR
|
2024-10-03 08:08:22 -04:00
|
|
|
DEFINED_VAR
|
2018-07-02 01:33:44 -04:00
|
|
|
`
|
2022-02-25 09:35:13 -05:00
|
|
|
tmpFile := tmpFileWithContent(t, content)
|
2018-07-02 01:33:44 -04:00
|
|
|
|
2024-10-03 08:08:22 -04:00
|
|
|
t.Setenv("DEFINED_VAR", "defined-value")
|
2018-07-02 01:33:44 -04:00
|
|
|
variables, err := ParseEnvFile(tmpFile)
|
2024-10-03 08:08:22 -04:00
|
|
|
assert.NilError(t, err)
|
2018-07-02 01:33:44 -04:00
|
|
|
|
2024-10-03 08:08:22 -04:00
|
|
|
expectedLines := []string{"DEFINED_VAR=defined-value"}
|
|
|
|
assert.Check(t, is.DeepEqual(variables, expectedLines))
|
2018-07-02 01:33:44 -04:00
|
|
|
}
|