70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package util
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
)
|
|
|
|
func TestFileExists(t *testing.T) {
|
|
Convey("FileExists should report existing and missing files", t, func() {
|
|
tmpDir := t.TempDir()
|
|
tmpFile := filepath.Join(tmpDir, "exists.txt")
|
|
|
|
err := os.WriteFile(tmpFile, []byte("ok"), 0o600)
|
|
So(err, ShouldBeNil)
|
|
|
|
So(FileExists(tmpFile), ShouldBeTrue)
|
|
So(FileExists(filepath.Join(tmpDir, "missing.txt")), ShouldBeFalse)
|
|
})
|
|
}
|
|
|
|
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, "")
|
|
})
|
|
}
|
|
|
|
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")
|
|
})
|
|
}
|
|
|
|
func TestGetGlobalConfiguration(t *testing.T) {
|
|
Convey("GetGlobalConfigurationFile should create the expected path", t, func() {
|
|
appName := "myapp"
|
|
fileName := "config.yaml"
|
|
|
|
dir := GetGlobalConfigurationDirectory(appName)
|
|
So(GetGlobalConfigurationFile(appName, fileName), ShouldEqual, filepath.Join(dir, fileName))
|
|
})
|
|
}
|
|
|
|
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})
|
|
})
|
|
}
|