Skip to content

Commit 1753beb

Browse files
committed
test
1 parent 53dc29f commit 1753beb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

coderd/notifications/notifications_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import (
55
"context"
66
_ "embed"
77
"encoding/json"
8+
"flag"
89
"fmt"
910
"go/ast"
1011
"go/parser"
1112
"go/token"
1213
"net/http"
1314
"net/http/httptest"
1415
"net/url"
16+
"os"
17+
"path/filepath"
1518
"slices"
1619
"sort"
20+
"strings"
1721
"sync"
1822
"sync/atomic"
1923
"testing"
@@ -46,6 +50,9 @@ import (
4650
"github.com/coder/coder/v2/testutil"
4751
)
4852

53+
// updateGoldenFiles is a flag that can be set to update golden files.
54+
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")
55+
4956
func TestMain(m *testing.M) {
5057
goleak.VerifyTestMain(m)
5158
}
@@ -927,6 +934,24 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
927934
body, err := render.GoTemplate(bodyTmpl, tc.payload, defaultHelpers())
928935
require.NoError(t, err, "failed to render notification body template")
929936
require.NotEmpty(t, body, "body should not be empty")
937+
938+
partialName := strings.Join(strings.Split(t.Name(), "/")[1:], "_")
939+
goldenFile := filepath.Join("testdata", "rendered-templates", partialName+"-body.md.golden")
940+
941+
if *updateGoldenFiles {
942+
err = os.MkdirAll(filepath.Dir(goldenFile), 0o755)
943+
require.NoError(t, err, "want no error creating golden file directory")
944+
f, err := os.Create(goldenFile)
945+
require.NoError(t, err, "want no error creating golden file")
946+
defer f.Close()
947+
_, err = f.WriteString(body)
948+
require.NoError(t, err, "want no error writing golden file")
949+
return
950+
}
951+
952+
want, err := os.ReadFile(goldenFile)
953+
require.NoError(t, err, "open golden file, run \"make update-golden-files\" and commit the changes")
954+
require.Equal(t, string(want), body, "body should be equal")
930955
})
931956
}
932957
}

0 commit comments

Comments
 (0)