File tree Expand file tree Collapse file tree 2 files changed +16
-5
lines changed Expand file tree Collapse file tree 2 files changed +16
-5
lines changed Original file line number Diff line number Diff 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].
673673func (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
Original file line number Diff line number Diff 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+
21032114func TestHash_ReturnsErrorGivenReadErrorOnPipe (t * testing.T ) {
21042115 t .Parallel ()
21052116 brokenReader := iotest .ErrReader (errors .New ("oh no" ))
You can’t perform that action at this time.
0 commit comments