github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/cmds/truncate/truncate_test.go (about) 1 // Copyright 2016-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 "os/exec" 12 "path/filepath" 13 "testing" 14 15 "github.com/u-root/u-root/pkg/testutil" 16 ) 17 18 var truncateTests = []struct { 19 flags []string 20 ret int // -1 for an expected error 21 genFile bool // if set, a temporary file will be created before the test (used for -c) 22 fileExistsAfter bool // if set, we expect that the file will exist after the test 23 size int64 // -1 to signal we don't care for size test, early continue 24 initSize int64 // only used when genFile is true 25 }{ 26 { 27 // Without args 28 flags: []string{}, 29 ret: -1, 30 }, { 31 // Invalid, valid args, but -s is missing 32 flags: []string{"-c"}, 33 ret: -1, 34 }, { 35 // Invalid, invalid flag 36 flags: []string{"-x"}, 37 ret: -1, 38 }, { 39 // Valid, file does not exist 40 flags: []string{"-s", "0"}, 41 ret: 0, 42 genFile: false, 43 fileExistsAfter: true, 44 size: 0, 45 }, { 46 // Valid, file does exist and is smaller 47 flags: []string{"-s", "1"}, 48 ret: 0, 49 genFile: true, 50 fileExistsAfter: true, 51 initSize: 0, 52 size: 1, 53 }, { 54 // Valid, file does exist and is bigger 55 flags: []string{"-s", "1"}, 56 ret: 0, 57 genFile: true, 58 fileExistsAfter: true, 59 initSize: 2, 60 size: 1, 61 }, { 62 // Valid, file does exist grow 63 flags: []string{"-s", "+3K"}, 64 ret: 0, 65 genFile: true, 66 fileExistsAfter: true, 67 initSize: 2, 68 size: 2 + 3*1024, 69 }, { 70 // Valid, file does exist shrink 71 flags: []string{"-s", "-3"}, 72 ret: 0, 73 genFile: true, 74 fileExistsAfter: true, 75 initSize: 5, 76 size: 2, 77 }, { 78 // Valid, file does exist shrink lower than 0 79 flags: []string{"-s", "-3M"}, 80 ret: 0, 81 genFile: true, 82 fileExistsAfter: true, 83 initSize: 2, 84 size: 0, 85 }, { 86 // Weird GNU behavior that this actual error is ignored 87 flags: []string{"-c", "-s", "2"}, 88 ret: 0, 89 genFile: false, 90 fileExistsAfter: false, 91 size: -1, 92 }, { 93 // Existing one 94 flags: []string{"-c", "-s", "3"}, 95 ret: 0, 96 genFile: true, 97 fileExistsAfter: true, 98 initSize: 0, 99 size: 3, 100 }, 101 } 102 103 // TestTruncate implements a table-driven test. 104 func TestTruncate(t *testing.T) { 105 // Compile truncate. 106 tmpDir, truncatePath := testutil.CompileInTempDir(t) 107 defer os.RemoveAll(tmpDir) 108 109 for i, test := range truncateTests { 110 testfile := filepath.Join(tmpDir, fmt.Sprintf("txt%d", i)) 111 if test.genFile { 112 data := make([]byte, test.initSize) 113 if err := ioutil.WriteFile(testfile, data, 0600); err != nil { 114 t.Errorf("Failed to create test file %s: %v", testfile, err) 115 continue 116 } 117 } 118 // Execute truncate.go 119 args := append(append([]string{}, test.flags...), testfile) 120 cmd := exec.Command(truncatePath, args...) 121 err := cmd.Run() 122 if err != nil { 123 if test.ret == 0 { 124 t.Fatalf("Truncate exited with error: %v, but return code %d expected\n", err, test.ret) 125 } else if test.ret == -1 { // expected error, nothing more to see 126 continue 127 } 128 t.Fatalf("Truncate exited with error: %v, test specified: %d, something is terribly wrong\n", err, test.ret) 129 } 130 if test.size == -1 { 131 continue 132 } 133 st, err := os.Stat(testfile) 134 if err != nil && test.fileExistsAfter { 135 t.Fatalf("Expected %s to exist, but os.Stat() retuned error: %v\n", testfile, err) 136 } 137 if s := st.Size(); s != test.size { 138 t.Fatalf("Expected that %s has size: %d, but it has size: %d\n", testfile, test.size, s) 139 } 140 } 141 }