shell_parser_test.go 937 B

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