github.com/winebarrel/terraform-provider-lambdazip@v0.6.1-0.20240313233639-361839f8c5c5/internal/hash/sha_test.go (about)

     1  package hash_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/winebarrel/terraform-provider-lambdazip/internal/hash"
    10  )
    11  
    12  func TestBase64Sha256(t *testing.T) {
    13  	assert := assert.New(t)
    14  	require := require.New(t)
    15  
    16  	cwd, _ := os.Getwd()
    17  	os.Chdir(t.TempDir())
    18  	defer os.Chdir(cwd)
    19  
    20  	os.WriteFile("hello.rb", []byte("puts 'world'"), 0755)
    21  
    22  	base64Sha256, err := hash.Base64Sha256("hello.rb")
    23  	require.NoError(err)
    24  	assert.Equal("BtsseiYO+vbi4/TGNcg1BvH0D204mODmAl4+VfRN3r4=", base64Sha256)
    25  }
    26  
    27  func TestSha256Map(t *testing.T) {
    28  	assert := assert.New(t)
    29  	require := require.New(t)
    30  
    31  	cwd, _ := os.Getwd()
    32  	os.Chdir(t.TempDir())
    33  	defer os.Chdir(cwd)
    34  
    35  	os.WriteFile("hello.rb", []byte("puts 'world'"), 0755)
    36  	os.WriteFile("world.rb", []byte("puts 'hello'"), 0755)
    37  
    38  	m, err := hash.Sha256Map([]string{"hello.rb", "world.rb"})
    39  	require.NoError(err)
    40  
    41  	assert.Equal(map[string]string{
    42  		"hello.rb": "06db2c7a260efaf6e2e3f4c635c83506f1f40f6d3898e0e6025e3e55f44ddebe",
    43  		"world.rb": "293c10e07909b3a823d7d2ba87c6cdf7400c9ed70132c2c952d7c8147d945a74",
    44  	}, m)
    45  }