Fix tests on Windows.

This commit is contained in:
Qi Xiao 2019-11-16 21:17:11 +00:00
parent fa32890e81
commit c0d4bcd116
4 changed files with 299 additions and 143 deletions

View File

@ -3,6 +3,8 @@ package location
import (
"errors"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/elves/elvish/cli"
@ -67,19 +69,19 @@ func TestStart_Hidden(t *testing.T) {
defer cleanup()
dirs := []storedefs.Dir{
{Path: "/usr/bin", Score: 200},
{Path: "/usr", Score: 100},
{Path: "/tmp", Score: 50},
{Path: fix("/usr/bin"), Score: 200},
{Path: fix("/usr"), Score: 100},
{Path: fix("/tmp"), Score: 50},
}
Start(app, Config{
Store: testStore{storedDirs: dirs},
IterateHidden: func(f func(string)) { f("/usr") },
IterateHidden: func(f func(string)) { f(fix("/usr")) },
})
// Test UI.
wantBuf := listingBuf(
"",
"200 /usr/bin", "<- selected",
" 50 /tmp")
"200 "+fix("/usr/bin"), "<- selected",
" 50 "+fix("/tmp"))
ttyCtrl.TestBuffer(t, wantBuf)
}
@ -88,21 +90,21 @@ func TestStart_Pinned(t *testing.T) {
defer cleanup()
dirs := []storedefs.Dir{
{Path: "/usr/bin", Score: 200},
{Path: "/usr", Score: 100},
{Path: "/tmp", Score: 50},
{Path: fix("/usr/bin"), Score: 200},
{Path: fix("/usr"), Score: 100},
{Path: fix("/tmp"), Score: 50},
}
Start(app, Config{
Store: testStore{storedDirs: dirs},
IteratePinned: func(f func(string)) { f("/home"); f("/usr") },
IteratePinned: func(f func(string)) { f(fix("/home")); f(fix("/usr")) },
})
// Test UI.
wantBuf := listingBuf(
"",
" * /home", "<- selected",
" * /usr",
"200 /usr/bin",
" 50 /tmp")
" * "+fix("/home"), "<- selected",
" * "+fix("/usr"),
"200 "+fix("/usr/bin"),
" 50 "+fix("/tmp"))
ttyCtrl.TestBuffer(t, wantBuf)
}
@ -111,14 +113,14 @@ func TestStart_HideWd(t *testing.T) {
defer cleanup()
dirs := []storedefs.Dir{
{Path: "/home", Score: 200},
{Path: "/tmp", Score: 50},
{Path: fix("/home"), Score: 200},
{Path: fix("/tmp"), Score: 50},
}
Start(app, Config{Store: testStore{storedDirs: dirs, wd: "/home"}})
Start(app, Config{Store: testStore{storedDirs: dirs, wd: fix("/home")}})
// Test UI.
wantBuf := listingBuf(
"",
" 50 /tmp", "<- selected")
" 50 "+fix("/tmp"), "<- selected")
ttyCtrl.TestBuffer(t, wantBuf)
}
@ -128,38 +130,45 @@ func TestStart_Workspace(t *testing.T) {
chdir := ""
dirs := []storedefs.Dir{
{Path: "home/src", Score: 200},
{Path: "ws1/src", Score: 150},
{Path: "ws2/bin", Score: 100},
{Path: "/tmp", Score: 50},
{Path: fix("home/src"), Score: 200},
{Path: fix("ws1/src"), Score: 150},
{Path: fix("ws2/bin"), Score: 100},
{Path: fix("/tmp"), Score: 50},
}
Start(app, Config{
Store: testStore{
storedDirs: dirs,
wd: "/home/elf/bin",
wd: fix("/home/elf/bin"),
chdir: func(dir string) error {
chdir = dir
return nil
},
},
IterateWorkspaces: func(f func(kind, pattern string) bool) {
// Invalid patterns are ignored.
f("ws1", "/usr/[^/+")
f("home", "/home/[^/]+")
f("ws2", "/tmp/[^/]+")
if runtime.GOOS == "windows" {
// Invalid patterns are ignored.
f("ws1", `C:\\usr\\[^\\+`)
f("home", `C:\\home\\[^\\]+`)
f("ws2", `C:\\tmp\[^\]+`)
} else {
// Invalid patterns are ignored.
f("ws1", "/usr/[^/+")
f("home", "/home/[^/]+")
f("ws2", "/tmp/[^/]+")
}
},
})
wantBuf := listingBuf(
"",
"200 home/src", "<- selected",
" 50 /tmp")
"200 "+fix("home/src"), "<- selected",
" 50 "+fix("/tmp"))
ttyCtrl.TestBuffer(t, wantBuf)
ttyCtrl.Inject(term.K(ui.Enter))
wantBuf = bb().Buffer()
ttyCtrl.TestBuffer(t, wantBuf)
wantChdir := "/home/elf/src"
wantChdir := fix("/home/elf/src")
if chdir != wantChdir {
t.Errorf("got chdir %q, want %q", chdir, wantChdir)
}
@ -176,7 +185,7 @@ func TestStart_OK(t *testing.T) {
dirs := []storedefs.Dir{
{Path: filepath.Join(home, "go"), Score: 200},
{Path: home, Score: 100},
{Path: "/tmp", Score: 50},
{Path: fix("/tmp"), Score: 50},
}
Start(app, Config{Store: testStore{
storedDirs: dirs,
@ -188,7 +197,7 @@ func TestStart_OK(t *testing.T) {
"",
"200 "+filepath.Join("~", "go"), "<- selected",
"100 ~",
" 50 /tmp")
" 50 "+fix("/tmp"))
ttyCtrl.TestBuffer(t, wantBuf)
// Test filtering.
@ -196,7 +205,7 @@ func TestStart_OK(t *testing.T) {
wantBuf = listingBuf(
"tmp",
" 50 /tmp", "<- selected")
" 50 "+fix("/tmp"), "<- selected")
ttyCtrl.TestBuffer(t, wantBuf)
// Test accepting.
@ -208,8 +217,9 @@ func TestStart_OK(t *testing.T) {
wantNotesBuf := bb().WritePlain("mock chdir error").Buffer()
ttyCtrl.TestNotesBuffer(t, wantNotesBuf)
// Chdir should be called.
if got := <-chdirCh; got != "/tmp" {
t.Errorf("Chdir called with %s, want /tmp", got)
wantChdir := fix("/tmp")
if got := <-chdirCh; got != wantChdir {
t.Errorf("Chdir called with %s, want %s", got, wantChdir)
}
}
@ -235,3 +245,13 @@ func listingBuf(filter string, lines ...string) *ui.Buffer {
layout.WriteListing(b, "LOCATION", filter, lines...)
return b.Buffer()
}
func fix(path string) string {
if runtime.GOOS != "windows" {
return path
}
if path[0] == '/' {
path = "C:" + path
}
return strings.ReplaceAll(path, "/", "\\")
}

View File

@ -0,0 +1,121 @@
// +build !windows
package cliedit
import (
"os"
"path/filepath"
"reflect"
"sort"
"testing"
"github.com/elves/elvish/cli/el/layout"
"github.com/elves/elvish/cli/term"
"github.com/elves/elvish/edit/ui"
"github.com/elves/elvish/store/storedefs"
"github.com/elves/elvish/styled"
"github.com/elves/elvish/util"
)
func TestLocationAddon(t *testing.T) {
f := setupWithOpt(setupOpt{StoreOp: func(s storedefs.Store) {
s.AddDir("/usr/bin", 1)
s.AddDir("/tmp", 1)
s.AddDir("/home/elf", 1)
}})
f.TTYCtrl.SetSize(24, 30) // Set width to 30
defer f.Cleanup()
evals(f.Evaler,
`edit:location:pinned = [/opt]`,
`edit:location:hidden = [/tmp]`)
f.TTYCtrl.Inject(term.K('L', ui.Ctrl))
wantBuf := bbAddon("LOCATION").
WriteStyled(styled.MarkLines(
" * /opt ", styles,
"##############################",
" 10 /home/elf",
" 10 /usr/bin",
)).Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
}
func TestLocationAddon_Workspace(t *testing.T) {
f := setupWithOpt(setupOpt{StoreOp: func(s storedefs.Store) {
s.AddDir("/usr/bin", 1)
s.AddDir("ws/bin", 1)
s.AddDir("other-ws/bin", 1)
}})
defer f.Cleanup()
util.ApplyDir(
util.Dir{
"ws1": util.Dir{
"bin": util.Dir{},
"tmp": util.Dir{}}})
err := os.Chdir("ws1/tmp")
if err != nil {
t.Skip("chdir:", err)
}
f.TTYCtrl.SetSize(24, 30) // Set width to 30
evals(f.Evaler,
`edit:location:workspaces = [&ws=$E:HOME/ws.]`)
f.TTYCtrl.Inject(term.K('L', ui.Ctrl))
wantBuf := ui.NewBufferBuilder(30).
WritePlain("~/ws1/tmp> ").Newline().
WriteStyled(layout.ModeLine("LOCATION", true)).SetDotToCursor().Newline().
WriteStyled(styled.MarkLines(
" 10 ws/bin ", styles,
"##############################",
" 10 /usr/bin",
)).Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
f.TTYCtrl.Inject(term.K(ui.Enter))
wantBuf = ui.NewBufferBuilder(30).
WritePlain("~/ws1/bin> ").SetDotToCursor().Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
}
func TestLocation_AddDir(t *testing.T) {
f := setup()
defer f.Cleanup()
util.ApplyDir(
util.Dir{
"bin": util.Dir{},
"ws1": util.Dir{
"bin": util.Dir{}}})
evals(f.Evaler, `edit:location:workspaces = [&ws=$E:HOME/ws.]`)
chdir := func(path string) {
err := f.Evaler.Chdir(path)
if err != nil {
t.Skip("chdir:", err)
}
}
chdir("bin")
chdir("../ws1/bin")
entries, err := f.Store.Dirs(map[string]struct{}{})
if err != nil {
t.Error("unable to list dir history:", err)
}
dirs := make([]string, len(entries))
for i, entry := range entries {
dirs[i] = entry.Path
}
wantDirs := []string{
filepath.Join(f.Home, "bin"),
filepath.Join(f.Home, "ws1", "bin"),
filepath.Join("ws", "bin"),
}
sort.Strings(dirs)
sort.Strings(wantDirs)
if !reflect.DeepEqual(dirs, wantDirs) {
t.Errorf("got dirs %v, want %v", dirs, wantDirs)
}
}

View File

@ -1,10 +1,6 @@
package cliedit
import (
"os"
"path/filepath"
"reflect"
"sort"
"testing"
"github.com/elves/elvish/cli/el/layout"
@ -12,7 +8,6 @@ import (
"github.com/elves/elvish/edit/ui"
"github.com/elves/elvish/store/storedefs"
"github.com/elves/elvish/styled"
"github.com/elves/elvish/util"
)
/*
@ -86,109 +81,6 @@ func TestLastCmdAddon(t *testing.T) {
f.TTYCtrl.TestBuffer(t, wantBuf)
}
func TestLocationAddon(t *testing.T) {
f := setupWithOpt(setupOpt{StoreOp: func(s storedefs.Store) {
s.AddDir("/usr/bin", 1)
s.AddDir("/tmp", 1)
s.AddDir("/home/elf", 1)
}})
f.TTYCtrl.SetSize(24, 30) // Set width to 30
defer f.Cleanup()
evals(f.Evaler,
`edit:location:pinned = [/opt]`,
`edit:location:hidden = [/tmp]`)
f.TTYCtrl.Inject(term.K('L', ui.Ctrl))
wantBuf := bbAddon("LOCATION").
WriteStyled(styled.MarkLines(
" * /opt ", styles,
"##############################",
" 10 /home/elf",
" 10 /usr/bin",
)).Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
}
func TestLocationAddon_Workspace(t *testing.T) {
f := setupWithOpt(setupOpt{StoreOp: func(s storedefs.Store) {
s.AddDir("/usr/bin", 1)
s.AddDir("ws/bin", 1)
s.AddDir("other-ws/bin", 1)
}})
defer f.Cleanup()
util.ApplyDir(
util.Dir{
"ws1": util.Dir{
"bin": util.Dir{},
"tmp": util.Dir{}}})
err := os.Chdir("ws1/tmp")
if err != nil {
t.Skip("chdir:", err)
}
f.TTYCtrl.SetSize(24, 30) // Set width to 30
evals(f.Evaler,
`edit:location:workspaces = [&ws=$E:HOME/ws.]`)
f.TTYCtrl.Inject(term.K('L', ui.Ctrl))
wantBuf := ui.NewBufferBuilder(30).
WritePlain("~/ws1/tmp> ").Newline().
WriteStyled(layout.ModeLine("LOCATION", true)).SetDotToCursor().Newline().
WriteStyled(styled.MarkLines(
" 10 ws/bin ", styles,
"##############################",
" 10 /usr/bin",
)).Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
f.TTYCtrl.Inject(term.K(ui.Enter))
wantBuf = ui.NewBufferBuilder(30).
WritePlain("~/ws1/bin> ").SetDotToCursor().Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
}
func TestLocation_AddDir(t *testing.T) {
f := setup()
defer f.Cleanup()
util.ApplyDir(
util.Dir{
"bin": util.Dir{},
"ws1": util.Dir{
"bin": util.Dir{}}})
evals(f.Evaler, `edit:location:workspaces = [&ws=$E:HOME/ws.]`)
chdir := func(path string) {
err := f.Evaler.Chdir(path)
if err != nil {
t.Skip("chdir:", err)
}
}
chdir("bin")
chdir("../ws1/bin")
entries, err := f.Store.Dirs(map[string]struct{}{})
if err != nil {
t.Error("unable to list dir history:", err)
}
dirs := make([]string, len(entries))
for i, entry := range entries {
dirs[i] = entry.Path
}
wantDirs := []string{
filepath.Join(f.Home, "bin"),
filepath.Join(f.Home, "ws1", "bin"),
filepath.Join("ws", "bin"),
}
sort.Strings(dirs)
sort.Strings(wantDirs)
if !reflect.DeepEqual(dirs, wantDirs) {
t.Errorf("got dirs %v, want %v", dirs, wantDirs)
}
}
func bbAddon(name string) *ui.BufferBuilder {
return ui.NewBufferBuilder(30).
WritePlain("~> ").Newline().

View File

@ -0,0 +1,123 @@
package cliedit
import (
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"testing"
"github.com/elves/elvish/cli/el/layout"
"github.com/elves/elvish/cli/term"
"github.com/elves/elvish/edit/ui"
"github.com/elves/elvish/store/storedefs"
"github.com/elves/elvish/styled"
"github.com/elves/elvish/util"
)
func TestLocationAddon(t *testing.T) {
f := setupWithOpt(setupOpt{StoreOp: func(s storedefs.Store) {
s.AddDir(`C:\usr\bin`, 1)
s.AddDir(`C:\tmp`, 1)
s.AddDir(`C:\home\elf`, 1)
}})
f.TTYCtrl.SetSize(24, 30) // Set width to 30
defer f.Cleanup()
evals(f.Evaler,
`edit:location:pinned = ['C:\opt']`,
`edit:location:hidden = ['C:\tmp']`)
f.TTYCtrl.Inject(term.K('L', ui.Ctrl))
wantBuf := bbAddon("LOCATION").
WriteStyled(styled.MarkLines(
` * C:\opt `, styles,
"##############################",
` 10 C:\home\elf`,
` 10 C:\usr\bin`,
)).Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
}
func TestLocationAddon_Workspace(t *testing.T) {
f := setupWithOpt(setupOpt{StoreOp: func(s storedefs.Store) {
s.AddDir(`C:\usr\bin`, 1)
s.AddDir(`ws\bin`, 1)
s.AddDir(`other-ws\bin`, 1)
}})
defer f.Cleanup()
util.ApplyDir(
util.Dir{
"ws1": util.Dir{
"bin": util.Dir{},
"tmp": util.Dir{}}})
err := os.Chdir(`ws1\tmp`)
if err != nil {
t.Skip("chdir:", err)
}
f.TTYCtrl.SetSize(24, 30) // Set width to 30
evals(f.Evaler,
`edit:location:workspaces = [&ws='`+
regexp.QuoteMeta(f.Home)+`\\'ws.]`)
f.TTYCtrl.Inject(term.K('L', ui.Ctrl))
wantBuf := ui.NewBufferBuilder(30).
WritePlain(`~\ws1\tmp> `).Newline().
WriteStyled(layout.ModeLine("LOCATION", true)).SetDotToCursor().Newline().
WriteStyled(styled.MarkLines(
` 10 ws\bin `, styles,
"##############################",
` 10 C:\usr\bin`,
)).Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
f.TTYCtrl.Inject(term.K(ui.Enter))
wantBuf = ui.NewBufferBuilder(30).
WritePlain(`~\ws1\bin> `).SetDotToCursor().Buffer()
f.TTYCtrl.TestBuffer(t, wantBuf)
}
func TestLocation_AddDir(t *testing.T) {
f := setup()
defer f.Cleanup()
util.ApplyDir(
util.Dir{
"bin": util.Dir{},
"ws1": util.Dir{
"bin": util.Dir{}}})
evals(f.Evaler,
`edit:location:workspaces = [&ws='`+
regexp.QuoteMeta(f.Home)+`\\'ws.]`)
chdir := func(path string) {
err := f.Evaler.Chdir(path)
if err != nil {
t.Skip("chdir:", err)
}
}
chdir("bin")
chdir(`..\ws1\bin`)
entries, err := f.Store.Dirs(map[string]struct{}{})
if err != nil {
t.Error("unable to list dir history:", err)
}
dirs := make([]string, len(entries))
for i, entry := range entries {
dirs[i] = entry.Path
}
wantDirs := []string{
filepath.Join(f.Home, "bin"),
filepath.Join(f.Home, "ws1", "bin"),
filepath.Join("ws", "bin"),
}
sort.Strings(dirs)
sort.Strings(wantDirs)
if !reflect.DeepEqual(dirs, wantDirs) {
t.Errorf("got dirs %v, want %v", dirs, wantDirs)
}
}