github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/builder/dockerfile/shell_parser_test.go (about)

     1  package dockerfile
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestShellParser4EnvVars(t *testing.T) {
    14  	fn := "envVarTest"
    15  	lineCount := 0
    16  
    17  	file, err := os.Open(fn)
    18  	assert.NoError(t, err)
    19  	defer file.Close()
    20  
    21  	shlex := NewShellLex('\\')
    22  	scanner := bufio.NewScanner(file)
    23  	envs := []string{"PWD=/home", "SHELL=bash", "KOREAN=한국어"}
    24  	for scanner.Scan() {
    25  		line := scanner.Text()
    26  		lineCount++
    27  
    28  		// Trim comments and blank lines
    29  		i := strings.Index(line, "#")
    30  		if i >= 0 {
    31  			line = line[:i]
    32  		}
    33  		line = strings.TrimSpace(line)
    34  
    35  		if line == "" {
    36  			continue
    37  		}
    38  
    39  		words := strings.Split(line, "|")
    40  		assert.Len(t, words, 3)
    41  
    42  		platform := strings.TrimSpace(words[0])
    43  		source := strings.TrimSpace(words[1])
    44  		expected := strings.TrimSpace(words[2])
    45  
    46  		// Key W=Windows; A=All; U=Unix
    47  		if platform != "W" && platform != "A" && platform != "U" {
    48  			t.Fatalf("Invalid tag %s at line %d of %s. Must be W, A or U", platform, lineCount, fn)
    49  		}
    50  
    51  		if ((platform == "W" || platform == "A") && runtime.GOOS == "windows") ||
    52  			((platform == "U" || platform == "A") && runtime.GOOS != "windows") {
    53  			newWord, err := shlex.ProcessWord(source, envs)
    54  			if expected == "error" {
    55  				assert.Error(t, err)
    56  			} else {
    57  				assert.NoError(t, err)
    58  				assert.Equal(t, newWord, expected)
    59  			}
    60  		}
    61  	}
    62  }
    63  
    64  func TestShellParser4Words(t *testing.T) {
    65  	fn := "wordsTest"
    66  
    67  	file, err := os.Open(fn)
    68  	if err != nil {
    69  		t.Fatalf("Can't open '%s': %s", err, fn)
    70  	}
    71  	defer file.Close()
    72  
    73  	shlex := NewShellLex('\\')
    74  	envs := []string{}
    75  	scanner := bufio.NewScanner(file)
    76  	lineNum := 0
    77  	for scanner.Scan() {
    78  		line := scanner.Text()
    79  		lineNum = lineNum + 1
    80  
    81  		if strings.HasPrefix(line, "#") {
    82  			continue
    83  		}
    84  
    85  		if strings.HasPrefix(line, "ENV ") {
    86  			line = strings.TrimLeft(line[3:], " ")
    87  			envs = append(envs, line)
    88  			continue
    89  		}
    90  
    91  		words := strings.Split(line, "|")
    92  		if len(words) != 2 {
    93  			t.Fatalf("Error in '%s'(line %d) - should be exactly one | in: %q", fn, lineNum, line)
    94  		}
    95  		test := strings.TrimSpace(words[0])
    96  		expected := strings.Split(strings.TrimLeft(words[1], " "), ",")
    97  
    98  		result, err := shlex.ProcessWords(test, envs)
    99  
   100  		if err != nil {
   101  			result = []string{"error"}
   102  		}
   103  
   104  		if len(result) != len(expected) {
   105  			t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
   106  		}
   107  		for i, w := range expected {
   108  			if w != result[i] {
   109  				t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
   110  			}
   111  		}
   112  	}
   113  }
   114  
   115  func TestGetEnv(t *testing.T) {
   116  	sw := &shellWord{envs: nil}
   117  
   118  	sw.envs = []string{}
   119  	if sw.getEnv("foo") != "" {
   120  		t.Fatal("2 - 'foo' should map to ''")
   121  	}
   122  
   123  	sw.envs = []string{"foo"}
   124  	if sw.getEnv("foo") != "" {
   125  		t.Fatal("3 - 'foo' should map to ''")
   126  	}
   127  
   128  	sw.envs = []string{"foo="}
   129  	if sw.getEnv("foo") != "" {
   130  		t.Fatal("4 - 'foo' should map to ''")
   131  	}
   132  
   133  	sw.envs = []string{"foo=bar"}
   134  	if sw.getEnv("foo") != "bar" {
   135  		t.Fatal("5 - 'foo' should map to 'bar'")
   136  	}
   137  
   138  	sw.envs = []string{"foo=bar", "car=hat"}
   139  	if sw.getEnv("foo") != "bar" {
   140  		t.Fatal("6 - 'foo' should map to 'bar'")
   141  	}
   142  	if sw.getEnv("car") != "hat" {
   143  		t.Fatal("7 - 'car' should map to 'hat'")
   144  	}
   145  
   146  	// Make sure we grab the first 'car' in the list
   147  	sw.envs = []string{"foo=bar", "car=hat", "car=bike"}
   148  	if sw.getEnv("car") != "hat" {
   149  		t.Fatal("8 - 'car' should map to 'hat'")
   150  	}
   151  }