github.com/saucelabs/saucectl@v0.175.1/internal/hashio/files_test.go (about)

     1  package hashio
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/fs"
     7  )
     8  
     9  func TestSHA256(t *testing.T) {
    10  	dir := fs.NewDir(t, "checksums", fs.WithFile("hello.txt", "world!"))
    11  	defer dir.Remove()
    12  
    13  	type args struct {
    14  		filename string
    15  	}
    16  	tests := []struct {
    17  		name    string
    18  		args    args
    19  		want    string
    20  		wantErr bool
    21  	}{
    22  		{
    23  			name: "compute checksum",
    24  			args: args{
    25  				dir.Join("hello.txt"),
    26  			},
    27  			want:    "711e9609339e92b03ddc0a211827dba421f38f9ed8b9d806e1ffdd8c15ffa03d",
    28  			wantErr: false,
    29  		},
    30  		{
    31  			name: "file does not exist",
    32  			args: args{
    33  				dir.Join("rude.txt"),
    34  			},
    35  			want:    "",
    36  			wantErr: true,
    37  		},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			got, err := SHA256(tt.args.filename)
    42  			if (err != nil) != tt.wantErr {
    43  				t.Errorf("SHA256() error = %v, wantErr %v", err, tt.wantErr)
    44  				return
    45  			}
    46  			if got != tt.want {
    47  				t.Errorf("SHA256() got = %v, want %v", got, tt.want)
    48  			}
    49  		})
    50  	}
    51  }