/
nickolasfox
/
kek
Обзор
Документация
Войти
/
nickolasfox
/
kek
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
pkg/tools/exec_test.go
155 строк
3 KB
Nickolas Fox
Alpha version
29 мар 2024, 20:17
29 мар 2024, 20:17
f14be9d
Код
Авторство
О чём код?
package tools import ( "errors" "reflect" "testing" ) const ( testScriptFile = "testdata/test.sh" ) // WithLookPath overrides lookPath which is "link" to original exec.LookPath. // For test purposes only. func WithLookPath(t testing.TB, newLookPath lookPathFunc) { orig := lookPath lookPath = newLookPath t.Cleanup(func() { lookPath = orig }) } // WithLookPathShort overrides only path return part, excluding passing error back. func WithLookPathShort(t testing.TB, result string) { WithLookPath(t, func(file string) (string, error) { return result, nil }) } // WithGOOS overrides target operating system (for tests purposes only). func WithGOOS(t testing.TB, os string) { orig := goos goos = func() string { return os } t.Cleanup(func() { goos = orig }) } func Test_lookPathWithExt(t *testing.T) { t.Run("ok", func(sub *testing.T) { expected := "/bin/app" WithLookPath(sub, func(file string) (string, error) { return expected, nil }) if result := lookPathWithExt("app", nil); result != expected { sub.Errorf("expected: %v, got: %v", expected, result) } }) t.Run("with-extensions", func(sub *testing.T) { expected := "C:\\bin\\app.exe" WithGOOS(sub, "windows") counter := 0 // it's used to "emulate" a sequential run WithLookPath(sub, func(file string) (string, error) { if counter == 0 { counter++ return "", errors.New("unexpected") } return expected, nil }) if result := lookPathWithExt("app", extensions()); result != expected { sub.Errorf("expected: %v, got: %v", expected, result) } }) } func TestPopExecutable(t *testing.T) { t.Setenv("MSYSTEM", "") for _, test := range []struct { name string in string call func(t testing.TB) expected string }{ { name: "app", in: "app", call: func(t testing.TB) { WithLookPathShort(t, "/bin/app") }, expected: "/bin/app", }, { name: "shell-mingw", in: "bash", call: func(t testing.TB) { t.Setenv("MSYSTEM", MINGW64) t.Setenv("EXEPATH", "C:\\Git\\bin") WithGOOS(t, "windows") }, expected: "C:\\Git\\bin\\bash", }, { name: "shell-default", in: "bash", expected: "bash", }, } { t.Run(test.name, func(sub *testing.T) { if test.call != nil { test.call(sub) } if result := PopExecutable(test.in); result != test.expected { sub.Errorf("expected: %v, got: %v", test.expected, result) } }) } } func TestShellExec(t *testing.T) { // disable detecting mingw in tests running on Windows t.Setenv("MSYSTEM", "") type expected struct { exec string args []string } for _, test := range []struct { name string in string call func(t testing.TB) expected expected errExpected error }{ { name: "ok", in: testScriptFile, expected: expected{"/bin/sh", []string{"-xe"}}, }, { name: "non-existent", in: "non existent", expected: expected{"", nil}, }, } { t.Run(test.name, func(sub *testing.T) { executable, args, err := ShellExec(test.in) result := expected{executable, args} if !reflect.DeepEqual(result, test.expected) { sub.Errorf("\nexp: %#v\ngot: %#v\n", test.expected, result) } // check errors only if they set non-nil in the test if test.errExpected != nil && !errors.Is(err, test.errExpected) { sub.Errorf("expected error: %v, got: %v", test.errExpected, err) } }) } }