|
| 1 | +package assets |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestLogo(t *testing.T) { |
| 9 | + logo := Logo() |
| 10 | + if logo == "" { |
| 11 | + t.Fatal("Logo wasn't embedded") |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +func TestVersionString(t *testing.T) { |
| 16 | + // Reset v singleton |
| 17 | + v = nil |
| 18 | + vers := RocketPoolVersion() |
| 19 | + if vers == "" { |
| 20 | + t.Fatal("Version string wasn't embedded") |
| 21 | + } |
| 22 | + |
| 23 | + if v == nil { |
| 24 | + t.Fatalf("v should be initialized") |
| 25 | + } |
| 26 | + |
| 27 | + oldV := v |
| 28 | + |
| 29 | + // Make sure subsequent calls to RocketPoolVersion are amortized |
| 30 | + RocketPoolVersion() |
| 31 | + if oldV != v { |
| 32 | + t.Fatalf("v was reallocated") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func shouldPanic(t *testing.T) { |
| 37 | + if r := recover(); r == nil { |
| 38 | + t.Fatal("should have panicked!") |
| 39 | + } else { |
| 40 | + t.Log(r) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestInvalidJsonVersionPanics(t *testing.T) { |
| 45 | + // Reset v singleton |
| 46 | + v = nil |
| 47 | + |
| 48 | + // Save the version JSON so we can restore it |
| 49 | + oldVersionJSON := versionJSON |
| 50 | + defer func() { |
| 51 | + versionJSON = oldVersionJSON |
| 52 | + }() |
| 53 | + // We want to panic when version can't be parsed. |
| 54 | + defer shouldPanic(t) |
| 55 | + |
| 56 | + versionJSON = []byte("this is not valid json") |
| 57 | + _ = RocketPoolVersion() |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +func TestEmptyVersionPanics(t *testing.T) { |
| 62 | + // Reset v singleton |
| 63 | + v = nil |
| 64 | + |
| 65 | + // Save the version JSON so we can restore it |
| 66 | + oldVersionJSON := versionJSON |
| 67 | + defer func() { |
| 68 | + versionJSON = oldVersionJSON |
| 69 | + }() |
| 70 | + // We want to panic when version is empty but json is valid |
| 71 | + defer shouldPanic(t) |
| 72 | + |
| 73 | + versionJSON = []byte("{}") |
| 74 | + _ = RocketPoolVersion() |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +func TestPrintPatchNotes(t *testing.T) { |
| 79 | + // Reset v singleton |
| 80 | + v = &version{"test"} |
| 81 | + notes, err := GetPatchNotes() |
| 82 | + if err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + |
| 86 | + t.Log("Rendered patch notes: ", notes) |
| 87 | + |
| 88 | + // Make sure there are no template directives unpopulated |
| 89 | + if strings.Contains(notes, "{{") || strings.Contains(notes, "}}") { |
| 90 | + t.Fatal("encountered unexpanded template directive") |
| 91 | + } |
| 92 | + |
| 93 | + if !strings.Contains(notes, Logo()) { |
| 94 | + t.Fatal("expected logo in patch notes") |
| 95 | + } |
| 96 | + |
| 97 | + if !strings.Contains(notes, "test patch notes") { |
| 98 | + t.Fatal("expected template body in patch notes") |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// This tests ensures each version has a patchnotes template |
| 103 | +func TestPrintCurrentPatchNotes(t *testing.T) { |
| 104 | + // Reset v singleton |
| 105 | + v = nil |
| 106 | + |
| 107 | + notes, err := GetPatchNotes() |
| 108 | + if err != nil { |
| 109 | + t.Fatal(err) |
| 110 | + } |
| 111 | + |
| 112 | + t.Log("Rendered patch notes: ", notes) |
| 113 | + |
| 114 | + // Make sure there are no template directives unpopulated |
| 115 | + if strings.Contains(notes, "{{") || strings.Contains(notes, "}}") { |
| 116 | + t.Fatal("encountered unexpanded template directive") |
| 117 | + } |
| 118 | + |
| 119 | + if !strings.Contains(notes, Logo()) { |
| 120 | + t.Fatal("expected logo in patch notes") |
| 121 | + } |
| 122 | +} |
0 commit comments