@@ -5,15 +5,19 @@ import (
5
5
"context"
6
6
_ "embed"
7
7
"encoding/json"
8
+ "flag"
8
9
"fmt"
9
10
"go/ast"
10
11
"go/parser"
11
12
"go/token"
12
13
"net/http"
13
14
"net/http/httptest"
14
15
"net/url"
16
+ "os"
17
+ "path/filepath"
15
18
"slices"
16
19
"sort"
20
+ "strings"
17
21
"sync"
18
22
"sync/atomic"
19
23
"testing"
@@ -46,6 +50,9 @@ import (
46
50
"github.com/coder/coder/v2/testutil"
47
51
)
48
52
53
+ // updateGoldenFiles is a flag that can be set to update golden files.
54
+ var updateGoldenFiles = flag .Bool ("update" , false , "Update golden files" )
55
+
49
56
func TestMain (m * testing.M ) {
50
57
goleak .VerifyTestMain (m )
51
58
}
@@ -927,6 +934,24 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
927
934
body , err := render .GoTemplate (bodyTmpl , tc .payload , defaultHelpers ())
928
935
require .NoError (t , err , "failed to render notification body template" )
929
936
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" )
930
955
})
931
956
}
932
957
}
0 commit comments