|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "os" |
| 11 | + "rockstaedt/commit-message-check/internal/model" |
| 12 | + "runtime" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +func TestUpdate(t *testing.T) { |
| 17 | + buffer := &bytes.Buffer{} |
| 18 | + log.SetOutput(buffer) |
| 19 | + |
| 20 | + t.Run("returns 0 and", func(t *testing.T) { |
| 21 | + |
| 22 | + t.Run("logs a message when local version is latest", func(t *testing.T) { |
| 23 | + buffer.Reset() |
| 24 | + ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.0.0"}`)) |
| 25 | + defer ts.Close() |
| 26 | + config := &model.UpdateConfig{Version: "v1.0.0", TagUrl: ts.URL} |
| 27 | + |
| 28 | + status := Update(config) |
| 29 | + |
| 30 | + assert.Equal(t, 0, status) |
| 31 | + assert.Contains(t, buffer.String(), "Current version is latest version.") |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("downloads install script if newer version available", func(t *testing.T) { |
| 35 | + ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.1.0"}`)) |
| 36 | + defer ts.Close() |
| 37 | + tempDir := t.TempDir() |
| 38 | + config := &model.UpdateConfig{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: tempDir} |
| 39 | + |
| 40 | + _ = Update(config) |
| 41 | + |
| 42 | + assert.FileExists(t, tempDir+"/commit-message-check") |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("returns 1 and message when error at request", func(t *testing.T) { |
| 47 | + buffer.Reset() |
| 48 | + ts := httptest.NewServer(getHandlerFor("", 500)) |
| 49 | + defer ts.Close() |
| 50 | + config := &model.UpdateConfig{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: ""} |
| 51 | + |
| 52 | + status := Update(config) |
| 53 | + |
| 54 | + assert.Equal(t, 1, status) |
| 55 | + assert.Contains(t, buffer.String(), "Error at retrieving latest version.") |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func TestGetLatestTag(t *testing.T) { |
| 60 | + t.Run("returns latest tag when request is successfully", func(t *testing.T) { |
| 61 | + ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.2.0"}`)) |
| 62 | + defer ts.Close() |
| 63 | + |
| 64 | + tag := getLatestTag(ts.URL) |
| 65 | + |
| 66 | + assert.Equal(t, "v1.2.0", tag) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("returns empty string when", func(t *testing.T) { |
| 70 | + |
| 71 | + t.Run("HTTP protocol error", func(t *testing.T) { |
| 72 | + tag := getLatestTag("xxx") |
| 73 | + |
| 74 | + assert.Empty(t, tag) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("response status code is not 200", func(t *testing.T) { |
| 78 | + ts := httptest.NewServer(getHandlerFor("", 500)) |
| 79 | + defer ts.Close() |
| 80 | + |
| 81 | + tag := getLatestTag(ts.URL) |
| 82 | + |
| 83 | + assert.Empty(t, tag) |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("response body is empty", func(t *testing.T) { |
| 87 | + ts := httptest.NewServer(getHandlerFor("")) |
| 88 | + defer ts.Close() |
| 89 | + |
| 90 | + tag := getLatestTag(ts.URL) |
| 91 | + |
| 92 | + assert.Empty(t, tag) |
| 93 | + }) |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +func getHandlerFor(resBody string, statusCode ...int) http.HandlerFunc { |
| 98 | + sc := 200 |
| 99 | + if len(statusCode) > 0 { |
| 100 | + sc = statusCode[0] |
| 101 | + } |
| 102 | + |
| 103 | + return func(w http.ResponseWriter, r *http.Request) { |
| 104 | + w.WriteHeader(sc) |
| 105 | + w.Header().Set("Content-Type", "application/json") |
| 106 | + _, _ = w.Write([]byte(resBody)) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestDownloadScript(t *testing.T) { |
| 111 | + |
| 112 | + getProtectedPath := func(t *testing.T) string { |
| 113 | + tempDir := t.TempDir() |
| 114 | + protectedPath := tempDir + "/protected" |
| 115 | + err := os.Mkdir(protectedPath, 0000) |
| 116 | + assert.Nil(t, err) |
| 117 | + |
| 118 | + return protectedPath |
| 119 | + } |
| 120 | + |
| 121 | + t.Run("returns 0 and writes downloaded binary content to file", func(t *testing.T) { |
| 122 | + tempDir := t.TempDir() |
| 123 | + err := os.WriteFile(tempDir+"/dummy", []byte("i am a go binary"), os.ModePerm) |
| 124 | + assert.Nil(t, err) |
| 125 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 126 | + targetUrl := fmt.Sprintf("/commit-message-check-v1.1.1-%s-%s", runtime.GOOS, runtime.GOARCH) |
| 127 | + if r.URL.String() == targetUrl { |
| 128 | + http.ServeFile(w, r, tempDir+"/dummy") |
| 129 | + } |
| 130 | + })) |
| 131 | + defer ts.Close() |
| 132 | + config := &model.UpdateConfig{LatestVersion: "v1.1.1", DownloadPath: tempDir, BinaryBaseUrl: ts.URL} |
| 133 | + |
| 134 | + status := downloadScript(config) |
| 135 | + |
| 136 | + assert.Equal(t, 0, status) |
| 137 | + contentBytes, err := os.ReadFile(tempDir + "/commit-message-check") |
| 138 | + assert.Nil(t, err) |
| 139 | + assert.Contains(t, string(contentBytes), "i am a go binary") |
| 140 | + }) |
| 141 | + |
| 142 | + t.Run("returns 1 when error at creating file", func(t *testing.T) { |
| 143 | + config := &model.UpdateConfig{DownloadPath: getProtectedPath(t)} |
| 144 | + |
| 145 | + status := downloadScript(config) |
| 146 | + |
| 147 | + assert.Equal(t, 1, status) |
| 148 | + }) |
| 149 | + |
| 150 | + t.Run("return 2 when http protocol error", func(t *testing.T) { |
| 151 | + tempDir := t.TempDir() |
| 152 | + config := &model.UpdateConfig{DownloadPath: tempDir, BinaryBaseUrl: "/xxx"} |
| 153 | + |
| 154 | + status := downloadScript(config) |
| 155 | + |
| 156 | + assert.Equal(t, 2, status) |
| 157 | + }) |
| 158 | + |
| 159 | + t.Run("return 3 when request not successfully", func(t *testing.T) { |
| 160 | + tempDir := t.TempDir() |
| 161 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 162 | + w.WriteHeader(500) |
| 163 | + })) |
| 164 | + defer ts.Close() |
| 165 | + config := &model.UpdateConfig{DownloadPath: tempDir, BinaryBaseUrl: ts.URL} |
| 166 | + |
| 167 | + status := downloadScript(config) |
| 168 | + |
| 169 | + assert.Equal(t, 3, status) |
| 170 | + }) |
| 171 | +} |
0 commit comments