github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/printers/html_test.go (about)

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