util/utils_test.go

70 lines
2.2 KiB
Go
Raw Normal View History

2022-03-17 09:52:22 +00:00
package util
import (
2022-04-06 08:00:23 +00:00
"os"
2022-04-06 08:17:36 +00:00
"path/filepath"
2022-03-17 09:52:22 +00:00
"testing"
. "github.com/smartystreets/goconvey/convey"
2022-03-17 09:52:22 +00:00
)
func TestFileExists(t *testing.T) {
Convey("FileExists should report existing and missing files", t, func() {
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "exists.txt")
2022-04-06 08:00:23 +00:00
err := os.WriteFile(tmpFile, []byte("ok"), 0o600)
So(err, ShouldBeNil)
2022-03-17 09:52:22 +00:00
So(FileExists(tmpFile), ShouldBeTrue)
So(FileExists(filepath.Join(tmpDir, "missing.txt")), ShouldBeFalse)
})
2022-04-06 08:00:23 +00:00
}
func TestJoiningSlash(t *testing.T) {
Convey("JoiningSlash should combine URL-like segments safely", t, func() {
const (
baseURL = "http://my.tld"
docsURL = "http://my.tld/docs"
expectedRoot = "http://my.tld/bla/blub"
expectedDocs = "http://my.tld/docs/bla/blub"
expectedDocsS = "http://my.tld/docs/bla/blub/"
)
So(JoiningSlash(docsURL+"/", "bla/", "blub/"), ShouldEqual, expectedDocsS)
So(JoiningSlash(baseURL, "bla", "blub"), ShouldEqual, expectedRoot)
So(JoiningSlash(baseURL+"/", "bla", "blub"), ShouldEqual, expectedRoot)
So(JoiningSlash(baseURL, "bla/", "blub"), ShouldEqual, expectedRoot)
So(JoiningSlash(docsURL, "bla/", "blub"), ShouldEqual, expectedDocs)
So(JoiningSlash(docsURL+"/", "bla/", "blub"), ShouldEqual, expectedDocs)
So(JoiningSlash("", "api", "v1"), ShouldEqual, "api/v1")
So(JoiningSlash("", "", ""), ShouldEqual, "")
})
2022-04-06 08:00:23 +00:00
}
func TestSingleJoiningSlash(t *testing.T) {
Convey("singleJoiningSlash should handle slash edge cases", t, func() {
So(singleJoiningSlash("a/", "/b"), ShouldEqual, "a/b")
So(singleJoiningSlash("a", "b"), ShouldEqual, "a/b")
So(singleJoiningSlash("a/", "b"), ShouldEqual, "a/b")
So(singleJoiningSlash("a", "/b"), ShouldEqual, "a/b")
})
2022-03-17 09:52:22 +00:00
}
2022-04-06 07:43:17 +00:00
func TestGetGlobalConfiguration(t *testing.T) {
Convey("GetGlobalConfigurationFile should create the expected path", t, func() {
appName := "myapp"
fileName := "config.yaml"
2022-04-06 08:00:23 +00:00
dir := GetGlobalConfigurationDirectory(appName)
So(GetGlobalConfigurationFile(appName, fileName), ShouldEqual, filepath.Join(dir, fileName))
})
2022-04-06 07:43:17 +00:00
}
2022-04-06 08:17:36 +00:00
func TestIsSuperUser(t *testing.T) {
Convey("IsSuperUser should return a boolean without requiring elevated rights", t, func() {
result := IsSuperUser()
So(result, ShouldBeIn, []bool{true, false})
})
}