github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/rck/unit"
    13  )
    14  
    15  func TestTruncate(t *testing.T) {
    16  	tmpdir := t.TempDir()
    17  	for _, tt := range []struct {
    18  		name     string
    19  		args     []string
    20  		create   bool
    21  		size     unit.Value
    22  		sizeWant int64
    23  		rfile    string
    24  		want     string
    25  	}{
    26  		{
    27  			name: "!size.IsSet && *rfile == \"\"",
    28  			want: "you need to specify size via -s <number> or -r <rfile>",
    29  		},
    30  		{
    31  			name: "size.IsSet && *rfile == \"\"",
    32  			size: unit.Value{
    33  				IsSet: true,
    34  			},
    35  			rfile: "testfile",
    36  			want:  "you need to specify size via -s <number> or -r <rfile>",
    37  		},
    38  		{
    39  			name:  "len(args) == 0",
    40  			rfile: "testfile",
    41  			want:  "you need to specify one or more files as argument",
    42  		},
    43  		{
    44  			name:   "create non existing file with err in rfile path",
    45  			create: false,
    46  			rfile:  "testfile",
    47  			args:   []string{filepath.Join(tmpdir, "file1")},
    48  			want:   "could not stat reference file: stat testfile: no such file or directory",
    49  		},
    50  		{
    51  			name:   "create non existing file without err in rfile path",
    52  			create: false,
    53  			rfile:  filepath.Join(tmpdir, "file1"),
    54  			args:   []string{filepath.Join(tmpdir, "file1")},
    55  		},
    56  		{
    57  			name:   "truncate existing file without err and positive size",
    58  			create: false,
    59  			size: unit.Value{
    60  				Value:        -50,
    61  				IsSet:        true,
    62  				ExplicitSign: unit.Sign(2),
    63  			},
    64  			sizeWant: 0,
    65  			args:     []string{filepath.Join(tmpdir, "file1")},
    66  		},
    67  		{
    68  			name:   "truncate existing file without err and negative size",
    69  			create: false,
    70  			size: unit.Value{
    71  				Value:        50,
    72  				IsSet:        true,
    73  				ExplicitSign: unit.Sign(1),
    74  			},
    75  			sizeWant: 50,
    76  			args:     []string{filepath.Join(tmpdir, "file1")},
    77  		},
    78  	} {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			*create = tt.create
    81  			*size = tt.size
    82  			*rfile = tt.rfile
    83  			if got := truncate(tt.args...); got != nil {
    84  				if got.Error() != tt.want {
    85  					t.Errorf("truncate() = %q, want: %q", got.Error(), tt.want)
    86  				}
    87  			} else {
    88  				st, err := os.Stat(filepath.Join(tmpdir, "file1"))
    89  				if err != nil {
    90  					t.Errorf("failed to get file stats")
    91  				}
    92  				if st.Size() != tt.sizeWant {
    93  					t.Errorf("the file was not truncated right, got file size %v, want: %v", st.Size(), tt.sizeWant)
    94  				}
    95  			}
    96  		})
    97  	}
    98  }