github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/printers/html_test.go (about)

     1  package printers
     2  
     3  import (
     4  	"bytes"
     5  	"go/token"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/vanstinator/golangci-lint/pkg/result"
    12  )
    13  
    14  //nolint:lll
    15  const expectedHTML = `<!doctype html>
    16  <html lang="en">
    17  <head>
    18      <meta charset="utf-8">
    19      <title>golangci-lint</title>
    20      <link rel="shortcut icon" type="image/png" href="https://golangci-lint.run/favicon-32x32.png">
    21      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.2/css/bulma.min.css"
    22            integrity="sha512-byErQdWdTqREz6DLAA9pCnLbdoGGhXfU6gm1c8bkf7F51JVmUBlayGe2A31VpXWQP+eiJ3ilTAZHCR3vmMyybA=="
    23            crossorigin="anonymous" referrerpolicy="no-referrer"/>
    24      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/styles/default.min.css"
    25            integrity="sha512-kZqGbhf9JTB4bVJ0G8HCkqmaPcRgo88F0dneK30yku5Y/dep7CZfCnNml2Je/sY4lBoqoksXz4PtVXS4GHSUzQ=="
    26            crossorigin="anonymous" referrerpolicy="no-referrer"/>
    27      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/highlight.min.js"
    28              integrity="sha512-s+tOYYcC3Jybgr9mVsdAxsRYlGNq4mlAurOrfNuGMQ/SCofNPu92tjE7YRZCsdEtWL1yGkqk15fU/ark206YTg=="
    29              crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    30      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/languages/go.min.js"
    31              integrity="sha512-+UYV2NyyynWEQcZ4sMTKmeppyV331gqvMOGZ61/dqc89Tn1H40lF05ACd03RSD9EWwGutNwKj256mIR8waEJBQ=="
    32              crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    33      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"
    34              integrity="sha512-qlzIeUtTg7eBpmEaS12NZgxz52YYZVF5myj89mjJEesBd/oE9UPsYOX2QAXzvOAZYEvQohKdcY8zKE02ifXDmA=="
    35              crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    36      <script type="text/javascript"
    37              src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"
    38              integrity="sha512-9jGNr5Piwe8nzLLYTk8QrEMPfjGU0px80GYzKZUxi7lmCfrBjtyCc1V5kkS5vxVwwIB7Qpzc7UxLiQxfAN30dw=="
    39              crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    40      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"
    41              integrity="sha512-kp7YHLxuJDJcOzStgd6vtpxr4ZU9kjn77e6dBsivSz+pUuAuMlE2UTdKB7jjsWT84qbS8kdCWHPETnP/ctrFsA=="
    42              crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    43  </head>
    44  <body>
    45  <section class="section">
    46      <div class="container">
    47          <div id="content"></div>
    48      </div>
    49  </section>
    50  <script>
    51      const data = {"Issues":[{"Title":"some issue","Pos":"path/to/filea.go:10:4","Linter":"linter-a","Code":""},{"Title":"another issue","Pos":"path/to/fileb.go:300:9","Linter":"linter-b","Code":"func foo() {\n\tfmt.Println(\"bar\")\n}"}]};
    52  </script>
    53  <script type="text/babel">
    54    class Highlight extends React.Component {
    55      componentDidMount() {
    56        hljs.highlightElement(ReactDOM.findDOMNode(this));
    57      }
    58  
    59      render() {
    60        return <pre className="go"><code>{this.props.code}</code></pre>;
    61      }
    62    }
    63  
    64    class Issue extends React.Component {
    65      render() {
    66        return (
    67          <div className="issue box">
    68            <div>
    69              <div className="columns">
    70                <div className="column is-four-fifths">
    71                  <h5 className="title is-5 has-text-danger-dark">{this.props.data.Title}</h5>
    72                </div>
    73                <div className="column is-one-fifth">
    74                  <h6 className="title is-6">{this.props.data.Linter}</h6>
    75                </div>
    76              </div>
    77              <strong>{this.props.data.Pos}</strong>
    78            </div>
    79            <div className="highlight">
    80              <Highlight code={this.props.data.Code}/>
    81            </div>
    82          </div>
    83        );
    84      }
    85    }
    86  
    87    class Issues extends React.Component {
    88      render() {
    89        if (!this.props.data.Issues || this.props.data.Issues.length === 0) {
    90          return (
    91            <div>
    92              <div className="notification">
    93                No issues found!
    94              </div>
    95            </div>
    96          );
    97        }
    98  
    99        return (
   100          <div className="issues">
   101            {this.props.data.Issues.map(issue => (<Issue data={issue}/>))}
   102          </div>
   103        );
   104      }
   105    }
   106  
   107    ReactDOM.render(
   108      <div className="content">
   109        <div className="columns is-centered">
   110          <div className="column is-three-quarters">
   111            <Issues data={data}/>
   112          </div>
   113        </div>
   114      </div>,
   115      document.getElementById("content")
   116    );
   117  </script>
   118  </body>
   119  </html>`
   120  
   121  func TestHTML_Print(t *testing.T) {
   122  	issues := []result.Issue{
   123  		{
   124  			FromLinter: "linter-a",
   125  			Severity:   "warning",
   126  			Text:       "some issue",
   127  			Pos: token.Position{
   128  				Filename: "path/to/filea.go",
   129  				Offset:   2,
   130  				Line:     10,
   131  				Column:   4,
   132  			},
   133  		},
   134  		{
   135  			FromLinter: "linter-b",
   136  			Severity:   "error",
   137  			Text:       "another issue",
   138  			SourceLines: []string{
   139  				"func foo() {",
   140  				"\tfmt.Println(\"bar\")",
   141  				"}",
   142  			},
   143  			Pos: token.Position{
   144  				Filename: "path/to/fileb.go",
   145  				Offset:   5,
   146  				Line:     300,
   147  				Column:   9,
   148  			},
   149  		},
   150  	}
   151  
   152  	buf := new(bytes.Buffer)
   153  	printer := NewHTML(buf)
   154  
   155  	err := printer.Print(issues)
   156  	require.NoError(t, err)
   157  
   158  	assert.Equal(t, expectedHTML, buf.String())
   159  }