github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/highlight/highlight_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package highlight
     7  
     8  import (
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/gitbundle/modules/setting"
    13  	"github.com/gitbundle/modules/util"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  	"gopkg.in/ini.v1"
    17  )
    18  
    19  func TestFile(t *testing.T) {
    20  	setting.Cfg = ini.Empty()
    21  	tests := []struct {
    22  		name     string
    23  		numLines int
    24  		fileName string
    25  		code     string
    26  		want     string
    27  	}{
    28  		{
    29  			name:     ".drone.yml",
    30  			numLines: 12,
    31  			fileName: ".drone.yml",
    32  			code: util.Dedent(`
    33  				kind: pipeline
    34  				name: default
    35  
    36  				steps:
    37  				- name: test
    38  					image: golang:1.13
    39  					environment:
    40  						GOPROXY: https://goproxy.cn
    41  					commands:
    42  					- go get -u
    43  					- go build -v
    44  					- go test -v -race -coverprofile=coverage.txt -covermode=atomic
    45  			`),
    46  			want: util.Dedent(`
    47  				<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span><span class="w">
    48  				</span>
    49  				<span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span><span class="w">
    50  				</span>
    51  				<span class="w">
    52  				</span>
    53  				<span class="w"></span><span class="nt">steps</span><span class="p">:</span><span class="w">
    54  				</span>
    55  				<span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span><span class="w">
    56  				</span>
    57  				<span class="w">	</span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span><span class="w">
    58  				</span>
    59  				<span class="w">	</span><span class="nt">environment</span><span class="p">:</span><span class="w">
    60  				</span>
    61  				<span class="w"></span><span class="w">		</span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span><span class="w">
    62  				</span>
    63  				<span class="w">	</span><span class="nt">commands</span><span class="p">:</span><span class="w">
    64  				</span>
    65  				<span class="w"></span><span class="w">	</span>- <span class="l">go get -u</span><span class="w">
    66  				</span>
    67  				<span class="w">	</span>- <span class="l">go build -v</span><span class="w">
    68  				</span>
    69  				<span class="w">	</span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>
    70  			`),
    71  		},
    72  		{
    73  			name:     ".drone.yml - trailing space",
    74  			numLines: 13,
    75  			fileName: ".drone.yml",
    76  			code: strings.Replace(util.Dedent(`
    77  				kind: pipeline
    78  				name: default
    79  
    80  				steps:
    81  				- name: test
    82  					image: golang:1.13
    83  					environment:
    84  						GOPROXY: https://goproxy.cn
    85  					commands:
    86  					- go get -u
    87  					- go build -v
    88  					- go test -v -race -coverprofile=coverage.txt -covermode=atomic
    89  			`)+"\n", "name: default", "name: default  ", 1),
    90  			want: util.Dedent(`
    91  				<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span><span class="w">
    92  				</span>
    93  				<span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default  </span><span class="w">
    94  				</span>
    95  				<span class="w">
    96  				</span>
    97  				<span class="w"></span><span class="nt">steps</span><span class="p">:</span><span class="w">
    98  				</span>
    99  				<span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span><span class="w">
   100  				</span>
   101  				<span class="w">	</span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span><span class="w">
   102  				</span>
   103  				<span class="w">	</span><span class="nt">environment</span><span class="p">:</span><span class="w">
   104  				</span>
   105  				<span class="w"></span><span class="w">		</span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span><span class="w">
   106  				</span>
   107  				<span class="w">	</span><span class="nt">commands</span><span class="p">:</span><span class="w">
   108  				</span>
   109  				<span class="w"></span><span class="w">	</span>- <span class="l">go get -u</span><span class="w">
   110  				</span>
   111  				<span class="w">	</span>- <span class="l">go build -v</span><span class="w">
   112  				</span>
   113  				<span class="w">	</span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span><span class="w">
   114  				</span>
   115  				<span class="w">
   116  				</span>
   117  			`),
   118  		},
   119  	}
   120  
   121  	for _, tt := range tests {
   122  		t.Run(tt.name, func(t *testing.T) {
   123  			got := strings.Join(File(tt.numLines, tt.fileName, "", []byte(tt.code)), "\n")
   124  			assert.Equal(t, tt.want, got)
   125  		})
   126  	}
   127  }