github.com/elves/elvish@v0.15.0/pkg/eval/mods/unix/umask_test.go (about) 1 // +build !windows,!plan9,!js 2 3 package unix 4 5 import ( 6 "testing" 7 8 "github.com/elves/elvish/pkg/eval" 9 "github.com/elves/elvish/pkg/eval/errs" 10 . "github.com/elves/elvish/pkg/eval/evaltest" 11 ) 12 13 // Note that this unit test assumes a UNIX environment with a POSIX compatible 14 // /bin/sh program. 15 func TestUmask(t *testing.T) { 16 setup := func(ev *eval.Evaler) { 17 ev.AddGlobal(eval.NsBuilder{}.AddNs("unix", Ns).Ns()) 18 } 19 TestWithSetup(t, setup, 20 // We have to start with a known umask value. 21 That(`unix:umask = 022`).Puts(), 22 That(`put $unix:umask`).Puts(`0o022`), 23 // Verify that mutating the value and outputting the new value works. 24 That(`unix:umask = 23`).Puts(), 25 That(`put $unix:umask`).Puts(`0o023`), 26 That(`unix:umask = 0o75`).Puts(), 27 That(`put $unix:umask`).Puts(`0o075`), 28 // Verify that a temporary umask change is reverted upon completion of 29 // the command. Both for builtin and external commands. 30 That(`unix:umask=012 put $unix:umask`).Puts(`0o012`), 31 That(`unix:umask=0o23 /bin/sh -c 'umask'`).Prints("0023\n"), 32 That(`unix:umask=56 /bin/sh -c 'umask'`).Prints("0056\n"), 33 That(`put $unix:umask`).Puts(`0o075`), 34 // People won't normally use non-octal bases but make sure these cases 35 // behave sensibly given that Elvish supports number literals with an 36 // explicit base. 37 That(`unix:umask=0x43 /bin/sh -c 'umask'`).Prints("0103\n"), 38 That(`unix:umask=0b001010100 sh -c 'umask'`).Prints("0124\n"), 39 // We should be back to our expected umask given the preceding tests 40 // applied a temporary change to that process attribute. 41 That(`put $unix:umask`).Puts(`0o075`), 42 // An explicit float64 value should be handled correctly. 43 That(`unix:umask=(float64 0o17) put $unix:umask`).Puts(`0o017`), 44 That(`unix:umask=(float64 123.4)`).Throws(errs.BadValue{ 45 What: "umask", Valid: validUmaskMsg, Actual: "123.4"}), 46 47 // An invalid string should raise the expected exception. 48 That(`unix:umask=022z`).Throws(errs.BadValue{ 49 What: "umask", Valid: validUmaskMsg, Actual: "022z"}), 50 51 // An invalid data type should raise the expected exception. 52 That(`unix:umask=[1]`).Throws(errs.BadValue{ 53 What: "umask", Valid: validUmaskMsg, Actual: "[1]"}), 54 55 // Values outside the legal range should raise the expected exception. 56 // 57 // TODO: Switch to `%O` when Go 1.15 is the minimum acceptable version. 58 // Until then the formatting of negative numbers will be weird. 59 That(`unix:umask=0o1000`).Throws(errs.OutOfRange{ 60 What: "umask", ValidLow: "0", ValidHigh: "0o777", Actual: "0o1000"}), 61 62 That(`unix:umask=-1`).Throws(errs.OutOfRange{ 63 What: "umask", ValidLow: "0", ValidHigh: "0o777", Actual: "0o-1"}), 64 ) 65 }