Increase test coverage, refactor tests (#62)

master
Ivan 3 years ago committed by GitHub
parent a95d13315b
commit b8f0d7bc29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -56,7 +56,12 @@ func GetConfig(path string, settings map[string]string) (Config, error) {
} }
config := string(f) config := string(f)
config = os.Expand(config, func(v string) string {
return ParseConfig(config, settings)
}
func ParseConfig(data string, settings map[string]string) (Config, error) {
data = os.Expand(data, func(v string) string {
if val, ok := settings[v]; ok { if val, ok := settings[v]; ok {
return val return val
} }
@ -64,10 +69,6 @@ func GetConfig(path string, settings map[string]string) (Config, error) {
return v return v
}) })
return ParseConfig(config)
}
func ParseConfig(data string) (Config, error) {
c := Config{} c := Config{}
err := yaml.Unmarshal([]byte(data), &c) err := yaml.Unmarshal([]byte(data), &c)

@ -0,0 +1,46 @@
package main
import (
"reflect"
"testing"
)
func TestParseConfig(t *testing.T) {
yaml := `
session: ${session}
windows:
- layout: tiled
commands:
- echo 1
panes:
- commands:
- echo 2
type: horizontal`
config, err := ParseConfig(yaml, map[string]string{
"session": "test",
})
if err != nil {
t.Fatal(err)
}
expected := Config{
Session: "test",
Windows: []Window{
Window{
Layout: "tiled",
Commands: []string{"echo 1"},
Panes: []Pane{
Pane{
Type: "horizontal",
Commands: []string{"echo 2"},
},
},
},
},
}
if !reflect.DeepEqual(expected, config) {
t.Fatalf("expected %v, got %v", expected, config)
}
}

@ -1,13 +1,14 @@
package main package main
import ( import (
"os"
"os/exec" "os/exec"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
) )
var testTable = []struct { var testTable = map[string]struct {
config Config config Config
options Options options Options
context Context context Context
@ -15,37 +16,38 @@ var testTable = []struct {
stopCommands []string stopCommands []string
commanderOutputs []string commanderOutputs []string
}{ }{
{ "test with 1 window": {
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "~/root",
BeforeStart: []string{"command1", "command2"}, BeforeStart: []string{"command1", "command2"},
Windows: []Window{ Windows: []Window{
{ {
Name: "win1", Name: "win1",
Commands: []string{"command1"},
}, },
}, },
}, },
Options{ Options{},
Detach: true,
},
Context{}, Context{},
[]string{ []string{
"tmux has-session -t ses:", "tmux has-session -t ses:",
"/bin/sh -c command1", "/bin/sh -c command1",
"/bin/sh -c command2", "/bin/sh -c command2",
"tmux new -Pd -s ses -n smug_def -c root", "tmux new -Pd -s ses -n smug_def -c smug/root",
"tmux neww -Pd -t ses: -c root -F #{window_id} -n win1", "tmux neww -Pd -t ses: -c smug/root -F #{window_id} -n win1",
"tmux select-layout -t xyz even-horizontal", "tmux send-keys -t win1 command1 Enter",
"tmux select-layout -t win1 even-horizontal",
"tmux kill-window -t ses:smug_def", "tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:", "tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1",
}, },
[]string{ []string{
"tmux kill-session -t ses", "tmux kill-session -t ses",
}, },
[]string{"xyz"}, []string{"ses", "win1"},
}, },
{ "test with 1 window and Detach: true": {
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
@ -57,7 +59,7 @@ var testTable = []struct {
}, },
}, },
Options{ Options{
Windows: []string{}, Detach: true,
}, },
Context{}, Context{},
[]string{ []string{
@ -69,14 +71,13 @@ var testTable = []struct {
"tmux select-layout -t xyz even-horizontal", "tmux select-layout -t xyz even-horizontal",
"tmux kill-window -t ses:smug_def", "tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:", "tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1",
}, },
[]string{ []string{
"tmux kill-session -t ses", "tmux kill-session -t ses",
}, },
[]string{"xyz"}, []string{"xyz"},
}, },
{ "test with multiple windows and panes": {
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
@ -87,7 +88,8 @@ var testTable = []struct {
Layout: "main-horizontal", Layout: "main-horizontal",
Panes: []Pane{ Panes: []Pane{
{ {
Type: "horizontal", Type: "horizontal",
Commands: []string{"command1"},
}, },
}, },
}, },
@ -108,8 +110,9 @@ var testTable = []struct {
"tmux has-session -t ses:", "tmux has-session -t ses:",
"tmux new -Pd -s ses -n smug_def -c root", "tmux new -Pd -s ses -n smug_def -c root",
"tmux neww -Pd -t ses: -c root -F #{window_id} -n win1", "tmux neww -Pd -t ses: -c root -F #{window_id} -n win1",
"tmux split-window -Pd -h -t 1 -c root -F #{pane_id}", "tmux split-window -Pd -h -t win1 -c root -F #{pane_id}",
"tmux select-layout -t 1 main-horizontal", "tmux send-keys -t win1.1 command1 Enter",
"tmux select-layout -t win1 main-horizontal",
"tmux kill-window -t ses:smug_def", "tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:", "tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1", "tmux attach -d -t ses:win1",
@ -119,9 +122,9 @@ var testTable = []struct {
"/bin/sh -c stop2 -d --foo=bar", "/bin/sh -c stop2 -d --foo=bar",
"tmux kill-session -t ses", "tmux kill-session -t ses",
}, },
[]string{"1"}, []string{"ses", "ses", "win1", "1"},
}, },
{ "test start windows from option's Windows parameter": {
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
@ -153,92 +156,10 @@ var testTable = []struct {
}, },
[]string{"xyz"}, []string{"xyz"},
}, },
"test attach to the existing session": {
{
Config{
Session: "ses",
Root: "root",
Windows: []Window{
{
Name: "win1",
Manual: false,
Commands: []string{"command1", "command2"},
},
{
Name: "win2",
Manual: false,
Commands: []string{"command3", "command4"},
},
},
},
Options{
Windows: []string{},
},
Context{},
[]string{
"tmux has-session -t ses:",
"tmux new -Pd -s ses -n smug_def -c root",
"tmux neww -Pd -t ses: -c root -F #{window_id} -n win1",
"tmux send-keys -t xyz command1 Enter",
"tmux send-keys -t xyz command2 Enter",
"tmux select-layout -t xyz even-horizontal",
"tmux neww -Pd -t ses: -c root -F #{window_id} -n win2",
"tmux send-keys -t xyz command3 Enter",
"tmux send-keys -t xyz command4 Enter",
"tmux select-layout -t xyz even-horizontal",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1",
},
[]string{
"tmux kill-session -t ses",
},
[]string{"xyz"},
},
{
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{
{
Name: "win1",
Manual: false,
Root: "./win1",
Panes: []Pane{
{
Root: "pane1",
Type: "vertical",
Commands: []string{
"command1",
},
},
},
},
},
},
Options{},
Context{},
[]string{
"tmux has-session -t ses:",
"tmux new -Pd -s ses -n smug_def -c root",
"tmux neww -Pd -t ses: -c root/win1 -F #{window_id} -n win1",
"tmux split-window -Pd -v -t 1 -c root/win1/pane1 -F #{pane_id}",
"tmux send-keys -t 1.1 command1 Enter",
"tmux select-layout -t 1 even-horizontal",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1",
},
[]string{
"tmux kill-session -t ses",
},
[]string{"1"},
},
{
Config{
Session: "ses",
Root: "root",
BeforeStart: []string{"command1", "command2"},
Windows: []Window{ Windows: []Window{
{Name: "win1"}, {Name: "win1"},
}, },
@ -254,33 +175,7 @@ var testTable = []struct {
}, },
[]string{""}, []string{""},
}, },
{ "test start a new session from another tmux session": {
Config{
Session: "ses",
Root: "root",
Windows: []Window{
{
Name: "win1",
},
},
},
Options{Attach: true},
Context{InsideTmuxSession: true},
[]string{
"tmux has-session -t ses:",
"tmux new -Pd -s ses -n smug_def -c root",
"tmux neww -Pd -t ses: -c root -F #{window_id} -n win1",
"tmux select-layout -t xyz even-horizontal",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
"tmux switch-client -t ses:win1",
},
[]string{
"tmux kill-session -t ses",
},
[]string{"xyz"},
},
{
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
@ -298,26 +193,7 @@ var testTable = []struct {
}, },
[]string{"xyz"}, []string{"xyz"},
}, },
{ "test switch a client from another tmux session": {
Config{
Session: "ses",
Root: "root",
Windows: []Window{
{Name: "win1"},
},
},
Options{Attach: true},
Context{InsideTmuxSession: true},
[]string{
"tmux has-session -t ses:",
"tmux switch-client -t ses:",
},
[]string{
"tmux kill-session -t ses",
},
[]string{""},
},
{
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
@ -362,9 +238,11 @@ func (c *MockCommander) ExecSilently(cmd *exec.Cmd) error {
} }
func TestStartStopSession(t *testing.T) { func TestStartStopSession(t *testing.T) {
for _, params := range testTable { os.Setenv("HOME", "smug") // Needed for testing ExpandPath function
for testDescription, params := range testTable {
t.Run("test start session", func(t *testing.T) { t.Run("start session: "+testDescription, func(t *testing.T) {
commander := &MockCommander{[]string{}, params.commanderOutputs} commander := &MockCommander{[]string{}, params.commanderOutputs}
tmux := Tmux{commander} tmux := Tmux{commander}
smug := Smug{tmux, commander} smug := Smug{tmux, commander}
@ -379,7 +257,7 @@ func TestStartStopSession(t *testing.T) {
} }
}) })
t.Run("test stop session", func(t *testing.T) { t.Run("stop session: "+testDescription, func(t *testing.T) {
commander := &MockCommander{[]string{}, params.commanderOutputs} commander := &MockCommander{[]string{}, params.commanderOutputs}
tmux := Tmux{commander} tmux := Tmux{commander}
smug := Smug{tmux, commander} smug := Smug{tmux, commander}

Loading…
Cancel
Save