github.com/elves/elvish@v0.15.0/pkg/eval/mods/platform/platform_test.go (about) 1 package platform 2 3 import ( 4 "errors" 5 "runtime" 6 "testing" 7 8 "github.com/elves/elvish/pkg/eval" 9 . "github.com/elves/elvish/pkg/eval/evaltest" 10 ) 11 12 const ( 13 testHostname = "mach1.domain.tld" 14 testMachname = "mach1" 15 ) 16 17 var hostnameFail = true 18 19 func hostnameMock() (string, error) { 20 if hostnameFail { 21 hostnameFail = false 22 return "", errors.New("hostname cannot be determined") 23 } 24 return testHostname, nil 25 } 26 27 func TestPlatform(t *testing.T) { 28 savedOsHostname := osHostname 29 osHostname = hostnameMock 30 defer func() { osHostname = savedOsHostname }() 31 setup := func(ev *eval.Evaler) { 32 ev.AddGlobal(eval.NsBuilder{}.AddNs("platform", Ns).Ns()) 33 } 34 TestWithSetup(t, setup, 35 That(`put $platform:arch`).Puts(runtime.GOARCH), 36 That(`put $platform:os`).Puts(runtime.GOOS), 37 That(`put $platform:is-windows`).Puts(runtime.GOOS == "windows"), 38 That(`put $platform:is-unix`).Puts( 39 // Convert to bool type explicitly, to workaround gccgo bug. 40 // https://github.com/golang/go/issues/40152 41 // TODO(zhsj): remove workaround after gcc 11 is the default in CI. 42 bool(runtime.GOOS != "windows" && runtime.GOOS != "plan9" && runtime.GOOS != "js")), 43 // The first time we invoke the mock it acts as if we can't determine 44 // the hostname. Make sure that is turned into the expected exception. 45 That(`platform:hostname`).Throws( 46 errors.New("hostname cannot be determined")), 47 48 That(`platform:hostname`).Puts(testHostname), 49 That(`platform:hostname &strip-domain`).Puts(testMachname), 50 ) 51 }