Browse Source

create temp file for repo release test

This is primarily needed to allow the test to run unmodified in certain
specialized environments (namely, Google's build clusters)
Will Norris 12 years ago
parent
commit
2d7c049105
3 changed files with 43 additions and 14 deletions
  1. +38
    -10
      github/github_test.go
  2. +5
    -3
      github/repos_releases_test.go
  3. +0
    -1
      github/testdata/upload.txt

+ 38
- 10
github/github_test.go View File

@ -13,6 +13,8 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"reflect"
"strings"
"testing"
@ -50,6 +52,33 @@ func teardown() {
server.Close()
}
// openTestFile creates a new file with the given name and content for testing.
// In order to ensure the exact file name, this function will create a new temp
// directory, and create the file in that directory. It is the caller's
// responsibility to remove the directy and its contents when no longer needed.
func openTestFile(name, content string) (file *os.File, dir string, err error) {
dir, err = ioutil.TempDir("", "go-github")
if err != nil {
return nil, dir, err
}
file, err = os.OpenFile(path.Join(dir, name), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return nil, dir, err
}
fmt.Fprint(file, content)
// close and re-open the file to keep file.Stat() happy
file.Close()
file, err = os.Open(file.Name())
if err != nil {
return nil, dir, err
}
return file, dir, err
}
func testMethod(t *testing.T, r *http.Request, want string) {
if want != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, want)
@ -85,17 +114,16 @@ func testURLParseError(t *testing.T, err error) {
}
}
func testBody(t *testing.T, r *http.Request, want string){
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Unable to read body")
}
str := string(b)
if want != str {
t.Errorf("Body = %s, want: %s", str, want)
}
func testBody(t *testing.T, r *http.Request, want string) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Unable to read body")
}
str := string(b)
if want != str {
t.Errorf("Body = %s, want: %s", str, want)
}
}
// Helper function to test that a value is marshalled to JSON as expected.
func testJSONMarshal(t *testing.T, v interface{}, want string) {


+ 5
- 3
github/repos_releases_test.go View File

@ -215,11 +215,13 @@ func TestRepositoriesService_UploadReleaseAsset(t *testing.T) {
fmt.Fprintf(w, `{"id":1}`)
})
opt := &UploadOptions{Name: "n"}
file, err := os.Open("testdata/upload.txt")
file, dir, err := openTestFile("upload.txt", "Upload me !\n")
if err != nil {
t.Errorf("Unable to open file testdata/upload.txt")
t.Fatalf("Unable to create temp file: %v", err)
}
defer os.RemoveAll(dir)
opt := &UploadOptions{Name: "n"}
asset, _, err := client.Repositories.UploadReleaseAsset("o", "r", 1, opt, file)
if err != nil {
t.Errorf("Repositories.UploadReleaseAssert returned error: %v", err)


+ 0
- 1
github/testdata/upload.txt View File

@ -1 +0,0 @@
Upload me !

Loading…
Cancel
Save