2015-06-13 15:53:45 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
2016-10-24 03:45:45 +00:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2015-06-13 15:53:45 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHistory(t *testing.T) {
|
|
|
|
maxHistory := 50
|
|
|
|
|
|
|
|
// Invalid arguments
|
2016-10-24 03:45:45 +00:00
|
|
|
var paths []string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// GOPATH should exist, so we shouldn't be able to override it
|
|
|
|
paths = []string{os.Getenv("GOPATH")}
|
|
|
|
} else {
|
|
|
|
paths = []string{"/etc", "/proc"}
|
2015-06-15 13:45:31 +00:00
|
|
|
}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
2015-06-15 13:45:31 +00:00
|
|
|
for _, path := range paths {
|
2015-06-13 15:53:45 +00:00
|
|
|
if _, e := NewHistory(path, maxHistory); e == nil {
|
|
|
|
t.Error("Error expected for: " + path)
|
|
|
|
}
|
|
|
|
}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
2023-07-16 08:14:22 +00:00
|
|
|
f, _ := os.CreateTemp("", "fzf-history")
|
2016-10-24 03:45:45 +00:00
|
|
|
f.Close()
|
|
|
|
|
2015-06-13 15:53:45 +00:00
|
|
|
{ // Append lines
|
2016-10-24 03:45:45 +00:00
|
|
|
h, _ := NewHistory(f.Name(), maxHistory)
|
2015-06-13 15:53:45 +00:00
|
|
|
for i := 0; i < maxHistory+10; i++ {
|
|
|
|
h.append("foobar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{ // Read lines
|
2016-10-24 03:45:45 +00:00
|
|
|
h, _ := NewHistory(f.Name(), maxHistory)
|
2015-06-13 15:53:45 +00:00
|
|
|
if len(h.lines) != maxHistory+1 {
|
|
|
|
t.Errorf("Expected: %d, actual: %d\n", maxHistory+1, len(h.lines))
|
|
|
|
}
|
|
|
|
for i := 0; i < maxHistory; i++ {
|
|
|
|
if h.lines[i] != "foobar" {
|
|
|
|
t.Error("Expected: foobar, actual: " + h.lines[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{ // Append lines
|
2016-10-24 03:45:45 +00:00
|
|
|
h, _ := NewHistory(f.Name(), maxHistory)
|
2015-06-13 15:53:45 +00:00
|
|
|
h.append("barfoo")
|
|
|
|
h.append("")
|
|
|
|
h.append("foobarbaz")
|
|
|
|
}
|
|
|
|
{ // Read lines again
|
2016-10-24 03:45:45 +00:00
|
|
|
h, _ := NewHistory(f.Name(), maxHistory)
|
2015-06-13 15:53:45 +00:00
|
|
|
if len(h.lines) != maxHistory+1 {
|
|
|
|
t.Errorf("Expected: %d, actual: %d\n", maxHistory+1, len(h.lines))
|
|
|
|
}
|
|
|
|
compare := func(idx int, exp string) {
|
|
|
|
if h.lines[idx] != exp {
|
|
|
|
t.Errorf("Expected: %s, actual: %s\n", exp, h.lines[idx])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
compare(maxHistory-3, "foobar")
|
|
|
|
compare(maxHistory-2, "barfoo")
|
|
|
|
compare(maxHistory-1, "foobarbaz")
|
|
|
|
}
|
|
|
|
}
|