@@ -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,8 +157,16 @@ 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 )
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 )
152170 assertFileContent ("include/foo/bar/test.txt" , "include subdir TemplateRepoName" )
153171 assertFileContent (".git/config" , "" )
154172 }
@@ -186,32 +204,38 @@ func TestProcessGiteaTemplateFile(t *testing.T) {
186204 assertSymLink ("subst-${TEMPLATE_NAME}-from-link" , tmpDir + "/sub/link-target" )
187205 }
188206
207+ // case-5
189208 {
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 )
209+ assertFileContent ("real-dir/real-file" , "origin content" )
212210 }
213211}
214212
213+ func TestProcessGiteaTemplateFileRead (t * testing.T ) {
214+ tmpDir := t .TempDir ()
215+ _ = os .Mkdir (tmpDir + "/.gitea" , 0o755 )
216+ templateFilePath := tmpDir + "/.gitea/template"
217+ _ = os .Remove (templateFilePath )
218+ _ , err := os .Lstat (templateFilePath )
219+ require .ErrorIs (t , err , fs .ErrNotExist )
220+ _ , err = readGiteaTemplateFile (tmpDir ) // no template file
221+ require .ErrorIs (t , err , fs .ErrNotExist )
222+
223+ _ = os .WriteFile (templateFilePath + ".target" , []byte ("test-data-target" ), 0o644 )
224+ _ = os .Symlink (templateFilePath + ".target" , templateFilePath )
225+ content , _ := os .ReadFile (templateFilePath )
226+ require .Equal (t , "test-data-target" , string (content ))
227+ _ , err = readGiteaTemplateFile (tmpDir ) // symlinked template file
228+ require .ErrorIs (t , err , fs .ErrNotExist )
229+
230+ _ = os .Remove (templateFilePath )
231+ _ = os .WriteFile (templateFilePath , []byte ("test-data-regular" ), 0o644 )
232+ content , _ = os .ReadFile (templateFilePath )
233+ require .Equal (t , "test-data-regular" , string (content ))
234+ fm , err := readGiteaTemplateFile (tmpDir ) // regular template file
235+ require .NoError (t , err )
236+ assert .Len (t , fm .globs , 1 )
237+ }
238+
215239func TestTransformers (t * testing.T ) {
216240 cases := []struct {
217241 name string
0 commit comments