2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2015-01-28 21:28:48 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
2016-11-22 14:26:02 -05:00
|
|
|
"runtime"
|
2015-01-28 21:28:48 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-03-13 17:25:37 -04:00
|
|
|
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-01-28 21:28:48 -05:00
|
|
|
)
|
|
|
|
|
2015-11-07 15:05:55 -05:00
|
|
|
func TestShellParser4EnvVars(t *testing.T) {
|
|
|
|
fn := "envVarTest"
|
2016-11-22 14:26:02 -05:00
|
|
|
lineCount := 0
|
2015-11-07 15:05:55 -05:00
|
|
|
|
|
|
|
file, err := os.Open(fn)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
2015-01-28 21:28:48 -05:00
|
|
|
defer file.Close()
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
shlex := NewShellLex('\\')
|
2015-01-28 21:28:48 -05:00
|
|
|
scanner := bufio.NewScanner(file)
|
2015-10-18 23:55:53 -04:00
|
|
|
envs := []string{"PWD=/home", "SHELL=bash", "KOREAN=한국어"}
|
2015-01-28 21:28:48 -05:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2016-11-22 14:26:02 -05:00
|
|
|
lineCount++
|
2015-01-28 21:28:48 -05:00
|
|
|
|
|
|
|
// Trim comments and blank lines
|
|
|
|
i := strings.Index(line, "#")
|
|
|
|
if i >= 0 {
|
|
|
|
line = line[:i]
|
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
words := strings.Split(line, "|")
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Len(t, words, 3)
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-03-13 17:25:37 -04:00
|
|
|
platform := strings.TrimSpace(words[0])
|
|
|
|
source := strings.TrimSpace(words[1])
|
|
|
|
expected := strings.TrimSpace(words[2])
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2016-11-22 14:26:02 -05:00
|
|
|
// Key W=Windows; A=All; U=Unix
|
2017-03-13 17:25:37 -04:00
|
|
|
if platform != "W" && platform != "A" && platform != "U" {
|
|
|
|
t.Fatalf("Invalid tag %s at line %d of %s. Must be W, A or U", platform, lineCount, fn)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 17:25:37 -04:00
|
|
|
if ((platform == "W" || platform == "A") && runtime.GOOS == "windows") ||
|
|
|
|
((platform == "U" || platform == "A") && runtime.GOOS != "windows") {
|
2017-04-26 18:24:41 -04:00
|
|
|
newWord, err := shlex.ProcessWord(source, envs)
|
2017-03-13 17:25:37 -04:00
|
|
|
if expected == "error" {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Error(t, err)
|
2017-03-13 17:25:37 -04:00
|
|
|
} else {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
2017-04-04 13:40:37 -04:00
|
|
|
assert.Equal(t, newWord, expected)
|
2016-11-22 14:26:02 -05:00
|
|
|
}
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-22 16:12:43 -04:00
|
|
|
|
2015-11-07 15:05:55 -05:00
|
|
|
func TestShellParser4Words(t *testing.T) {
|
|
|
|
fn := "wordsTest"
|
|
|
|
|
|
|
|
file, err := os.Open(fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't open '%s': %s", err, fn)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
shlex := NewShellLex('\\')
|
2015-11-07 15:05:55 -05:00
|
|
|
envs := []string{}
|
|
|
|
scanner := bufio.NewScanner(file)
|
2017-04-03 13:45:39 -04:00
|
|
|
lineNum := 0
|
2015-11-07 15:05:55 -05:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2017-04-03 13:45:39 -04:00
|
|
|
lineNum = lineNum + 1
|
2015-11-07 15:05:55 -05:00
|
|
|
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "ENV ") {
|
|
|
|
line = strings.TrimLeft(line[3:], " ")
|
|
|
|
envs = append(envs, line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
words := strings.Split(line, "|")
|
|
|
|
if len(words) != 2 {
|
2017-04-03 13:45:39 -04:00
|
|
|
t.Fatalf("Error in '%s'(line %d) - should be exactly one | in: %q", fn, lineNum, line)
|
2015-11-07 15:05:55 -05:00
|
|
|
}
|
|
|
|
test := strings.TrimSpace(words[0])
|
|
|
|
expected := strings.Split(strings.TrimLeft(words[1], " "), ",")
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
result, err := shlex.ProcessWords(test, envs)
|
2015-11-07 15:05:55 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
result = []string{"error"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != len(expected) {
|
2017-04-03 13:45:39 -04:00
|
|
|
t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
|
2015-11-07 15:05:55 -05:00
|
|
|
}
|
|
|
|
for i, w := range expected {
|
|
|
|
if w != result[i] {
|
2017-04-03 13:45:39 -04:00
|
|
|
t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
|
2015-11-07 15:05:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 16:12:43 -04:00
|
|
|
func TestGetEnv(t *testing.T) {
|
2017-04-26 18:24:41 -04:00
|
|
|
sw := &shellWord{envs: nil}
|
2015-08-22 16:12:43 -04:00
|
|
|
|
|
|
|
sw.envs = []string{}
|
|
|
|
if sw.getEnv("foo") != "" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("2 - 'foo' should map to ''")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{"foo"}
|
|
|
|
if sw.getEnv("foo") != "" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("3 - 'foo' should map to ''")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{"foo="}
|
|
|
|
if sw.getEnv("foo") != "" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("4 - 'foo' should map to ''")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{"foo=bar"}
|
|
|
|
if sw.getEnv("foo") != "bar" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("5 - 'foo' should map to 'bar'")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{"foo=bar", "car=hat"}
|
|
|
|
if sw.getEnv("foo") != "bar" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("6 - 'foo' should map to 'bar'")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
if sw.getEnv("car") != "hat" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("7 - 'car' should map to 'hat'")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we grab the first 'car' in the list
|
|
|
|
sw.envs = []string{"foo=bar", "car=hat", "car=bike"}
|
|
|
|
if sw.getEnv("car") != "hat" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("8 - 'car' should map to 'hat'")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
}
|