@@ -194,6 +194,76 @@ func TestFetchAndApply_Success(t *testing.T) {
194194 }
195195}
196196
197+ func TestFetchAndApply_DefaultTempDirIsRemoved (t * testing.T ) {
198+ tests := []struct {
199+ name string
200+ blob func (* testing.T ) []byte
201+ wantErr bool
202+ }{
203+ {
204+ name : "success" ,
205+ blob : func (t * testing.T ) []byte {
206+ return buildTemplateTarGz (t , templateEntries ())
207+ },
208+ },
209+ {
210+ name : "extraction error" ,
211+ blob : func (* testing.T ) []byte {
212+ return []byte ("not a valid gzip tarball" )
213+ },
214+ wantErr : true ,
215+ },
216+ }
217+
218+ for _ , tt := range tests {
219+ t .Run (tt .name , func (t * testing.T ) {
220+ tempRoot := t .TempDir ()
221+ t .Setenv ("TMPDIR" , tempRoot )
222+ params := ApplyParams {
223+ OCIClient : & fakeTemplateRegistry {blob : tt .blob (t )},
224+ FS : pfs.OSFS {},
225+ Logger : log .Null ,
226+ WorkspaceFolder : t .TempDir (),
227+ }
228+
229+ _ , err := FetchAndApply (params , SelectedTemplate {ID : "ghcr.io/devcontainers/templates/sample:1" })
230+ if (err != nil ) != tt .wantErr {
231+ t .Fatalf ("FetchAndApply() error = %v, wantErr %v" , err , tt .wantErr )
232+ }
233+ entries , readErr := os .ReadDir (tempRoot )
234+ if readErr != nil {
235+ t .Fatal (readErr )
236+ }
237+ if len (entries ) != 0 {
238+ t .Fatalf ("default temp root contains %v after FetchAndApply" , entries )
239+ }
240+ })
241+ }
242+ }
243+
244+ func TestFetchAndApply_CallerTempDirIsPreserved (t * testing.T ) {
245+ tempRoot := t .TempDir ()
246+ params := ApplyParams {
247+ OCIClient : & fakeTemplateRegistry {blob : buildTemplateTarGz (t , templateEntries ())},
248+ FS : pfs.OSFS {},
249+ Logger : log .Null ,
250+ WorkspaceFolder : t .TempDir (),
251+ TmpDir : tempRoot ,
252+ }
253+
254+ _ , err := FetchAndApply (params , SelectedTemplate {ID : "ghcr.io/devcontainers/templates/sample:1" })
255+ if err != nil {
256+ t .Fatalf ("FetchAndApply: %v" , err )
257+ }
258+ entries , err := os .ReadDir (tempRoot )
259+ if err != nil {
260+ t .Fatal (err )
261+ }
262+ if len (entries ) != 1 || entries [0 ].Name () != "template-sample" {
263+ t .Fatalf ("caller temp root contains %v, want preserved template-sample directory" , entries )
264+ }
265+ }
266+
197267// TestFetchAndApply_PartialWorkspaceWrite covers the partial-write risk: a WriteFile
198268// that fails mid-Walk. The first workspace file is written, the second fails,
199269// and the error must propagate (wrapped) instead of silently leaving a partial
0 commit comments