@@ -74,7 +74,7 @@ func TestFilePathSanitize(t *testing.T) {
7474 assert .Equal (t , "." , filePathSanitize ("/" ))
7575}
7676
77- func TestProcessGiteaTemplateFile (t * testing.T ) {
77+ func TestProcessGiteaTemplateFileGenerate (t * testing.T ) {
7878 tmpDir := filepath .Join (t .TempDir (), "gitea-template-test" )
7979
8080 assertFileContent := func (path , expected string ) {
@@ -129,10 +129,20 @@ func TestProcessGiteaTemplateFile(t *testing.T) {
129129 assertFileContent ("subst-${TEMPLATE_NAME}-to-link" , toLinkContent )
130130 assertFileContent ("subst-${TEMPLATE_NAME}-from-link" , fromLinkContent )
131131 }
132+
133+ // case-5
134+ {
135+ require .NoError (t , os .MkdirAll (tmpDir + "/real-dir" , 0o755 ))
136+ require .NoError (t , os .WriteFile (tmpDir + "/real-dir/real-file" , []byte ("origin content" ), 0o644 ))
137+ require .NoError (t , os .MkdirAll (tmpDir + "/include/subst-${TEMPLATE_NAME}-link-dir" , 0o755 ))
138+ require .NoError (t , os .WriteFile (tmpDir + "/include/subst-${TEMPLATE_NAME}-link-dir/real-file" , []byte ("template content" ), 0o644 ))
139+ require .NoError (t , os .Symlink (tmpDir + "/real-dir" , tmpDir + "/include/subst-TemplateRepoName-link-dir" ))
140+ }
141+
132142 {
133143 // will succeed
134144 require .NoError (t , os .WriteFile (tmpDir + "/subst-${TEMPLATE_NAME}-normal" , []byte ("dummy subst template name normal" ), 0o644 ))
135- // will skil if the path subst result is a link
145+ // will be skipped if the path subst result is a link
136146 require .NoError (t , os .WriteFile (tmpDir + "/subst-${TEMPLATE_NAME}-to-link" , []byte ("dummy subst template name to link" ), 0o644 ))
137147 require .NoError (t , os .Symlink (tmpDir + "/sub/link-target" , tmpDir + "/subst-TemplateRepoName-to-link" ))
138148 // will be skipped since the source is a symlink
@@ -147,10 +157,19 @@ func TestProcessGiteaTemplateFile(t *testing.T) {
147157 generatedRepo := & repo_model.Repository {Name : "/../.gIt/name" }
148158 assertFileContent (".git/config" , "git-config-dummy" )
149159 fileMatcher , _ := readGiteaTemplateFile (tmpDir )
150- err := processGiteaTemplateFile (t .Context (), tmpDir , templateRepo , generatedRepo , fileMatcher )
160+ skippedFiles , err := processGiteaTemplateFile (t .Context (), tmpDir , templateRepo , generatedRepo , fileMatcher )
151161 require .NoError (t , err )
152- assertFileContent ("include/foo/bar/test.txt" , "include subdir TemplateRepoName" )
162+ assert .Equal (t , []string {
163+ "include/subst-${TEMPLATE_NAME}-link-dir/real-file" ,
164+ "include/subst-TemplateRepoName-link-dir" ,
165+ "link" ,
166+ "subst-${TEMPLATE_NAME}-from-link" ,
167+ "subst-${TEMPLATE_NAME}-to-link" ,
168+ "subst-TemplateRepoName-to-link" ,
169+ }, skippedFiles )
153170 assertFileContent (".git/config" , "" )
171+ assertFileContent (".gitea/template" , "" )
172+ assertFileContent ("include/foo/bar/test.txt" , "include subdir TemplateRepoName" )
154173 }
155174
156175 // the lin target should never be modified, and since it is in a subdirectory, it is not affected by the template either
@@ -186,32 +205,38 @@ func TestProcessGiteaTemplateFile(t *testing.T) {
186205 assertSymLink ("subst-${TEMPLATE_NAME}-from-link" , tmpDir + "/sub/link-target" )
187206 }
188207
208+ // case-5
189209 {
190- templateFilePath := tmpDir + "/.gitea/template"
191-
192- _ = os .Remove (templateFilePath )
193- _ , err := os .Lstat (templateFilePath )
194- require .ErrorIs (t , err , fs .ErrNotExist )
195- _ , err = readGiteaTemplateFile (tmpDir ) // no template file
196- require .ErrorIs (t , err , fs .ErrNotExist )
197-
198- _ = os .WriteFile (templateFilePath + ".target" , []byte ("test-data-target" ), 0o644 )
199- _ = os .Symlink (templateFilePath + ".target" , templateFilePath )
200- content , _ := os .ReadFile (templateFilePath )
201- require .Equal (t , "test-data-target" , string (content ))
202- _ , err = readGiteaTemplateFile (tmpDir ) // symlinked template file
203- require .ErrorIs (t , err , fs .ErrNotExist )
204-
205- _ = os .Remove (templateFilePath )
206- _ = os .WriteFile (templateFilePath , []byte ("test-data-regular" ), 0o644 )
207- content , _ = os .ReadFile (templateFilePath )
208- require .Equal (t , "test-data-regular" , string (content ))
209- fm , err := readGiteaTemplateFile (tmpDir ) // regular template file
210- require .NoError (t , err )
211- assert .Len (t , fm .globs , 1 )
210+ assertFileContent ("real-dir/real-file" , "origin content" )
212211 }
213212}
214213
214+ func TestProcessGiteaTemplateFileRead (t * testing.T ) {
215+ tmpDir := t .TempDir ()
216+ _ = os .Mkdir (tmpDir + "/.gitea" , 0o755 )
217+ templateFilePath := tmpDir + "/.gitea/template"
218+ _ = os .Remove (templateFilePath )
219+ _ , err := os .Lstat (templateFilePath )
220+ require .ErrorIs (t , err , fs .ErrNotExist )
221+ _ , err = readGiteaTemplateFile (tmpDir ) // no template file
222+ require .ErrorIs (t , err , fs .ErrNotExist )
223+
224+ _ = os .WriteFile (templateFilePath + ".target" , []byte ("test-data-target" ), 0o644 )
225+ _ = os .Symlink (templateFilePath + ".target" , templateFilePath )
226+ content , _ := os .ReadFile (templateFilePath )
227+ require .Equal (t , "test-data-target" , string (content ))
228+ _ , err = readGiteaTemplateFile (tmpDir ) // symlinked template file
229+ require .ErrorIs (t , err , fs .ErrNotExist )
230+
231+ _ = os .Remove (templateFilePath )
232+ _ = os .WriteFile (templateFilePath , []byte ("test-data-regular" ), 0o644 )
233+ content , _ = os .ReadFile (templateFilePath )
234+ require .Equal (t , "test-data-regular" , string (content ))
235+ fm , err := readGiteaTemplateFile (tmpDir ) // regular template file
236+ require .NoError (t , err )
237+ assert .Len (t , fm .globs , 1 )
238+ }
239+
215240func TestTransformers (t * testing.T ) {
216241 cases := []struct {
217242 name string
0 commit comments