-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support checksum files without filename inside
- Loading branch information
1 parent
7d31171
commit 0ed36ec
Showing
3 changed files
with
74 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 |