-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add tests for autoupdate and GitHub API
- Loading branch information
Showing
7 changed files
with
245 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package builder | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/meroxa/cli/cmd/meroxa/global" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func TestNeedToCheckNewerCLIVersion(t *testing.T) { | ||
oldConfig := global.Config | ||
|
||
tests := []struct { | ||
desc string | ||
config func() *viper.Viper | ||
expected bool | ||
}{ | ||
{ | ||
desc: "Global config is nil", | ||
config: func() *viper.Viper { | ||
return nil | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
desc: "Notifications are disabled by the user", | ||
config: func() *viper.Viper { | ||
cfg := viper.New() | ||
cfg.Set(global.DisableNotificationsUpdate, "true") | ||
return cfg | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
desc: "Version has never been checked", | ||
config: viper.New, | ||
expected: true, | ||
}, | ||
{ | ||
desc: "Version was recently checked", | ||
config: func() *viper.Viper { | ||
cfg := viper.New() | ||
// Checked last time less than a week before | ||
lastTimeChecked := time.Now().UTC().AddDate(0, 0, -7).Add(time.Minute * 1) | ||
cfg.Set(global.LatestCLIVersionUpdatedAtEnv, lastTimeChecked) | ||
return cfg | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
desc: "Version was checked more than a week before", | ||
config: func() *viper.Viper { | ||
cfg := viper.New() | ||
// Checked last time more than a week before | ||
lastTimeChecked := time.Now().UTC().AddDate(0, 0, -7).Add(-time.Minute * 1) | ||
cfg.Set(global.LatestCLIVersionUpdatedAtEnv, lastTimeChecked) | ||
return cfg | ||
}, | ||
expected: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
global.Config = tc.config() | ||
got := needToCheckNewerCLIVersion() | ||
|
||
if tc.expected != got { | ||
t.Fatalf("expected needToCheckNewerCLIVersion to be %v, got %v", tc.expected, got) | ||
} | ||
}) | ||
} | ||
|
||
global.Config = oldConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright © 2022 Meroxa Inc | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package github | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"regexp" | ||
) | ||
|
||
type HTTPClient interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
var ( | ||
Client HTTPClient | ||
) | ||
|
||
// getContentHomebrewFormula returns from GitHub the content of the formula file for Meroxa CLI. | ||
func getContentHomebrewFormula(ctx context.Context) (string, error) { | ||
url := "https://api.github.com/repos/meroxa/homebrew-taps/contents/Formula/meroxa.rb" | ||
req, err := http.NewRequestWithContext(ctx, "GET", url, http.NoBody) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
req.Header.Add("Accept", "application/vnd.github.v3+json") | ||
resp, err := Client.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if resp.Body != nil { | ||
defer resp.Body.Close() | ||
} | ||
|
||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
type FormulaDefinition struct { | ||
Content []byte `json:"content,omitempty"` | ||
Encoding string `json:"encoding,omitempty"` | ||
} | ||
|
||
var f FormulaDefinition | ||
if err := json.Unmarshal(b, &f); err != nil { | ||
return "", err | ||
} | ||
|
||
return bytes.NewBuffer(f.Content).String(), nil | ||
} | ||
|
||
// parseVersionFromFormulaFile receives the content of a formula file such as the one in | ||
// "https://api.github.com/repos/meroxa/homebrew-taps/contents/Formula/meroxa.rb" | ||
// and extracts its version number. | ||
func parseVersionFromFormulaFile(content string) string { | ||
r := regexp.MustCompile(`version "(\d+.\d+.\d+)"`) | ||
return r.FindStringSubmatch(content)[1] | ||
} | ||
|
||
// GetLatestCLITag fetches the content formula file from GitHub and then parses its version. | ||
func GetLatestCLITag(ctx context.Context) (string, error) { | ||
brewFormulaFile, err := getContentHomebrewFormula(ctx) | ||
if err != nil { | ||
return brewFormulaFile, err | ||
} | ||
|
||
return parseVersionFromFormulaFile(brewFormulaFile), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package github | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type MockDoType func(req *http.Request) (*http.Response, error) | ||
|
||
type MockClient struct { | ||
MockDo MockDoType | ||
} | ||
|
||
func (m *MockClient) Do(req *http.Request) (*http.Response, error) { | ||
return m.MockDo(req) | ||
} | ||
|
||
func TestGetLatestCLITag(t *testing.T) { | ||
version := "2.2.0" | ||
|
||
f := fmt.Sprintf(` | ||
class Meroxa < Formula | ||
desc "The Meroxa CLI" | ||
homepage "https://meroxa.io" | ||
version "%s" | ||
end | ||
`, version) | ||
|
||
sEnc := base64.StdEncoding.EncodeToString([]byte(f)) | ||
|
||
jsonResponse := fmt.Sprintf(` | ||
{ | ||
"content": "%s", | ||
"encoding": "base64" | ||
} | ||
`, sEnc) | ||
|
||
r := io.NopCloser(bytes.NewReader([]byte(jsonResponse))) | ||
Client = &MockClient{ | ||
MockDo: func(*http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: r, | ||
}, nil | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
gotContent, err := getContentHomebrewFormula(ctx) | ||
|
||
if err != nil { | ||
t.Error("unexpected error, got: ", err) | ||
return | ||
} | ||
|
||
if !reflect.DeepEqual(gotContent, f) { | ||
t.Errorf("expected %v, got %v", f, gotContent) | ||
} | ||
|
||
gotVersion := parseVersionFromFormulaFile(gotContent) | ||
|
||
if gotVersion != version { | ||
t.Errorf("expected version %q, got %q", version, gotVersion) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters