2021-08-14 01:16:56 +02:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package highlight
|
|
|
|
|
|
|
|
import (
|
2022-06-10 15:45:28 +02:00
|
|
|
"strings"
|
2021-08-14 01:16:56 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-10 15:45:28 +02:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-11-17 13:34:35 +01:00
|
|
|
|
2022-06-10 15:45:28 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-08-14 01:16:56 +02:00
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFile(t *testing.T) {
|
|
|
|
setting.Cfg = ini.Empty()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
numLines int
|
|
|
|
fileName string
|
|
|
|
code string
|
2022-06-10 15:45:28 +02:00
|
|
|
want string
|
2021-08-14 01:16:56 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: ".drone.yml",
|
|
|
|
numLines: 12,
|
|
|
|
fileName: ".drone.yml",
|
2022-06-10 15:45:28 +02:00
|
|
|
code: util.Dedent(`
|
|
|
|
kind: pipeline
|
|
|
|
name: default
|
2021-08-14 01:16:56 +02:00
|
|
|
|
2022-06-10 15:45:28 +02:00
|
|
|
steps:
|
|
|
|
- name: test
|
|
|
|
image: golang:1.13
|
|
|
|
environment:
|
|
|
|
GOPROXY: https://goproxy.cn
|
|
|
|
commands:
|
|
|
|
- go get -u
|
|
|
|
- go build -v
|
|
|
|
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
|
|
|
`),
|
|
|
|
want: util.Dedent(`
|
2022-08-02 19:37:28 +02:00
|
|
|
<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<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">
|
|
|
|
</span>
|
|
|
|
<span class="w">
|
|
|
|
</span>
|
|
|
|
<span class="w"></span><span class="nt">steps</span><span class="p">:</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<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">
|
|
|
|
</span>
|
|
|
|
<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">
|
|
|
|
</span>
|
|
|
|
<span class="w"> </span><span class="nt">environment</span><span class="p">:</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<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">
|
|
|
|
</span>
|
|
|
|
<span class="w"> </span><span class="nt">commands</span><span class="p">:</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<span class="w"> </span>- <span class="l">go build -v</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>
|
2022-06-10 15:45:28 +02:00
|
|
|
`),
|
2021-08-14 01:16:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: ".drone.yml - trailing space",
|
|
|
|
numLines: 13,
|
|
|
|
fileName: ".drone.yml",
|
2022-06-10 15:45:28 +02:00
|
|
|
code: strings.Replace(util.Dedent(`
|
|
|
|
kind: pipeline
|
|
|
|
name: default
|
2021-08-14 01:16:56 +02:00
|
|
|
|
2022-06-10 15:45:28 +02:00
|
|
|
steps:
|
|
|
|
- name: test
|
|
|
|
image: golang:1.13
|
|
|
|
environment:
|
|
|
|
GOPROXY: https://goproxy.cn
|
|
|
|
commands:
|
|
|
|
- go get -u
|
|
|
|
- go build -v
|
|
|
|
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
|
|
|
`)+"\n", "name: default", "name: default ", 1),
|
|
|
|
want: util.Dedent(`
|
2022-08-02 19:37:28 +02:00
|
|
|
<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<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">
|
|
|
|
</span>
|
|
|
|
<span class="w">
|
|
|
|
</span>
|
|
|
|
<span class="w"></span><span class="nt">steps</span><span class="p">:</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<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">
|
|
|
|
</span>
|
|
|
|
<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">
|
|
|
|
</span>
|
|
|
|
<span class="w"> </span><span class="nt">environment</span><span class="p">:</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<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">
|
|
|
|
</span>
|
|
|
|
<span class="w"> </span><span class="nt">commands</span><span class="p">:</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<span class="w"> </span>- <span class="l">go build -v</span><span class="w">
|
|
|
|
</span>
|
|
|
|
<span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span><span class="w">
|
|
|
|
</span>
|
2022-06-10 15:45:28 +02:00
|
|
|
<span class="w">
|
|
|
|
</span>
|
|
|
|
`),
|
2021-08-14 01:16:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-06-10 15:45:28 +02:00
|
|
|
got := strings.Join(File(tt.numLines, tt.fileName, "", []byte(tt.code)), "\n")
|
|
|
|
assert.Equal(t, tt.want, got)
|
2021-08-14 01:16:56 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|