github.com/narayandesai/docker@v1.6.0-rc5/builder/shell_parser_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestShellParser(t *testing.T) {
    11  	file, err := os.Open("words")
    12  	if err != nil {
    13  		t.Fatalf("Can't open 'words': %s", err)
    14  	}
    15  	defer file.Close()
    16  
    17  	scanner := bufio.NewScanner(file)
    18  	envs := []string{"PWD=/home", "SHELL=bash"}
    19  	for scanner.Scan() {
    20  		line := scanner.Text()
    21  
    22  		// Trim comments and blank lines
    23  		i := strings.Index(line, "#")
    24  		if i >= 0 {
    25  			line = line[:i]
    26  		}
    27  		line = strings.TrimSpace(line)
    28  
    29  		if line == "" {
    30  			continue
    31  		}
    32  
    33  		words := strings.Split(line, "|")
    34  		if len(words) != 2 {
    35  			t.Fatalf("Error in 'words' - should be 2 words:%q", words)
    36  		}
    37  
    38  		words[0] = strings.TrimSpace(words[0])
    39  		words[1] = strings.TrimSpace(words[1])
    40  
    41  		newWord, err := ProcessWord(words[0], envs)
    42  
    43  		if err != nil {
    44  			newWord = "error"
    45  		}
    46  
    47  		if newWord != words[1] {
    48  			t.Fatalf("Error. Src: %s  Calc: %s  Expected: %s", words[0], newWord, words[1])
    49  		}
    50  	}
    51  }