save_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package save
  2. import (
  3. "bytes"
  4. "errors"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func TestResolve(t *testing.T) {
  13. dir := t.TempDir()
  14. existing := filepath.Join(dir, "existing")
  15. if err := os.Mkdir(existing, 0o755); err != nil {
  16. t.Fatal(err)
  17. }
  18. sep := string(filepath.Separator)
  19. tests := []struct {
  20. name string
  21. arg string
  22. kepub bool
  23. wantFile string // set for file targets
  24. wantDir string // set for folder targets
  25. wantNote bool
  26. }{
  27. {name: "epub file", arg: filepath.Join(dir, "a.epub"), wantFile: filepath.Join(dir, "a.epub")},
  28. {name: "uppercase extension kept", arg: filepath.Join(dir, "a.EPUB"), wantFile: filepath.Join(dir, "a.EPUB")},
  29. {name: "extension appended", arg: filepath.Join(dir, "a"), wantFile: filepath.Join(dir, "a.epub")},
  30. {name: "new path without slash is a file", arg: filepath.Join(dir, "new"), wantFile: filepath.Join(dir, "new.epub")},
  31. {name: "trailing slash is a folder", arg: filepath.Join(dir, "new") + sep, wantDir: filepath.Join(dir, "new")},
  32. {name: "forward slash is a folder", arg: filepath.Join(dir, "new") + "/", wantDir: filepath.Join(dir, "new")},
  33. {name: "existing folder", arg: existing, wantDir: existing},
  34. {name: "kepub folder", arg: existing, kepub: true, wantDir: existing},
  35. {name: "kepub file kept", arg: filepath.Join(dir, "a.kepub.epub"), kepub: true, wantFile: filepath.Join(dir, "a.kepub.epub")},
  36. {name: "kepub renames epub", arg: filepath.Join(dir, "a.epub"), kepub: true, wantFile: filepath.Join(dir, "a.kepub.epub"), wantNote: true},
  37. {name: "kepub extension appended", arg: filepath.Join(dir, "a"), kepub: true, wantFile: filepath.Join(dir, "a.kepub.epub")},
  38. }
  39. for _, tt := range tests {
  40. t.Run(tt.name, func(t *testing.T) {
  41. got, err := Resolve(tt.arg, tt.kepub)
  42. if err != nil {
  43. t.Fatalf("Resolve(%q) error: %v", tt.arg, err)
  44. }
  45. if tt.wantFile != "" {
  46. if !got.Known() || got.Path("ignored") != tt.wantFile {
  47. t.Errorf("Resolve(%q) = %+v, want file %q", tt.arg, got, tt.wantFile)
  48. }
  49. } else if got.Known() || got.dir != tt.wantDir {
  50. t.Errorf("Resolve(%q) = %+v, want folder %q", tt.arg, got, tt.wantDir)
  51. }
  52. if (got.Note != "") != tt.wantNote {
  53. t.Errorf("Resolve(%q) note = %q, want note: %v", tt.arg, got.Note, tt.wantNote)
  54. }
  55. })
  56. }
  57. }
  58. func TestResolveFolderNaming(t *testing.T) {
  59. dir := t.TempDir()
  60. for _, kepub := range []bool{false, true} {
  61. got, err := Resolve(dir, kepub)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. want := filepath.Join(dir, "hello-world.epub")
  66. if kepub {
  67. want = filepath.Join(dir, "hello-world.kepub.epub")
  68. }
  69. if p := got.Path("Hello, World!"); p != want {
  70. t.Errorf("kepub=%v: Path = %q, want %q", kepub, p, want)
  71. }
  72. }
  73. }
  74. func TestResolveKEPUBNameWithoutFlag(t *testing.T) {
  75. _, err := Resolve(filepath.Join(t.TempDir(), "a.kepub.epub"), false)
  76. if !errors.Is(err, ErrKEPUBName) {
  77. t.Fatalf("error = %v, want ErrKEPUBName", err)
  78. }
  79. }
  80. func TestResolveHome(t *testing.T) {
  81. home := t.TempDir()
  82. t.Setenv("HOME", home)
  83. t.Setenv("USERPROFILE", home) // os.UserHomeDir on Windows
  84. got, err := Resolve("~/Books/a.epub", false)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if want := filepath.Join(home, "Books", "a.epub"); got.Path("") != want {
  89. t.Errorf("Path = %q, want %q", got.Path(""), want)
  90. }
  91. got, err = Resolve("~", false)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if got.Known() || got.dir != home {
  96. t.Errorf("Resolve(~) = %+v, want folder %q", got, home)
  97. }
  98. }
  99. func TestSlug(t *testing.T) {
  100. tests := []struct{ in, want string }{
  101. {"Hello, World!", "hello-world"},
  102. {" Leading and trailing ", "leading-and-trailing"},
  103. {"Café crème brûlée", "cafe-creme-brulee"},
  104. {"Go 1.24: What's New?", "go-1-24-what-s-new"},
  105. {"../../etc/passwd", "etc-passwd"},
  106. {`a\b/c:d*e?f"g<h>i|j`, "a-b-c-d-e-f-g-h-i-j"},
  107. {"日本語のタイトル", "article"},
  108. {"", "article"},
  109. {"---", "article"},
  110. {strings.Repeat("ab ", 50), strings.TrimRight(strings.Repeat("ab-", 27)[:80], "-")},
  111. }
  112. for _, tt := range tests {
  113. if got := Slug(tt.in); got != tt.want {
  114. t.Errorf("Slug(%q) = %q, want %q", tt.in, got, tt.want)
  115. }
  116. if got := Slug(tt.in); len(got) > maxSlugLen || strings.ContainsAny(got, `/\`) {
  117. t.Errorf("Slug(%q) = %q: too long or contains a separator", tt.in, got)
  118. }
  119. }
  120. }
  121. func TestCheckExists(t *testing.T) {
  122. dir := t.TempDir()
  123. file := filepath.Join(dir, "a.epub")
  124. writeFile(t, file, "old")
  125. if err := CheckExists(filepath.Join(dir, "missing.epub")); err != nil {
  126. t.Errorf("missing file: %v", err)
  127. }
  128. if err := CheckExists(file); !errors.Is(err, ErrExists) {
  129. t.Errorf("existing file: error = %v, want ErrExists", err)
  130. }
  131. if err := CheckExists(dir); err == nil || errors.Is(err, ErrExists) {
  132. t.Errorf("folder: error = %v, want a non-ErrExists error", err)
  133. }
  134. }
  135. func TestWriteCreatesFolders(t *testing.T) {
  136. dir := t.TempDir()
  137. path := filepath.Join(dir, "a", "b", "c", "book.epub")
  138. if err := Write(path, strings.NewReader("book"), false); err != nil {
  139. t.Fatal(err)
  140. }
  141. assertContent(t, path, "book")
  142. assertNoLeftovers(t, filepath.Dir(path), "book.epub")
  143. }
  144. func TestWriteRefusesExistingFile(t *testing.T) {
  145. dir := t.TempDir()
  146. path := filepath.Join(dir, "book.epub")
  147. writeFile(t, path, "old")
  148. old := time.Now().Add(-time.Hour).Truncate(time.Second)
  149. if err := os.Chtimes(path, old, old); err != nil {
  150. t.Fatal(err)
  151. }
  152. err := Write(path, strings.NewReader("new"), false)
  153. if !errors.Is(err, ErrExists) {
  154. t.Fatalf("error = %v, want ErrExists", err)
  155. }
  156. assertContent(t, path, "old")
  157. info, err := os.Stat(path)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. if !info.ModTime().Equal(old) {
  162. t.Errorf("modification time changed: %v, want %v", info.ModTime(), old)
  163. }
  164. assertNoLeftovers(t, dir, "book.epub")
  165. }
  166. func TestWriteForceReplaces(t *testing.T) {
  167. dir := t.TempDir()
  168. path := filepath.Join(dir, "book.epub")
  169. writeFile(t, path, "old")
  170. if err := Write(path, strings.NewReader("new"), true); err != nil {
  171. t.Fatal(err)
  172. }
  173. assertContent(t, path, "new")
  174. assertNoLeftovers(t, dir, "book.epub")
  175. }
  176. func TestWriteFailureKeepsOldFile(t *testing.T) {
  177. dir := t.TempDir()
  178. path := filepath.Join(dir, "book.epub")
  179. writeFile(t, path, "old")
  180. err := Write(path, failingWriter{}, true)
  181. if err == nil {
  182. t.Fatal("Write succeeded, want error")
  183. }
  184. assertContent(t, path, "old")
  185. assertNoLeftovers(t, dir, "book.epub")
  186. }
  187. func TestWriteFailureLeavesNothing(t *testing.T) {
  188. dir := t.TempDir()
  189. path := filepath.Join(dir, "book.epub")
  190. if err := Write(path, failingWriter{}, false); err == nil {
  191. t.Fatal("Write succeeded, want error")
  192. }
  193. assertNoLeftovers(t, dir)
  194. }
  195. func TestWritePathThroughFile(t *testing.T) {
  196. dir := t.TempDir()
  197. blocker := filepath.Join(dir, "Books")
  198. writeFile(t, blocker, "not a folder")
  199. err := Write(filepath.Join(blocker, "book.epub"), strings.NewReader("book"), false)
  200. if err == nil || errors.Is(err, ErrExists) {
  201. t.Fatalf("error = %v, want a folder creation error", err)
  202. }
  203. assertContent(t, blocker, "not a folder")
  204. }
  205. // failingWriter writes part of a book and then fails, like a build error
  206. // half-way through.
  207. type failingWriter struct{}
  208. func (failingWriter) WriteTo(w io.Writer) (int64, error) {
  209. n, _ := w.Write([]byte("partial"))
  210. return int64(n), errors.New("build failed")
  211. }
  212. func writeFile(t *testing.T, path, content string) {
  213. t.Helper()
  214. if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
  215. t.Fatal(err)
  216. }
  217. }
  218. func assertContent(t *testing.T, path, want string) {
  219. t.Helper()
  220. got, err := os.ReadFile(path)
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. if !bytes.Equal(got, []byte(want)) {
  225. t.Errorf("%s contains %q, want %q", path, got, want)
  226. }
  227. }
  228. // assertNoLeftovers checks that dir contains exactly the named files, so no
  229. // temp files or reservations were left behind.
  230. func assertNoLeftovers(t *testing.T, dir string, want ...string) {
  231. t.Helper()
  232. entries, err := os.ReadDir(dir)
  233. if err != nil {
  234. t.Fatal(err)
  235. }
  236. var got []string
  237. for _, e := range entries {
  238. got = append(got, e.Name())
  239. }
  240. if strings.Join(got, ",") != strings.Join(want, ",") {
  241. t.Errorf("%s contains %v, want %v", dir, got, want)
  242. }
  243. }