forked from toolshed/abra
proper env modifiers support
This implements proper modifier support in the env file using this new fork of the godotenv library. The modifier implementation is quite basic for but can be improved later if needed. See this commit for the actual implementation. Because we are now using proper modifer parsing, it does not affect the parsing of value, so this is possible again: ``` MY_VAR="#foo" ``` Closes coop-cloud/organising#535
This commit is contained in:
@ -13,15 +13,21 @@ import (
|
||||
"coopcloud.tech/abra/pkg/recipe"
|
||||
)
|
||||
|
||||
var TestFolder = os.ExpandEnv("$PWD/../../tests/resources/test_folder")
|
||||
var ValidAbraConf = os.ExpandEnv("$PWD/../../tests/resources/valid_abra_config")
|
||||
var (
|
||||
TestFolder = os.ExpandEnv("$PWD/../../tests/resources/test_folder")
|
||||
ValidAbraConf = os.ExpandEnv("$PWD/../../tests/resources/valid_abra_config")
|
||||
)
|
||||
|
||||
// make sure these are in alphabetical order
|
||||
var TFolders = []string{"folder1", "folder2"}
|
||||
var TFiles = []string{"bar.env", "foo.env"}
|
||||
var (
|
||||
TFolders = []string{"folder1", "folder2"}
|
||||
TFiles = []string{"bar.env", "foo.env"}
|
||||
)
|
||||
|
||||
var AppName = "ecloud"
|
||||
var ServerName = "evil.corp"
|
||||
var (
|
||||
AppName = "ecloud"
|
||||
ServerName = "evil.corp"
|
||||
)
|
||||
|
||||
var ExpectedAppEnv = config.AppEnv{
|
||||
"DOMAIN": "ecloud.evil.corp",
|
||||
@ -71,7 +77,7 @@ func TestGetAllFilesInDirectory(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadEnv(t *testing.T) {
|
||||
env, err := config.ReadEnv(ExpectedAppFile.Path, config.ReadEnvOptions{})
|
||||
env, err := config.ReadEnv(ExpectedAppFile.Path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -149,7 +155,7 @@ func TestCheckEnv(t *testing.T) {
|
||||
}
|
||||
|
||||
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
|
||||
envSample, err := config.ReadEnv(envSamplePath, config.ReadEnvOptions{})
|
||||
envSample, err := config.ReadEnv(envSamplePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -183,7 +189,7 @@ func TestCheckEnvError(t *testing.T) {
|
||||
}
|
||||
|
||||
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
|
||||
envSample, err := config.ReadEnv(envSamplePath, config.ReadEnvOptions{})
|
||||
envSample, err := config.ReadEnv(envSamplePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -211,19 +217,7 @@ func TestCheckEnvError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainsEnvVarModifier(t *testing.T) {
|
||||
if ok := config.ContainsEnvVarModifier("FOO=bar # bing"); ok {
|
||||
t.Fatal("FOO contains no env var modifier")
|
||||
}
|
||||
|
||||
if ok := config.ContainsEnvVarModifier("FOO=bar # length=3"); !ok {
|
||||
t.Fatal("FOO contains an env var modifier (length)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvVarCommentsRemoved(t *testing.T) {
|
||||
t.Skip("https://git.coopcloud.tech/coop-cloud/organising/issues/535")
|
||||
|
||||
offline := true
|
||||
r, err := recipe.Get("abra-test-recipe", offline)
|
||||
if err != nil {
|
||||
@ -231,7 +225,7 @@ func TestEnvVarCommentsRemoved(t *testing.T) {
|
||||
}
|
||||
|
||||
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
|
||||
envSample, err := config.ReadEnv(envSamplePath, config.ReadEnvOptions{})
|
||||
envSample, err := config.ReadEnv(envSamplePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -263,12 +257,19 @@ func TestEnvVarModifiersIncluded(t *testing.T) {
|
||||
}
|
||||
|
||||
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
|
||||
envSample, err := config.ReadEnv(envSamplePath, config.ReadEnvOptions{IncludeModifiers: true})
|
||||
envSample, modifiers, err := config.ReadEnvWithModifiers(envSamplePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !strings.Contains(envSample["SECRET_TEST_PASS_TWO_VERSION"], "length") {
|
||||
t.Fatal("comment from env var SECRET_TEST_PASS_TWO_VERSION should not be removed")
|
||||
if !strings.Contains(envSample["SECRET_TEST_PASS_TWO_VERSION"], "v1") {
|
||||
t.Errorf("value should be 'v1', got: '%s'", envSample["SECRET_TEST_PASS_TWO_VERSION"])
|
||||
}
|
||||
if modifiers == nil || modifiers["SECRET_TEST_PASS_TWO_VERSION"] == nil {
|
||||
t.Errorf("no modifiers included")
|
||||
} else {
|
||||
if modifiers["SECRET_TEST_PASS_TWO_VERSION"]["length"] != "10" {
|
||||
t.Errorf("length modifier should be '10', got: '%s'", modifiers["SECRET_TEST_PASS_TWO_VERSION"]["length"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user