fuzzyworld

template / ci-testing / files / extras_test.go

Branch ci-testing · default branch · all branches

extras_test.go

1.2 KB · history · blame

1package main
2
3import (
4 "os"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "gopkg.in/yaml.v3"
9)
10
11func TestExtras(t *testing.T) {
12 // setup code
13 extras := map[string]string{
14 "version": "1",
15 "id": "123",
16 "customer": "abc",
17 }
18
19 file, err := os.CreateTemp("", "extras.yaml")
20 if err != nil {
21 t.Error("Error creating temp file: ", err)
22 }
23
24 // cleanup code
25 deleteFile := func() {
26 os.Remove(file.Name())
27 }
28 defer deleteFile()
29
30 // write extras to file
31 encoder := yaml.NewEncoder(file)
32 err = encoder.Encode(extras)
33 if err != nil {
34 t.Error("Error encoding extras: ", err)
35 }
36
37 // close file
38 err = file.Close()
39 if err != nil {
40 t.Error("Error closing file: ", err)
41 }
42
43 // file name
44 fileName := file.Name()
45
46 // happy path
47 loadedExtras, err := loadExtras(fileName)
48 if err != nil {
49 t.Error("Error loading extras: ", err)
50 }
51 assert.Equal(t, extras, loadedExtras)
52
53 // test path errors
54 _, err = loadExtras("non-existent-file.yaml")
55 assert.NotNil(t, err, "Expected error loading non-existent file")
56
57 err = os.WriteFile(file.Name(), []byte("invalid yaml"), 0644)
58 assert.Nil(t, err, "Error writing invalid yaml file")
59 _, err = loadExtras(fileName)
60 assert.NotNil(t, err, "Expected to fail to parse extras, but succeeded")
61}