File: exists_test.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (87 lines) | stat: -rw-r--r-- 2,504 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package fileutils

import (
	"os"
	"path"
	"runtime"
	"syscall"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestExist(t *testing.T) {
	tempDir := t.TempDir()

	symlinkPath := path.Join(tempDir, "sl-working")
	danglingSymlinkPath := path.Join(tempDir, "sl-broken")

	err := os.Symlink(tempDir, symlinkPath)
	require.NoError(t, err)

	err = os.Symlink("fooobar123", danglingSymlinkPath)
	require.NoError(t, err)

	assertSameError := func(err1, err2 error, description string) {
		assert.Equal(t, err1 == nil, err2 == nil, description+": only one error is set")
		if err1 == nil {
			return
		}

		var pathErr1 *os.PathError
		var pathErr2 *os.PathError
		assert.ErrorAs(t, err1, &pathErr1, description+": wrong error type")
		assert.ErrorAs(t, err2, &pathErr2, description+": wrong error type")
		assert.Equal(t, pathErr1.Path, pathErr1.Path, description+": different file path")

		// on Linux validates that the syscall error is the same
		if runtime.GOOS == "linux" {
			var syscallErr1 syscall.Errno
			var syscallErr2 syscall.Errno
			assert.ErrorAs(t, err1, &syscallErr1, description+": wrong error type")
			assert.ErrorAs(t, err2, &syscallErr2, description+": wrong error type")
			assert.Equal(t, syscallErr1, syscallErr2, description+": same error for existing path (follow=false)")
		}
	}

	err = Lexists(tempDir)
	_, err2 := os.Lstat(tempDir)
	assertSameError(err, err2, "same error for existing path (follow=false)")

	err = Lexists("foo123shouldnotexist")
	_, err2 = os.Lstat("foo123shouldnotexist")
	assertSameError(err, err2, "same error for not existing path (follow=false)")

	err = Lexists(symlinkPath)
	_, err2 = os.Lstat(symlinkPath)
	assertSameError(err, err2, "same error for existing symlink (follow=false)")

	err = Exists(symlinkPath)
	_, err2 = os.Stat(symlinkPath)
	assertSameError(err, err2, "same error for existing symlink (follow=true)")

	err = Lexists(danglingSymlinkPath)
	_, err2 = os.Lstat(danglingSymlinkPath)
	assertSameError(err, err2, "same error for not existing symlink (follow=false)")

	err = Exists(danglingSymlinkPath)
	_, err2 = os.Stat(danglingSymlinkPath)
	assertSameError(err, err2, "same error for not existing symlink (follow=true)")
}

func BenchmarkExists(b *testing.B) {
	tempDir := b.TempDir()
	for range b.N {
		_ = Exists(tempDir)
		_ = Lexists(tempDir)
	}
}

func BenchmarkStat(b *testing.B) {
	tempDir := b.TempDir()
	for range b.N {
		_, _ = os.Stat(tempDir)
		_, _ = os.Lstat(tempDir)
	}
}