update tests in utilities

This commit is contained in:
Dan Anglin 2024-08-18 18:47:36 +01:00
parent 9acf562ccc
commit b35040aa79
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 77 additions and 37 deletions

View file

@ -7,14 +7,26 @@ import (
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
)
func TestCalculateMediaCacheDir(t *testing.T) {
t.Parallel()
func TestDirectoryCalculations(t *testing.T) {
t.Log("Testing the directory calculations")
projectDir, err := projectRoot()
if err != nil {
t.Fatalf("Unable to get the project root directory: %v", err)
}
t.Setenv("XDG_CACHE_HOME", "/home/enbas/.cache")
t.Setenv("XDG_CONFIG_HOME", "/home/enbas/.config")
t.Run("Media Cache Directory Calculation", testCalculateMediaCacheDir(projectDir))
t.Run("Media Cache Directory Calculation (with XDG_CACHE_HOME)", testCalculateMediaCacheDirWithXDG)
t.Run("Statuses Cache Directory Calculation", testCalculateStatusesCacheDir(projectDir))
t.Run("Configuration Directory Calculation", testCalculateConfigDir(projectDir))
t.Run("Configuration Directory Calculation (with XDG_CONFIG_HOME)", testCalculateConfigCacheDirWithXDG)
}
func testCalculateMediaCacheDir(projectDir string) func(t *testing.T) {
return func(t *testing.T) {
cacheRoot := filepath.Join(projectDir, "test", "cache")
instance := "http://gotosocial.yellow-desert.social"
@ -31,10 +43,9 @@ func TestCalculateMediaCacheDir(t *testing.T) {
t.Logf("Expected media cache directory calculated: got %s", got)
}
}
}
func TestCalculateMediaCacheDirWithXDG(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", "/home/enbas/.cache")
func testCalculateMediaCacheDirWithXDG(t *testing.T) {
cacheRoot := ""
instance := "https://gotosocial.yellow-desert.social"
@ -52,14 +63,8 @@ func TestCalculateMediaCacheDirWithXDG(t *testing.T) {
}
}
func TestCalculateStatusesCacheDir(t *testing.T) {
t.Parallel()
projectDir, err := projectRoot()
if err != nil {
t.Fatalf("Unable to get the project root directory: %v", err)
}
func testCalculateStatusesCacheDir(projectDir string) func(t *testing.T) {
return func(t *testing.T) {
cacheRoot := filepath.Join(projectDir, "test", "cache")
instance := "https://fedi.blue-mammoth.party"
@ -76,3 +81,40 @@ func TestCalculateStatusesCacheDir(t *testing.T) {
t.Logf("Expected statuses cache directory calculated: got %s", got)
}
}
}
func testCalculateConfigDir(projectDir string) func(t *testing.T) {
return func(t *testing.T) {
configDir := filepath.Join(projectDir, "test", "config")
got, err := utilities.CalculateConfigDir(configDir)
if err != nil {
t.Fatalf("Unable to calculate the config directory: %v", err)
}
want := projectDir + "/test/config"
if got != want {
t.Errorf("Unexpected config directory calculated: want %s, got %s", want, got)
} else {
t.Logf("Expected config directory calculated: got %s", got)
}
}
}
func testCalculateConfigCacheDirWithXDG(t *testing.T) {
configDir := ""
got, err := utilities.CalculateConfigDir(configDir)
if err != nil {
t.Fatalf("Unable to calculate the config directory: %v", err)
}
want := "/home/enbas/.config/enbas"
if got != want {
t.Errorf("Unexpected config directory calculated: want %s, got %s", want, got)
} else {
t.Logf("Expected config directory calculated: got %s", got)
}
}

View file

@ -7,8 +7,6 @@ import (
)
func TestGetFQDN(t *testing.T) {
t.Parallel()
cases := []struct {
instance string
want string