github.com/jfrog/frogbot/v2@v2.21.0/utils/outputwriter/standardoutput_test.go (about) 1 package outputwriter 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestStandardOutputFlags(t *testing.T) { 10 testCases := []struct { 11 name string 12 entitled bool 13 showCaColumn bool 14 }{ 15 { 16 name: "entitled", 17 entitled: true, 18 showCaColumn: false, 19 }, 20 { 21 name: "not entitled", 22 entitled: false, 23 showCaColumn: false, 24 }, 25 { 26 name: "entitled with ca column", 27 entitled: true, 28 showCaColumn: true, 29 }, 30 { 31 name: "not entitled with ca column", 32 entitled: false, 33 showCaColumn: true, 34 }, 35 } 36 for _, tc := range testCases { 37 t.Run(tc.name, func(t *testing.T) { 38 smo := &StandardOutput{} 39 smo.SetJasOutputFlags(tc.entitled, tc.showCaColumn) 40 assert.Equal(t, tc.entitled, smo.entitledForJas) 41 assert.Equal(t, tc.showCaColumn, smo.showCaColumn) 42 assert.Equal(t, tc.entitled, smo.IsEntitledForJas()) 43 assert.Equal(t, tc.showCaColumn, smo.IsShowingCaColumn()) 44 }) 45 } 46 } 47 48 func TestStandardSeparator(t *testing.T) { 49 smo := &StandardOutput{} 50 assert.Equal(t, "<br>", smo.Separator()) 51 } 52 53 func TestStandardFormattedSeverity(t *testing.T) { 54 testCases := []struct { 55 name string 56 severity string 57 applicability string 58 expectedOutput string 59 }{ 60 { 61 name: "Applicable severity", 62 severity: "Low", 63 applicability: "Applicable", 64 expectedOutput: "<br> Low", 65 }, 66 { 67 name: "Not applicable severity", 68 severity: "Medium", 69 applicability: "Not Applicable", 70 expectedOutput: "<br> Medium", 71 }, 72 } 73 for _, tc := range testCases { 74 t.Run(tc.name, func(t *testing.T) { 75 smo := &StandardOutput{} 76 assert.Equal(t, tc.expectedOutput, smo.FormattedSeverity(tc.severity, tc.applicability)) 77 }) 78 } 79 } 80 81 func TestStandardImage(t *testing.T) { 82 testCases := []struct { 83 name string 84 source ImageSource 85 expectedOutput string 86 }{ 87 { 88 name: "no vulnerability pr banner", 89 source: NoVulnerabilityPrBannerSource, 90 expectedOutput: "<div align='center'>\n\n[](https://docs.jfrog-applications.jfrog.io/jfrog-applications/frogbot)\n\n</div>\n", 91 }, 92 { 93 name: "vulnerabilities pr banner", 94 source: VulnerabilitiesPrBannerSource, 95 expectedOutput: "<div align='center'>\n\n[](https://docs.jfrog-applications.jfrog.io/jfrog-applications/frogbot)\n\n</div>\n", 96 }, 97 { 98 name: "no vulnerability mr banner", 99 source: NoVulnerabilityMrBannerSource, 100 expectedOutput: "<div align='center'>\n\n[](https://docs.jfrog-applications.jfrog.io/jfrog-applications/frogbot)\n\n</div>\n", 101 }, 102 { 103 name: "vulnerabilities mr banner", 104 source: VulnerabilitiesMrBannerSource, 105 expectedOutput: "<div align='center'>\n\n[](https://docs.jfrog-applications.jfrog.io/jfrog-applications/frogbot)\n\n</div>\n", 106 }, 107 { 108 name: "vulnerabilities fix pr banner", 109 source: VulnerabilitiesFixPrBannerSource, 110 expectedOutput: "<div align='center'>\n\n[](https://docs.jfrog-applications.jfrog.io/jfrog-applications/frogbot)\n\n</div>\n", 111 }, 112 { 113 name: "vulnerabilities fix mr banner", 114 source: VulnerabilitiesFixMrBannerSource, 115 expectedOutput: "<div align='center'>\n\n[](https://docs.jfrog-applications.jfrog.io/jfrog-applications/frogbot)\n\n</div>\n", 116 }, 117 } 118 for _, tc := range testCases { 119 t.Run(tc.name, func(t *testing.T) { 120 smo := &StandardOutput{MarkdownOutput{hasInternetConnection: true}} 121 assert.Equal(t, tc.expectedOutput, smo.Image(tc.source)) 122 }) 123 } 124 } 125 126 func TestStandardMarkInCenter(t *testing.T) { 127 testCases := []struct { 128 name string 129 content string 130 expectedOutput string 131 }{ 132 { 133 name: "empty content", 134 content: "", 135 expectedOutput: "<div align='center'>\n\n\n\n</div>\n", 136 }, 137 { 138 name: "non empty content", 139 content: "content", 140 expectedOutput: "<div align='center'>\n\ncontent\n\n</div>\n", 141 }, 142 } 143 for _, tc := range testCases { 144 t.Run(tc.name, func(t *testing.T) { 145 smo := &StandardOutput{} 146 assert.Equal(t, tc.expectedOutput, smo.MarkInCenter(tc.content)) 147 }) 148 } 149 } 150 151 func TestStandardMarkAsDetails(t *testing.T) { 152 testCases := []struct { 153 name string 154 summary string 155 content string 156 expectedOutput string 157 subTitleDepth int 158 }{ 159 { 160 name: "empty", 161 summary: "", 162 subTitleDepth: 0, 163 content: "", 164 expectedOutput: "<details>\n\n\n\n</details>\n", 165 }, 166 { 167 name: "empty content", 168 summary: "summary", 169 subTitleDepth: 1, 170 content: "", 171 expectedOutput: "<details>\n<summary> <b>summary</b> </summary>\n<br>\n\n\n\n</details>\n", 172 }, 173 { 174 name: "empty summary", 175 summary: "", 176 subTitleDepth: 0, 177 content: "content", 178 expectedOutput: "<details>\n\ncontent\n\n</details>\n", 179 }, 180 { 181 name: "Main details", 182 summary: "summary", 183 subTitleDepth: 1, 184 content: "content", 185 expectedOutput: "<details>\n<summary> <b>summary</b> </summary>\n<br>\n\ncontent\n\n</details>\n", 186 }, 187 { 188 name: "Sub details", 189 summary: "summary", 190 subTitleDepth: 2, 191 content: "content", 192 expectedOutput: "<details>\n<summary> <b>summary</b> </summary>\n<br>\n\ncontent\n\n</details>\n", 193 }, 194 { 195 name: "Sub sub details", 196 summary: "summary", 197 subTitleDepth: 3, 198 content: "content", 199 expectedOutput: "<details>\n<summary> <b>summary</b> </summary>\n<br>\n\ncontent\n\n</details>\n", 200 }, 201 } 202 for _, tc := range testCases { 203 t.Run(tc.name, func(t *testing.T) { 204 smo := &StandardOutput{} 205 assert.Equal(t, tc.expectedOutput, smo.MarkAsDetails(tc.summary, tc.subTitleDepth, tc.content)) 206 }) 207 } 208 } 209 210 func TestStandardMarkAsTitle(t *testing.T) { 211 testCases := []struct { 212 name string 213 title string 214 expectedOutput string 215 subTitleDepth int 216 }{ 217 { 218 name: "empty", 219 title: "", 220 subTitleDepth: 0, 221 expectedOutput: "", 222 }, 223 { 224 name: "Main title", 225 title: "title", 226 subTitleDepth: 1, 227 expectedOutput: "# title", 228 }, 229 { 230 name: "Sub title", 231 title: "title", 232 subTitleDepth: 2, 233 expectedOutput: "## title", 234 }, 235 { 236 name: "Sub sub title", 237 title: "title", 238 subTitleDepth: 3, 239 expectedOutput: "### title", 240 }, 241 } 242 for _, tc := range testCases { 243 t.Run(tc.name, func(t *testing.T) { 244 smo := &StandardOutput{} 245 assert.Equal(t, tc.expectedOutput, smo.MarkAsTitle(tc.title, tc.subTitleDepth)) 246 }) 247 } 248 }