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
152 lines
2.8 KiB
Go
152 lines
2.8 KiB
Go
package xexec_test
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/xexec"
|
|
)
|
|
|
|
func TestIsExec(t *testing.T) {
|
|
cases := []struct {
|
|
name, path string
|
|
want bool
|
|
}{
|
|
{
|
|
name: "executable",
|
|
path: "testdata/bin.sh",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "not_executable",
|
|
path: "testdata/nobin",
|
|
want: false,
|
|
},
|
|
}
|
|
for _, tt := range cases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Act
|
|
ok, err := xexec.IsExec(tt.path)
|
|
|
|
// Assert
|
|
require.NoError(t, err)
|
|
require.Equal(t, tt.want, ok)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolveAbsPath(t *testing.T) {
|
|
// Get the absolute path to the testdata directory
|
|
testdata, err := filepath.Abs("testdata")
|
|
require.NoError(t, err)
|
|
|
|
cases := []struct {
|
|
name, path, want string
|
|
env []string
|
|
}{
|
|
{
|
|
name: "relative",
|
|
path: "testdata/bin.sh",
|
|
want: filepath.Join(testdata, "bin.sh"),
|
|
},
|
|
{
|
|
name: "path",
|
|
path: "bin.sh",
|
|
env: []string{"PATH", testdata},
|
|
want: filepath.Join(testdata, "bin.sh"),
|
|
},
|
|
{
|
|
name: "go bin path",
|
|
path: "bin.sh",
|
|
env: []string{"GOBIN", testdata},
|
|
want: filepath.Join(testdata, "bin.sh"),
|
|
},
|
|
}
|
|
for _, tt := range cases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Arrange
|
|
if tt.env != nil {
|
|
t.Setenv(tt.env[0], tt.env[1])
|
|
}
|
|
|
|
// Act
|
|
path, err := xexec.ResolveAbsPath(tt.path)
|
|
|
|
// Assert
|
|
require.NoError(t, err)
|
|
require.Equal(t, tt.want, path)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolveAbsPathError(t *testing.T) {
|
|
// Arrange
|
|
fileName := "invalid-file.ko"
|
|
|
|
// Act
|
|
_, err := xexec.ResolveAbsPath(fileName)
|
|
|
|
// Assert
|
|
require.Errorf(t, err, `exec: "%s": executable file not found in $PATH`, fileName)
|
|
}
|
|
|
|
func TestTryResolveAbsPath(t *testing.T) {
|
|
// Get the absolute path to the testdata directory
|
|
testdata, err := filepath.Abs("testdata")
|
|
require.NoError(t, err)
|
|
|
|
cases := []struct {
|
|
name, path, want string
|
|
env []string
|
|
}{
|
|
{
|
|
name: "valid file",
|
|
path: "testdata/bin.sh",
|
|
want: filepath.Join(testdata, "bin.sh"),
|
|
},
|
|
{
|
|
name: "invalid file",
|
|
path: "invalid-file.ko",
|
|
want: "invalid-file.ko",
|
|
},
|
|
}
|
|
for _, tt := range cases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Act
|
|
path := xexec.TryResolveAbsPath(tt.path)
|
|
|
|
// Assert
|
|
require.NoError(t, err)
|
|
require.Equal(t, tt.want, path)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsCommandAvailable(t *testing.T) {
|
|
cases := []struct {
|
|
name, path string
|
|
want bool
|
|
}{
|
|
{
|
|
name: "available",
|
|
path: "testdata/bin.sh",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "not_available",
|
|
path: "invalid-file.ko",
|
|
want: false,
|
|
},
|
|
}
|
|
for _, tt := range cases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Act
|
|
ok := xexec.IsCommandAvailable(tt.path)
|
|
|
|
// Assert
|
|
require.Equal(t, tt.want, ok)
|
|
})
|
|
}
|
|
}
|