gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/upath/symlink_test.go (about) 1 // Copyright 2015-2017 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package upath 6 7 import ( 8 "io/ioutil" 9 "log" 10 "os" 11 "path/filepath" 12 "testing" 13 ) 14 15 func TestSymlink(t *testing.T) { 16 td, err := ioutil.TempDir("", "testsymlink") 17 if err != nil { 18 t.Fatal(err) 19 } 20 for _, n := range []string{"bin", "buildbin"} { 21 p := filepath.Join(td, n) 22 if err := os.Mkdir(p, 0777); err != nil { 23 log.Fatal(err) 24 } 25 } 26 var tab = []struct { 27 s, t, v string 28 }{ 29 {filepath.Join(td, "bin/ash"), "sh", filepath.Join(td, "buildbin/elvish")}, 30 {filepath.Join(td, "bin/sh"), "../buildbin/elvish", filepath.Join(td, "buildbin/elvish")}, 31 {filepath.Join(td, "buildbin/elvish"), "installcommand", filepath.Join(td, "buildbin/elvish")}, 32 } 33 for _, s := range tab { 34 if err := os.Symlink(s.t, s.s); err != nil { 35 t.Fatalf("symlink %s->%s: got %v, want nil", s.s, s.t, err) 36 } 37 } 38 for _, s := range tab { 39 t.Logf("Check %v", s) 40 v, err := os.Readlink(s.s) 41 t.Logf("Symlink val %v", v) 42 if err != nil || v != s.t { 43 t.Errorf("readlink %v: got (%v, %v), want (%v, nil)", s.s, v, err, s.t) 44 } 45 v = ResolveUntilLastSymlink(s.s) 46 t.Logf("ResolveUntilLastSymlink val %v", v) 47 if v != s.v { 48 t.Errorf("ResolveUntilLastSymlink %v: got %v want %v", s.s, v, s.v) 49 } 50 } 51 // test to make sure a plain file gives a reasonable result. 52 ic := filepath.Join(td, "x") 53 if err := ioutil.WriteFile(ic, nil, 0666); err != nil { 54 t.Fatalf("WriteFile %v: got %v, want nil", ic, err) 55 } 56 v := ResolveUntilLastSymlink(ic) 57 t.Logf("ResolveUntilLastSymlink %v gets %v", ic, v) 58 if v != ic { 59 t.Errorf("ResolveUntilLastSymlink %v: got %v want %v", ic, v, ic) 60 } 61 62 }