Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not html escape repo view output #1657

Merged
merged 1 commit into from Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cmd/repo/view/view.go
Expand Up @@ -2,9 +2,9 @@ package view

import (
"fmt"
"html/template"
"net/http"
"strings"
"text/template"

"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/api"
Expand Down
83 changes: 83 additions & 0 deletions pkg/cmd/repo/view/view_test.go
Expand Up @@ -515,3 +515,86 @@ func Test_ViewRun_WithoutUsername(t *testing.T) {
assert.Equal(t, "", stderr.String())
reg.Verify(t)
}

func Test_ViewRun_HandlesSpecialCharacters(t *testing.T) {
tests := []struct {
name string
opts *ViewOptions
repoName string
stdoutTTY bool
wantOut string
wantStderr string
wantErr bool
}{
{
name: "nontty",
wantOut: heredoc.Doc(`
name: OWNER/REPO
description: Some basic special characters " & / < > '
--
# < is always > than & ' and "
`),
},
{
name: "no args",
stdoutTTY: true,
wantOut: heredoc.Doc(`
OWNER/REPO
Some basic special characters " & / < > '


# < is always > than & ' and "



View this repository on GitHub: https://github.com/OWNER/REPO
`),
},
}
for _, tt := range tests {
if tt.opts == nil {
tt.opts = &ViewOptions{}
}

if tt.repoName == "" {
tt.repoName = "OWNER/REPO"
}

tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
repo, _ := ghrepo.FromFullName(tt.repoName)
return repo, nil
}

reg := &httpmock.Registry{}
reg.Register(
httpmock.GraphQL(`query RepositoryInfo\b`),
httpmock.StringResponse(`
{ "data": {
"repository": {
"description": "Some basic special characters \" & / < > '"
} } }`))
reg.Register(
httpmock.REST("GET", fmt.Sprintf("repos/%s/readme", tt.repoName)),
httpmock.StringResponse(`
{ "name": "readme.md",
"content": "IyA8IGlzIGFsd2F5cyA+IHRoYW4gJiAnIGFuZCAi"}`))

tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}

io, _, stdout, stderr := iostreams.Test()
tt.opts.IO = io

t.Run(tt.name, func(t *testing.T) {
io.SetStdoutTTY(tt.stdoutTTY)

if err := viewRun(tt.opts); (err != nil) != tt.wantErr {
t.Errorf("viewRun() error = %v, wantErr %v", err, tt.wantErr)
}
assert.Equal(t, tt.wantStderr, stderr.String())
assert.Equal(t, tt.wantOut, stdout.String())
reg.Verify(t)
})
}
}