2017-04-14 19:06:50 +02:00
|
|
|
package checksum
|
|
|
|
|
|
|
|
import (
|
2017-04-22 15:58:29 +02:00
|
|
|
"crypto/sha256"
|
2017-04-14 19:06:50 +02:00
|
|
|
"io/ioutil"
|
2017-04-22 15:58:29 +02:00
|
|
|
"os"
|
2017-04-14 19:06:50 +02:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestChecksums(t *testing.T) {
|
2017-04-15 19:18:17 +02:00
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-04-14 21:27:59 +02:00
|
|
|
var file = filepath.Join(folder, "subject")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0644))
|
2017-04-14 21:27:59 +02:00
|
|
|
sum, err := SHA256(file)
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "5e2bf57d3f40c4b6df69daf1936cb766f832374b4fc0259a7cbff06e2f70f269", sum)
|
2017-04-14 19:06:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenFailure(t *testing.T) {
|
2017-04-14 19:53:25 +02:00
|
|
|
sum, err := SHA256("/tmp/this-file-wont-exist-I-hope")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.Empty(t, sum)
|
|
|
|
assert.Error(t, err)
|
2017-04-14 19:06:50 +02:00
|
|
|
}
|
2017-04-22 15:58:29 +02:00
|
|
|
|
|
|
|
func TestFileDoesntExist(t *testing.T) {
|
|
|
|
folder, err := ioutil.TempDir("", "goreleasertest")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-04-22 15:58:29 +02:00
|
|
|
var path = filepath.Join(folder, "subject")
|
|
|
|
file, err := os.Create(path)
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, file.Close())
|
2017-04-22 15:58:29 +02:00
|
|
|
_, err = doCalculate(sha256.New(), file)
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.Error(t, err)
|
2017-04-22 15:58:29 +02:00
|
|
|
}
|