@@ -23,25 +23,66 @@ func TestBuiltInReader(t *testing.T) {
2323 for _ , name := range exists {
2424 t .Run (name , func (t * testing.T ) {
2525 r := & builtinReader {name : name }
26- fsys , err := r .FS (context .Background ())
26+ schema , fsys , err := r .LoadSchemaAndTemplateFS (context .Background ())
2727 assert .NoError (t , err )
2828 assert .NotNil (t , fsys )
29+ assert .NotNil (t , schema )
2930
30- // Assert file content returned is accurate and every template has a welcome
31- // message defined.
32- b , err := fs .ReadFile (fsys , "databricks_template_schema.json" )
33- require .NoError (t , err )
34- assert .Contains (t , string (b ), "welcome_message" )
31+ // Assert schema has a welcome message defined.
32+ assert .NotEmpty (t , schema .WelcomeMessage )
3533 })
3634 }
3735
3836 t .Run ("doesnotexist" , func (t * testing.T ) {
3937 r := & builtinReader {name : "doesnotexist" }
40- _ , err := r .FS (context .Background ())
38+ _ , _ , err := r .LoadSchemaAndTemplateFS (context .Background ())
4139 assert .EqualError (t , err , "builtin template doesnotexist not found" )
4240 })
4341}
4442
43+ func TestBuiltInReaderTemplateDir (t * testing.T ) {
44+ // Test that template_dir property works correctly
45+ // default-python template should use schema from default-python/ but template files from default/
46+ r := & builtinReader {name : "default-python" }
47+ schema , fsys , err := r .LoadSchemaAndTemplateFS (context .Background ())
48+ require .NoError (t , err )
49+ assert .NotNil (t , schema )
50+ assert .NotNil (t , fsys )
51+
52+ // Verify the schema contains default-python specific content
53+ assert .Contains (t , schema .WelcomeMessage , "default Python template" )
54+
55+ // Verify we can read template files (should come from default/)
56+ templateFiles , err := fs .ReadDir (fsys , "template" )
57+ require .NoError (t , err )
58+ assert .NotEmpty (t , templateFiles )
59+
60+ // Verify that a specific template file exists (this should come from default/ template)
61+ _ , err = fs .Stat (fsys , "template/{{.project_name}}/databricks.yml.tmpl" )
62+ assert .NoError (t , err )
63+
64+ // Test that a template without template_dir works normally
65+ r2 := & builtinReader {name : "default-sql" }
66+ schema2 , fsys2 , err := r2 .LoadSchemaAndTemplateFS (context .Background ())
67+ require .NoError (t , err )
68+ assert .NotNil (t , schema2 )
69+ assert .NotNil (t , fsys2 )
70+
71+ // For default-sql, the schema should not reference template_dir
72+ assert .Contains (t , schema2 .WelcomeMessage , "default SQL template" )
73+
74+ // Verify that lakeflow-pipelines also uses template_dir correctly
75+ r3 := & builtinReader {name : "lakeflow-pipelines" }
76+ schema3 , fsys3 , err := r3 .LoadSchemaAndTemplateFS (context .Background ())
77+ require .NoError (t , err )
78+ assert .NotNil (t , schema3 )
79+ assert .NotNil (t , fsys3 )
80+
81+ // lakeflow-pipelines should also have template files from default/
82+ _ , err = fs .Stat (fsys3 , "template/{{.project_name}}/databricks.yml.tmpl" )
83+ assert .NoError (t , err )
84+ }
85+
4586func TestGitUrlReader (t * testing.T ) {
4687 ctx := cmdio .MockDiscard (context .Background ())
4788
@@ -51,6 +92,7 @@ func TestGitUrlReader(t *testing.T) {
5192 numCalls ++
5293 args = []string {url , reference , targetPath }
5394 testutil .WriteFile (t , filepath .Join (targetPath , "a" , "b" , "c" , "somefile" ), "somecontent" )
95+ testutil .WriteFile (t , filepath .Join (targetPath , "a" , "b" , "c" , "databricks_template_schema.json" ), `{"welcome_message": "test"}` )
5496 return nil
5597 }
5698 r := & gitReader {
@@ -61,21 +103,22 @@ func TestGitUrlReader(t *testing.T) {
61103 }
62104
63105 // Assert cloneFunc is called with the correct args.
64- fsys , err := r .FS (ctx )
106+ schema , fsys , err := r .LoadSchemaAndTemplateFS (ctx )
65107 require .NoError (t , err )
66108 require .NotEmpty (t , r .tmpRepoDir )
67109 assert .Equal (t , 1 , numCalls )
68110 assert .DirExists (t , r .tmpRepoDir )
69111 assert .Equal (t , []string {"someurl" , "sometag" , r .tmpRepoDir }, args )
112+ assert .NotNil (t , schema )
70113
71114 // Assert the fs returned is rooted at the templateDir.
72115 b , err := fs .ReadFile (fsys , "somefile" )
73116 require .NoError (t , err )
74117 assert .Equal (t , "somecontent" , string (b ))
75118
76- // Assert second call to FS returns an error.
77- _ , err = r .FS (ctx )
78- assert .ErrorContains (t , err , "FS called twice on git reader" )
119+ // Assert second call returns an error.
120+ _ , _ , err = r .LoadSchemaAndTemplateFS (ctx )
121+ assert .ErrorContains (t , err , "LoadSchemaAndTemplateFS called twice on git reader" )
79122
80123 // Assert the downloaded repository is cleaned up.
81124 _ , err = fs .Stat (fsys , "." )
@@ -88,14 +131,87 @@ func TestGitUrlReader(t *testing.T) {
88131func TestLocalReader (t * testing.T ) {
89132 tmpDir := t .TempDir ()
90133 testutil .WriteFile (t , filepath .Join (tmpDir , "somefile" ), "somecontent" )
134+ testutil .WriteFile (t , filepath .Join (tmpDir , "databricks_template_schema.json" ), `{"welcome_message": "test"}` )
91135 ctx := context .Background ()
92136
93137 r := & localReader {path : tmpDir }
94- fsys , err := r .FS (ctx )
138+ schema , fsys , err := r .LoadSchemaAndTemplateFS (ctx )
95139 require .NoError (t , err )
140+ assert .NotNil (t , schema )
96141
97142 // Assert the fs returned is rooted at correct location.
98143 b , err := fs .ReadFile (fsys , "somefile" )
99144 require .NoError (t , err )
100145 assert .Equal (t , "somecontent" , string (b ))
101146}
147+
148+ func TestLocalReaderWithTemplateDir (t * testing.T ) {
149+ tmpDir := t .TempDir ()
150+
151+ // Create a template directory with template_dir pointing to another directory
152+ schemaDir := filepath .Join (tmpDir , "schema-template" )
153+ templateDir := filepath .Join (tmpDir , "actual-template" )
154+
155+ // Create the schema template directory with a schema that references ../actual-template
156+ testutil .WriteFile (t , filepath .Join (schemaDir , "databricks_template_schema.json" ),
157+ `{"welcome_message": "test with template_dir", "template_dir": "../actual-template"}` )
158+
159+ // Create the actual template directory with template files
160+ testutil .WriteFile (t , filepath .Join (templateDir , "template" , "somefile" ), "content from template_dir" )
161+ testutil .WriteFile (t , filepath .Join (templateDir , "template" , "{{.project_name}}" , "test.yml.tmpl" ), "test template content" )
162+
163+ ctx := context .Background ()
164+ r := & localReader {path : schemaDir }
165+ schema , fsys , err := r .LoadSchemaAndTemplateFS (ctx )
166+ require .NoError (t , err )
167+ assert .NotNil (t , schema )
168+ assert .Equal (t , "test with template_dir" , schema .WelcomeMessage )
169+
170+ // Assert the fs returned is rooted at the template_dir location
171+ b , err := fs .ReadFile (fsys , "template/somefile" )
172+ require .NoError (t , err )
173+ assert .Equal (t , "content from template_dir" , string (b ))
174+
175+ // Verify we can read the templated file
176+ b2 , err := fs .ReadFile (fsys , "template/{{.project_name}}/test.yml.tmpl" )
177+ require .NoError (t , err )
178+ assert .Equal (t , "test template content" , string (b2 ))
179+ }
180+
181+ func TestGitReaderWithTemplateDir (t * testing.T ) {
182+ ctx := cmdio .MockDiscard (context .Background ())
183+
184+ cloneFunc := func (ctx context.Context , url , reference , targetPath string ) error {
185+ // Create a template with template_dir reference
186+ schemaDir := filepath .Join (targetPath , "a" , "b" , "c" )
187+ templateDir := filepath .Join (targetPath , "a" , "b" , "actual-template" )
188+
189+ testutil .WriteFile (t , filepath .Join (schemaDir , "databricks_template_schema.json" ),
190+ `{"welcome_message": "git test with template_dir", "template_dir": "../actual-template"}` )
191+
192+ // Create the actual template directory with template files
193+ testutil .WriteFile (t , filepath .Join (templateDir , "template" , "gitfile" ), "content from git template_dir" )
194+
195+ return nil
196+ }
197+
198+ r := & gitReader {
199+ gitUrl : "someurl" ,
200+ cloneFunc : cloneFunc ,
201+ ref : "sometag" ,
202+ templateDir : "a/b/c" ,
203+ }
204+
205+ schema , fsys , err := r .LoadSchemaAndTemplateFS (ctx )
206+ require .NoError (t , err )
207+ assert .NotNil (t , schema )
208+ assert .Equal (t , "git test with template_dir" , schema .WelcomeMessage )
209+
210+ // Assert the fs returned is rooted at the template_dir location
211+ b , err := fs .ReadFile (fsys , "template/gitfile" )
212+ require .NoError (t , err )
213+ assert .Equal (t , "content from git template_dir" , string (b ))
214+
215+ // Cleanup
216+ r .Cleanup (ctx )
217+ }
0 commit comments