From d5f0be217b681aafbd75f701f951c469234f6a48 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 8 Apr 2016 12:15:27 -0700 Subject: [PATCH] add RepoContent.GetContent to replace Decode Decode only works if r.Content is actually encoded, which I'm not sure that it always will be. GetContent will return the content, decoding it only if necessary. Deprecate the old Decode method. Also expand and cleanup the tests for both of these methods. Related to #323 --- github/repos_contents.go | 23 ++++++++ github/repos_contents_test.go | 98 +++++++++++++++++++++++++++++------ 2 files changed, 104 insertions(+), 17 deletions(-) diff --git a/github/repos_contents.go b/github/repos_contents.go index 113d001..84b3400 100644 --- a/github/repos_contents.go +++ b/github/repos_contents.go @@ -61,6 +61,8 @@ func (r RepositoryContent) String() string { } // Decode decodes the file content if it is base64 encoded. +// +// Deprecated: Use GetContent instead. func (r *RepositoryContent) Decode() ([]byte, error) { if *r.Encoding != "base64" { return nil, errors.New("cannot decode non-base64") @@ -72,6 +74,27 @@ func (r *RepositoryContent) Decode() ([]byte, error) { return o, nil } +// GetContent returns the content of r, decoding it if necessary. +func (r *RepositoryContent) GetContent() (string, error) { + var encoding string + if r.Encoding != nil { + encoding = *r.Encoding + } + + switch encoding { + case "base64": + c, err := base64.StdEncoding.DecodeString(*r.Content) + return string(c), err + case "": + if r.Content == nil { + return "", nil + } + return *r.Content, nil + default: + return "", fmt.Errorf("unsupported content encoding: %v", encoding) + } +} + // GetReadme gets the Readme file for the repository. // // GitHub API docs: http://developer.github.com/v3/repos/contents/#get-the-readme diff --git a/github/repos_contents_test.go b/github/repos_contents_test.go index d7f8651..543d8be 100644 --- a/github/repos_contents_test.go +++ b/github/repos_contents_test.go @@ -8,27 +8,91 @@ import ( "testing" ) -func TestDecode(t *testing.T) { - setup() - defer teardown() - r := RepositoryContent{Encoding: String("base64"), Content: String("aGVsbG8=")} - o, err := r.Decode() - if err != nil { - t.Errorf("Failed to decode content.") +func TestRepositoryContent_Decode(t *testing.T) { + tests := []struct { + encoding, content *string // input encoding and content + want string // desired output + wantErr bool // whether an error is expected + }{ + { + encoding: String("base64"), + content: String("aGVsbG8="), + want: "hello", + wantErr: false, + }, + { + encoding: String("bad"), + content: String("aGVsbG8="), + want: "", + wantErr: true, + }, } - want := "hello" - if string(o) != want { - t.Errorf("RepositoryContent.Decode returned %+v, want %+v", string(o), want) + + for _, tt := range tests { + r := RepositoryContent{Encoding: tt.encoding, Content: tt.content} + o, err := r.Decode() + if err != nil && !tt.wantErr { + t.Errorf("RepositoryContent(%q, %q) returned unexpected error: %v", tt.encoding, tt.content, err) + } + if err == nil && tt.wantErr { + t.Errorf("RepositoryContent(%q, %q) did not return unexpected error", tt.encoding, tt.content) + } + if got, want := string(o), tt.want; got != want { + t.Errorf("RepositoryContent.Decode returned %+v, want %+v", got, want) + } } } -func TestDecodeBadEncoding(t *testing.T) { - setup() - defer teardown() - r := RepositoryContent{Encoding: String("bad")} - _, err := r.Decode() - if err == nil { - t.Errorf("Should fail to decode non-base64") +func TestRepositoryContent_GetContent(t *testing.T) { + tests := []struct { + encoding, content *string // input encoding and content + want string // desired output + wantErr bool // whether an error is expected + }{ + { + encoding: String(""), + content: String("hello"), + want: "hello", + wantErr: false, + }, + { + encoding: nil, + content: String("hello"), + want: "hello", + wantErr: false, + }, + { + encoding: nil, + content: nil, + want: "", + wantErr: false, + }, + { + encoding: String("base64"), + content: String("aGVsbG8="), + want: "hello", + wantErr: false, + }, + { + encoding: String("bad"), + content: String("aGVsbG8="), + want: "", + wantErr: true, + }, + } + + for _, tt := range tests { + r := RepositoryContent{Encoding: tt.encoding, Content: tt.content} + got, err := r.GetContent() + if err != nil && !tt.wantErr { + t.Errorf("RepositoryContent(%q, %q) returned unexpected error: %v", tt.encoding, tt.content, err) + } + if err == nil && tt.wantErr { + t.Errorf("RepositoryContent(%q, %q) did not return unexpected error", tt.encoding, tt.content) + } + if want := tt.want; got != want { + t.Errorf("RepositoryContent.GetContent returned %+v, want %+v", got, want) + } } }