github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/renameio/umask_test.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !plan9,!windows,!js
     6  
     7  package renameio
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"syscall"
    14  	"testing"
    15  )
    16  
    17  func TestWriteFileModeAppliesUmask(t *testing.T) {
    18  	dir, err := ioutil.TempDir("", "renameio")
    19  	if err != nil {
    20  		t.Fatalf("Failed to create temporary directory: %v", err)
    21  	}
    22  	defer os.RemoveAll(dir)
    23  
    24  	const mode = 0644
    25  	const umask = 0007
    26  	defer syscall.Umask(syscall.Umask(umask))
    27  
    28  	file := filepath.Join(dir, "testWrite")
    29  	err = WriteFile(file, []byte("go-build"), mode)
    30  	if err != nil {
    31  		t.Fatalf("Failed to write file: %v", err)
    32  	}
    33  
    34  	fi, err := os.Stat(file)
    35  	if err != nil {
    36  		t.Fatalf("Stat %q (looking for mode %#o): %s", file, mode, err)
    37  	}
    38  
    39  	if fi.Mode()&os.ModePerm != 0640 {
    40  		t.Errorf("Stat %q: mode %#o want %#o", file, fi.Mode()&os.ModePerm, 0640)
    41  	}
    42  }