No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers_test.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package caddyhugo
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "sync"
  8. "testing"
  9. "git.stephensearles.com/stephen/acedoc"
  10. )
  11. type World struct {
  12. CH *CaddyHugo
  13. BlogFolder string
  14. }
  15. func (w *World) Clean() {
  16. if w.BlogFolder != "" {
  17. os.RemoveAll(w.BlogFolder)
  18. }
  19. }
  20. func NewWorld(t *testing.T) *World {
  21. dir, err := ioutil.TempDir("", "caddy-hugo2-test-")
  22. if err != nil {
  23. t.Fatalf("error initializing test environment: %v", err)
  24. }
  25. w := &World{BlogFolder: dir}
  26. cmd := exec.Command("hugo", "new", "site", dir)
  27. cmd.Dir = dir
  28. out, err := cmd.CombinedOutput()
  29. if err != nil {
  30. t.Fatalf("error initializing test site: %v\n\n%v", err, string(out))
  31. }
  32. w.CH = &CaddyHugo{}
  33. w.CH.Setup(dir)
  34. return w
  35. }
  36. type WebsocketTester struct {
  37. receivedPointer int
  38. received [][]byte
  39. wroteMessages []Message
  40. wroteDeltas []acedoc.Delta
  41. mtx sync.Mutex
  42. }
  43. // ReadJSON reads the next pending message from the "client" into v
  44. func (ws *WebsocketTester) ReadJSON(v interface{}) error {
  45. ws.mtx.Lock()
  46. defer ws.mtx.Unlock()
  47. if len(ws.received) <= ws.receivedPointer {
  48. return nil
  49. }
  50. err := json.Unmarshal(ws.received[ws.receivedPointer], v)
  51. ws.receivedPointer++
  52. return err
  53. }
  54. // WriteJSON "sends" a message, v, to the "client"
  55. func (ws *WebsocketTester) WriteJSON(v interface{}) error {
  56. ws.mtx.Lock()
  57. defer ws.mtx.Unlock()
  58. m, ok := v.(Message)
  59. if !ok {
  60. panic("wrong type written to WebsocketTester")
  61. }
  62. ws.wroteMessages = append(ws.wroteMessages, m)
  63. ws.wroteDeltas = append(ws.wroteDeltas, m.Deltas...)
  64. return nil
  65. }
  66. // ReceiveJSON queues a message to be sent to the client
  67. func (ws *WebsocketTester) ReceiveJSON(v interface{}) error {
  68. ws.mtx.Lock()
  69. defer ws.mtx.Unlock()
  70. out, err := json.Marshal(v)
  71. if err != nil {
  72. return err
  73. }
  74. ws.received = append(ws.received, out)
  75. return nil
  76. }