handle github api rate limit, tests for the update command, and fix timezone bug in tests

This commit is contained in:
David Dworken 2022-04-09 12:50:01 -07:00
parent 99794191dc
commit c8ba560e0c
3 changed files with 39 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"log"
"net/http"
"os"
"strings"
"time"
"github.com/ddworken/hishtory/shared"
@ -159,15 +160,19 @@ func updateReleaseVersion() {
fmt.Printf("failed to get latest release version: %v\n", err)
return
}
if resp.StatusCode != 200 {
fmt.Printf("failed to call github API, status_code=%d\n", resp.StatusCode)
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("failed to read github API response body: %v\n", err)
return
}
if resp.StatusCode == 403 && strings.Contains(string(respBody), "API rate limit exceeded for ") {
// cannot update ReleaseVersion because we exceeded the rate limit, fail silently
return
}
if resp.StatusCode != 200 {
fmt.Printf("failed to call github API, status_code=%d, body=%#v\n", resp.StatusCode, string(respBody))
return
}
var info releaseInfo
err = json.Unmarshal(respBody, &info)
if err != nil {

View File

@ -349,6 +349,31 @@ func TestAdvancedQuery(t *testing.T) {
// TODO: test the username,hostname atoms
}
func TestUpdate(t *testing.T) {
// Set up
defer shared.BackupAndRestore(t)()
defer shared.RunTestServer(t)()
userSecret := installHishtory(t, "")
// Check the status command
out := RunInteractiveBashCommands(t, `hishtory status`)
if out != fmt.Sprintf("Hishtory: e2e sync\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
t.Fatalf("status command has unexpected output: %#v", out)
}
// Update
RunInteractiveBashCommands(t, `hishtory update`)
// Then check the status command again to confirm the update worked
out = RunInteractiveBashCommands(t, `hishtory status`)
if !strings.HasPrefix(out, fmt.Sprintf("Hishtory: e2e sync\nEnabled: true\nSecret Key: %s\nCommit Hash: ", userSecret)) {
t.Fatalf("status command has unexpected output: %#v", out)
}
if strings.Contains(out, "\nCommit Hash: Unknown\n") {
t.Fatalf("status command has unexpected output: %#v", out)
}
}
func TestGithubRedirects(t *testing.T) {
// Set up
defer shared.BackupAndRestore(t)()

View File

@ -53,8 +53,11 @@ func TestBuildHistoryEntry(t *testing.T) {
if entry.Command != "ls /" {
t.Fatalf("history entry has unexpected command: %v", entry.Command)
}
if entry.StartTime.Format(time.RFC3339) != "2022-01-09T16:35:58-08:00" {
t.Fatalf("history entry has incorrect start time: %v", entry.StartTime.Format(time.RFC3339))
if !strings.HasPrefix(entry.StartTime.Format(time.RFC3339), "2022-01-09T") {
t.Fatalf("history entry has incorrect date in the start time: %v", entry.StartTime.Format(time.RFC3339))
}
if entry.StartTime.Unix() != 1641774958 {
t.Fatalf("history entry has incorrect Unix time in the start time: %v", entry.StartTime.Unix())
}
}