cmd/gofmt: don't call Chmod on windows

Fixes #18026

Change-Id: Id510f427ceffb2441c3d6f5bb5c93244e46c6497
Reviewed-on: https://go-review.googlesource.com/33477
TryBot-Result: Gobot Gobot <gobot@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
This commit is contained in:
Brad Fitzpatrick 2016-11-23 01:43:33 +00:00
parent 8ace3461a4
commit 75c1381176
2 changed files with 23 additions and 5 deletions

View File

@ -18,6 +18,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
)
@ -252,6 +253,8 @@ func diff(b1, b2 []byte) (data []byte, err error) {
}
const chmodSupported = runtime.GOOS != "windows"
// backupFile writes data to a new file named filename<number> with permissions perm,
// with <number randomly chosen such that the file name is unique. backupFile returns
// the chosen file name.
@ -262,11 +265,13 @@ func backupFile(filename string, data []byte, perm os.FileMode) (string, error)
return "", err
}
bakname := f.Name()
err = f.Chmod(perm)
if err != nil {
f.Close()
os.Remove(bakname)
return bakname, err
if chmodSupported {
err = f.Chmod(perm)
if err != nil {
f.Close()
os.Remove(bakname)
return bakname, err
}
}
// write data to backup file

View File

@ -171,3 +171,16 @@ func TestCRLF(t *testing.T) {
t.Errorf("%s contains CR's", golden)
}
}
func TestBackupFile(t *testing.T) {
dir, err := ioutil.TempDir("", "gofmt_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
name, err := backupFile(filepath.Join(dir, "foo.go"), []byte(" package main"), 0644)
if err != nil {
t.Fatal(err)
}
t.Logf("Created: %s", name)
}