mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
1063b954de
* fix tests and github action annotations Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package file_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/micro/go-micro/v2/config"
|
|
"github.com/micro/go-micro/v2/config/source/file"
|
|
)
|
|
|
|
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()
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
f := file.NewSource(file.WithPath(path))
|
|
c, err := f.Read()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if string(c.Data) != string(data) {
|
|
t.Logf("%+v", c)
|
|
t.Error("data from file does not match")
|
|
}
|
|
}
|