|
| 1 | +// Copyright (c) 2017-2025 Carl Kittelberger. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE.txt file. |
| 4 | + |
| 5 | +package patch_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/icedream/go-bsdiff/internal/testing/fixtures" |
| 12 | + "github.com/icedream/go-bsdiff/patch" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestPatch(t *testing.T) { |
| 17 | + t.Run("succeeds with valid patch", func(t *testing.T) { |
| 18 | + oldReader := bytes.NewReader(fixtures.Old) |
| 19 | + patchReader := bytes.NewReader(fixtures.Patch) |
| 20 | + var buf bytes.Buffer |
| 21 | + |
| 22 | + require.NoError(t, patch.Patch(oldReader, &buf, patchReader)) |
| 23 | + require.Equal(t, fixtures.New, buf.Bytes()) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("fails with invalid patch", func(t *testing.T) { |
| 27 | + oldReader := bytes.NewReader(fixtures.Old) |
| 28 | + patchReader := bytes.NewReader([]byte("invalid patch")) |
| 29 | + var buf bytes.Buffer |
| 30 | + |
| 31 | + require.Error(t, patch.Patch(oldReader, &buf, patchReader)) |
| 32 | + require.Empty(t, buf.Bytes()) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("fails with invalid original file", func(t *testing.T) { |
| 36 | + // TODO - bsdiff currently seems to return 0 code...? |
| 37 | + t.Skip() |
| 38 | + |
| 39 | + oldReader := bytes.NewReader([]byte("bad file")) |
| 40 | + patchReader := bytes.NewReader(fixtures.Patch) |
| 41 | + var buf bytes.Buffer |
| 42 | + |
| 43 | + require.Error(t, patch.Patch(oldReader, &buf, patchReader)) |
| 44 | + require.Empty(t, buf.Bytes()) |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func BenchmarkPatch(b *testing.B) { |
| 49 | + // make sure we start in the middle of the loop from exactly 0 time passed |
| 50 | + b.StopTimer() |
| 51 | + b.ResetTimer() |
| 52 | + |
| 53 | + for i := 0; i < b.N; i++ { |
| 54 | + oldReader := bytes.NewReader(fixtures.Old) |
| 55 | + patchReader := bytes.NewReader(fixtures.Patch) |
| 56 | + var buf bytes.Buffer |
| 57 | + |
| 58 | + // measure actual patch |
| 59 | + b.StartTimer() |
| 60 | + err := patch.Patch(oldReader, &buf, patchReader) |
| 61 | + b.StopTimer() |
| 62 | + |
| 63 | + require.NoError(b, err) |
| 64 | + } |
| 65 | +} |
0 commit comments