1
0
mirror of https://github.com/golang/go synced 2024-11-15 02:50:31 -07:00

[release-branch.go1] os: Rename error to fit IsExist

««« backport 104eb57df01b
os: Rename error to fit IsExist

Fixes #3828.

R=golang-dev, iant, rsc
CC=golang-dev
https://golang.org/cl/6420056

»»»
This commit is contained in:
Alex Brainman 2012-09-22 05:54:23 +10:00
parent bedafd2ddb
commit 6410538c72
2 changed files with 50 additions and 0 deletions

View File

@ -10,6 +10,9 @@ func isExist(err error) bool {
if pe, ok := err.(*PathError); ok {
err = pe.Err
}
if pe, ok := err.(*LinkError); ok {
err = pe.Err
}
return err == syscall.ERROR_ALREADY_EXISTS ||
err == syscall.ERROR_FILE_EXISTS || err == ErrExist
}

View File

@ -0,0 +1,47 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestErrIsExistAfterRename(t *testing.T) {
dir, err := ioutil.TempDir("", "go-build")
if err != nil {
t.Fatalf("Create temp directory: %v", err)
}
defer os.RemoveAll(dir)
src := filepath.Join(dir, "src")
dest := filepath.Join(dir, "dest")
f, err := os.Create(src)
if err != nil {
t.Fatalf("Create file %v: %v", src, err)
}
f.Close()
err = os.Rename(src, dest)
if err != nil {
t.Fatalf("Rename %v to %v: %v", src, dest, err)
}
f, err = os.Create(src)
if err != nil {
t.Fatalf("Create file %v: %v", src, err)
}
f.Close()
err = os.Rename(src, dest)
if err == nil {
t.Fatal("Rename should have failed")
}
if s := checkErrorPredicate("os.IsExist", os.IsExist, err); s != "" {
t.Fatal(s)
return
}
}