fs_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package playground
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestClean(t *testing.T) {
  7. cases := map[string]string{
  8. "user:/a/../b": "user:/b",
  9. "user:": "user:/",
  10. "user:/../../x": "user:/x",
  11. "tmp:\\a\\b.txt": "tmp:/a/b.txt",
  12. "web:/App/": "web:/App",
  13. "nope:/x": "",
  14. "/x": "",
  15. }
  16. for in, want := range cases {
  17. if got := Clean(in); got != want {
  18. t.Errorf("Clean(%q) = %q, want %q", in, got, want)
  19. }
  20. }
  21. }
  22. func TestMemFSSemantics(t *testing.T) {
  23. m := newMemFS([]File{{Path: "user:/a/b.txt", Data: []byte("x")}})
  24. if !m.IsDir("user:/a") {
  25. t.Fatal("parent folders of a loaded file must exist")
  26. }
  27. if err := m.WriteFile("user:/missing/c.txt", nil); err == nil {
  28. t.Error("WriteFile without a parent folder must fail like os.WriteFile")
  29. }
  30. if err := m.Remove("user:/a"); err == nil {
  31. t.Error("Remove of a non-empty folder must fail like os.Remove")
  32. }
  33. if err := m.Remove("user:/"); err == nil {
  34. t.Error("a root must not be removable")
  35. }
  36. if err := m.RemoveAll("user:/a"); err != nil || m.Exists("user:/a/b.txt") {
  37. t.Errorf("RemoveAll: %v", err)
  38. }
  39. if err := m.MkdirAll("user:/p/q"); err != nil || !m.IsDir("user:/p/q") {
  40. t.Errorf("MkdirAll: %v", err)
  41. }
  42. m.WriteFile("user:/p/f.txt", []byte("1"))
  43. if err := m.MkdirAll("user:/p/f.txt/z"); err == nil {
  44. t.Error("MkdirAll through a file must fail")
  45. }
  46. written, removed := m.Changes()
  47. var w []string
  48. for _, f := range written {
  49. w = append(w, f.Path)
  50. }
  51. if strings.Join(w, ",") != "user:/p,user:/p/f.txt,user:/p/q" || strings.Join(removed, ",") != "user:/a,user:/a/b.txt" {
  52. t.Errorf("changes: written %v removed %v", w, removed)
  53. }
  54. }
  55. func TestGlobAndHidden(t *testing.T) {
  56. m := newMemFS([]File{{Path: "user:/d/a.mp3"}, {Path: "user:/d/b.txt"}, {Path: "user:/d/.c.mp3"}})
  57. list, _ := m.Glob("user:/d/*.mp3")
  58. var names []string
  59. for _, e := range list {
  60. if !isHiddenPath(e.Vpath) {
  61. names = append(names, e.Name)
  62. }
  63. }
  64. if strings.Join(names, ",") != "a.mp3" {
  65. t.Errorf("glob: %v", names)
  66. }
  67. if _, err := m.Glob("user:/d/[.mp3"); err == nil {
  68. t.Error("a bad pattern must report an error")
  69. }
  70. }
  71. func TestNaturalLess(t *testing.T) {
  72. cases := []struct {
  73. a, b string
  74. want bool
  75. }{
  76. {"file2", "file10", true},
  77. {"file10", "file2", false},
  78. {"a", "b", true},
  79. {"File1", "file02", true},
  80. }
  81. for _, c := range cases {
  82. if got := naturalLess(c.a, c.b); got != c.want {
  83. t.Errorf("naturalLess(%q, %q) = %v", c.a, c.b, got)
  84. }
  85. }
  86. }
  87. func TestMatchers(t *testing.T) {
  88. cases := []struct {
  89. f func(string, string) bool
  90. pattern, s string
  91. want bool
  92. }{
  93. {matchURL, "https://a.com/x", "https://a.com/x/", true},
  94. {matchURL, "https://a.com/*", "https://a.com/x/y", true},
  95. {matchURL, "https://a.com/*", "https://b.com/x", false},
  96. {matchURL, "/a\\.com\\/\\d+$/", "https://a.com/12", true},
  97. {matchText, "Capital", "what is the capital?", true},
  98. {matchText, "/^hi/i", "Hi there", true},
  99. {matchText, "", "anything", true},
  100. }
  101. for _, c := range cases {
  102. if got := c.f(c.pattern, c.s); got != c.want {
  103. t.Errorf("match(%q, %q) = %v", c.pattern, c.s, got)
  104. }
  105. }
  106. }