mirror of
https://github.com/ddworken/hishtory.git
synced 2025-08-19 03:06:45 +02:00
handle github api rate limit, tests for the update command, and fix timezone bug in tests
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
@@ -159,15 +160,19 @@ func updateReleaseVersion() {
|
|||||||
fmt.Printf("failed to get latest release version: %v\n", err)
|
fmt.Printf("failed to get latest release version: %v\n", err)
|
||||||
return
|
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)
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to read github API response body: %v\n", err)
|
fmt.Printf("failed to read github API response body: %v\n", err)
|
||||||
return
|
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
|
var info releaseInfo
|
||||||
err = json.Unmarshal(respBody, &info)
|
err = json.Unmarshal(respBody, &info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -349,6 +349,31 @@ func TestAdvancedQuery(t *testing.T) {
|
|||||||
// TODO: test the username,hostname atoms
|
// 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) {
|
func TestGithubRedirects(t *testing.T) {
|
||||||
// Set up
|
// Set up
|
||||||
defer shared.BackupAndRestore(t)()
|
defer shared.BackupAndRestore(t)()
|
||||||
|
@@ -53,8 +53,11 @@ func TestBuildHistoryEntry(t *testing.T) {
|
|||||||
if entry.Command != "ls /" {
|
if entry.Command != "ls /" {
|
||||||
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
||||||
}
|
}
|
||||||
if entry.StartTime.Format(time.RFC3339) != "2022-01-09T16:35:58-08:00" {
|
if !strings.HasPrefix(entry.StartTime.Format(time.RFC3339), "2022-01-09T") {
|
||||||
t.Fatalf("history entry has incorrect start time: %v", entry.StartTime.Format(time.RFC3339))
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user