(t *testing.T)
| 56 | } |
| 57 | |
| 58 | func TestCommandContextTimeout(t *testing.T) { |
| 59 | cmd := New("init", WithFlag("--bare"), WithArg("samplerepo")) |
| 60 | |
| 61 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 62 | defer cancel() |
| 63 | |
| 64 | err := cmd.Run(ctx) |
| 65 | defer os.RemoveAll("samplerepo") |
| 66 | if err != nil { |
| 67 | t.Errorf("expected: %v error, got: %v", nil, err) |
| 68 | } |
| 69 | |
| 70 | inbuff := &bytes.Buffer{} |
| 71 | inbuff.WriteString("some content") |
| 72 | outbuffer := &bytes.Buffer{} |
| 73 | |
| 74 | cmd = New("hash-object", WithFlag("--stdin")) |
| 75 | err = cmd.Run(ctx, |
| 76 | WithDir("./samplerepo"), |
| 77 | WithStdin(inbuff), |
| 78 | WithStdout(outbuffer), |
| 79 | ) |
| 80 | if err != nil { |
| 81 | t.Errorf("hashing object failed: %v", err) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | log.Info().Msgf("outbuffer %s", outbuffer.String()) |
| 86 | |
| 87 | cmd = New("cat-file", WithFlag("--batch")) |
| 88 | |
| 89 | pr, pw := io.Pipe() |
| 90 | defer pr.Close() |
| 91 | |
| 92 | outbuffer.Reset() |
| 93 | |
| 94 | go func() { |
| 95 | defer pw.Close() |
| 96 | for i := 0; i < 3; i++ { |
| 97 | _, _ = pw.Write(outbuffer.Bytes()) |
| 98 | time.Sleep(1 * time.Second) |
| 99 | } |
| 100 | }() |
| 101 | |
| 102 | runCtx, runCancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 103 | defer runCancel() |
| 104 | |
| 105 | err = cmd.Run(runCtx, |
| 106 | WithDir("./samplerepo"), |
| 107 | WithStdin(pr), |
| 108 | WithStdout(outbuffer), |
| 109 | ) |
| 110 | |
| 111 | if !errors.Is(err, context.DeadlineExceeded) { |
| 112 | t.Errorf("expected: %v error, got: %v", context.DeadlineExceeded, err) |
| 113 | } |
| 114 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…