profile_path_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package main
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. )
  9. // A .json argument is a path and everything else is a name, and that is the whole
  10. // rule. The suffix is matched without regard to case so it reads the same on
  11. // Linux, macOS and Windows, and no separator is looked for: the Windows file API
  12. // takes both, so a check for one would be a platform difference with no behaviour
  13. // behind it.
  14. func TestResolveProfileTargetTreatsAJsonSuffixAsAPath(t *testing.T) {
  15. userDir := t.TempDir()
  16. t.Cleanup(stubUserConfigDir(t, userDir))
  17. t.Cleanup(func() { profilesDirOverride = "" })
  18. user := filepath.Join(userDir, "qmk-rgb-tool", "profiles")
  19. for _, tc := range []struct {
  20. arg string
  21. wantPath string
  22. wantName string
  23. wantFile bool
  24. }{
  25. {"lala.json", "lala.json", "lala", true},
  26. {"./lala.json", "./lala.json", "lala", true},
  27. {"profiles/lava.json", "profiles/lava.json", "lava", true},
  28. {"LALA.JSON", "LALA.JSON", "LALA", true},
  29. {"lava", filepath.Join(user, "lava.json"), "lava", false},
  30. {"my profile", filepath.Join(user, "my-profile.json"), "my profile", false},
  31. } {
  32. path, name, isFile := resolveProfileTarget(tc.arg)
  33. if isFile != tc.wantFile {
  34. t.Errorf("resolveProfileTarget(%q) isPath = %v, want %v", tc.arg, isFile, tc.wantFile)
  35. }
  36. if path != tc.wantPath {
  37. t.Errorf("resolveProfileTarget(%q) path = %q, want %q", tc.arg, path, tc.wantPath)
  38. }
  39. if name != tc.wantName {
  40. t.Errorf("resolveProfileTarget(%q) name = %q, want %q", tc.arg, name, tc.wantName)
  41. }
  42. }
  43. }
  44. // The two forms read two different files, and the argument says which: a path
  45. // reads the file it names, a name reads the user's directory. The same fixture
  46. // holds a file of each, so a rule that guessed instead would have one answer for
  47. // both arguments.
  48. func TestLoadProfileReadsTheFileTheArgumentNames(t *testing.T) {
  49. userDir := t.TempDir()
  50. t.Cleanup(stubUserConfigDir(t, userDir))
  51. t.Cleanup(func() { profilesDirOverride = "" })
  52. here := t.TempDir()
  53. if err := os.MkdirAll(filepath.Join(here, "profiles"), 0o755); err != nil {
  54. t.Fatal(err)
  55. }
  56. if err := os.WriteFile(filepath.Join(here, "profiles", "lava.json"),
  57. []byte(`{"name":"from-the-checkout","version":1,"zones":{}}`), 0o644); err != nil {
  58. t.Fatal(err)
  59. }
  60. dir := filepath.Join(userDir, "qmk-rgb-tool", "profiles")
  61. if err := os.MkdirAll(dir, 0o755); err != nil {
  62. t.Fatal(err)
  63. }
  64. if err := os.WriteFile(filepath.Join(dir, "lava.json"),
  65. []byte(`{"name":"from-the-user-directory","version":1,"zones":{}}`), 0o644); err != nil {
  66. t.Fatal(err)
  67. }
  68. t.Chdir(here)
  69. path, err := LoadProfile("profiles/lava.json")
  70. if err != nil {
  71. t.Fatalf("LoadProfile(profiles/lava.json): %v", err)
  72. }
  73. if path.Name != "from-the-checkout" {
  74. t.Errorf("LoadProfile(profiles/lava.json).Name = %q, want the file it names", path.Name)
  75. }
  76. byName, err := LoadProfile("lava")
  77. if err != nil {
  78. t.Fatalf("LoadProfile(lava): %v", err)
  79. }
  80. if byName.Name != "from-the-user-directory" {
  81. t.Errorf("LoadProfile(lava).Name = %q, want the user's directory, not the checkout's", byName.Name)
  82. }
  83. }
  84. // A path that is not there says which path, and not the user's directory: it was
  85. // never looked for there, so naming it sends the user to read a file that is
  86. // present and say nothing about why it was ignored.
  87. func TestLoadProfileAMissingPathNamesThePath(t *testing.T) {
  88. userDir := t.TempDir()
  89. t.Cleanup(stubUserConfigDir(t, userDir))
  90. t.Cleanup(func() { profilesDirOverride = "" })
  91. t.Chdir(t.TempDir())
  92. _, err := LoadProfile("./nope.json")
  93. if err == nil {
  94. t.Fatal("LoadProfile(./nope.json) = nil error, want one")
  95. }
  96. if !strings.Contains(err.Error(), "./nope.json") {
  97. t.Errorf("LoadProfile(./nope.json) = %q, want it to name the path", err)
  98. }
  99. if strings.Contains(err.Error(), "qmk-rgb-tool") {
  100. t.Errorf("LoadProfile(./nope.json) = %q, want it not to name the user's directory", err)
  101. }
  102. }
  103. // A .json argument is read as a path and never sanitized into a name. This is the
  104. // case the rule turns on: `lala.json` used to mean the profile `lala-json`, and
  105. // the error for a missing one would name a file that never existed.
  106. func TestLoadProfileAJsonArgumentIsNotSanitized(t *testing.T) {
  107. userDir := t.TempDir()
  108. t.Cleanup(stubUserConfigDir(t, userDir))
  109. t.Cleanup(func() { profilesDirOverride = "" })
  110. t.Chdir(t.TempDir())
  111. _, err := LoadProfile("lala.json")
  112. if err == nil {
  113. t.Fatal("LoadProfile(lala.json) = nil error, want one")
  114. }
  115. if strings.Contains(err.Error(), "lala-json") {
  116. t.Errorf("LoadProfile(lala.json) = %q, want lala.json read as a path", err)
  117. }
  118. if !strings.Contains(err.Error(), "lala.json") {
  119. t.Errorf("LoadProfile(lala.json) = %q, want it to name the path it tried", err)
  120. }
  121. }
  122. // A save that names a file writes that file, and the profile's own name is the
  123. // file's name without the suffix — so the file and the name in it agree, and the
  124. // file is not silently also written where the names live.
  125. func TestSaveProfileToWritesThePathItWasGiven(t *testing.T) {
  126. userDir := t.TempDir()
  127. t.Cleanup(stubUserConfigDir(t, userDir))
  128. t.Cleanup(func() { profilesDirOverride = "" })
  129. here := t.TempDir()
  130. t.Chdir(here)
  131. path, name, isFile := resolveProfileTarget("profiles/new.json")
  132. if !isFile {
  133. t.Fatal(`resolveProfileTarget("profiles/new.json") isPath = false, want true`)
  134. }
  135. p := &Profile{Name: name, Version: 1}
  136. if err := p.saveTo(path); err != nil {
  137. t.Fatalf("saveTo(%q): %v", path, err)
  138. }
  139. written := filepath.Join(here, "profiles", "new.json")
  140. if _, err := os.Stat(written); err != nil {
  141. t.Fatalf("saveTo(%q) did not write the file it was given: %v", path, err)
  142. }
  143. data, err := os.ReadFile(written)
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. var got Profile
  148. if err := json.Unmarshal(data, &got); err != nil {
  149. t.Fatal(err)
  150. }
  151. if got.Name != "new" {
  152. t.Errorf("saved profile Name = %q, want %q", got.Name, "new")
  153. }
  154. if _, err := os.Stat(filepath.Join(userDir, "qmk-rgb-tool", "profiles", "new.json")); err == nil {
  155. t.Error("saveTo also wrote into the user's directory, which the argument did not ask for")
  156. }
  157. }
  158. // A delete says which file it removed, and saying so must not make it resolve a
  159. // path: `delete` is name-only, so `delete lava.json` names no file, it names the
  160. // profile `lava-json`. A same-named file in the working directory is the thing
  161. // that would go if the report were read as permission to touch it.
  162. func TestDeleteIsNameOnlyAndSaysWhichFileItRemoved(t *testing.T) {
  163. userDir := t.TempDir()
  164. t.Cleanup(stubUserConfigDir(t, userDir))
  165. t.Cleanup(func() { profilesDirOverride = "" })
  166. here := t.TempDir()
  167. t.Chdir(here)
  168. inWorkingDir := filepath.Join(here, "lava.json")
  169. if err := os.WriteFile(inWorkingDir, []byte(`{"name":"lava","version":1,"zones":{}}`), 0o644); err != nil {
  170. t.Fatal(err)
  171. }
  172. dir := filepath.Join(userDir, "qmk-rgb-tool", "profiles")
  173. if err := os.MkdirAll(dir, 0o755); err != nil {
  174. t.Fatal(err)
  175. }
  176. saved := filepath.Join(dir, "lava.json")
  177. if err := os.WriteFile(saved, []byte(`{"name":"lava","version":1,"zones":{}}`), 0o644); err != nil {
  178. t.Fatal(err)
  179. }
  180. path, err := DeleteProfile("lava")
  181. if err != nil {
  182. t.Fatalf("DeleteProfile(lava): %v", err)
  183. }
  184. if path != saved {
  185. t.Errorf("DeleteProfile(lava) = %q, want %q", path, saved)
  186. }
  187. line := deletedProfileLine("lava", path)
  188. if !strings.Contains(line, saved) {
  189. t.Errorf("deletedProfileLine() = %q, want the file it removed", line)
  190. }
  191. if !strings.Contains(line, "your user directory") {
  192. t.Errorf("deletedProfileLine() = %q, want a name's file marked as the user's", line)
  193. }
  194. if _, err := os.Stat(saved); err == nil {
  195. t.Error("DeleteProfile(lava) left the profile in place")
  196. }
  197. if _, err := os.Stat(inWorkingDir); err != nil {
  198. t.Errorf("DeleteProfile(lava) removed the working directory's file: %v", err)
  199. }
  200. // The .json suffix is not read as a path by a name-only command: it names the
  201. // profile `lava-json`, which is not there.
  202. if _, err := DeleteProfile("lava.json"); err == nil {
  203. t.Error(`DeleteProfile("lava.json") = nil error, want the name form refused`)
  204. }
  205. if _, err := os.Stat(inWorkingDir); err != nil {
  206. t.Errorf(`DeleteProfile("lava.json") removed the working directory's file: %v`, err)
  207. }
  208. }
  209. // A save says which file it wrote, and the path is annotated the way every other
  210. // message in the tool is: a bare path does not say whether it is the user's
  211. // directory or one the argument named.
  212. func TestSavedProfileLineNamesTheFileItWrote(t *testing.T) {
  213. userDir := t.TempDir()
  214. t.Cleanup(stubUserConfigDir(t, userDir))
  215. t.Cleanup(func() { profilesDirOverride = "" })
  216. here := t.TempDir()
  217. t.Chdir(here)
  218. path, name, _ := resolveProfileTarget("profiles/new.json")
  219. got := savedProfileLine(name, path)
  220. if !strings.Contains(got, "profiles/new.json") {
  221. t.Errorf("savedProfileLine() = %q, want the path it was given", got)
  222. }
  223. if strings.Contains(got, "your user directory") {
  224. t.Errorf("savedProfileLine() = %q, want a file the argument named left unannotated", got)
  225. }
  226. byName, byNameStr, _ := resolveProfileTarget("lava")
  227. if line := savedProfileLine(byNameStr, byName); !strings.Contains(line, "your user directory") {
  228. t.Errorf("savedProfileLine() = %q, want a name's file marked as the user's", line)
  229. }
  230. }
  231. // A save by name goes to the user directory, which is the point of the name form:
  232. // the argument is a name, so it is the directory that decides where it goes.
  233. func TestSaveByNameGoesToTheUserDirectory(t *testing.T) {
  234. userDir := t.TempDir()
  235. t.Cleanup(stubUserConfigDir(t, userDir))
  236. t.Cleanup(func() { profilesDirOverride = "" })
  237. here := t.TempDir()
  238. t.Chdir(here)
  239. path, name, isFile := resolveProfileTarget("lava")
  240. if isFile {
  241. t.Fatal(`resolveProfileTarget("lava") isPath = true, want false`)
  242. }
  243. if err := (&Profile{Name: name, Version: 1}).saveTo(path); err != nil {
  244. t.Fatalf("saveTo(%q): %v", path, err)
  245. }
  246. if _, err := os.Stat(filepath.Join(userDir, "qmk-rgb-tool", "profiles", "lava.json")); err != nil {
  247. t.Fatalf("saveTo(%q) did not write into the user's directory: %v", path, err)
  248. }
  249. if _, err := os.Stat(filepath.Join(here, "lava.json")); err == nil {
  250. t.Error("saveTo wrote into the working directory, which a name does not ask for")
  251. }
  252. }