github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/mods/unix/umask_test.go (about)

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