@@ -36,6 +36,25 @@ import (
3636 "github.com/valyala/fasthttp/fasthttputil"
3737)
3838
39+ type fileView struct {
40+ path string
41+ content string
42+ loads int
43+ }
44+
45+ func (v * fileView ) Load () error {
46+ contents , err := os .ReadFile (v .path )
47+ if err != nil {
48+ return fmt .Errorf ("read template: %w" , err )
49+ }
50+
51+ v .content = string (contents )
52+ v .loads ++
53+ return nil
54+ }
55+
56+ func (* fileView ) Render (io.Writer , string , any , ... string ) error { return nil }
57+
3958func testEmptyHandler (_ Ctx ) error {
4059 return nil
4160}
@@ -2047,6 +2066,77 @@ func Test_App_ReloadViews_InterfaceNilPointer(t *testing.T) {
20472066 require .ErrorIs (t , err , ErrNoViewEngineConfigured )
20482067}
20492068
2069+ func Test_App_ReloadViews_MountedViews (t * testing.T ) {
2070+ t .Parallel ()
2071+ tempDir := t .TempDir ()
2072+ templatePath := filepath .Join (tempDir , "template.html" )
2073+
2074+ require .NoError (t , os .WriteFile (templatePath , []byte ("before" ), 0o600 ))
2075+
2076+ view := & fileView {path : templatePath }
2077+ subApp := New (Config {Views : view })
2078+ app := New ()
2079+ app .Use ("/sub" , subApp )
2080+
2081+ require .NoError (t , view .Load ())
2082+ initialLoads := view .loads
2083+ require .Equal (t , "before" , view .content )
2084+
2085+ require .NoError (t , os .WriteFile (templatePath , []byte ("after" ), 0o600 ))
2086+ require .NoError (t , app .ReloadViews ())
2087+
2088+ require .Equal (t , "after" , view .content )
2089+ require .Greater (t , view .loads , initialLoads )
2090+ }
2091+
2092+ func Test_App_ReloadViews_MountedViews_Error (t * testing.T ) {
2093+ t .Parallel ()
2094+ expectedErr := errors .New ("sub view error" )
2095+ subView := & countingView {loadErr : expectedErr }
2096+ subApp := New (Config {Views : subView })
2097+ app := New ()
2098+ app .Use ("/sub" , subApp )
2099+
2100+ err := app .ReloadViews ()
2101+ require .ErrorIs (t , err , expectedErr )
2102+ }
2103+
2104+ func Test_App_ReloadViews_MountedViews_MultipleApps (t * testing.T ) {
2105+ t .Parallel ()
2106+ viewA := & countingView {}
2107+ viewB := & countingView {}
2108+ subAppA := New (Config {Views : viewA })
2109+ subAppB := New (Config {Views : viewB })
2110+ app := New ()
2111+ app .Use ("/a" , subAppA )
2112+ app .Use ("/b" , subAppB )
2113+
2114+ initialLoadsA := viewA .loads
2115+ initialLoadsB := viewB .loads
2116+
2117+ require .NoError (t , app .ReloadViews ())
2118+
2119+ require .Equal (t , initialLoadsA + 1 , viewA .loads )
2120+ require .Equal (t , initialLoadsB + 1 , viewB .loads )
2121+ }
2122+
2123+ func Test_App_ReloadViews_MountedViews_WithParentViews (t * testing.T ) {
2124+ t .Parallel ()
2125+ parentView := & countingView {}
2126+ subView := & countingView {}
2127+ subApp := New (Config {Views : subView })
2128+ app := New (Config {Views : parentView })
2129+ app .Use ("/sub" , subApp )
2130+
2131+ initialParentLoads := parentView .loads
2132+ initialSubLoads := subView .loads
2133+
2134+ require .NoError (t , app .ReloadViews ())
2135+
2136+ require .Equal (t , initialParentLoads + 1 , parentView .loads )
2137+ require .Equal (t , initialSubLoads + 1 , subView .loads )
2138+ }
2139+
20502140// go test -run Test_App_Init_Error_View
20512141func Test_App_Init_Error_View (t * testing.T ) {
20522142 t .Parallel ()
0 commit comments