-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: branch '3613-dotenv' into 'main'
Support .env [#3613] Closes #3613 See merge request accumulatenetwork/accumulate!1081
- Loading branch information
Showing
8 changed files
with
376 additions
and
4 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
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,101 @@ | ||
// Copyright 2024 The Accumulate Authors | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
package run | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
testutil "gitlab.com/accumulatenetwork/accumulate/test/util" | ||
) | ||
|
||
func TestDotenv(t *testing.T) { | ||
// When dot-env is set, ${FOO} is resolved | ||
t.Run("Set", func(t *testing.T) { | ||
fs := mkfs(map[string]string{ | ||
".env": ` | ||
FOO=bar`, | ||
"accumulate.toml": ` | ||
dot-env = true | ||
network = "${FOO}"`, | ||
}) | ||
|
||
cfg := new(Config) | ||
require.NoError(t, cfg.LoadFromFS(fs, "accumulate.toml")) | ||
require.Equal(t, "bar", cfg.Network) | ||
}) | ||
|
||
// When dot-env is unset, ${FOO} is left as is | ||
t.Run("Unset", func(t *testing.T) { | ||
fs := mkfs(map[string]string{ | ||
".env": ` | ||
FOO=bar`, | ||
"accumulate.toml": ` | ||
network = "${FOO}"`, | ||
}) | ||
|
||
cfg := new(Config) | ||
require.NoError(t, cfg.LoadFromFS(fs, "accumulate.toml")) | ||
require.Equal(t, "${FOO}", cfg.Network) | ||
}) | ||
|
||
// When dot-env is set, referencing an unset variable ${BAR} is an error | ||
t.Run("Wrong var", func(t *testing.T) { | ||
fs := mkfs(map[string]string{ | ||
".env": ` | ||
FOO=bar`, | ||
"accumulate.toml": ` | ||
dot-env = true | ||
network = "${BAR}"`, | ||
}) | ||
|
||
cfg := new(Config) | ||
err := cfg.LoadFromFS(fs, "accumulate.toml") | ||
require.EqualError(t, err, `"BAR" is not defined`) | ||
}) | ||
|
||
// Variable are resolved exclusively from .env, not from actual environment | ||
// variables | ||
t.Run("Ignore env", func(t *testing.T) { | ||
fs := mkfs(map[string]string{ | ||
"accumulate.toml": ` | ||
dot-env = true | ||
network = "${FOO}"`, | ||
}) | ||
require.NoError(t, os.Setenv("FOO", "bar")) | ||
|
||
cfg := new(Config) | ||
err := cfg.LoadFromFS(fs, "accumulate.toml") | ||
require.EqualError(t, err, `open .env: file does not exist`) | ||
}) | ||
} | ||
|
||
func TestSaveToFS(t *testing.T) { | ||
fs := mkfs(map[string]string{ | ||
"accumulate.toml": `network = "foo"`, | ||
}) | ||
|
||
cfg := new(Config) | ||
require.NoError(t, cfg.LoadFromFS(fs, "accumulate.toml")) | ||
|
||
// Ensure Save doesn't panic when loaded from [fs.FS] | ||
require.Error(t, cfg.Save()) | ||
} | ||
|
||
func mkfs(files map[string]string) fs.FS { | ||
root := new(testutil.Dir) | ||
for name, data := range files { | ||
root.Files = append(root.Files, &testutil.File{ | ||
Name: name, | ||
Data: strings.NewReader(data), | ||
}) | ||
} | ||
return root | ||
} |
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
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
Oops, something went wrong.