|
| 1 | +package version |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestString(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + name string |
| 8 | + version string |
| 9 | + commit string |
| 10 | + expectedOutput string |
| 11 | + }{ |
| 12 | + { |
| 13 | + name: "default values", |
| 14 | + version: "dev", |
| 15 | + commit: "unknown", |
| 16 | + expectedOutput: "dev", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "empty commit", |
| 20 | + version: "1.0.0", |
| 21 | + commit: "", |
| 22 | + expectedOutput: "1.0.0", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "full commit hash", |
| 26 | + version: "1.0.0", |
| 27 | + commit: "abc123def456789", |
| 28 | + expectedOutput: "1.0.0 (abc123d)", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "short commit (7 chars)", |
| 32 | + version: "1.0.0", |
| 33 | + commit: "abc123d", |
| 34 | + expectedOutput: "1.0.0 (abc123d)", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "very short commit (less than 7 chars)", |
| 38 | + version: "1.0.0", |
| 39 | + commit: "abc", |
| 40 | + expectedOutput: "1.0.0 (abc)", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "single char commit", |
| 44 | + version: "1.0.0", |
| 45 | + commit: "a", |
| 46 | + expectedOutput: "1.0.0 (a)", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "exactly 7 chars", |
| 50 | + version: "2.0.0", |
| 51 | + commit: "1234567", |
| 52 | + expectedOutput: "2.0.0 (1234567)", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + // Save original values |
| 59 | + origVersion := Version |
| 60 | + origCommit := Commit |
| 61 | + defer func() { |
| 62 | + Version = origVersion |
| 63 | + Commit = origCommit |
| 64 | + }() |
| 65 | + |
| 66 | + // Set test values |
| 67 | + Version = tt.version |
| 68 | + Commit = tt.commit |
| 69 | + |
| 70 | + result := String() |
| 71 | + if result != tt.expectedOutput { |
| 72 | + t.Errorf("String() = %q, want %q", result, tt.expectedOutput) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestStringNoPanic(t *testing.T) { |
| 79 | + // This test specifically ensures no panic occurs with edge cases |
| 80 | + origVersion := Version |
| 81 | + origCommit := Commit |
| 82 | + defer func() { |
| 83 | + Version = origVersion |
| 84 | + Commit = origCommit |
| 85 | + }() |
| 86 | + |
| 87 | + edgeCases := []struct { |
| 88 | + version string |
| 89 | + commit string |
| 90 | + }{ |
| 91 | + {"1.0.0", ""}, |
| 92 | + {"1.0.0", "a"}, |
| 93 | + {"1.0.0", "ab"}, |
| 94 | + {"1.0.0", "abc"}, |
| 95 | + {"1.0.0", "abcd"}, |
| 96 | + {"1.0.0", "abcde"}, |
| 97 | + {"1.0.0", "abcdef"}, |
| 98 | + {"1.0.0", "abcdefg"}, |
| 99 | + {"1.0.0", "abcdefgh"}, |
| 100 | + } |
| 101 | + |
| 102 | + for _, tc := range edgeCases { |
| 103 | + t.Run("commit_len_"+string(rune(len(tc.commit))+'0'), func(t *testing.T) { |
| 104 | + Version = tc.version |
| 105 | + Commit = tc.commit |
| 106 | + |
| 107 | + // Should not panic |
| 108 | + defer func() { |
| 109 | + if r := recover(); r != nil { |
| 110 | + t.Errorf("String() panicked with commit=%q: %v", tc.commit, r) |
| 111 | + } |
| 112 | + }() |
| 113 | + |
| 114 | + result := String() |
| 115 | + if result == "" { |
| 116 | + t.Error("String() returned empty string") |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
0 commit comments