From 75118c8a82f8c14fc87f2b21c99acf23af955c47 Mon Sep 17 00:00:00 2001 From: Yuichiro Kaneko Date: Fri, 20 Jul 2018 19:01:31 +0900 Subject: [PATCH] Add Map version `ProcessWords` Signed-off-by: Yuichiro Kaneko --- frontend/dockerfile/shell/lex.go | 5 +++++ frontend/dockerfile/shell/lex_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/frontend/dockerfile/shell/lex.go b/frontend/dockerfile/shell/lex.go index e9e9df0a..14af0548 100644 --- a/frontend/dockerfile/shell/lex.go +++ b/frontend/dockerfile/shell/lex.go @@ -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, diff --git a/frontend/dockerfile/shell/lex_test.go b/frontend/dockerfile/shell/lex_test.go index f1fc6d42..7095e0f2 100644 --- a/frontend/dockerfile/shell/lex_test.go +++ b/frontend/dockerfile/shell/lex_test.go @@ -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) + } + } } }