From d2465b1f9276c375bd72ea0e4892caf80c61ceed Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 24 May 2022 21:33:20 +0200 Subject: [PATCH] Fix copy/paste of empty newlines again Fixes: https://github.com/go-gitea/gitea/issues/19331 Regressed by: https://github.com/go-gitea/gitea/pull/18270 Needed to do another newline addition to the Chroma output HTML to get copy/paste work again. The previous replacement conditions are probably obsolete, but as I'm not 100% sure, I opted to keep them. Specifically, the Chroma HTML change mentioned in https://github.com/go-gitea/gitea/pull/18270#issuecomment-1013350246 broke our previous newline replacement for such empty lines. Also included are a few changes to make the test more pleasant to work with. --- go.mod | 1 + go.sum | 2 + modules/highlight/highlight.go | 2 + modules/highlight/highlight_test.go | 124 ++++++++++++++-------------- 4 files changed, 68 insertions(+), 61 deletions(-) diff --git a/go.mod b/go.mod index 23e922ecef2d..996488478bef 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 215450970fd6..148010199a68 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 344be78144ec..a72f26d5f075 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 2f305bb589c9..b3152768189c 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) }) } }