diff --git a/go.mod b/go.mod index 23e922ecef2df..996488478bef4 100644 --- a/go.mod +++ b/go.mod @@ -206,6 +206,7 @@ require ( github.com/kr/pretty v0.3.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/libdns/libdns v0.2.1 // indirect + github.com/lithammer/dedent v1.1.0 // indirect github.com/magiconair/properties v1.8.5 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/markbates/going v1.0.0 // indirect diff --git a/go.sum b/go.sum index 215450970fd61..148010199a681 100644 --- a/go.sum +++ b/go.sum @@ -1086,6 +1086,8 @@ github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis= github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= +github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY= +github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc= github.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96 h1:uNwtsDp7ci48vBTTxDuwcoTXz4lwtDTe7TjCQ0noaWY= github.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96/go.mod h1:mmIfjCSQlGYXmJ95jFN84AkQFnVABtKuJL8IrzwvUKQ= github.com/lunny/log v0.0.0-20160921050905-7887c61bf0de/go.mod h1:3q8WtuPQsoRbatJuy3nvq/hRSvuBJrHHr+ybPPiNvHQ= diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index 344be78144ec5..a72f26d5f0755 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -203,6 +203,8 @@ func File(numLines int, fileName, language string, code []byte) []string { content = "\n" } else if content == `` { content += "\n" + } else if content == `` { + content += "\n" } content = strings.TrimSuffix(content, ``) content = strings.TrimPrefix(content, ``) diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index 2f305bb589c93..b3152768189cc 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -5,11 +5,13 @@ package highlight import ( - "reflect" + "strings" "testing" "code.gitea.io/gitea/modules/setting" + "github.com/lithammer/dedent" + "github.com/stretchr/testify/assert" "gopkg.in/ini.v1" ) @@ -20,83 +22,83 @@ func TestFile(t *testing.T) { numLines int fileName string code string - want []string + want string }{ { name: ".drone.yml", numLines: 12, fileName: ".drone.yml", - code: `kind: pipeline -name: default + code: strings.TrimSpace(dedent.Dedent(` + kind: pipeline + name: default -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: []string{ - `kind: pipeline`, - `name: default`, - ``, - `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 -`, - ` -`, - }, + 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: strings.TrimSpace(dedent.Dedent(` + kind: pipeline + name: default + + 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 + `)), }, { name: ".drone.yml - trailing space", numLines: 13, fileName: ".drone.yml", - code: `kind: pipeline -name: default ` + ` + code: strings.Replace(strings.TrimSpace(dedent.Dedent(` + kind: pipeline + name: default -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: []string{ - `kind: pipeline`, - `name: default `, - ``, - `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`, - ` `, - }, + 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: strings.TrimSpace(dedent.Dedent(` + kind: pipeline + name: default + + 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 + + + + `)), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := File(tt.numLines, tt.fileName, "", []byte(tt.code)); !reflect.DeepEqual(got, tt.want) { - t.Errorf("File() = %v, want %v", got, tt.want) - } + got := strings.Join(File(tt.numLines, tt.fileName, "", []byte(tt.code)), "\n") + assert.Equal(t, tt.want, got) }) } }