Skip to content

Commit bef745d

Browse files
wait the zombies (gitleaks#1)
* wait the zombies * close the reader * defer order * partially update tests
1 parent 9c699f1 commit bef745d

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

gitdiff/apply_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"io"
77
"io/ioutil"
8+
"os/exec"
89
"path/filepath"
910
"testing"
1011
)
@@ -231,7 +232,9 @@ type applyTest struct {
231232
func (at applyTest) run(t *testing.T, apply func(io.Writer, *Applier, *File) error) {
232233
src, patch, out := at.Files.Load(t)
233234

234-
files, _, err := Parse(bytes.NewReader(patch))
235+
cmd := exec.Command("echo", "hello")
236+
237+
files, err := Parse(cmd, io.NopCloser(bytes.NewReader(patch)))
235238
if err != nil {
236239
t.Fatalf("failed to parse patch file: %v", err)
237240
}

gitdiff/parser.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bufio"
88
"fmt"
99
"io"
10+
"os/exec"
1011
"strings"
1112
)
1213

@@ -15,7 +16,7 @@ const commitPrefix = "commit"
1516
// Parse parses a patch with changes to one or more files. Any content before
1617
// the first file is returned as the second value. If an error occurs while
1718
// parsing, it returns all files parsed before the error.
18-
func Parse(r io.Reader) (<-chan *File, error) {
19+
func Parse(cmd *exec.Cmd, r io.ReadCloser) (<-chan *File, error) {
1920
p := newParser(r)
2021
out := make(chan *File)
2122

@@ -27,8 +28,10 @@ func Parse(r io.Reader) (<-chan *File, error) {
2728
return out, err
2829
}
2930

30-
go func() {
31+
go func(cmd *exec.Cmd, out chan *File, r io.ReadCloser) {
3132
defer close(out)
33+
defer cmd.Wait()
34+
defer r.Close()
3235

3336
ph := &PatchHeader{}
3437
for {
@@ -65,7 +68,7 @@ func Parse(r io.Reader) (<-chan *File, error) {
6568
file.PatchHeader = ph
6669
out <- file
6770
}
68-
}()
71+
}(cmd, out, r)
6972

7073
return out, nil
7174
}

gitdiff/parser_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"io"
88
"os"
9+
"os/exec"
910
"reflect"
1011
"testing"
1112
)
@@ -460,7 +461,9 @@ Date: Tue Apr 2 22:55:40 2019 -0700
460461
t.Fatalf("unexpected error opening input file: %v", err)
461462
}
462463

463-
files, pre, err := Parse(f)
464+
cmd := exec.Command("echo", "hello")
465+
466+
files, err := Parse(cmd, f)
464467
if test.Err {
465468
if err == nil || err == io.EOF {
466469
t.Fatalf("expected error parsing patch, but got %v", err)
@@ -474,9 +477,6 @@ Date: Tue Apr 2 22:55:40 2019 -0700
474477
if len(test.Output) != len(files) {
475478
t.Fatalf("incorrect number of parsed files: expected %d, actual %d", len(test.Output), len(files))
476479
}
477-
if test.Preamble != pre {
478-
t.Errorf("incorrect preamble\nexpected: %q\n actual: %q", test.Preamble, pre)
479-
}
480480
for i := range test.Output {
481481
if !reflect.DeepEqual(test.Output[i], files[i]) {
482482
exp, _ := json.MarshalIndent(test.Output[i], "", " ")

0 commit comments

Comments
 (0)