collection_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package themes
  3. import (
  4. "archive/zip"
  5. "bytes"
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "testing"
  14. "time"
  15. "github.com/google/go-cmp/cmp"
  16. )
  17. var _ = fmt.Print
  18. func TestThemeCollections(t *testing.T) {
  19. for fname, expected := range map[string]string{
  20. "moose": "Moose",
  21. "mooseCat": "Moose Cat",
  22. "a_bC": "A B C",
  23. } {
  24. actual := ThemeNameFromFileName(fname)
  25. if diff := cmp.Diff(expected, actual); diff != "" {
  26. t.Fatalf("Unexpected theme name for %s:\n%s", fname, diff)
  27. }
  28. }
  29. tdir := t.TempDir()
  30. pt := func(expected ThemeMetadata, lines ...string) {
  31. if err := os.WriteFile(filepath.Join(tdir, "temp.conf"), []byte(strings.Join(lines, "\n")), 0o600); err != nil {
  32. t.Fatal(err)
  33. }
  34. actual, _, err := ParseThemeMetadata(filepath.Join(tdir, "temp.conf"))
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if diff := cmp.Diff(&expected, actual); diff != "" {
  39. t.Fatalf("Failed to parse:\n%s\n\n%s", strings.Join(lines, "\n"), diff)
  40. }
  41. }
  42. pt(ThemeMetadata{Name: "XYZ", Blurb: "a b", Author: "A", Is_dark: true, Num_settings: 2},
  43. "# some crap", " ", "## ", "## author: A", "## name: XYZ", "## blurb: a", "## b", "", "color red", "background black", "include inc.conf")
  44. if err := os.WriteFile(filepath.Join(tdir, "inc.conf"), []byte("background white"), 0o600); err != nil {
  45. t.Fatal(err)
  46. }
  47. pt(ThemeMetadata{Name: "XYZ", Blurb: "a b", Author: "A", Num_settings: 2},
  48. "# some crap", " ", "## ", "## author: A", "## name: XYZ", "## blurb: a", "## b", "", "color red", "background black", "include inc.conf")
  49. buf := bytes.Buffer{}
  50. zw := zip.NewWriter(&buf)
  51. fw, _ := zw.Create("x/themes.json")
  52. if _, err := fw.Write([]byte(`[
  53. {
  54. "author": "X Y",
  55. "blurb": "A dark color scheme for the kitty terminal.",
  56. "file": "themes/Alabaster_Dark.conf",
  57. "is_dark": true,
  58. "license": "MIT",
  59. "name": "Alabaster Dark",
  60. "num_settings": 30,
  61. "upstream": "https://xxx.com"
  62. },
  63. {
  64. "name": "Empty", "file": "empty.conf"
  65. }
  66. ]`)); err != nil {
  67. t.Fatal(err)
  68. }
  69. fw, _ = zw.Create("x/empty.conf")
  70. if _, err := fw.Write([]byte("empty")); err != nil {
  71. t.Fatal(err)
  72. }
  73. fw, _ = zw.Create("x/themes/Alabaster_Dark.conf")
  74. if _, err := fw.Write([]byte("alabaster")); err != nil {
  75. t.Fatal(err)
  76. }
  77. zw.Close()
  78. received_etag := ""
  79. request_count := 0
  80. send_count := 0
  81. check_etag := true
  82. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  83. request_count++
  84. received_etag = r.Header.Get("If-None-Match")
  85. if check_etag && received_etag == `"xxx"` {
  86. w.WriteHeader(http.StatusNotModified)
  87. return
  88. }
  89. send_count++
  90. w.Header().Add("ETag", `"xxx"`)
  91. w.Write(buf.Bytes())
  92. }))
  93. defer ts.Close()
  94. if _, err := fetch_cached("test", ts.URL, tdir, 0); err != nil {
  95. t.Fatal(err)
  96. }
  97. r, err := zip.OpenReader(filepath.Join(tdir, "test.zip"))
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. var jm JSONMetadata
  102. err = json.Unmarshal([]byte(r.Comment), &jm)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if jm.Etag != `"xxx"` {
  107. t.Fatalf("Unexpected ETag: %#v", jm.Etag)
  108. }
  109. _, err = fetch_cached("test", ts.URL, tdir, time.Hour)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. if request_count != 1 {
  114. t.Fatalf("Cached zip file was not used: %d", request_count)
  115. }
  116. _, err = fetch_cached("test", ts.URL, tdir, 0)
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. if request_count != 2 {
  121. t.Fatalf("Cached zip file was incorrectly used: %d", request_count)
  122. }
  123. if received_etag != `"xxx"` {
  124. t.Fatalf("Got invalid ETag: %#v", received_etag)
  125. }
  126. if send_count != 1 {
  127. t.Fatalf("Cached zip file was incorrectly re-downloaded: %d", send_count)
  128. }
  129. check_etag = false
  130. _, err = fetch_cached("test", ts.URL, tdir, 0)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if send_count != 2 {
  135. t.Fatalf("Cached zip file was incorrectly not re-downloaded. %d", send_count)
  136. }
  137. coll := Themes{name_map: map[string]*Theme{}}
  138. closer, err := coll.add_from_zip_file(filepath.Join(tdir, "test.zip"))
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. defer closer.Close()
  143. if code, err := coll.ThemeByName("Empty").load_code(); code != "empty" {
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. t.Fatal("failed to load code for empty theme")
  148. }
  149. if code, err := coll.ThemeByName("Alabaster Dark").load_code(); code != "alabaster" {
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. t.Fatal("failed to load code for alabaster theme")
  154. }
  155. }