No Description

main_test.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "bytes"
  4. "io"
  5. "mime/multipart"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "testing"
  10. )
  11. func TestToInt(t *testing.T) {
  12. tempInt := toInt("20")
  13. if tempInt != 20 {
  14. t.Errorf("toInt returned %v, should be %v", tempInt, 20)
  15. }
  16. }
  17. func TestParseProfile(t *testing.T) {
  18. tempProfs, err := ParseProfiles("samples/coverage.out")
  19. if err != nil {
  20. t.Errorf("ParseProfile returned error: %v", err)
  21. }
  22. if len(tempProfs) != 1 {
  23. t.Errorf("ParseProfile length returned %v, should be %v", len(tempProfs), 1)
  24. }
  25. if tempProfs[0].FileName != "github.com/esell/hoptocopter/main.go" {
  26. t.Errorf("ParseProfile FileName returned %v, should be %v", tempProfs[0].FileName, "github.com/esell/hoptocopter/main.go")
  27. }
  28. }
  29. func TestUploadHandler(t *testing.T) {
  30. config := conf{ListenPort: "9666", ShieldURL: "blah.com"}
  31. uploadHandle := uploadHandler(config)
  32. // GET
  33. req, _ := http.NewRequest("GET", "", nil)
  34. w := httptest.NewRecorder()
  35. uploadHandle.ServeHTTP(w, req)
  36. if w.Code != http.StatusMethodNotAllowed {
  37. t.Errorf("uploadHandler GET returned %v, should be %v", w.Code, http.StatusMethodNotAllowed)
  38. }
  39. // POST
  40. sampleCover, err := os.Open("samples/coverage.out")
  41. if err != nil {
  42. t.Errorf("error opening sample coverage file: %s", err)
  43. }
  44. defer sampleCover.Close()
  45. body := &bytes.Buffer{}
  46. writer := multipart.NewWriter(body)
  47. part, err := writer.CreateFormFile("file", "coverage.out")
  48. if err != nil {
  49. t.Errorf("error FormFile: %s", err)
  50. }
  51. _, err = io.Copy(part, sampleCover)
  52. if err != nil {
  53. t.Errorf("error copying sampleCover to FormFile: %s", err)
  54. }
  55. if err := writer.Close(); err != nil {
  56. t.Errorf("error closing form writer: %s", err)
  57. }
  58. req, _ = http.NewRequest("POST", "?repo=blah", body)
  59. req.Header.Add("Content-Type", writer.FormDataContentType())
  60. w = httptest.NewRecorder()
  61. uploadHandle.ServeHTTP(w, req)
  62. if w.Code != http.StatusSeeOther {
  63. t.Errorf("uploadHandler POST returned %v, should be %v", w.Code, http.StatusSeeOther)
  64. }
  65. // cleanup
  66. if err := os.RemoveAll("blah"); err != nil {
  67. t.Errorf("error cleaning up after uploadHandler(): %s", err)
  68. }
  69. }
  70. func TestDisplayHandler(t *testing.T) {
  71. config := conf{ListenPort: "9666", ShieldURL: "https://img.shields.io/badge"}
  72. displayHandle := displayHandler(config)
  73. // GET
  74. req, _ := http.NewRequest("GET", "", nil)
  75. w := httptest.NewRecorder()
  76. displayHandle.ServeHTTP(w, req)
  77. if w.Code != http.StatusSeeOther {
  78. t.Errorf("displayHandler GET returned %v, should be %v", w.Code, http.StatusSeeOther)
  79. }
  80. // POST
  81. req, _ = http.NewRequest("POST", "?repo=blah", nil)
  82. w = httptest.NewRecorder()
  83. displayHandle.ServeHTTP(w, req)
  84. if w.Code != http.StatusMethodNotAllowed {
  85. t.Errorf("displayHandler POST returned %v, should be %v", w.Code, http.StatusMethodNotAllowed)
  86. }
  87. // real GET - should fail (500)
  88. req, _ = http.NewRequest("GET", "?repo=blah", nil)
  89. w = httptest.NewRecorder()
  90. displayHandle.ServeHTTP(w, req)
  91. if w.Code != http.StatusInternalServerError {
  92. t.Errorf("displayHandler GET returned %v, should be %v", w.Code, http.StatusInternalServerError)
  93. }
  94. // real GET - should work
  95. // stage test files
  96. origCvr, err := os.Open("samples/coverage.out")
  97. if err != nil {
  98. t.Errorf("error opening up sample coverage.out: %s", err)
  99. }
  100. defer origCvr.Close()
  101. if err := os.MkdirAll("blah", 0755); err != nil {
  102. t.Errorf("error creating directory for %s: %s\n", "blah", err)
  103. }
  104. copyCvr, err := os.Create("blah/coverage.out")
  105. if err != nil {
  106. t.Errorf("error creating copy of coverage.out: %s", err)
  107. }
  108. _, err = io.Copy(copyCvr, origCvr)
  109. if err != nil {
  110. t.Errorf("error writing copy of coverage.out: %s", err)
  111. }
  112. if err := copyCvr.Close(); err != nil {
  113. t.Errorf("error saving copy of coverage.out: %s", err)
  114. }
  115. req, _ = http.NewRequest("GET", "?repo=blah", nil)
  116. w = httptest.NewRecorder()
  117. displayHandle.ServeHTTP(w, req)
  118. if w.Code != http.StatusOK {
  119. t.Errorf("displayHandler GET returned %v, should be %v", w.Code, http.StatusOK)
  120. }
  121. }