From 2d7c049105604a76580977072763fd9991bfc7f0 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 30 Jan 2014 10:30:11 -0800 Subject: [PATCH] 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) --- github/github_test.go | 48 +++++++++++++++++++++++++++-------- github/repos_releases_test.go | 8 +++--- github/testdata/upload.txt | 1 - 3 files changed, 43 insertions(+), 14 deletions(-) delete mode 100644 github/testdata/upload.txt diff --git a/github/github_test.go b/github/github_test.go index 835592b..2ae4fb8 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -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) { diff --git a/github/repos_releases_test.go b/github/repos_releases_test.go index 6ae08b1..56e97d4 100644 --- a/github/repos_releases_test.go +++ b/github/repos_releases_test.go @@ -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) diff --git a/github/testdata/upload.txt b/github/testdata/upload.txt deleted file mode 100644 index 5863dc7..0000000 --- a/github/testdata/upload.txt +++ /dev/null @@ -1 +0,0 @@ -Upload me !