From 4b71b28738d06619331ce4471d0d652985e7d134 Mon Sep 17 00:00:00 2001 From: Boyan Soubachov Date: Thu, 12 Dec 2019 07:57:25 +0200 Subject: [PATCH] WIP --- assert/assertions.go | 3 -- assert/assertions_test.go | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 1bb67c1..8747573 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1391,9 +1391,6 @@ func NoFileExists(t TestingT, path string, msgAndArgs ...interface{}) bool { } info, err := os.Lstat(path) if err != nil { - if os.IsNotExist(err) { - return true - } return true } if info.IsDir() { diff --git a/assert/assertions_test.go b/assert/assertions_test.go index ee241b8..ac4dbe5 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "math" "os" "reflect" @@ -1482,6 +1483,63 @@ func TestNoFileExists(t *testing.T) { mockT = new(testing.T) True(t, NoFileExists(mockT, "../_codegen")) + + var tempFiles []string + + file, err := getTempFilePath() + if err != nil { + t.Fatal("could not create temp file, err:", err) + } + tempFiles = append(tempFiles, file) + mockT = new(testing.T) + False(t, NoFileExists(mockT, file)) + + link, err := getTempSymlinkPath(file) + if err != nil { + t.Fatal("could not create temp symlink, err:", err) + } + tempFiles = append(tempFiles, link) + mockT = new(testing.T) + False(t, NoFileExists(mockT, link)) + + // symlink with non-existent target + link, err = getTempSymlinkPath("random_file_which_does_not_exist") + if err != nil { + t.Fatal("could not create temp symlink, err:", err) + } + tempFiles = append(tempFiles, link) + mockT = new(testing.T) + True(t, NoFileExists(mockT, link)) + + errs := cleanUpTempFiles(tempFiles) + if len(errs) > 0 { + t.Fail() + } +} + +func getTempFilePath() (string, error) { + file, err := ioutil.TempFile("", "stretchr_file_test_") + if err != nil { + return "", err + } + return file.Name(), nil +} + +func getTempSymlinkPath(file string) (string, error) { + link := file + "_symlink" + err := os.Symlink(file, link) + return link, err +} + +func cleanUpTempFiles(paths []string) []error { + var res []error + for _, path := range paths { + err := os.Remove(path) + if err != nil { + res = append(res, err) + } + } + return res } func TestDirExists(t *testing.T) {