Skip to content

Commit 03222ca

Browse files
committed
use afero filesystem for createAppFromSubdir instead of os calls
1 parent 78ccb06 commit 03222ca

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

internal/pkg/create/create.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,20 @@ func normalizeSubdir(subdir string) (string, error) {
380380
// createAppFromSubdir clones the full template into a temp directory, then copies
381381
// only the specified subdirectory to the final project path.
382382
func createAppFromSubdir(ctx context.Context, dirPath string, template Template, gitBranch string, subdir string, log *logger.Logger, fs afero.Fs) error {
383-
tmpDir, err := os.MkdirTemp("", "slack-create-*")
383+
tmpDirRoot := afero.GetTempDir(fs, "")
384+
tmpDir, err := afero.TempDir(fs, tmpDirRoot, "slack-create-")
384385
if err != nil {
385386
return slackerror.Wrap(err, "failed to create temporary directory")
386387
}
387-
defer os.RemoveAll(tmpDir)
388+
defer func() { _ = fs.RemoveAll(tmpDir) }()
388389

389390
cloneDir := filepath.Join(tmpDir, "repo")
390391
if err := createApp(ctx, cloneDir, template, gitBranch, log, fs); err != nil {
391392
return err
392393
}
393394

394395
subdirPath := filepath.Join(cloneDir, subdir)
395-
info, err := os.Stat(subdirPath)
396+
info, err := fs.Stat(subdirPath)
396397
if err != nil {
397398
if os.IsNotExist(err) {
398399
return slackerror.New(slackerror.ErrSubdirNotFound).

internal/pkg/create/create_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package create
1717
import (
1818
"fmt"
1919
"net/http"
20-
"os"
2120
"path/filepath"
2221
"testing"
2322

@@ -239,38 +238,38 @@ func TestNormalizeSubdir(t *testing.T) {
239238

240239
func TestCreateAppFromSubdir(t *testing.T) {
241240
tests := map[string]struct {
242-
setupTemplate func(t *testing.T) string
241+
setupTemplate func(t *testing.T, fs afero.Fs) string
243242
subdir string
244243
expectError bool
245244
errorContains string
246245
expectFiles []string
247246
}{
248247
"extracts subdirectory from local template": {
249-
setupTemplate: func(t *testing.T) string {
248+
setupTemplate: func(t *testing.T, fs afero.Fs) string {
250249
tmpDir := t.TempDir()
251250
// Create a subdirectory with a file
252251
subdir := filepath.Join(tmpDir, "apps", "my-app")
253-
require.NoError(t, os.MkdirAll(subdir, 0755))
254-
require.NoError(t, os.WriteFile(filepath.Join(subdir, "manifest.json"), []byte(`{}`), 0644))
252+
require.NoError(t, fs.MkdirAll(subdir, 0755))
253+
require.NoError(t, afero.WriteFile(fs, filepath.Join(subdir, "manifest.json"), []byte(`{}`), 0644))
255254
// Create a file at root that should NOT be copied
256-
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "README.md"), []byte("root readme"), 0644))
255+
require.NoError(t, afero.WriteFile(fs, filepath.Join(tmpDir, "README.md"), []byte("root readme"), 0644))
257256
return tmpDir
258257
},
259258
subdir: "apps/my-app",
260259
expectFiles: []string{"manifest.json"},
261260
},
262261
"returns error for nonexistent subdirectory": {
263-
setupTemplate: func(t *testing.T) string {
262+
setupTemplate: func(t *testing.T, fs afero.Fs) string {
264263
return t.TempDir()
265264
},
266265
subdir: "nonexistent",
267266
expectError: true,
268267
errorContains: "was not found in the template",
269268
},
270269
"returns error when subdir path is a file": {
271-
setupTemplate: func(t *testing.T) string {
270+
setupTemplate: func(t *testing.T, fs afero.Fs) string {
272271
tmpDir := t.TempDir()
273-
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "not-a-dir"), []byte("file"), 0644))
272+
require.NoError(t, afero.WriteFile(fs, filepath.Join(tmpDir, "not-a-dir"), []byte("file"), 0644))
274273
return tmpDir
275274
},
276275
subdir: "not-a-dir",
@@ -280,14 +279,14 @@ func TestCreateAppFromSubdir(t *testing.T) {
280279
}
281280
for name, tc := range tests {
282281
t.Run(name, func(t *testing.T) {
283-
templateDir := tc.setupTemplate(t)
282+
fs := afero.NewOsFs()
283+
templateDir := tc.setupTemplate(t, fs)
284284
outputDir := t.TempDir()
285285
// Remove output dir so CopyDirectory can create it
286-
require.NoError(t, os.Remove(outputDir))
286+
require.NoError(t, fs.Remove(outputDir))
287287

288288
template := Template{path: templateDir, isLocal: true}
289289
log := logger.New(func(event *logger.LogEvent) {})
290-
fs := afero.NewOsFs()
291290

292291
err := createAppFromSubdir(t.Context(), outputDir, template, "", tc.subdir, log, fs)
293292

@@ -299,7 +298,7 @@ func TestCreateAppFromSubdir(t *testing.T) {
299298
} else {
300299
assert.NoError(t, err)
301300
for _, f := range tc.expectFiles {
302-
_, statErr := os.Stat(filepath.Join(outputDir, f))
301+
_, statErr := fs.Stat(filepath.Join(outputDir, f))
303302
assert.NoError(t, statErr, "expected file %s to exist", f)
304303
}
305304
}

0 commit comments

Comments
 (0)