profile_path_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 save says which file it wrote, and the path is annotated the way every other
  159. // message in the tool is: a bare path does not say whether it is the user's
  160. // directory or one the argument named.
  161. func TestSavedProfileLineNamesTheFileItWrote(t *testing.T) {
  162. userDir := t.TempDir()
  163. t.Cleanup(stubUserConfigDir(t, userDir))
  164. t.Cleanup(func() { profilesDirOverride = "" })
  165. here := t.TempDir()
  166. t.Chdir(here)
  167. path, name, _ := resolveProfileTarget("profiles/new.json")
  168. got := savedProfileLine(name, path)
  169. if !strings.Contains(got, "profiles/new.json") {
  170. t.Errorf("savedProfileLine() = %q, want the path it was given", got)
  171. }
  172. if strings.Contains(got, "your user directory") {
  173. t.Errorf("savedProfileLine() = %q, want a file the argument named left unannotated", got)
  174. }
  175. byName, byNameStr, _ := resolveProfileTarget("lava")
  176. if line := savedProfileLine(byNameStr, byName); !strings.Contains(line, "your user directory") {
  177. t.Errorf("savedProfileLine() = %q, want a name's file marked as the user's", line)
  178. }
  179. }
  180. // A save by name goes to the user directory, which is the point of the name form:
  181. // the argument is a name, so it is the directory that decides where it goes.
  182. func TestSaveByNameGoesToTheUserDirectory(t *testing.T) {
  183. userDir := t.TempDir()
  184. t.Cleanup(stubUserConfigDir(t, userDir))
  185. t.Cleanup(func() { profilesDirOverride = "" })
  186. here := t.TempDir()
  187. t.Chdir(here)
  188. path, name, isFile := resolveProfileTarget("lava")
  189. if isFile {
  190. t.Fatal(`resolveProfileTarget("lava") isPath = true, want false`)
  191. }
  192. if err := (&Profile{Name: name, Version: 1}).saveTo(path); err != nil {
  193. t.Fatalf("saveTo(%q): %v", path, err)
  194. }
  195. if _, err := os.Stat(filepath.Join(userDir, "qmk-rgb-tool", "profiles", "lava.json")); err != nil {
  196. t.Fatalf("saveTo(%q) did not write into the user's directory: %v", path, err)
  197. }
  198. if _, err := os.Stat(filepath.Join(here, "lava.json")); err == nil {
  199. t.Error("saveTo wrote into the working directory, which a name does not ask for")
  200. }
  201. }