Add Map version `ProcessWords`

Signed-off-by: Yuichiro Kaneko <spiketeika@gmail.com>
docker-18.09
Yuichiro Kaneko 2018-07-20 19:01:31 +09:00
parent 97b3652136
commit 75118c8a82
2 changed files with 22 additions and 0 deletions

View File

@ -51,6 +51,11 @@ func (s *Lex) ProcessWordWithMap(word string, env map[string]string) (string, er
return word, err
}
func (s *Lex) ProcessWordsWithMap(word string, env map[string]string) ([]string, error) {
_, words, err := s.process(word, env)
return words, err
}
func (s *Lex) process(word string, env map[string]string) (string, []string, error) {
sw := &shellWord{
envs: env,

View File

@ -103,6 +103,7 @@ func TestShellParser4Words(t *testing.T) {
test := strings.TrimSpace(words[0])
expected := strings.Split(strings.TrimLeft(words[1], " "), ",")
// test for ProcessWords
result, err := shlex.ProcessWords(test, envs)
if err != nil {
@ -117,6 +118,22 @@ func TestShellParser4Words(t *testing.T) {
t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
}
}
// test for ProcessWordsWithMap
result, err = shlex.ProcessWordsWithMap(test, buildEnvs(envs))
if err != nil {
result = []string{"error"}
}
if len(result) != len(expected) {
t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
}
for i, w := range expected {
if w != result[i] {
t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
}
}
}
}