enbas/internal/utilities/directories_test.go
Dan Anglin 7e4b8bb05f
All checks were successful
Tests / test (pull_request) Successful in 17s
REUSE Compliance Check / check (push) Successful in 6s
fix: fixed the calculations in cache directories
Changes:

- Fixed the issue where the instance's FQDN was not included in the
  cache directories' path when the user sets a custom path to the root
  cache directory.

- Added unit tests for the cache directory calculations.

- Updated REUSE.toml to fix compliance failures.

- Added small changes/fixes based on feedback from golangci-lint.

PR: #53
Resolves: #51
2024-08-18 00:06:18 +01:00

78 lines
2.1 KiB
Go

package utilities_test
import (
"path/filepath"
"testing"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
)
func TestCalculateMediaCacheDir(t *testing.T) {
t.Parallel()
projectDir, err := projectRoot()
if err != nil {
t.Fatalf("Unable to get the project root directory: %v", err)
}
cacheRoot := filepath.Join(projectDir, "test", "cache")
instance := "http://gotosocial.yellow-desert.social"
got, err := utilities.CalculateMediaCacheDir(cacheRoot, instance)
if err != nil {
t.Fatalf("Unable to calculate the media cache directory: %v", err)
}
want := projectDir + "/test/cache/gotosocial.yellow-desert.social/media"
if got != want {
t.Errorf("Unexpected media cache directory calculated: want %s, got %s", want, got)
} else {
t.Logf("Expected media cache directory calculated: got %s", got)
}
}
func TestCalculateMediaCacheDirWithXDG(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", "/home/enbas/.cache")
cacheRoot := ""
instance := "https://gotosocial.yellow-desert.social"
got, err := utilities.CalculateMediaCacheDir(cacheRoot, instance)
if err != nil {
t.Fatalf("Unable to calculate the media cache directory: %v", err)
}
want := "/home/enbas/.cache/enbas/gotosocial.yellow-desert.social/media"
if got != want {
t.Errorf("Unexpected media cache directory calculated: want %s, got %s", want, got)
} else {
t.Logf("Expected media cache directory calculated: got %s", got)
}
}
func TestCalculateStatusesCacheDir(t *testing.T) {
t.Parallel()
projectDir, err := projectRoot()
if err != nil {
t.Fatalf("Unable to get the project root directory: %v", err)
}
cacheRoot := filepath.Join(projectDir, "test", "cache")
instance := "https://fedi.blue-mammoth.party"
got, err := utilities.CalculateStatusesCacheDir(cacheRoot, instance)
if err != nil {
t.Fatalf("Unable to calculate the statuses cache directory: %v", err)
}
want := projectDir + "/test/cache/fedi.blue-mammoth.party/statuses"
if got != want {
t.Errorf("Unexpected statuses cache directory calculated: want %s, got %s", want, got)
} else {
t.Logf("Expected statuses cache directory calculated: got %s", got)
}
}