@@ -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
}
@@ -693,7 +700,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
693
700
name : "TemplateWorkspaceDeleted" ,
694
701
id : notifications .TemplateWorkspaceDeleted ,
695
702
payload : types.MessagePayload {
696
- UserName : "bobby " ,
703
+ UserName : "Bobby " ,
697
704
Labels : map [string ]string {
698
705
"name" : "bobby-workspace" ,
699
706
"reason" : "autodeleted due to dormancy" ,
@@ -705,7 +712,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
705
712
name : "TemplateWorkspaceAutobuildFailed" ,
706
713
id : notifications .TemplateWorkspaceAutobuildFailed ,
707
714
payload : types.MessagePayload {
708
- UserName : "bobby " ,
715
+ UserName : "Bobby " ,
709
716
Labels : map [string ]string {
710
717
"name" : "bobby-workspace" ,
711
718
"reason" : "autostart" ,
@@ -716,7 +723,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
716
723
name : "TemplateWorkspaceDormant" ,
717
724
id : notifications .TemplateWorkspaceDormant ,
718
725
payload : types.MessagePayload {
719
- UserName : "bobby " ,
726
+ UserName : "Bobby " ,
720
727
Labels : map [string ]string {
721
728
"name" : "bobby-workspace" ,
722
729
"reason" : "breached the template's threshold for inactivity" ,
@@ -730,7 +737,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
730
737
name : "TemplateWorkspaceAutoUpdated" ,
731
738
id : notifications .TemplateWorkspaceAutoUpdated ,
732
739
payload : types.MessagePayload {
733
- UserName : "bobby " ,
740
+ UserName : "Bobby " ,
734
741
Labels : map [string ]string {
735
742
"name" : "bobby-workspace" ,
736
743
"template_version_name" : "1.0" ,
@@ -742,7 +749,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
742
749
name : "TemplateWorkspaceMarkedForDeletion" ,
743
750
id : notifications .TemplateWorkspaceMarkedForDeletion ,
744
751
payload : types.MessagePayload {
745
- UserName : "bobby " ,
752
+ UserName : "Bobby " ,
746
753
Labels : map [string ]string {
747
754
"name" : "bobby-workspace" ,
748
755
"reason" : "template updated to new dormancy policy" ,
@@ -755,7 +762,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
755
762
name : "TemplateUserAccountCreated" ,
756
763
id : notifications .TemplateUserAccountCreated ,
757
764
payload : types.MessagePayload {
758
- UserName : "bobby " ,
765
+ UserName : "Bobby " ,
759
766
Labels : map [string ]string {
760
767
"created_account_name" : "bobby" ,
761
768
},
@@ -765,7 +772,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
765
772
name : "TemplateUserAccountDeleted" ,
766
773
id : notifications .TemplateUserAccountDeleted ,
767
774
payload : types.MessagePayload {
768
- UserName : "bobby " ,
775
+ UserName : "Bobby " ,
769
776
Labels : map [string ]string {
770
777
"deleted_account_name" : "bobby" ,
771
778
},
@@ -775,7 +782,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
775
782
name : "TemplateUserAccountSuspended" ,
776
783
id : notifications .TemplateUserAccountSuspended ,
777
784
payload : types.MessagePayload {
778
- UserName : "bobby " ,
785
+ UserName : "Bobby " ,
779
786
Labels : map [string ]string {
780
787
"suspended_account_name" : "bobby" ,
781
788
},
@@ -785,7 +792,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
785
792
name : "TemplateUserAccountActivated" ,
786
793
id : notifications .TemplateUserAccountActivated ,
787
794
payload : types.MessagePayload {
788
- UserName : "bobby " ,
795
+ UserName : "Bobby " ,
789
796
Labels : map [string ]string {
790
797
"activated_account_name" : "bobby" ,
791
798
},
@@ -795,7 +802,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
795
802
name : "TemplateYourAccountSuspended" ,
796
803
id : notifications .TemplateYourAccountSuspended ,
797
804
payload : types.MessagePayload {
798
- UserName : "bobby " ,
805
+ UserName : "Bobby " ,
799
806
Labels : map [string ]string {
800
807
"suspended_account_name" : "bobby" ,
801
808
},
@@ -805,7 +812,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
805
812
name : "TemplateYourAccountActivated" ,
806
813
id : notifications .TemplateYourAccountActivated ,
807
814
payload : types.MessagePayload {
808
- UserName : "bobby " ,
815
+ UserName : "Bobby " ,
809
816
Labels : map [string ]string {
810
817
"activated_account_name" : "bobby" ,
811
818
},
@@ -815,7 +822,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
815
822
name : "TemplateTemplateDeleted" ,
816
823
id : notifications .TemplateTemplateDeleted ,
817
824
payload : types.MessagePayload {
818
- UserName : "bobby " ,
825
+ UserName : "Bobby " ,
819
826
Labels : map [string ]string {
820
827
"name" : "bobby-template" ,
821
828
"initiator" : "rob" ,
@@ -826,7 +833,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
826
833
name : "TemplateWorkspaceManualBuildFailed" ,
827
834
id : notifications .TemplateWorkspaceManualBuildFailed ,
828
835
payload : types.MessagePayload {
829
- UserName : "bobby " ,
836
+ UserName : "Bobby " ,
830
837
Labels : map [string ]string {
831
838
"name" : "bobby-workspace" ,
832
839
"template_name" : "bobby-template" ,
@@ -869,14 +876,36 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
869
876
Scan (& titleTmpl , & bodyTmpl )
870
877
require .NoError (t , err , "failed to query body template for template:" , tc .id )
871
878
872
- title , err := render .GoTemplate (titleTmpl , tc .payload , nil )
879
+ title , err := render .GoTemplate (titleTmpl , tc .payload , defaultHelpers () )
873
880
require .NotContainsf (t , title , render .NoValue , "template %q is missing a label value" , tc .name )
874
881
require .NoError (t , err , "failed to render notification title template" )
875
882
require .NotEmpty (t , title , "title should not be empty" )
876
883
877
- body , err := render .GoTemplate (bodyTmpl , tc .payload , nil )
884
+ body , err := render .GoTemplate (bodyTmpl , tc .payload , defaultHelpers () )
878
885
require .NoError (t , err , "failed to render notification body template" )
879
886
require .NotEmpty (t , body , "body should not be empty" )
887
+
888
+ partialName := strings .Split (t .Name (), "/" )[1 ]
889
+ bodyGoldenFile := filepath .Join ("testdata" , "rendered-templates" , partialName + "-body.md.golden" )
890
+ titleGoldenFile := filepath .Join ("testdata" , "rendered-templates" , partialName + "-title.md.golden" )
891
+
892
+ if * updateGoldenFiles {
893
+ err = os .MkdirAll (filepath .Dir (bodyGoldenFile ), 0o755 )
894
+ require .NoError (t , err , "want no error creating golden file directory" )
895
+ err = os .WriteFile (bodyGoldenFile , []byte (body ), 0o600 )
896
+ require .NoError (t , err , "want no error writing body golden file" )
897
+ err = os .WriteFile (titleGoldenFile , []byte (title ), 0o600 )
898
+ require .NoError (t , err , "want no error writing title golden file" )
899
+ return
900
+ }
901
+
902
+ wantBody , err := os .ReadFile (bodyGoldenFile )
903
+ require .NoError (t , err , "open golden file, run \" DB=ci make update-golden-files\" and commit the changes" )
904
+ wantTitle , err := os .ReadFile (titleGoldenFile )
905
+ require .NoError (t , err , "open golden file, run \" DB=ci make update-golden-files\" and commit the changes" )
906
+
907
+ require .Equal (t , string (wantBody ), body , "body should be equal" )
908
+ require .Equal (t , string (wantTitle ), title , "title should be equal" )
880
909
})
881
910
}
882
911
}
0 commit comments