Skip to content

Commit

Permalink
support checksum files without filename inside
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed Apr 12, 2023
1 parent 7d31171 commit 0ed36ec
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
33 changes: 27 additions & 6 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -48,25 +49,45 @@ func Parse(checksumFile string) ([]Pair, error) {
}

cs := parseChecksumLines(b)
if len(cs) == 0 {
return checksums, fmt.Errorf("no checksums in %s", checksumFile)
}
for i := range cs {
// Since there is no filename field in the checksumFile get the
// filename from the checksumFile name.
if cs[i].Filename == "" {
cs[i].Filename = trimSuffix(checksumFile)
}
}
checksums = append(checksums, cs...)

return checksums, nil
}

// trimSuffix removes suffix (like .sha256) from the filename.
func trimSuffix(filename string) string {
suffix := filepath.Ext(filename)
return strings.TrimSuffix(filename, suffix)
}

// parseChecksumLines parses bytes from a checksums file. The bytes look like:
// 712f37d14687e10ae0425bf7e5d0faf17c49f9476b8bb6a96f2a3f91b0436db2 checkip_0.45.1_linux_armv6.tar.gz
// ba47c83b6038dda089dd1410b9e97d1de7e4adea7620c856f9b74a782048e272 checkip_0.45.1_linux_amd64.tar.gz
func parseChecksumLines(b []byte) []Pair {
var checksums []Pair
for _, line := range strings.Split(string(b), "\n") {
fields := strings.Fields(line)
if len(fields) != 2 {
continue
switch len(fields) {
case 2:
checksums = append(checksums, Pair{
Checksum: fields[0],
Filename: fields[1],
})
case 1:
checksums = append(checksums, Pair{
Checksum: fields[0],
})
}
checksums = append(checksums, Pair{
Checksum: fields[0],
Filename: fields[1],
})
}
return checksums
}
70 changes: 46 additions & 24 deletions checksum/checksum_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
package checksum

import (
"io"
"os"
"testing"
)

var test = struct {
fileName string
fileChecksum string // calculated using sha256sum
checksumsFile string // contains checksum of file and file name
var tests = []struct {
fileName string
fileChecksum string // calculated using sha256sum
checksumFile string // contains checksum of file and optionally file name
}{
fileName: "testdata/file-to-checksum",
fileChecksum: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
checksumsFile: "testdata/checksum-file", // only one line here
{
fileName: "testdata/file-to-checksum",
fileChecksum: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
checksumFile: "testdata/checksum-file",
},
{
fileName: "testdata/file-to-checksum",
fileChecksum: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
checksumFile: "testdata/file-to-checksum.sha256",
},
}

func TestParseChecksumsLines(t *testing.T) {
file, err := os.Open(test.checksumsFile)
if err != nil {
t.Error(err)
}
defer file.Close()
func TestParse(t *testing.T) {
for _, test := range tests {
checksums, err := Parse(test.checksumFile)
if err != nil {
t.Error(err)
}

for _, c := range checksums {
got := c.Checksum
want := test.fileChecksum
if got != want {
t.Errorf("wrong checksum: got %q, want %q", got, want)
}

b, err := io.ReadAll(file)
if err != nil {
t.Error(err)
got = c.Filename
want = test.fileName
if got != want {
t.Errorf("wrong filename: got %q, want %q", got, want)
}
}
}
}

cs := parseChecksumLines(b)
for _, c := range cs {
got := c.Checksum
want := test.fileChecksum
if got != want {
t.Errorf("wrong checksum: got %s, want %s", got, want)
func TestTrimSuffix(t *testing.T) {
tests := []struct {
filename string
want string
}{
{"", ""},
{"starship-aarch64-apple-darwin.tar.gz.sha256", "starship-aarch64-apple-darwin.tar.gz"},
}
for _, test := range tests {
got := trimSuffix(test.filename)
if got != test.want {
t.Errorf("trimSuffix(%s) = %s, want %s", test.filename, got, test.want)
}
}
}
1 change: 1 addition & 0 deletions checksum/testdata/file-to-checksum.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

0 comments on commit 0ed36ec

Please sign in to comment.