umask_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //go:build !windows && !plan9 && !js
  2. package unix
  3. import (
  4. "testing"
  5. "src.elv.sh/pkg/eval/errs"
  6. "src.elv.sh/pkg/eval/evaltest"
  7. )
  8. // Note that this unit test assumes a UNIX environment with a POSIX compatible
  9. // /bin/sh program.
  10. func TestUmask(t *testing.T) {
  11. evaltest.TestWithSetup(t, useUNIX,
  12. // We have to start with a known umask value.
  13. That(`set unix:umask = 022`).Puts(),
  14. That(`put $unix:umask`).Puts(`0o022`),
  15. // Verify that mutating the value and outputting the new value works.
  16. That(`set unix:umask = 23`).Puts(),
  17. That(`put $unix:umask`).Puts(`0o023`),
  18. That(`set unix:umask = 0o75`).Puts(),
  19. That(`put $unix:umask`).Puts(`0o075`),
  20. // Verify that a temporary umask change is reverted upon completion of
  21. // the command. Both for builtin and external commands.
  22. That(`{ tmp unix:umask = 012; put $unix:umask }`).Puts(`0o012`),
  23. That(`{ tmp unix:umask = 0o23; /bin/sh -c 'umask' }`).Prints("0023\n"),
  24. That(`{ tmp unix:umask = 56; /bin/sh -c 'umask' }`).Prints("0056\n"),
  25. That(`put $unix:umask`).Puts(`0o075`),
  26. // People won't normally use non-octal bases but make sure these cases
  27. // behave sensibly given that Elvish supports number literals with an
  28. // explicit base.
  29. That(`{ tmp unix:umask = 0x43; /bin/sh -c 'umask' }`).Prints("0103\n"),
  30. That(`{ tmp unix:umask = 0b001010100; sh -c 'umask' }`).Prints("0124\n"),
  31. // We should be back to our expected umask given the preceding tests
  32. // applied a temporary change to that process attribute.
  33. That(`put $unix:umask`).Puts(`0o075`),
  34. // An explicit num (int) value is handled correctly.
  35. That(`{ tmp unix:umask = (num 0o123); put $unix:umask }`).Puts(`0o123`),
  36. // An explicit float64 value is handled correctly.
  37. That(`{ tmp unix:umask = (float64 0o17); put $unix:umask }`).Puts(`0o017`),
  38. That(`set unix:umask = (float64 123.4)`).Throws(
  39. errs.BadValue{What: "umask", Valid: validUmaskMsg, Actual: "123.4"}),
  40. // An invalid string should raise the expected exception.
  41. That(`set unix:umask = 022z`).Throws(errs.BadValue{
  42. What: "umask", Valid: validUmaskMsg, Actual: "022z"}),
  43. // An invalid data type should raise the expected exception.
  44. That(`set unix:umask = [1]`).Throws(errs.BadValue{
  45. What: "umask", Valid: validUmaskMsg, Actual: "list"}),
  46. // Values outside the legal range should raise the expected exception.
  47. That(`set unix:umask = 0o1000`).Throws(errs.OutOfRange{
  48. What: "umask", ValidLow: "0", ValidHigh: "0o777", Actual: "0o1000"}),
  49. That(`set unix:umask = -1`).Throws(errs.OutOfRange{
  50. What: "umask", ValidLow: "0", ValidHigh: "0o777", Actual: "-0o1"}),
  51. )
  52. }