Skip to content

Commit c26095f

Browse files
committed
Use FilterLine Instead of FilterScan
1 parent 76323c3 commit c26095f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

script.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,20 +668,20 @@ func (p *Pipe) Hash(hasher hash.Hash) (string, error) {
668668

669669
// HashSums reads paths from the pipe, one per line, and produces the
670670
// hex-encoded hash of each corresponding file based on the provided hasher,
671-
// one per line. Any files that cannot be opened or read will be ignored.
671+
// one per line. Any files that cannot be opened or read will print an empty line.
672672
// To perform hashing on the contents of the pipe, see [Pipe.Hash].
673673
func (p *Pipe) HashSums(hasher hash.Hash) *Pipe {
674-
return p.FilterScan(func(line string, w io.Writer) {
674+
return p.FilterLine(func(line string) string {
675675
f, err := os.Open(line)
676676
if err != nil {
677-
return // skip unopenable files
677+
return "" // skip unopenable files
678678
}
679679
defer f.Close()
680680
_, err = io.Copy(hasher, f)
681681
if err != nil {
682-
return // skip unreadable files
682+
return "" // skip unreadable files
683683
}
684-
fmt.Fprintln(w, hex.EncodeToString(hasher.Sum(nil)))
684+
return hex.EncodeToString(hasher.Sum(nil))
685685
})
686686
}
687687

script_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,17 @@ func TestHashSums_OutputsCorrectHashForEachSpecifiedFile(t *testing.T) {
21002100
}
21012101
}
21022102

2103+
func TestHashSums_OutputsEmptyLineForFileThatCannotBeHashed(t *testing.T) {
2104+
got, err := script.Echo("file_does_not_exist.txt").HashSums(sha256.New()).String()
2105+
if err != nil {
2106+
t.Fatal(err)
2107+
}
2108+
want := "\n"
2109+
if got != want {
2110+
t.Errorf("want %q, got %q", want, got)
2111+
}
2112+
}
2113+
21032114
func TestHash_ReturnsErrorGivenReadErrorOnPipe(t *testing.T) {
21042115
t.Parallel()
21052116
brokenReader := iotest.ErrReader(errors.New("oh no"))

0 commit comments

Comments
 (0)