file_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package file_test
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "gogs.baozhida.cn/zoie/OAuth-core/config"
  9. "gogs.baozhida.cn/zoie/OAuth-core/config/source/file"
  10. )
  11. func TestConfig(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. _, err = fh.Write(data)
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. conf, err := config.NewConfig()
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. conf.Load(file.NewSource(file.WithPath(path)))
  31. // simulate multiple close
  32. go conf.Close()
  33. go conf.Close()
  34. }
  35. func TestFile(t *testing.T) {
  36. data := []byte(`{"foo": "bar"}`)
  37. path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
  38. fh, err := os.Create(path)
  39. if err != nil {
  40. t.Error(err)
  41. }
  42. defer func() {
  43. fh.Close()
  44. os.Remove(path)
  45. }()
  46. _, err = fh.Write(data)
  47. if err != nil {
  48. t.Error(err)
  49. }
  50. f := file.NewSource(file.WithPath(path))
  51. c, err := f.Read()
  52. if err != nil {
  53. t.Error(err)
  54. }
  55. if string(c.Data) != string(data) {
  56. t.Logf("%+v", c)
  57. t.Error("data from file does not match")
  58. }
  59. }