|
| 1 | +package plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/version-fox/vfox/internal/shared" |
| 7 | +) |
| 8 | + |
| 9 | +func TestPreInstallPackageItem_Checksum(t *testing.T) { |
| 10 | + t.Run("Returns NoneChecksum when CheckSumItem is nil", func(t *testing.T) { |
| 11 | + // This is the case that was causing the nil pointer dereference |
| 12 | + item := &PreInstallPackageItem{ |
| 13 | + Name: "test-sdk", |
| 14 | + Version: "1.0.0", |
| 15 | + Path: "https://example.com/test.tar.gz", |
| 16 | + CheckSumItem: nil, // Explicitly nil |
| 17 | + } |
| 18 | + |
| 19 | + checksum := item.Checksum() |
| 20 | + |
| 21 | + if checksum != shared.NoneChecksum { |
| 22 | + t.Errorf("Expected NoneChecksum, got %v", checksum) |
| 23 | + } |
| 24 | + |
| 25 | + if checksum.Type != "none" { |
| 26 | + t.Errorf("Expected checksum type 'none', got '%s'", checksum.Type) |
| 27 | + } |
| 28 | + |
| 29 | + if checksum.Value != "" { |
| 30 | + t.Errorf("Expected empty checksum value, got '%s'", checksum.Value) |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("Returns checksum when CheckSumItem has sha256", func(t *testing.T) { |
| 35 | + item := &PreInstallPackageItem{ |
| 36 | + Name: "test-sdk", |
| 37 | + Version: "1.0.0", |
| 38 | + Path: "https://example.com/test.tar.gz", |
| 39 | + CheckSumItem: &CheckSumItem{ |
| 40 | + Sha256: "abc123def456", |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + checksum := item.Checksum() |
| 45 | + |
| 46 | + if checksum.Type != "sha256" { |
| 47 | + t.Errorf("Expected checksum type 'sha256', got '%s'", checksum.Type) |
| 48 | + } |
| 49 | + |
| 50 | + if checksum.Value != "abc123def456" { |
| 51 | + t.Errorf("Expected checksum value 'abc123def456', got '%s'", checksum.Value) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("Returns checksum when CheckSumItem has md5", func(t *testing.T) { |
| 56 | + item := &PreInstallPackageItem{ |
| 57 | + Name: "test-sdk", |
| 58 | + Version: "1.0.0", |
| 59 | + Path: "https://example.com/test.tar.gz", |
| 60 | + CheckSumItem: &CheckSumItem{ |
| 61 | + Md5: "xyz789", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + checksum := item.Checksum() |
| 66 | + |
| 67 | + if checksum.Type != "md5" { |
| 68 | + t.Errorf("Expected checksum type 'md5', got '%s'", checksum.Type) |
| 69 | + } |
| 70 | + |
| 71 | + if checksum.Value != "xyz789" { |
| 72 | + t.Errorf("Expected checksum value 'xyz789', got '%s'", checksum.Value) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("Returns NoneChecksum when CheckSumItem has no checksums", func(t *testing.T) { |
| 77 | + item := &PreInstallPackageItem{ |
| 78 | + Name: "test-sdk", |
| 79 | + Version: "1.0.0", |
| 80 | + Path: "https://example.com/test.tar.gz", |
| 81 | + CheckSumItem: &CheckSumItem{ |
| 82 | + // All fields empty |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + checksum := item.Checksum() |
| 87 | + |
| 88 | + if checksum != shared.NoneChecksum { |
| 89 | + t.Errorf("Expected NoneChecksum when all checksum fields are empty, got %v", checksum) |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
0 commit comments