Skip to content

Commit 65ed829

Browse files
committed
Added some tests
1 parent 6390d83 commit 65ed829

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

location/location.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ type SLocation struct {
2121
paths *Paths
2222
}
2323

24-
//const perm os.FileMode = os.ModePerm
25-
26-
// For mocking
27-
/*var (
28-
osGetwd = os.Getwd
29-
filepathAbs = filepath.Abs
30-
filepathJoin = filepath.Join
31-
fileExists = file.Exists
32-
osMkdirAll = os.MkdirAll
33-
osMkdirTemp = os.MkdirTemp
34-
)*/
35-
3624
var (
3725
xdgCacheFile = xdg.CacheFile
3826
xdgConfigFile = xdg.ConfigFile
@@ -42,6 +30,7 @@ var (
4230

4331
func (service *SLocation) setAll() error {
4432
service.paths.ThisApplicationPaths.Name = service.appName
33+
service.paths.ThisApplicationPaths.PluginTypeNames = service.pluginTypeNames
4534

4635
relFilePath := fmt.Sprintf("%s/%s", service.appName, service.appName)
4736

@@ -154,7 +143,8 @@ func (service *SLocation) setConfigPaths(relFilePath string) error {
154143
if err != nil {
155144
return errors.Join(fmt.Errorf(`xdg.ConfigFile was unable to set "%s" path`, configFile), err)
156145
}
157-
service.paths.ThisApplicationPaths.Paths.ConfigFile = filepath.Dir(configFile)
146+
service.paths.ThisApplicationPaths.Paths.ConfigFile = configFile
147+
service.paths.ThisApplicationPaths.Paths.ConfigHome = filepath.Dir(configFile)
158148

159149
return nil
160150
}

location/service.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ package location
88
// - ♻️ appVersion - The version of the application.
99
// - 💊 pluginTypeName - The plugin type names (e.g.: HERY => entity, doorman => clerk).
1010
func NewLocationService(appName AppName, appVersion AppVersion, pluginTypeNames PluginTypeNames) (ILocation, error) {
11+
thisPaths := &ApplicationPaths{
12+
PluginsHome: map[string]string{},
13+
}
14+
15+
paths := &Paths{
16+
SystemPaths: &SystemPaths{},
17+
ThisApplicationPaths: &Application{
18+
Paths: thisPaths,
19+
},
20+
Applications: &[]Application{},
21+
}
22+
1123
serviceLocation := &SLocation{
1224
appName: appName,
1325
appVersion: appVersion,
1426
pluginTypeNames: pluginTypeNames,
15-
paths: &Paths{
16-
SystemPaths: &SystemPaths{},
17-
ThisApplicationPaths: &Application{
18-
Paths: &ApplicationPaths{},
19-
},
20-
Applications: &[]Application{},
21-
},
27+
paths: paths,
2228
}
2329

2430
// Set

location/service_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package location
22

33
import (
4+
"fmt"
45
"github.com/stretchr/testify/assert"
6+
"os/user"
57
"testing"
68
)
79

@@ -11,5 +13,27 @@ func TestNewLocationService(t *testing.T) {
1113
assert.NoError(t, err)
1214
assert.NotNil(t, service)
1315
assert.IsType(t, &SLocation{}, service)
16+
17+
currentUser, err := user.Current()
18+
if err != nil {
19+
t.Fatalf("Error getting current user: %v", err)
20+
}
21+
22+
homeDir := fmt.Sprintf("/home/%s", currentUser.Username)
23+
24+
assert.Equal(t, homeDir+"/.local/share/mockname", service.ThisAppPaths().DataHome)
25+
assert.Equal(t, homeDir+"/.config/mockname", service.ThisAppPaths().ConfigHome)
26+
assert.Equal(t, homeDir+"/.local/state/mockname", service.ThisAppPaths().StateHome)
27+
assert.Equal(t, homeDir+"/.cache/mockname", service.ThisAppPaths().CacheHome)
28+
assert.Equal(t, "/run/user/1000/mockname", service.ThisAppPaths().RuntimeDir)
29+
assert.Equal(t, homeDir+"/.local/bin/mockname", service.ThisAppPaths().BinFile)
30+
assert.Equal(t, homeDir+"/.config/mockname/mockname.yaml", service.ThisAppPaths().ConfigFile)
31+
assert.Equal(t, homeDir+"/.local/share/nix.d", service.ThisAppPaths().PluginsHome["nix"])
32+
assert.Equal(t, homeDir+"/.cache/mockname/mockname.cache", service.ThisAppPaths().CacheFile)
33+
assert.Equal(t,
34+
homeDir+"/.local/share/applications/mockname.desktop",
35+
service.ThisAppPaths().ApplicationsDesktopFile)
36+
assert.Equal(t, "/run/user/1000/mockname/secrets", service.ThisAppPaths().SecretsHome)
37+
assert.Equal(t, "/run/user/1000/mockname/secrets/mTLS", service.ThisAppPaths().MTLSHome)
1438
})
1539
}

0 commit comments

Comments
 (0)