SEBRAUC/src/util/util_test.go
Theta-Dev b1b3ab3ff6
All checks were successful
continuous-integration/drone/push Build is passing
add dynamic versioning
2021-11-21 23:29:37 +01:00

110 lines
1.8 KiB
Go

package util
import (
"os"
"path/filepath"
"testing"
"code.thetadev.de/TSGRain/SEBRAUC/src/fixtures"
"github.com/stretchr/testify/assert"
)
func TestDoesFileExist(t *testing.T) {
fixtures.CdProjectRoot()
type args struct {
filepath string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "exist",
args: args{"go.mod"},
want: true,
},
{
name: "not_exist",
args: args{"banana.mod"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := DoesFileExist(tt.args.filepath); got != tt.want {
t.Errorf("DoesFileExist() = %v, want %v", got, tt.want)
}
})
}
}
func TestTmpdir(t *testing.T) {
td, err := GetTmpdir()
if err != nil {
panic(err)
}
tfile := filepath.Join(td, "test.txt")
f, err := os.Create(tfile)
if err != nil {
panic(err)
}
_, err = f.WriteString("Hello")
if err != nil {
panic(err)
}
err = f.Close()
if err != nil {
panic(err)
}
assert.FileExists(t, tfile)
}
func TestCommandFromString(t *testing.T) {
tests := []struct {
name string
cmdString string
expCmd []string
}{
{
name: "single",
cmdString: "ls",
expCmd: []string{"ls"},
},
{
name: "1arg",
cmdString: "echo Hello",
expCmd: []string{"echo", "Hello"},
},
{
name: "3args",
cmdString: "echo Hello its me",
expCmd: []string{"echo", "Hello", "its", "me"},
},
{
name: "empty",
cmdString: "",
expCmd: []string{""},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := CommandFromString(tt.cmdString)
assert.Equal(t, tt.expCmd, cmd.Args)
})
}
}
func TestReboot(t *testing.T) {
testfile := "/tmp/sebrauc_reboot_test"
_ = os.Remove(testfile)
Reboot(0)
assert.FileExists(t, testfile)
}