diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-03-26 16:50:58 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-03-26 16:50:58 +0200 |
commit | 519725bb3c075ee2462c929f5997cb068e18466a (patch) | |
tree | 5b162e8488ad147a645048c073577821b4a2bee9 /src/pkg/os/error_test.go | |
parent | 842623c5dd2819d980ca9c58048d6bc6ed82475f (diff) | |
download | golang-upstream-weekly/2012.03.22.tar.gz |
Imported Upstream version 2012.03.22upstream-weekly/2012.03.22
Diffstat (limited to 'src/pkg/os/error_test.go')
-rw-r--r-- | src/pkg/os/error_test.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/pkg/os/error_test.go b/src/pkg/os/error_test.go new file mode 100644 index 000000000..42f846fa3 --- /dev/null +++ b/src/pkg/os/error_test.go @@ -0,0 +1,81 @@ +// 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 ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +func TestErrIsExist(t *testing.T) { + f, err := ioutil.TempFile("", "_Go_ErrIsExist") + if err != nil { + t.Fatalf("open ErrIsExist tempfile: %s", err) + return + } + defer os.Remove(f.Name()) + defer f.Close() + f2, err := os.OpenFile(f.Name(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) + if err == nil { + f2.Close() + t.Fatal("Open should have failed") + return + } + if s := checkErrorPredicate("os.IsExist", os.IsExist, err); s != "" { + t.Fatal(s) + return + } +} + +func testErrNotExist(name string) string { + f, err := os.Open(name) + if err == nil { + f.Close() + return "Open should have failed" + } + if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" { + return s + } + + err = os.Chdir(name) + if err == nil { + return "Chdir should have failed" + } + if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" { + return s + } + return "" +} + +func TestErrIsNotExist(t *testing.T) { + tmpDir, err := ioutil.TempDir("", "_Go_ErrIsNotExist") + if err != nil { + t.Fatalf("create ErrIsNotExist tempdir: %s", err) + return + } + defer os.RemoveAll(tmpDir) + + name := filepath.Join(tmpDir, "NotExists") + if s := testErrNotExist(name); s != "" { + t.Fatal(s) + return + } + + name = filepath.Join(name, "NotExists2") + if s := testErrNotExist(name); s != "" { + t.Fatal(s) + return + } +} + +func checkErrorPredicate(predName string, pred func(error) bool, err error) string { + if !pred(err) { + return fmt.Sprintf("%s does not work as expected for %#v", predName, err) + } + return "" +} |