■ ■ ■ ■ ■ ■
models/attachment_test.go
| skipped 33 lines |
34 | 34 | | log.Fatalf("Failed to open attachment folder 'testdata': %v\n", err) |
35 | 35 | | } |
36 | 36 | | for _, ff := range files { |
37 | | - | if !ff.IsDir() { |
| 37 | + | if !ff.IsDir() && !strings.Contains(ff.Name(), "templated") { |
38 | 38 | | fname := ff.Name() |
39 | 39 | | fmt.Printf("Checking attachment file -> %s\n", fname) |
40 | | - | f, err := os.Open("testdata/" + fname) |
41 | | - | if err != nil { |
42 | | - | log.Fatalf("Failed to open attachment test file '%s': %v\n", fname, err) |
43 | | - | } |
44 | | - | reader := bufio.NewReader(f) |
45 | | - | content, err := ioutil.ReadAll(reader) |
46 | | - | if err != nil { |
47 | | - | log.Fatalf("Failed to read attachment test file '%s': %v\n", fname, err) |
48 | | - | } |
49 | | - | |
50 | | - | data := "" |
| 40 | + | data := readFile("testdata/" + fname) |
51 | 41 | | if filepath.Ext(fname) == ".b64" { |
52 | | - | data = string(content) |
53 | 42 | | fname = fname[:len(fname)-4] |
54 | | - | } else { |
55 | | - | data = base64.StdEncoding.EncodeToString(content) |
56 | 43 | | } |
57 | | - | |
58 | 44 | | a := Attachment{ |
59 | 45 | | Content: data, |
60 | 46 | | Name: fname, |
61 | 47 | | } |
62 | | - | |
63 | | - | _, err = a.ApplyTemplate(ptx) |
| 48 | + | t, err := a.ApplyTemplate(ptx) |
64 | 49 | | c.Assert(err, check.Equals, nil) |
65 | 50 | | c.Assert(a.vanillaFile, check.Equals, strings.Contains(fname, "without-vars")) |
66 | 51 | | c.Assert(a.vanillaFile, check.Not(check.Equals), strings.Contains(fname, "with-vars")) |
67 | 52 | | |
| 53 | + | // Verfify template was applied as expected |
| 54 | + | tt, err := ioutil.ReadAll(t) |
| 55 | + | if err != nil { |
| 56 | + | log.Fatalf("Failed to parse templated file '%s': %v\n", fname, err) |
| 57 | + | } |
| 58 | + | templatedFile := base64.StdEncoding.EncodeToString(tt) |
| 59 | + | expectedOutput := readFile("testdata/" + strings.TrimSuffix(ff.Name(), filepath.Ext(ff.Name())) + ".templated" + filepath.Ext(ff.Name())) // e.g text-file-with-vars.templated.txt |
| 60 | + | c.Assert(templatedFile, check.Equals, expectedOutput) |
68 | 61 | | } |
69 | 62 | | } |
| 63 | + | } |
70 | 64 | | |
| 65 | + | func readFile(fname string) string { |
| 66 | + | f, err := os.Open(fname) |
| 67 | + | if err != nil { |
| 68 | + | log.Fatalf("Failed to open file '%s': %v\n", fname, err) |
| 69 | + | } |
| 70 | + | reader := bufio.NewReader(f) |
| 71 | + | content, err := ioutil.ReadAll(reader) |
| 72 | + | if err != nil { |
| 73 | + | log.Fatalf("Failed to read file '%s': %v\n", fname, err) |
| 74 | + | } |
| 75 | + | data := "" |
| 76 | + | if filepath.Ext(fname) == ".b64" { |
| 77 | + | data = string(content) |
| 78 | + | } else { |
| 79 | + | data = base64.StdEncoding.EncodeToString(content) |
| 80 | + | } |
| 81 | + | return data |
71 | 82 | | } |
72 | 83 | | |