Skip to content

Commit d1f148f

Browse files
authored
chore: stop creating directory when creating workspace (#16)
Signed-off-by: Donnie Adams <[email protected]>
1 parent b24dbdd commit d1f148f

File tree

6 files changed

+20
-43
lines changed

6 files changed

+20
-43
lines changed

pkg/client/client.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020

2121
type workspaceFactory interface {
2222
New(string) (workspaceClient, error)
23-
Create() (string, error)
23+
Create() string
2424
Rm(context.Context, string) error
2525
}
2626

@@ -102,11 +102,7 @@ func (c *Client) Create(ctx context.Context, provider string, fromWorkspaces ...
102102
return "", err
103103
}
104104

105-
id, err := factory.Create()
106-
if err != nil {
107-
return "", err
108-
}
109-
105+
id := factory.Create()
110106
destClient, err := factory.New(id)
111107
if err != nil {
112108
return "", err

pkg/client/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ func TestCreateAndRmDirectoryProvider(t *testing.T) {
4141
t.Errorf("unexpected id: %s", id)
4242
}
4343

44-
// The directory should exist
45-
if _, err = os.Stat(strings.TrimPrefix(id, DirectoryProvider+"://")); err != nil {
44+
// The directory should not exist yet
45+
if _, err := os.Stat(strings.TrimPrefix(id, DirectoryProvider+"://")); !errors.Is(err, os.ErrNotExist) {
4646
t.Errorf("unexpected error when checking if directory exists: %v", err)
4747
}
4848

49-
if err = c.Rm(context.Background(), id); err != nil {
49+
if err := c.Rm(context.Background(), id); err != nil {
5050
t.Errorf("unexpected error when removing workspace: %v", err)
5151
}
5252

pkg/client/directory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ func (d *directoryProvider) New(id string) (workspaceClient, error) {
7777
}, nil
7878
}
7979

80-
func (d *directoryProvider) Create() (string, error) {
80+
func (d *directoryProvider) Create() string {
8181
dir := uuid.NewString()
82-
return DirectoryProvider + "://" + filepath.Join(d.dataHome, dir), os.MkdirAll(filepath.Join(d.dataHome, dir), 0o755)
82+
return DirectoryProvider + "://" + filepath.Join(d.dataHome, dir)
8383
}
8484

8585
func (d *directoryProvider) Rm(ctx context.Context, id string) error {

pkg/client/directory_test.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,14 @@ var (
2424
)
2525

2626
func TestMain(m *testing.M) {
27-
var err error
2827
directoryFactory = newDirectory("")
29-
directoryTestingID, err = directoryFactory.Create()
30-
if err != nil {
31-
panic(err)
32-
}
33-
28+
directoryTestingID = directoryFactory.Create()
3429
dirPrv, _ = directoryFactory.New(directoryTestingID)
3530

3631
if !skipS3Tests {
3732
s3Factory, _ = newS3(context.Background(), os.Getenv("WORKSPACE_PROVIDER_S3_BUCKET"), os.Getenv("WORKSPACE_PROVIDER_S3_BASE_ENDPOINT"))
3833
// This won't ever error because it doesn't create anything.
39-
s3TestingID, _ = s3Factory.Create()
34+
s3TestingID = s3Factory.Create()
4035

4136
s3Client, _ := s3Factory.New(s3TestingID)
4237
s3Prv = s3Client.(*s3Provider)
@@ -63,18 +58,14 @@ func TestMain(m *testing.M) {
6358
}
6459

6560
func TestCreateAndRm(t *testing.T) {
66-
id, err := directoryFactory.Create()
67-
if err != nil {
68-
t.Errorf("error creating workspace: %v", err)
69-
}
70-
61+
id := directoryFactory.Create()
7162
if !strings.HasPrefix(id, DirectoryProvider+"://") {
7263
t.Errorf("unexpected id: %s", id)
7364
}
7465

75-
// The directory should exist
76-
if _, err := os.Stat(strings.TrimPrefix(id, DirectoryProvider+"://")); err != nil {
77-
t.Errorf("unexpcted error when checking if directory exists: %v", err)
66+
// The directory should not exist yet
67+
if _, err := os.Stat(strings.TrimPrefix(id, DirectoryProvider+"://")); !errors.Is(err, os.ErrNotExist) {
68+
t.Errorf("unexpected error when checking if directory exists: %v", err)
7869
}
7970

8071
if err := directoryFactory.Rm(context.Background(), id); err != nil {
@@ -88,13 +79,10 @@ func TestCreateAndRm(t *testing.T) {
8879
}
8980

9081
func TestWriteFileWorkspaceDNE(t *testing.T) {
91-
id, err := directoryFactory.Create()
92-
if err != nil {
93-
t.Fatalf("error creating workspace: %v", err)
94-
}
82+
id := directoryFactory.Create()
9583

9684
// Delete the directory
97-
if err = os.RemoveAll(strings.TrimPrefix(id, DirectoryProvider+"://")); err != nil {
85+
if err := os.RemoveAll(strings.TrimPrefix(id, DirectoryProvider+"://")); err != nil {
9886
t.Errorf("unexpected error when removing workspace: %v", err)
9987
}
10088

@@ -113,12 +101,9 @@ func TestWriteFileWorkspaceDNE(t *testing.T) {
113101
}
114102

115103
func TestEnsureCannotCreateUnsafeWorkspace(t *testing.T) {
116-
id, err := directoryFactory.Create()
117-
if err != nil {
118-
t.Fatalf("error creating workspace: %v", err)
119-
}
104+
id := directoryFactory.Create()
120105

121-
_, err = directoryFactory.New(id + "/..")
106+
_, err := directoryFactory.New(id + "/..")
122107
if err == nil {
123108
t.Fatalf("expected error when creating directory outside of workspace")
124109
}

pkg/client/s3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func (s *s3Provider) New(id string) (workspaceClient, error) {
6262
}, nil
6363
}
6464

65-
func (s *s3Provider) Create() (string, error) {
66-
return S3Provider + "://" + filepath.Join(s.bucket, uuid.NewString()), nil
65+
func (s *s3Provider) Create() string {
66+
return S3Provider + "://" + filepath.Join(s.bucket, uuid.NewString())
6767
}
6868

6969
func (s *s3Provider) Rm(ctx context.Context, id string) error {

pkg/client/s3_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ func TestCreateAndRmS3(t *testing.T) {
2222
t.Skip("Skipping S3 tests")
2323
}
2424

25-
id, err := s3Factory.Create()
26-
if err != nil {
27-
t.Errorf("error creating workspace: %v", err)
28-
}
29-
25+
id := s3Factory.Create()
3026
if !strings.HasPrefix(id, S3Provider+"://") {
3127
t.Errorf("unexpected id: %s", id)
3228
}

0 commit comments

Comments
 (0)