mukan-ignite/ignite/pkg/xfilepath/xfilepath_test.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

237 lines
4.6 KiB
Go

package xfilepath_test
import (
"os"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/errors"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/xfilepath"
)
func TestJoin(t *testing.T) {
retriever := xfilepath.Join(
xfilepath.Path("foo"),
xfilepath.PathWithError("bar", nil),
xfilepath.Path("foobar/barfoo"),
)
p, err := retriever()
require.NoError(t, err)
require.Equal(t, filepath.Join(
"foo",
"bar",
"foobar",
"barfoo",
), p)
retriever = xfilepath.Join(
xfilepath.Path("foo"),
xfilepath.PathWithError("bar", errors.New("foo")),
xfilepath.Path("foobar/barfoo"),
)
_, err = retriever()
require.Error(t, err)
}
func TestJoinFromHome(t *testing.T) {
home, err := os.UserHomeDir()
require.NoError(t, err)
retriever := xfilepath.JoinFromHome(
xfilepath.Path("foo"),
xfilepath.PathWithError("bar", nil),
xfilepath.Path("foobar/barfoo"),
)
p, err := retriever()
require.NoError(t, err)
require.Equal(t, filepath.Join(
home,
"foo",
"bar",
"foobar",
"barfoo",
), p)
retriever = xfilepath.JoinFromHome(
xfilepath.Path("foo"),
xfilepath.PathWithError("bar", errors.New("foo")),
xfilepath.Path("foobar/barfoo"),
)
_, err = retriever()
require.Error(t, err)
}
func TestList(t *testing.T) {
retriever := xfilepath.List()
list, err := retriever()
require.NoError(t, err)
require.Equal(t, []string(nil), list)
retriever1 := xfilepath.Join(
xfilepath.Path("foo/bar"),
)
retriever2 := xfilepath.Join(
xfilepath.Path("bar/foo"),
)
retriever = xfilepath.List(retriever1, retriever2)
list, err = retriever()
require.NoError(t, err)
require.Equal(t, []string{
filepath.Join("foo", "bar"),
filepath.Join("bar", "foo"),
}, list)
retrieverError := xfilepath.PathWithError("foo", errors.New("foo"))
retriever = xfilepath.List(retriever1, retrieverError, retriever2)
_, err = retriever()
require.Error(t, err)
}
func TestMkdir(t *testing.T) {
newdir := path.Join(t.TempDir(), "hey")
dir, err := xfilepath.Mkdir(xfilepath.Path(newdir))()
require.NoError(t, err)
require.Equal(t, newdir, dir)
require.DirExists(t, dir)
}
func TestRelativePath(t *testing.T) {
pwd, err := os.Getwd()
require.NoError(t, err)
rootRelative, err := filepath.Rel(pwd, "/")
require.NoError(t, err)
tests := []struct {
name string
appPath string
want string
err error
}{
{
name: "same directory",
appPath: filepath.Join(pwd, "file.go"),
want: "file.go",
},
{
name: "previous directory",
appPath: filepath.Join(filepath.Dir(pwd), "file.go"),
want: "../file.go",
},
{
name: "root directory",
appPath: "/file.go",
want: filepath.Join(rootRelative, "file.go"),
},
{
name: "absolute path",
appPath: pwd,
want: ".",
},
{
name: "NonExistentPath",
appPath: filepath.Join(filepath.Base(pwd), "file.go"),
want: "",
err: errors.Errorf("Rel: can't make xfilepath/file.go relative to %s", pwd),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := xfilepath.RelativePath(tt.appPath)
if tt.err != nil {
require.Error(t, err)
require.Equal(t, tt.err.Error(), err.Error())
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}
func TestIsDir(t *testing.T) {
tests := []struct {
name string
path string
want bool
}{
{
name: "existing directory",
path: ".",
want: true,
},
{
name: "existing sub directory",
path: "./testdata",
want: true,
},
{
name: "existing file",
path: "./testdata/testfile",
want: false,
},
{
name: "non-existing directory",
path: "nonexistent",
want: false,
},
{
name: "parent directory",
path: "..",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := xfilepath.IsDir(tt.path)
require.Equal(t, tt.want, got)
})
}
}
func TestMustAbs(t *testing.T) {
pwd, err := os.Getwd()
require.NoError(t, err)
tests := []struct {
name string
path string
want string
err error
}{
{
name: "already absolute path",
path: "/absolute/path",
want: "/absolute/path",
err: nil,
},
{
name: "relative path",
path: "relative/path",
want: filepath.Join(pwd, "relative/path"),
err: nil,
},
{
name: "current directory",
path: ".",
want: pwd,
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := xfilepath.MustAbs(tt.path)
if tt.err != nil {
require.Error(t, err)
require.Equal(t, tt.err.Error(), err.Error())
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}