-
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.
- Loading branch information
1 parent
1dca716
commit 8346987
Showing
5 changed files
with
274 additions
and
19 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,83 @@ | ||
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package testutil | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Dir struct { | ||
Name string | ||
Files []DirEntry | ||
} | ||
|
||
type DirEntry interface { | ||
fs.File | ||
stat() fsFileInfo | ||
} | ||
|
||
func (d *Dir) Read(b []byte) (int, error) { return 0, io.EOF } | ||
func (d *Dir) Close() error { return nil } | ||
func (d *Dir) Stat() (fs.FileInfo, error) { return d.stat(), nil } | ||
|
||
func (d *Dir) stat() fsFileInfo { | ||
return fsFileInfo{d.Name, 0, true} | ||
} | ||
|
||
func (d *Dir) Open(name string) (fs.File, error) { | ||
if !fs.ValidPath(name) { | ||
return nil, &fs.PathError{ | ||
Op: "open", | ||
Path: name, | ||
Err: fs.ErrInvalid, | ||
} | ||
} | ||
|
||
s := strings.SplitN(name, "/", 2) | ||
var f DirEntry | ||
f, ok := d.open(s[0]) | ||
if !ok { | ||
return nil, &fs.PathError{ | ||
Op: "open", | ||
Path: name, | ||
Err: fs.ErrNotExist, | ||
} | ||
} | ||
|
||
if len(s) == 1 { | ||
return f, nil | ||
} | ||
|
||
e, ok := f.(*Dir) | ||
if !ok { | ||
return nil, &fs.PathError{ | ||
Op: "open", | ||
Path: name, | ||
Err: fmt.Errorf("%w: %q is not a directory", fs.ErrInvalid, s[0]), | ||
} | ||
} | ||
|
||
return e.Open(s[1]) | ||
} | ||
|
||
func (d *Dir) open(name string) (DirEntry, bool) { | ||
for _, f := range d.Files { | ||
if f.stat().name == name { | ||
return f, true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func (d *Dir) ReadDir(n int) ([]fs.DirEntry, error) { | ||
var entries []fs.DirEntry | ||
for _, e := range d.Files { | ||
entries = append(entries, fsDirEntry(e.stat())) | ||
if n--; n == 0 { | ||
break | ||
} | ||
} | ||
return entries, nil | ||
} | ||
|
||
type File struct { | ||
Name string | ||
Data FileData | ||
} | ||
|
||
type FileData interface { | ||
io.Reader | ||
Len() int | ||
} | ||
|
||
func (f *File) Read(b []byte) (int, error) { return f.Data.Read(b) } | ||
func (f *File) Close() error { return nil } | ||
func (f *File) Stat() (fs.FileInfo, error) { return f.stat(), nil } | ||
|
||
func (f *File) stat() fsFileInfo { | ||
return fsFileInfo{f.Name, int64(f.Data.Len()), false} | ||
} | ||
|
||
type fsDirEntry fsFileInfo | ||
|
||
func (e fsDirEntry) Name() string { return e.name } | ||
func (e fsDirEntry) IsDir() bool { return e.isDir } | ||
func (e fsDirEntry) Type() fs.FileMode { return fsFileInfo(e).Mode() & fs.ModeType } | ||
func (e fsDirEntry) Info() (fs.FileInfo, error) { return fsFileInfo(e), nil } | ||
|
||
type fsFileInfo struct { | ||
name string | ||
size int64 | ||
isDir bool | ||
} | ||
|
||
func (f fsFileInfo) Name() string { return f.name } | ||
func (f fsFileInfo) Size() int64 { return f.size } | ||
func (f fsFileInfo) ModTime() time.Time { return time.Now() } | ||
func (f fsFileInfo) IsDir() bool { return f.isDir } | ||
func (f fsFileInfo) Sys() any { return nil } | ||
|
||
func (f fsFileInfo) Mode() fs.FileMode { | ||
if f.isDir { | ||
return fs.ModeDir | fs.ModePerm | ||
} | ||
return fs.ModePerm | ||
} |