github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/cmds/mv/mv_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 main 6 7 import ( 8 "fmt" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "testing" 13 ) 14 15 type makeit struct { 16 n string // name 17 m os.FileMode // mode 18 s string // for symlinks or content 19 } 20 21 var tests = []makeit{ 22 { 23 n: "hi1.txt", 24 m: 0666, 25 s: "", 26 }, 27 { 28 n: "hi2.txt", 29 m: 0777, 30 s: "", 31 }, 32 } 33 34 func setup() (string, error) { 35 d, err := ioutil.TempDir(os.TempDir(), "hi.dir") 36 if err != nil { 37 return "", err 38 } 39 40 tmpdir := filepath.Join(d, "hi.sub.dir") 41 if err := os.Mkdir(tmpdir, 0777); err != nil { 42 return "", err 43 } 44 45 for _, t := range tests { 46 if err := ioutil.WriteFile(filepath.Join(d, t.n), []byte("hi"), t.m); err != nil { 47 return "", err 48 } 49 } 50 51 return d, nil 52 } 53 54 func Test_mv_1(t *testing.T) { 55 d, err := setup() 56 if err != nil { 57 t.Fatal("err") 58 } 59 defer os.RemoveAll(d) 60 61 fmt.Println("Renaming file...") 62 files1 := []string{filepath.Join(d, "hi1.txt"), filepath.Join(d, "hi4.txt")} 63 if err := mv(files1, false); err != nil { 64 t.Error(err) 65 } 66 67 dsub := filepath.Join(d, "hi.sub.dir") 68 69 fmt.Println("Moving files to directory...") 70 files2 := []string{filepath.Join(d, "hi2.txt"), filepath.Join(d, "hi4.txt"), dsub} 71 if err := mv(files2, true); err != nil { 72 t.Error(err) 73 } 74 }