From 955c0fd2f9080b808be789f68be5fa37a0bb4778 Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Fri, 17 Jul 2015 10:46:38 -0700 Subject: [PATCH] os: add test to ensure Rename returns *LinkError 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 --- src/os/os_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/os/os_test.go b/src/os/os_test.go index 635842693a..6b726745cf 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -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 { -- 2.50.0