github.com/Finschia/finschia-sdk@v0.48.1/internal/os/file_test.go (about)

     1  package os
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestReadFileWithSizeLimit(t *testing.T) {
    12  	filename := "file.go"
    13  	file, err := os.ReadFile(filename)
    14  	require.NoError(t, err)
    15  
    16  	type args struct {
    17  		name      string
    18  		sizeLimit int64
    19  	}
    20  	tests := []struct {
    21  		name    string
    22  		args    args
    23  		want    []byte
    24  		wantErr bool
    25  	}{
    26  		{"cannot open error", args{"", 0}, nil, true},
    27  		{"size limit over error", args{filename, 0}, nil, true},
    28  		{"simple reading file success", args{filename, 100000}, file, false},
    29  	}
    30  	for _, tt := range tests {
    31  		t.Run(tt.name, func(t *testing.T) {
    32  			got, err := ReadFileWithSizeLimit(tt.args.name, tt.args.sizeLimit)
    33  			if (err != nil) != tt.wantErr {
    34  				t.Errorf("ReadFileWithSizeLimit() error = %v, wantErr %v", err, tt.wantErr)
    35  				return
    36  			}
    37  			if !reflect.DeepEqual(got, tt.want) {
    38  				t.Errorf("ReadFileWithSizeLimit() got = %v, want %v", got, tt.want)
    39  			}
    40  		})
    41  	}
    42  }