fix(tests): fix all problems related to test

This commit is contained in:
verse91
2026-05-09 23:37:13 +07:00
parent 17f33ff480
commit e587f02798
11 changed files with 301 additions and 172 deletions
+29 -39
View File
@@ -1,26 +1,48 @@
package shell_test
package tests
import (
"os"
"path/filepath"
"reflect"
"testing"
"github.com/versenilvis/iris/integration/shell"
)
func TestScanPosixAliases(t *testing.T) {
// REQUIREMENT: Parse single alias, multi alias, comments, value with space
input := `
alias gca='git commit -a'
alias ta="tmux a -t" # this is a comment
# alias hidden="not found"
alias l='ls' ll='ls -l'
`
expected := map[string]string{
"gca": "git commit -a",
"ta": "tmux a -t",
"l": "ls",
"ll": "ls -l",
}
got := shell.ParseAliases(input)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ScanPosixAliases() = %v; want %v", got, expected)
}
}
func TestSplitAliasTokens(t *testing.T) {
tests := []struct {
name string
input string
expected []string
}{
{"g='git commit'", []string{"g='git commit'"}},
{"a=b c=d", []string{"a=b", "c=d"}},
{"ta='tmux a -t' l='ls -l'", []string{"ta='tmux a -t'", "l='ls -l'"}},
// REQUIREMENT: Parse multi alias on one line
{"Single", "a='b'", []string{"a='b'"}},
{"Multi", "a='b' c=\"d\"", []string{"a='b'", "c=\"d\""}},
// REQUIREMENT: Value with space in quote
{"With Space", "ta='tmux a -t' l='ls -l'", []string{"ta='tmux a -t'", "l='ls -l'"}},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got := shell.SplitAliasTokens(tt.input)
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("SplitAliasTokens(%q) = %v; want %v", tt.input, got, tt.expected)
@@ -28,35 +50,3 @@ func TestSplitAliasTokens(t *testing.T) {
})
}
}
func TestScanPosixAliases(t *testing.T) {
tmp := t.TempDir()
aliasFile := filepath.Join(tmp, ".bashrc")
content := `
# some comments
alias g='git'
alias gca='git commit -a'
alias multi="a" b="c"
`
os.WriteFile(aliasFile, []byte(content), 0644)
oldHome := os.Getenv("HOME")
os.Setenv("HOME", tmp)
defer os.Setenv("HOME", oldHome)
aliases := shell.ScanPosixAliases([]string{".bashrc"})
expected := map[string]string{
"g": "git",
"gca": "git commit -a",
"multi": "a",
"b": "c",
}
for k, v := range expected {
if aliases[k] != v {
t.Errorf("Expected alias %s=%s, got %s", k, v, aliases[k])
}
}
}