github.com/haraldrudell/parl@v0.4.176/pfs/abs-eval_test.go (about) 1 /* 2 © 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package pfs 7 8 import ( 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/haraldrudell/parl/perrors" 14 "github.com/haraldrudell/parl/perrors/errorglue" 15 "github.com/haraldrudell/parl/punix" 16 ) 17 18 func TestAbsEval(t *testing.T) { 19 //t.Fail() 20 var dir = t.TempDir() 21 var symlink = filepath.Join(dir, "symlink") 22 if err := os.Symlink(dir, symlink); err != nil { 23 panic(err) 24 } 25 var symlinkAbsEval = func() (symlinkAbsEval string) { 26 var err error 27 if symlinkAbsEval, err = filepath.Abs(dir); err != nil { 28 panic(err) 29 } else if symlinkAbsEval, err = filepath.EvalSymlinks(dir); err != nil { 30 panic(err) 31 } 32 return 33 }() 34 var wd = func() (wd string) { 35 var err error 36 if wd, err = os.Getwd(); err != nil { 37 panic(err) 38 } 39 return 40 }() 41 var symlinkRel = func() (symlinkRel string) { 42 var err error 43 if symlinkRel, err = filepath.Rel(wd, symlink); err != nil { 44 panic(err) 45 } 46 return 47 }() 48 var empty = "" 49 50 var absPath string 51 var err error 52 53 absPath, err = AbsEval(symlinkRel) 54 55 // absPath "/private/var/folders/sq/0x1_9fyn1bv907s7ypfryt1c0000gn/T/TestAbsEval2996198706/001" 56 t.Logf("absPath %q", absPath) 57 58 // absPath should not have error 59 if err != nil { 60 t.Errorf("AbsEval err %s", perrors.Short(err)) 61 } 62 63 // absPath should retruned evaluated, absolute symlink 64 if absPath != symlinkAbsEval { 65 t.Errorf("AbsPath %q exp %q", absPath, symlinkAbsEval) 66 } 67 68 absPath, err = AbsEval(empty) 69 70 // absPath empty should return wd 71 if err != nil { 72 t.Errorf("AbsEval2 err %s", perrors.Short(err)) 73 } 74 if absPath != wd { 75 t.Errorf("AbsPath2 %q exp %q", absPath, wd) 76 } 77 } 78 79 func TestAbsEvalBadSymLink(t *testing.T) { 80 //t.Fail() 81 var badPath = "/%" 82 var dir = t.TempDir() 83 var symlink = filepath.Join(dir, "symlink") 84 if err := os.Symlink(badPath, symlink); err != nil { 85 panic(err) 86 } 87 88 var absPath string 89 var err error 90 91 // non-existing path 92 absPath, err = AbsEval(symlink) 93 94 // absPath "" 95 t.Logf("absPath %q", absPath) 96 // err: 97 // ‘*errorglue.errorStack *fmt.wrapError *fs.PathError syscall.Errno’ 98 // ‘EvalSymlinks lstat /opt/sw/parl/pfs/%: 99 // no such file or directory at pfs.AbsEval()-abs-eval.go:30’ 100 t.Logf("err: ‘%s’ ‘%s’", errorglue.DumpChain(err), perrors.Short(err)) 101 // is not exist: false 102 t.Logf("is not exist: %t", os.IsNotExist(err)) 103 // errno: ENOENT 2 0x2 104 t.Logf("errno: %s", punix.ErrnoString("", err)) 105 106 if err == nil { 107 t.Error("AbsEval expected error missing") 108 } else if !punix.IsENOENT(err) { 109 t.Errorf("AbsEval bad err %s expected ENOENT", perrors.Short(err)) 110 } 111 } 112 113 func TestAbsEvalBadPath(t *testing.T) { 114 var badPath = "/%" 115 116 var absPath string 117 var err error 118 119 // non-existing path 120 absPath, err = AbsEval(badPath) 121 122 // absPath "" 123 t.Logf("absPath %q", absPath) 124 // err: 125 // ‘*errorglue.errorStack *fmt.wrapError *fs.PathError syscall.Errno’ 126 // ‘EvalSymlinks lstat /opt/sw/parl/pfs/%: 127 // no such file or directory at pfs.AbsEval()-abs-eval.go:30’ 128 t.Logf("err: ‘%s’ ‘%s’", errorglue.DumpChain(err), perrors.Short(err)) 129 // is not exist: false 130 t.Logf("is not exist: %t", os.IsNotExist(err)) 131 // errno: ENOENT 2 0x2 132 t.Logf("errno: %s", punix.ErrnoString("", err)) 133 134 if err == nil { 135 t.Error("AbsEval expected error missing") 136 } else if !punix.IsENOENT(err) { 137 t.Errorf("AbsEval bad err %s expected ENOENT", perrors.Short(err)) 138 } 139 }