datadir_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. // Profiles and definitions are the tool's own files, and a binary installed with
  8. // `go install` sits in $GOPATH/bin where neither of them exists. They need a
  9. // per-user directory, and they need the same one on Linux, macOS and Windows,
  10. // which is what os.UserConfigDir answers and a hardcoded ~/.config does not.
  11. func TestDataDirectoryPrefersTheExecutableDirectory(t *testing.T) {
  12. userDir := t.TempDir()
  13. t.Cleanup(stubUserConfigDir(t, userDir))
  14. exeDir := t.TempDir()
  15. if err := os.MkdirAll(filepath.Join(exeDir, "definitions"), 0o755); err != nil {
  16. t.Fatal(err)
  17. }
  18. cwd := t.TempDir()
  19. if err := os.MkdirAll(filepath.Join(cwd, "definitions"), 0o755); err != nil {
  20. t.Fatal(err)
  21. }
  22. got := resolveDataDir(exeDir, cwd, "definitions")
  23. if want := filepath.Join(exeDir, "definitions"); got != want {
  24. t.Errorf("resolveDataDir() = %q, want %q: the directory holding the binary wins", got, want)
  25. }
  26. }
  27. func TestDataDirectoryFallsBackToTheWorkingDirectory(t *testing.T) {
  28. userDir := t.TempDir()
  29. t.Cleanup(stubUserConfigDir(t, userDir))
  30. exeDir := t.TempDir() // no definitions/ beside the binary
  31. cwd := t.TempDir()
  32. if err := os.MkdirAll(filepath.Join(cwd, "definitions"), 0o755); err != nil {
  33. t.Fatal(err)
  34. }
  35. got := resolveDataDir(exeDir, cwd, "definitions")
  36. if want := filepath.Join(cwd, "definitions"); got != want {
  37. t.Errorf("resolveDataDir() = %q, want %q: a repository checkout wins over the user directory", got, want)
  38. }
  39. }
  40. func TestDataDirectoryFallsBackToTheUserDirectory(t *testing.T) {
  41. userDir := t.TempDir()
  42. t.Cleanup(stubUserConfigDir(t, userDir))
  43. got := resolveDataDir(t.TempDir(), t.TempDir(), "definitions")
  44. if want := filepath.Join(userDir, "qmk-rgb-tool", "definitions"); got != want {
  45. t.Errorf("resolveDataDir() = %q, want %q", got, want)
  46. }
  47. }
  48. // When nothing exists yet, the directory a fetch or a save creates is the user's,
  49. // not one beside a binary in $GOPATH/bin that nobody owns and that a reinstall
  50. // would replace.
  51. func TestDataDirectoryIsCreatedUnderTheUserDirectory(t *testing.T) {
  52. userDir := t.TempDir()
  53. t.Cleanup(stubUserConfigDir(t, userDir))
  54. got := resolveDataDir(t.TempDir(), t.TempDir(), "profiles")
  55. if want := filepath.Join(userDir, "qmk-rgb-tool", "profiles"); got != want {
  56. t.Errorf("resolveDataDir() = %q, want %q", got, want)
  57. }
  58. }
  59. // Both directories resolve the same way, or a user who installed the binary
  60. // globally finds definitions and not profiles.
  61. func TestProfilesAndDefinitionsUseTheSameOrder(t *testing.T) {
  62. userDir := t.TempDir()
  63. t.Cleanup(stubUserConfigDir(t, userDir))
  64. t.Cleanup(definitionFlagRestore(t))
  65. t.Cleanup(func() { profilesDirOverride = "" })
  66. exeDir := t.TempDir()
  67. t.Cleanup(stubExecutableDir(t, exeDir))
  68. t.Cleanup(stubWorkingDir(t, t.TempDir()))
  69. for _, name := range []string{"definitions", "profiles"} {
  70. if err := os.MkdirAll(filepath.Join(exeDir, name), 0o755); err != nil {
  71. t.Fatal(err)
  72. }
  73. }
  74. t.Cleanup(func() {
  75. for _, name := range []string{"definitions", "profiles"} {
  76. _ = os.RemoveAll(filepath.Join(exeDir, name))
  77. }
  78. })
  79. if got, want := definitionsPath(), filepath.Join(exeDir, "definitions"); got != want {
  80. t.Errorf("definitionsPath() = %q, want %q", got, want)
  81. }
  82. if got, want := profilesPath(), filepath.Join(exeDir, "profiles"); got != want {
  83. t.Errorf("profilesPath() = %q, want %q", got, want)
  84. }
  85. }
  86. // The user directory is reported so the commands can say where a file came from.
  87. func TestDataDirectoryIsReportedForTheUser(t *testing.T) {
  88. userDir := t.TempDir()
  89. t.Cleanup(stubUserConfigDir(t, userDir))
  90. got := definitionsPath()
  91. if got == "" {
  92. t.Fatal("definitionsPath() = \"\", want a path")
  93. }
  94. if !filepath.IsAbs(got) {
  95. t.Errorf("definitionsPath() = %q, want an absolute path so it can be printed", got)
  96. }
  97. }
  98. // stubUserConfigDir points the platform's configuration directory at a temp one.
  99. func stubUserConfigDir(t *testing.T, dir string) func() {
  100. t.Helper()
  101. original := userConfigDir
  102. userConfigDir = func() (string, error) { return dir, nil }
  103. return func() { userConfigDir = original }
  104. }
  105. func stubExecutableDir(t *testing.T, dir string) func() {
  106. t.Helper()
  107. original := executableDir
  108. executableDir = func() string { return dir }
  109. return func() { executableDir = original }
  110. }
  111. func stubWorkingDir(t *testing.T, dir string) func() {
  112. t.Helper()
  113. original := workingDir
  114. workingDir = func() (string, error) { return dir, nil }
  115. return func() { workingDir = original }
  116. }