Compare commits

...

5 commits

Author SHA1 Message Date
4c87993667
added test for config saving and loading
All checks were successful
Tests / test (pull_request) Has been skipped
2024-08-18 19:40:12 +01:00
b35040aa79
update tests in utilities 2024-08-18 18:47:36 +01:00
9acf562ccc
checkpoint: remove default config dir; replace with err if user's congig home not found 2024-08-18 09:58:47 +01:00
15b9761497
refactor: clean up unused const
All checks were successful
REUSE Compliance Check / check (push) Successful in 5s
2024-08-18 09:57:48 +01:00
277bec49ef
fix: add MissingIDError type
Add the MissingIDError type for when the ID of a resource is not
provided where required.
2024-08-18 08:59:44 +01:00
19 changed files with 312 additions and 119 deletions

View file

@ -10,7 +10,11 @@ import (
) )
const ( const (
configFileName = "config.json" configFileName string = "config.json"
defaultHTTPTimeout int = 5
defaultHTTPMediaTimeout int = 30
defaultLineWrapMaxWidth int = 80
) )
type Config struct { type Config struct {
@ -35,7 +39,10 @@ type Integrations struct {
} }
func NewConfigFromFile(configDir string) (*Config, error) { func NewConfigFromFile(configDir string) (*Config, error) {
path := configFile(configDir) path, err := configPath(configDir)
if err != nil {
return nil, fmt.Errorf("unable to calculate the path to your config file: %w", err)
}
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
@ -53,13 +60,19 @@ func NewConfigFromFile(configDir string) (*Config, error) {
} }
func FileExists(configDir string) (bool, error) { func FileExists(configDir string) (bool, error) {
path := configFile(configDir) path, err := configPath(configDir)
if err != nil {
return false, fmt.Errorf("unable to calculate the path to your config file: %w", err)
}
return utilities.FileExists(path) return utilities.FileExists(path)
} }
func SaveDefaultConfigToFile(configDir string) error { func SaveDefaultConfigToFile(configDir string) error {
path := configFile(configDir) path, err := configPath(configDir)
if err != nil {
return fmt.Errorf("unable to calculate the path to your config file: %w", err)
}
file, err := os.Create(path) file, err := os.Create(path)
if err != nil { if err != nil {
@ -69,7 +82,7 @@ func SaveDefaultConfigToFile(configDir string) error {
config := defaultConfig() config := defaultConfig()
credentialsFilePath, err := utilities.AbsolutePath(defaultCredentialsConfigFile(configDir)) credentialsFilePath, err := defaultCredentialsConfigFile(configDir)
if err != nil { if err != nil {
return fmt.Errorf("unable to calculate the path to the credentials file: %w", err) return fmt.Errorf("unable to calculate the path to the credentials file: %w", err)
} }
@ -86,8 +99,13 @@ func SaveDefaultConfigToFile(configDir string) error {
return nil return nil
} }
func configFile(configDir string) string { func configPath(configDir string) (string, error) {
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName) configDir, err := utilities.CalculateConfigDir(configDir)
if err != nil {
return "", fmt.Errorf("unable to get the config directory: %w", err)
}
return filepath.Join(configDir, configFileName), nil
} }
func defaultConfig() Config { func defaultConfig() Config {
@ -95,10 +113,10 @@ func defaultConfig() Config {
CredentialsFile: "", CredentialsFile: "",
CacheDirectory: "", CacheDirectory: "",
HTTP: HTTPConfig{ HTTP: HTTPConfig{
Timeout: 5, Timeout: defaultHTTPTimeout,
MediaTimeout: 30, MediaTimeout: defaultHTTPMediaTimeout,
}, },
LineWrapMaxWidth: 80, LineWrapMaxWidth: defaultLineWrapMaxWidth,
Integrations: Integrations{ Integrations: Integrations{
Browser: "", Browser: "",
Editor: "", Editor: "",

View file

@ -0,0 +1,83 @@
package config_test
import (
"fmt"
"os"
"path/filepath"
"testing"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
)
func TestConfigFile(t *testing.T) {
t.Log("Testing saving and loading the configuration")
projectDir, err := projectRoot()
if err != nil {
t.Fatalf("Unable to get the project root directory: %v", err)
}
configDir := filepath.Join(projectDir, "test", "config")
t.Run("Save the default configuration to file", testSaveDefaultConfigToFile(configDir))
t.Run("Load the configuration from file", testLoadConfigFromFile(configDir))
expectedConfigFile := filepath.Join(configDir, "config.json")
if err := os.Remove(expectedConfigFile); err != nil {
t.Fatalf(
"received an error after trying to clean up the test configuration at %q: %v",
expectedConfigFile,
err,
)
}
}
func testSaveDefaultConfigToFile(configDir string) func(t *testing.T) {
return func(t *testing.T) {
if err := config.SaveDefaultConfigToFile(configDir); err != nil {
t.Fatalf("Unable to save the default configuration within %q: %v", configDir, err)
}
fileExists, err := config.FileExists(configDir)
if err != nil {
t.Fatalf("Unable to determine if the configuration exists within %q: %v", configDir, err)
}
if !fileExists {
t.Fatalf("The configuration does not appear to exist within %q", configDir)
}
}
}
func testLoadConfigFromFile(configDir string) func(t *testing.T) {
return func(t *testing.T) {
config, err := config.NewConfigFromFile(configDir)
if err != nil {
t.Fatalf("Unable to load the configuration from file: %v", err)
}
expectedDefaultHTTPTimeout := 5
if config.HTTP.Timeout != expectedDefaultHTTPTimeout {
t.Errorf(
"Unexpected HTTP Timeout settings loaded from the configuration: want %d, got %d",
expectedDefaultHTTPTimeout,
config.HTTP.Timeout,
)
} else {
t.Logf(
"Expected HTTP Timeout settings loaded from the configuration: got %d",
config.HTTP.Timeout,
)
}
}
}
func projectRoot() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("unable to get the current working directory, %w", err)
}
return filepath.Join(cwd, "..", ".."), nil
}

View file

@ -38,9 +38,14 @@ func (e CredentialsNotFoundError) Error() string {
// directory. If the directory is not specified then the default directory is used. If the directory // directory. If the directory is not specified then the default directory is used. If the directory
// is not present, it will be created. // is not present, it will be created.
func SaveCredentials(filePath, username string, credentials Credentials) (string, error) { func SaveCredentials(filePath, username string, credentials Credentials) (string, error) {
directory := filepath.Dir(filePath) part := filepath.Dir(filePath)
if err := utilities.EnsureDirectory(utilities.CalculateConfigDir(directory)); err != nil { credentialsDir, err := utilities.CalculateConfigDir(part)
if err != nil {
fmt.Errorf("unable to calculate the directory to your credentials file: %w", err)
}
if err := utilities.EnsureDirectory(credentialsDir); err != nil {
return "", fmt.Errorf("unable to ensure the configuration directory: %w", err) return "", fmt.Errorf("unable to ensure the configuration directory: %w", err)
} }
@ -128,6 +133,16 @@ func saveCredentialsConfigFile(authConfig CredentialsConfig, filePath string) er
return nil return nil
} }
func defaultCredentialsConfigFile(configDir string) string { func defaultCredentialsConfigFile(configDir string) (string, error) {
return filepath.Join(utilities.CalculateConfigDir(configDir), defaultCredentialsFileName) dir, err := utilities.CalculateConfigDir(configDir)
if err != nil {
return "", fmt.Errorf("unable to calculate the config directory: %w", err)
}
path, err := utilities.AbsolutePath(filepath.Join(dir, defaultCredentialsFileName))
if err != nil {
return "", fmt.Errorf("unable to get the absolute path to the credentials config file: %w", err)
}
return path, nil
} }

View file

@ -32,6 +32,13 @@ func (a *AddExecutor) Execute() error {
} }
func (a *AddExecutor) addToList(gtsClient *client.Client) error { func (a *AddExecutor) addToList(gtsClient *client.Client) error {
if a.listID == "" {
return MissingIDError{
resource: resourceList,
action: "add to",
}
}
funcMap := map[string]func(*client.Client) error{ funcMap := map[string]func(*client.Client) error{
resourceAccount: a.addAccountsToList, resourceAccount: a.addAccountsToList,
} }
@ -48,10 +55,6 @@ func (a *AddExecutor) addToList(gtsClient *client.Client) error {
} }
func (a *AddExecutor) addAccountsToList(gtsClient *client.Client) error { func (a *AddExecutor) addAccountsToList(gtsClient *client.Client) error {
if a.listID == "" {
return FlagNotSetError{flagText: flagListID}
}
if a.accountNames.Empty() { if a.accountNames.Empty() {
return NoAccountSpecifiedError{} return NoAccountSpecifiedError{}
} }
@ -138,7 +141,10 @@ func (a *AddExecutor) addToBookmarks(gtsClient *client.Client) error {
func (a *AddExecutor) addStatusToBookmarks(gtsClient *client.Client) error { func (a *AddExecutor) addStatusToBookmarks(gtsClient *client.Client) error {
if a.statusID == "" { if a.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "add to your bookmarks",
}
} }
if err := gtsClient.AddStatusToBookmarks(a.statusID); err != nil { if err := gtsClient.AddStatusToBookmarks(a.statusID); err != nil {
@ -152,7 +158,10 @@ func (a *AddExecutor) addStatusToBookmarks(gtsClient *client.Client) error {
func (a *AddExecutor) addToStatus(gtsClient *client.Client) error { func (a *AddExecutor) addToStatus(gtsClient *client.Client) error {
if a.statusID == "" { if a.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "add to",
}
} }
funcMap := map[string]func(*client.Client) error{ funcMap := map[string]func(*client.Client) error{

View file

@ -0,0 +1,29 @@
package executor
const (
flagFrom string = "from"
flagTo string = "to"
flagType string = "type"
resourceAccount string = "account"
resourceBlocked string = "blocked"
resourceBookmarks string = "bookmarks"
resourceBoost string = "boost"
resourceFollowers string = "followers"
resourceFollowing string = "following"
resourceFollowRequest string = "follow-request"
resourceInstance string = "instance"
resourceLike string = "like"
resourceLiked string = "liked"
resourceList string = "list"
resourceMedia string = "media"
resourceMediaAttachment string = "media-attachment"
resourceMutedAccounts string = "muted-accounts"
resourceNote string = "note"
resourcePoll string = "poll"
resourceStatus string = "status"
resourceStar string = "star"
resourceStarred string = "starred"
resourceTimeline string = "timeline"
resourceVote string = "vote"
)

View file

@ -34,12 +34,12 @@ func (c *CreateExecutor) Execute() error {
func (c *CreateExecutor) createList(gtsClient *client.Client) error { func (c *CreateExecutor) createList(gtsClient *client.Client) error {
if c.listTitle == "" { if c.listTitle == "" {
return FlagNotSetError{flagText: flagListTitle} return Error{"please provide the title of the list that you want to create"}
} }
parsedListRepliesPolicy, err := model.ParseListRepliesPolicy(c.listRepliesPolicy) parsedListRepliesPolicy, err := model.ParseListRepliesPolicy(c.listRepliesPolicy)
if err != nil { if err != nil {
return err return err //nolint:wrapcheck
} }
form := client.CreateListForm{ form := client.CreateListForm{

View file

@ -33,7 +33,10 @@ func (d *DeleteExecutor) Execute() error {
func (d *DeleteExecutor) deleteList(gtsClient *client.Client) error { func (d *DeleteExecutor) deleteList(gtsClient *client.Client) error {
if d.listID == "" { if d.listID == "" {
return FlagNotSetError{flagText: flagListID} return MissingIDError{
resource: resourceList,
action: "delete",
}
} }
if err := gtsClient.DeleteList(d.listID); err != nil { if err := gtsClient.DeleteList(d.listID); err != nil {
@ -47,7 +50,10 @@ func (d *DeleteExecutor) deleteList(gtsClient *client.Client) error {
func (d *DeleteExecutor) deleteStatus(gtsClient *client.Client) error { func (d *DeleteExecutor) deleteStatus(gtsClient *client.Client) error {
if d.statusID == "" { if d.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "delete",
}
} }
status, err := gtsClient.GetStatus(d.statusID) status, err := gtsClient.GetStatus(d.statusID)

View file

@ -33,7 +33,10 @@ func (e *EditExecutor) Execute() error {
func (e *EditExecutor) editList(gtsClient *client.Client) error { func (e *EditExecutor) editList(gtsClient *client.Client) error {
if e.listID == "" { if e.listID == "" {
return FlagNotSetError{flagText: flagListID} return MissingIDError{
resource: resourceList,
action: "edit",
}
} }
list, err := gtsClient.GetList(e.listID) list, err := gtsClient.GetList(e.listID)

View file

@ -117,3 +117,12 @@ func (e UnexpectedNumValuesError) Error() string {
e.expected, e.expected,
) )
} }
type MissingIDError struct {
resource string
action string
}
func (e MissingIDError) Error() string {
return "please provide the ID of the " + e.resource + " you want to " + e.action
}

View file

@ -1,14 +0,0 @@
package executor
const (
flagAttachmentID = "attachment-id"
flagContent = "content"
flagFrom = "from"
flagInstance = "instance"
flagListID = "list-id"
flagListTitle = "list-title"
flagStatusID = "status-id"
flagTag = "tag"
flagTo = "to"
flagType = "type"
)

View file

@ -13,7 +13,7 @@ func (l *LoginExecutor) Execute() error {
var err error var err error
if l.instance == "" { if l.instance == "" {
return FlagNotSetError{flagText: flagInstance} return Error{"please specify the instance that you want to log into"}
} }
instance := l.instance instance := l.instance

View file

@ -47,7 +47,10 @@ func (m *MuteExecutor) muteAccount(gtsClient *client.Client) error {
func (m *MuteExecutor) muteStatus(gtsClient *client.Client) error { func (m *MuteExecutor) muteStatus(gtsClient *client.Client) error {
if m.statusID == "" { if m.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "mute",
}
} }
status, err := gtsClient.GetStatus(m.statusID) status, err := gtsClient.GetStatus(m.statusID)

View file

@ -49,7 +49,10 @@ func (r *RemoveExecutor) removeFromList(gtsClient *client.Client) error {
func (r *RemoveExecutor) removeAccountsFromList(gtsClient *client.Client) error { func (r *RemoveExecutor) removeAccountsFromList(gtsClient *client.Client) error {
if r.listID == "" { if r.listID == "" {
return FlagNotSetError{flagText: flagListID} return MissingIDError{
resource: resourceList,
action: "remove from",
}
} }
if r.accountNames.Empty() { if r.accountNames.Empty() {
@ -125,7 +128,10 @@ func (r *RemoveExecutor) removeFromBookmarks(gtsClient *client.Client) error {
func (r *RemoveExecutor) removeStatusFromBookmarks(gtsClient *client.Client) error { func (r *RemoveExecutor) removeStatusFromBookmarks(gtsClient *client.Client) error {
if r.statusID == "" { if r.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "remove",
}
} }
if err := gtsClient.RemoveStatusFromBookmarks(r.statusID); err != nil { if err := gtsClient.RemoveStatusFromBookmarks(r.statusID); err != nil {
@ -139,7 +145,10 @@ func (r *RemoveExecutor) removeStatusFromBookmarks(gtsClient *client.Client) err
func (r *RemoveExecutor) removeFromStatus(gtsClient *client.Client) error { func (r *RemoveExecutor) removeFromStatus(gtsClient *client.Client) error {
if r.statusID == "" { if r.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "remove from",
}
} }
funcMap := map[string]func(*client.Client) error{ funcMap := map[string]func(*client.Client) error{

View file

@ -1,25 +0,0 @@
package executor
const (
resourceAccount = "account"
resourceBlocked = "blocked"
resourceBookmarks = "bookmarks"
resourceBoost = "boost"
resourceFollowers = "followers"
resourceFollowing = "following"
resourceFollowRequest = "follow-request"
resourceInstance = "instance"
resourceLike = "like"
resourceLiked = "liked"
resourceList = "list"
resourceMedia = "media"
resourceMediaAttachment = "media-attachment"
resourceMutedAccounts = "muted-accounts"
resourceNote = "note"
resourcePoll = "poll"
resourceStatus = "status"
resourceStar = "star"
resourceStarred = "starred"
resourceTimeline = "timeline"
resourceVote = "vote"
)

View file

@ -118,7 +118,10 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
func (s *ShowExecutor) showStatus(gtsClient *client.Client) error { func (s *ShowExecutor) showStatus(gtsClient *client.Client) error {
if s.statusID == "" { if s.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "view",
}
} }
status, err := gtsClient.GetStatus(s.statusID) status, err := gtsClient.GetStatus(s.statusID)
@ -157,7 +160,10 @@ func (s *ShowExecutor) showTimeline(gtsClient *client.Client) error {
timeline, err = gtsClient.GetPublicTimeline(s.limit) timeline, err = gtsClient.GetPublicTimeline(s.limit)
case model.TimelineCategoryList: case model.TimelineCategoryList:
if s.listID == "" { if s.listID == "" {
return FlagNotSetError{flagText: flagListID} return MissingIDError{
resource: resourceList,
action: "view the timeline in",
}
} }
var list model.List var list model.List
@ -170,7 +176,7 @@ func (s *ShowExecutor) showTimeline(gtsClient *client.Client) error {
timeline, err = gtsClient.GetListTimeline(list.ID, list.Title, s.limit) timeline, err = gtsClient.GetListTimeline(list.ID, list.Title, s.limit)
case model.TimelineCategoryTag: case model.TimelineCategoryTag:
if s.tag == "" { if s.tag == "" {
return FlagNotSetError{flagText: flagTag} return Error{"please provide the name of the tag"}
} }
timeline, err = gtsClient.GetTagTimeline(s.tag, s.limit) timeline, err = gtsClient.GetTagTimeline(s.tag, s.limit)
@ -410,10 +416,6 @@ func (s *ShowExecutor) showMutedAccounts(gtsClient *client.Client) error {
} }
func (s *ShowExecutor) showMediaAttachment(gtsClient *client.Client) error { func (s *ShowExecutor) showMediaAttachment(gtsClient *client.Client) error {
if len(s.attachmentIDs) == 0 {
return FlagNotSetError{flagText: flagAttachmentID}
}
if len(s.attachmentIDs) != 1 { if len(s.attachmentIDs) != 1 {
return fmt.Errorf( return fmt.Errorf(
"unexpected number of attachment IDs received: want 1, got %d", "unexpected number of attachment IDs received: want 1, got %d",
@ -453,7 +455,10 @@ func (s *ShowExecutor) showMedia(gtsClient *client.Client) error {
func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error { func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error {
if s.statusID == "" { if s.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "view the media from",
}
} }
status, err := gtsClient.GetStatus(s.statusID) status, err := gtsClient.GetStatus(s.statusID)

View file

@ -42,7 +42,10 @@ func (m *UnmuteExecutor) unmuteAccount(gtsClient *client.Client) error {
func (m *UnmuteExecutor) unmuteStatus(gtsClient *client.Client) error { func (m *UnmuteExecutor) unmuteStatus(gtsClient *client.Client) error {
if m.statusID == "" { if m.statusID == "" {
return FlagNotSetError{flagText: flagStatusID} return MissingIDError{
resource: resourceStatus,
action: "unmute",
}
} }
status, err := gtsClient.GetStatus(m.statusID) status, err := gtsClient.GetStatus(m.statusID)

View file

@ -14,17 +14,17 @@ const (
cacheStatusesDir = "statuses" cacheStatusesDir = "statuses"
) )
func CalculateConfigDir(configDir string) string { func CalculateConfigDir(configDir string) (string, error) {
if configDir != "" { if configDir != "" {
return configDir return configDir, nil
} }
configRoot, err := os.UserConfigDir() configRoot, err := os.UserConfigDir()
if err != nil { if err != nil {
return filepath.Join(os.Getenv("HOME"), "."+internal.ApplicationName, "config") return "", fmt.Errorf("unable to get your default config diretory: %w", err)
} }
return filepath.Join(configRoot, internal.ApplicationName) return filepath.Join(configRoot, internal.ApplicationName), nil
} }
func CalculateMediaCacheDir(cacheRoot, instance string) (string, error) { func CalculateMediaCacheDir(cacheRoot, instance string) (string, error) {

View file

@ -7,34 +7,45 @@ import (
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
) )
func TestCalculateMediaCacheDir(t *testing.T) { func TestDirectoryCalculations(t *testing.T) {
t.Parallel() t.Log("Testing the directory calculations")
projectDir, err := projectRoot() projectDir, err := projectRoot()
if err != nil { if err != nil {
t.Fatalf("Unable to get the project root directory: %v", err) t.Fatalf("Unable to get the project root directory: %v", err)
} }
cacheRoot := filepath.Join(projectDir, "test", "cache") t.Setenv("XDG_CACHE_HOME", "/home/enbas/.cache")
instance := "http://gotosocial.yellow-desert.social" t.Setenv("XDG_CONFIG_HOME", "/home/enbas/.config")
got, err := utilities.CalculateMediaCacheDir(cacheRoot, instance) t.Run("Media Cache Directory Calculation", testCalculateMediaCacheDir(projectDir))
if err != nil { t.Run("Media Cache Directory Calculation (with XDG_CACHE_HOME)", testCalculateMediaCacheDirWithXDG)
t.Fatalf("Unable to calculate the media cache directory: %v", err) 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)
}
want := projectDir + "/test/cache/gotosocial.yellow-desert.social/media" 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"
if got != want { got, err := utilities.CalculateMediaCacheDir(cacheRoot, instance)
t.Errorf("Unexpected media cache directory calculated: want %s, got %s", want, got) if err != nil {
} else { t.Fatalf("Unable to calculate the media cache directory: %v", err)
t.Logf("Expected media cache directory calculated: got %s", got) }
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) { func testCalculateMediaCacheDirWithXDG(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", "/home/enbas/.cache")
cacheRoot := "" cacheRoot := ""
instance := "https://gotosocial.yellow-desert.social" instance := "https://gotosocial.yellow-desert.social"
@ -52,27 +63,58 @@ func TestCalculateMediaCacheDirWithXDG(t *testing.T) {
} }
} }
func TestCalculateStatusesCacheDir(t *testing.T) { func testCalculateStatusesCacheDir(projectDir string) func(t *testing.T) {
t.Parallel() return func(t *testing.T) {
cacheRoot := filepath.Join(projectDir, "test", "cache")
instance := "https://fedi.blue-mammoth.party"
projectDir, err := projectRoot() got, err := utilities.CalculateStatusesCacheDir(cacheRoot, instance)
if err != nil { if err != nil {
t.Fatalf("Unable to get the project root directory: %v", err) t.Fatalf("Unable to calculate the statuses cache directory: %v", err)
} }
cacheRoot := filepath.Join(projectDir, "test", "cache") want := projectDir + "/test/cache/fedi.blue-mammoth.party/statuses"
instance := "https://fedi.blue-mammoth.party"
got, err := utilities.CalculateStatusesCacheDir(cacheRoot, instance) if got != want {
if err != nil { t.Errorf("Unexpected statuses cache directory calculated: want %s, got %s", want, got)
t.Fatalf("Unable to calculate the statuses cache directory: %v", err) } else {
} t.Logf("Expected statuses cache directory calculated: got %s", got)
}
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) func testCalculateConfigDir(projectDir string) func(t *testing.T) {
} else { return func(t *testing.T) {
t.Logf("Expected statuses cache directory calculated: got %s", got) 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) { func TestGetFQDN(t *testing.T) {
t.Parallel()
cases := []struct { cases := []struct {
instance string instance string
want string want string