]> Cypherpunks repositories - gostls13.git/commitdiff
os: add test to ensure Rename returns *LinkError
authorDave Cheney <dave@cheney.net>
Fri, 17 Jul 2015 17:46:38 +0000 (10:46 -0700)
committerDave Cheney <dave@cheney.net>
Fri, 17 Jul 2015 22:46:05 +0000 (22:46 +0000)
Updates #10061

CL 12353 updated the documentation for os.Rename to stipulate the function will
return errors of type *os.LinkError. This CL adds a test to ensure that the
implementations continue to obey this contract.

Change-Id: I41beb8c9d8356c737de251fdc6f652caab3ee636
Reviewed-on: https://go-review.googlesource.com/12329
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/os/os_test.go

index 635842693a991689b2a2f538041be7f5a63a2123..6b726745cfd8a05a0d799afebf0b526ec911f776 100644 (file)
@@ -768,6 +768,35 @@ func TestRenameOverwriteDest(t *testing.T) {
        }
 }
 
+func TestRenameFailed(t *testing.T) {
+       defer chtmpdir(t)()
+       from, to := "renamefrom", "renameto"
+       // Ensure we are not testing the overwrite case here.
+       Remove(from)
+       Remove(to)
+
+       err := Rename(from, to)
+       switch err := err.(type) {
+       case *LinkError:
+               if err.Op != "rename" {
+                       t.Errorf("rename %q, %q: err.Op: want %q, got %q", from, to, "rename", err.Op)
+               }
+               if err.Old != from {
+                       t.Errorf("rename %q, %q: err.Old: want %q, got %q", from, to, from, err.Old)
+               }
+               if err.New != to {
+                       t.Errorf("rename %q, %q: err.New: want %q, got %q", from, to, to, err.New)
+               }
+       case nil:
+               t.Errorf("rename %q, %q: expected error, got nil", from, to)
+
+               // cleanup whatever was placed in "renameto"
+               Remove(to)
+       default:
+               t.Errorf("rename %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
+       }
+}
+
 func exec(t *testing.T, dir, cmd string, args []string, expect string) {
        r, w, err := Pipe()
        if err != nil {