gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/config/source/file/file_test.go (about)

     1  package file
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestFile(t *testing.T) {
    12  	data := []byte(`{"foo": "bar"}`)
    13  	path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
    14  	fh, err := os.Create(path)
    15  	if err != nil {
    16  		t.Error(err)
    17  	}
    18  	defer func() {
    19  		fh.Close()
    20  		os.Remove(path)
    21  	}()
    22  
    23  	_, err = fh.Write(data)
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  
    28  	f := NewSource(WithPath(path))
    29  	c, err := f.Read()
    30  	if err != nil {
    31  		t.Error(err)
    32  	}
    33  	t.Logf("%+v", c)
    34  	if string(c.Data) != string(data) {
    35  		t.Error("data from file does not match")
    36  	}
    37  }