Skip to content

Commit

Permalink
Do not html escape repo view output
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Coe committed Sep 10, 2020
1 parent 33e11f0 commit e50833c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
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)
})
}
}

0 comments on commit e50833c

Please sign in to comment.