implemented after and before atoms w/ tests for them

This commit is contained in:
David Dworken 2022-04-08 18:23:17 -07:00
parent 1adcaeb6cf
commit 39b9b15d53
3 changed files with 93 additions and 2 deletions

View File

@ -271,6 +271,15 @@ func TestAdvancedQuery(t *testing.T) {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
// Query based on cwd without the slash
out = RunInteractiveBashCommands(t, `hishtory query cwd:tmp`)
if !strings.Contains(out, "echo querybydir") {
t.Fatalf("hishtory query doesn't contain result matching cwd:tmp, out=%#v", out)
}
if strings.Count(out, "\n") != 3 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
// Query based on cwd and another term
out = RunInteractiveBashCommands(t, `hishtory query cwd:/tmp querybydir`)
if !strings.Contains(out, "echo querybydir") {
@ -298,5 +307,31 @@ func TestAdvancedQuery(t *testing.T) {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
// Query based on before: and cwd:
out = RunInteractiveBashCommands(t, `hishtory query before:2025-07-02 cwd:/tmp`)
if strings.Count(out, "\n") != 3 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
out = RunInteractiveBashCommands(t, `hishtory query before:2025-07-02 cwd:tmp`)
if strings.Count(out, "\n") != 3 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
out = RunInteractiveBashCommands(t, `hishtory query before:2025-07-02 cwd:mp`)
if strings.Count(out, "\n") != 3 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
// Query based on after: and cwd:
out = RunInteractiveBashCommands(t, `hishtory query after:2020-07-02 cwd:/tmp`)
if strings.Count(out, "\n") != 3 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
// Query based on after: that returns no results
out = RunInteractiveBashCommands(t, `hishtory query after:2120-07-02 cwd:/tmp`)
if strings.Count(out, "\n") != 1 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
// TODO: test the username,hostname atoms
}

View File

@ -125,6 +125,31 @@ func DecryptHistoryEntry(userSecret string, entry shared.EncHistoryEntry) (Histo
return decryptedEntry, nil
}
func parseTimeGenerously(input string) (time.Time, error) {
t, err := time.Parse(time.RFC3339, input)
if err == nil {
return t, nil
}
_, offset := time.Now().Zone()
if offset%(60*60) != 0 {
panic("timezone isn't aligned on the hour! This is unimplemented!")
}
inputWithTimeZone := fmt.Sprintf("%s %03d00", input, (offset / 60 / 60))
t, err = time.Parse("2006-01-02T15:04:05 -0700", inputWithTimeZone)
if err == nil {
return t.Add(time.Hour), nil
}
t, err = time.Parse("2006-01-02T15:04 -0700", inputWithTimeZone)
if err == nil {
return t.Add(time.Hour), nil
}
t, err = time.Parse("2006-01-02", input)
if err == nil {
return t.Local(), nil
}
return time.Now(), fmt.Errorf("failed to parse time %#v, please format like \"2006-01-02T15:04\" or like \"2006-01-02\"", input)
}
func Search(db *gorm.DB, query string, limit int) ([]*HistoryEntry, error) {
tokens, err := tokenize(query)
if err != nil {
@ -146,9 +171,17 @@ func Search(db *gorm.DB, query string, limit int) ([]*HistoryEntry, error) {
case "exit_code":
tx = tx.Where("exit_code = ?", val)
case "before":
panic("TODO(ddworken): Implement before")
t, err := parseTimeGenerously(val)
if err != nil {
return nil, fmt.Errorf("failed to parse before:%s as a timestamp: %v", val, err)
}
tx = tx.Where("CAST(strftime(\"%s\",start_time) AS INTEGER) < ?", t.Unix())
case "after":
panic("TODO(ddworken): Implement after")
t, err := parseTimeGenerously(val)
if err != nil {
return nil, fmt.Errorf("failed to parse after:%s as a timestamp: %v", val, err)
}
tx = tx.Where("CAST(strftime(\"%s\",start_time) AS INTEGER) > ?", t.Unix())
default:
panic("TODO: probably return an error?")
}

View File

@ -21,3 +21,26 @@ func TestEncryptDecrypt(t *testing.T) {
t.Fatalf("Expected decrypt(encrypt(x)) to work, but it didn't!")
}
}
func TestParseTimeGenerously(t *testing.T) {
ts, err := parseTimeGenerously("2006-01-02T15:04:00-08:00")
shared.Check(t, err)
if ts.Unix() != 1136243040 {
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
}
ts, err = parseTimeGenerously("2006-01-02T15:04:00")
shared.Check(t, err)
if ts.Unix() != 1136243040 {
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
}
ts, err = parseTimeGenerously("2006-01-02T15:04")
shared.Check(t, err)
if ts.Unix() != 1136243040 {
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
}
ts, err = parseTimeGenerously("2006-01-02")
shared.Check(t, err)
if ts.Unix() != 1136160000 {
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
}
}