2020-03-19 12:54:59 +03:00
|
|
|
package file_test
|
2019-05-31 01:11:13 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-03-19 12:54:59 +03:00
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config"
|
|
|
|
"github.com/unistack-org/micro/v3/config/source/file"
|
2019-05-31 01:11:13 +03:00
|
|
|
)
|
|
|
|
|
2020-03-19 12:54:59 +03:00
|
|
|
func TestConfig(t *testing.T) {
|
|
|
|
data := []byte(`{"foo": "bar"}`)
|
|
|
|
path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
|
|
|
|
fh, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
fh.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
}()
|
|
|
|
_, err = fh.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := config.NewConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
conf.Load(file.NewSource(file.WithPath(path)))
|
|
|
|
// simulate multiple close
|
|
|
|
go conf.Close()
|
|
|
|
go conf.Close()
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:11:13 +03:00
|
|
|
func TestFile(t *testing.T) {
|
|
|
|
data := []byte(`{"foo": "bar"}`)
|
|
|
|
path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
|
|
|
|
fh, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
fh.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err = fh.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:54:59 +03:00
|
|
|
f := file.NewSource(file.WithPath(path))
|
2019-05-31 01:11:13 +03:00
|
|
|
c, err := f.Read()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if string(c.Data) != string(data) {
|
2020-04-09 14:05:46 +03:00
|
|
|
t.Logf("%+v", c)
|
2019-05-31 01:11:13 +03:00
|
|
|
t.Error("data from file does not match")
|
|
|
|
}
|
|
|
|
}
|