github.com/naohirotamura/u-root@v7.0.0+incompatible/cmds/core/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  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/testutil"
    15  )
    16  
    17  var truncateTests = []struct {
    18  	flags           []string
    19  	ret             int   // -1 for an expected error
    20  	genFile         bool  // if set, a temporary file will be created before the test (used for -c)
    21  	fileExistsAfter bool  // if set, we expect that the file will exist after the test
    22  	size            int64 // -1 to signal we don't care for size test, early continue
    23  	initSize        int64 // only used when genFile is true
    24  }{
    25  	{
    26  		// Without args
    27  		flags: []string{},
    28  		ret:   -1,
    29  	}, {
    30  		// Invalid, valid args, but -s is missing
    31  		flags: []string{"-c"},
    32  		ret:   -1,
    33  	}, {
    34  		// Invalid, invalid flag
    35  		flags: []string{"-x"},
    36  		ret:   -1,
    37  	}, {
    38  		// Valid, file does not exist
    39  		flags:           []string{"-s", "0"},
    40  		ret:             0,
    41  		genFile:         false,
    42  		fileExistsAfter: true,
    43  		size:            0,
    44  	}, {
    45  		// Valid, file does exist and is smaller
    46  		flags:           []string{"-s", "1"},
    47  		ret:             0,
    48  		genFile:         true,
    49  		fileExistsAfter: true,
    50  		initSize:        0,
    51  		size:            1,
    52  	}, {
    53  		// Valid, file does exist and is bigger
    54  		flags:           []string{"-s", "1"},
    55  		ret:             0,
    56  		genFile:         true,
    57  		fileExistsAfter: true,
    58  		initSize:        2,
    59  		size:            1,
    60  	}, {
    61  		// Valid, file does exist grow
    62  		flags:           []string{"-s", "+3K"},
    63  		ret:             0,
    64  		genFile:         true,
    65  		fileExistsAfter: true,
    66  		initSize:        2,
    67  		size:            2 + 3*1024,
    68  	}, {
    69  		// Valid, file does exist shrink
    70  		flags:           []string{"-s", "-3"},
    71  		ret:             0,
    72  		genFile:         true,
    73  		fileExistsAfter: true,
    74  		initSize:        5,
    75  		size:            2,
    76  	}, {
    77  		// Valid, file does exist shrink lower than 0
    78  		flags:           []string{"-s", "-3M"},
    79  		ret:             0,
    80  		genFile:         true,
    81  		fileExistsAfter: true,
    82  		initSize:        2,
    83  		size:            0,
    84  	}, {
    85  		// Weird GNU behavior that this actual error is ignored
    86  		flags:           []string{"-c", "-s", "2"},
    87  		ret:             0,
    88  		genFile:         false,
    89  		fileExistsAfter: false,
    90  		size:            -1,
    91  	}, {
    92  		// Existing one
    93  		flags:           []string{"-c", "-s", "3"},
    94  		ret:             0,
    95  		genFile:         true,
    96  		fileExistsAfter: true,
    97  		initSize:        0,
    98  		size:            3,
    99  	},
   100  }
   101  
   102  // TestTruncate implements a table-driven test.
   103  func TestTruncate(t *testing.T) {
   104  	tmpDir, err := ioutil.TempDir("", "truncate")
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	defer os.RemoveAll(tmpDir)
   109  
   110  	for i, test := range truncateTests {
   111  		testfile := filepath.Join(tmpDir, fmt.Sprintf("txt%d", i))
   112  		if test.genFile {
   113  			data := make([]byte, test.initSize)
   114  			if err := ioutil.WriteFile(testfile, data, 0600); err != nil {
   115  				t.Errorf("Failed to create test file %s: %v", testfile, err)
   116  				continue
   117  			}
   118  		}
   119  		// Execute truncate.go
   120  		cmd := testutil.Command(t, append(test.flags, testfile)...)
   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  }
   142  
   143  func TestMain(m *testing.M) {
   144  	testutil.Run(m, main)
   145  }