github.com/uppal0016/docker_new@v0.0.0-20240123060250-1c98be13ac2c/builder/dockerfile/shell_parser_test.go (about)

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