@@ -3,6 +3,7 @@ package gptscript
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "os"
6
7
"testing"
7
8
)
8
9
@@ -18,7 +19,7 @@ func TestCreateAndDeleteWorkspace(t *testing.T) {
18
19
}
19
20
}
20
21
21
- func TestCreateDirectory (t * testing.T ) {
22
+ func TestWriteReadAndDeleteFileFromWorkspace (t * testing.T ) {
22
23
id , err := g .CreateWorkspace (context .Background (), "directory" )
23
24
if err != nil {
24
25
t .Fatalf ("Error creating workspace: %v" , err )
@@ -31,18 +32,27 @@ func TestCreateDirectory(t *testing.T) {
31
32
}
32
33
})
33
34
34
- err = g .CreateDirectoryInWorkspace (context .Background (), id , "test" )
35
+ err = g .WriteFileInWorkspace (context .Background (), id , "test.txt" , []byte ("test" ))
36
+ if err != nil {
37
+ t .Fatalf ("Error creating file: %v" , err )
38
+ }
39
+
40
+ content , err := g .ReadFileInWorkspace (context .Background (), id , "test.txt" )
35
41
if err != nil {
36
- t .Fatalf ("Error creating directory : %v" , err )
42
+ t .Errorf ("Error reading file : %v" , err )
37
43
}
38
44
39
- err = g .DeleteDirectoryInWorkspace (context .Background (), id , "test" )
45
+ if ! bytes .Equal (content , []byte ("test" )) {
46
+ t .Errorf ("Unexpected content: %s" , content )
47
+ }
48
+
49
+ err = g .DeleteFileInWorkspace (context .Background (), id , "test.txt" )
40
50
if err != nil {
41
- t .Errorf ("Error listing files : %v" , err )
51
+ t .Errorf ("Error deleting file : %v" , err )
42
52
}
43
53
}
44
54
45
- func TestWriteReadAndDeleteFileFromWorkspace (t * testing.T ) {
55
+ func TestLsComplexWorkspace (t * testing.T ) {
46
56
id , err := g .CreateWorkspace (context .Background (), "directory" )
47
57
if err != nil {
48
58
t .Fatalf ("Error creating workspace: %v" , err )
@@ -55,6 +65,96 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
55
65
}
56
66
})
57
67
68
+ err = g .WriteFileInWorkspace (context .Background (), id , "test/test1.txt" , []byte ("hello1" ))
69
+ if err != nil {
70
+ t .Fatalf ("Error creating file: %v" , err )
71
+ }
72
+
73
+ err = g .WriteFileInWorkspace (context .Background (), id , "test1/test2.txt" , []byte ("hello2" ))
74
+ if err != nil {
75
+ t .Fatalf ("Error creating file: %v" , err )
76
+ }
77
+
78
+ err = g .WriteFileInWorkspace (context .Background (), id , "test1/test3.txt" , []byte ("hello3" ))
79
+ if err != nil {
80
+ t .Fatalf ("Error creating file: %v" , err )
81
+ }
82
+
83
+ err = g .WriteFileInWorkspace (context .Background (), id , ".hidden.txt" , []byte ("hidden" ))
84
+ if err != nil {
85
+ t .Fatalf ("Error creating hidden file: %v" , err )
86
+ }
87
+
88
+ // List all files
89
+ content , err := g .ListFilesInWorkspace (context .Background (), id )
90
+ if err != nil {
91
+ t .Fatalf ("Error listing files: %v" , err )
92
+ }
93
+
94
+ if len (content ) != 4 {
95
+ t .Errorf ("Unexpected number of files: %d" , len (content ))
96
+ }
97
+
98
+ // List files in subdirectory
99
+ content , err = g .ListFilesInWorkspace (context .Background (), id , ListFilesInWorkspaceOptions {Prefix : "test1" })
100
+ if err != nil {
101
+ t .Fatalf ("Error listing files: %v" , err )
102
+ }
103
+
104
+ if len (content ) != 2 {
105
+ t .Errorf ("Unexpected number of files: %d" , len (content ))
106
+ }
107
+
108
+ // Remove all files with test1 prefix
109
+ err = g .RemoveAllWithPrefix (context .Background (), id , "test1" )
110
+ if err != nil {
111
+ t .Fatalf ("Error removing files: %v" , err )
112
+ }
113
+
114
+ // List files in subdirectory
115
+ content , err = g .ListFilesInWorkspace (context .Background (), id )
116
+ if err != nil {
117
+ t .Fatalf ("Error listing files: %v" , err )
118
+ }
119
+
120
+ if len (content ) != 2 {
121
+ t .Errorf ("Unexpected number of files: %d" , len (content ))
122
+ }
123
+ }
124
+
125
+ func TestCreateAndDeleteWorkspaceS3 (t * testing.T ) {
126
+ if os .Getenv ("AWS_ACCESS_KEY_ID" ) == "" || os .Getenv ("AWS_SECRET_ACCESS_KEY" ) == "" || os .Getenv ("WORKSPACE_PROVIDER_S3_BUCKET" ) == "" {
127
+ t .Skip ("Skipping test because AWS credentials are not set" )
128
+ }
129
+
130
+ id , err := g .CreateWorkspace (context .Background (), "s3" )
131
+ if err != nil {
132
+ t .Fatalf ("Error creating workspace: %v" , err )
133
+ }
134
+
135
+ err = g .DeleteWorkspace (context .Background (), id )
136
+ if err != nil {
137
+ t .Errorf ("Error deleting workspace: %v" , err )
138
+ }
139
+ }
140
+
141
+ func TestWriteReadAndDeleteFileFromWorkspaceS3 (t * testing.T ) {
142
+ if os .Getenv ("AWS_ACCESS_KEY_ID" ) == "" || os .Getenv ("AWS_SECRET_ACCESS_KEY" ) == "" || os .Getenv ("WORKSPACE_PROVIDER_S3_BUCKET" ) == "" {
143
+ t .Skip ("Skipping test because AWS credentials are not set" )
144
+ }
145
+
146
+ id , err := g .CreateWorkspace (context .Background (), "s3" )
147
+ if err != nil {
148
+ t .Fatalf ("Error creating workspace: %v" , err )
149
+ }
150
+
151
+ t .Cleanup (func () {
152
+ err := g .DeleteWorkspace (context .Background (), id )
153
+ if err != nil {
154
+ t .Errorf ("Error deleting workspace: %v" , err )
155
+ }
156
+ })
157
+
58
158
err = g .WriteFileInWorkspace (context .Background (), id , "test.txt" , []byte ("test" ))
59
159
if err != nil {
60
160
t .Fatalf ("Error creating file: %v" , err )
@@ -75,8 +175,12 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
75
175
}
76
176
}
77
177
78
- func TestLsComplexWorkspace (t * testing.T ) {
79
- id , err := g .CreateWorkspace (context .Background (), "directory" )
178
+ func TestLsComplexWorkspaceS3 (t * testing.T ) {
179
+ if os .Getenv ("AWS_ACCESS_KEY_ID" ) == "" || os .Getenv ("AWS_SECRET_ACCESS_KEY" ) == "" || os .Getenv ("WORKSPACE_PROVIDER_S3_BUCKET" ) == "" {
180
+ t .Skip ("Skipping test because AWS credentials are not set" )
181
+ }
182
+
183
+ id , err := g .CreateWorkspace (context .Background (), "s3" )
80
184
if err != nil {
81
185
t .Fatalf ("Error creating workspace: %v" , err )
82
186
}
@@ -88,29 +192,19 @@ func TestLsComplexWorkspace(t *testing.T) {
88
192
}
89
193
})
90
194
91
- err = g .CreateDirectoryInWorkspace (context .Background (), id , "test" )
92
- if err != nil {
93
- t .Fatalf ("Error creating directory: %v" , err )
94
- }
95
-
96
195
err = g .WriteFileInWorkspace (context .Background (), id , "test/test1.txt" , []byte ("hello1" ))
97
196
if err != nil {
98
197
t .Fatalf ("Error creating file: %v" , err )
99
198
}
100
199
101
- err = g .WriteFileInWorkspace (context .Background (), id , "test1/test2.txt" , []byte ("hello2" ), CreateFileInWorkspaceOptions { CreateDirs : true } )
200
+ err = g .WriteFileInWorkspace (context .Background (), id , "test1/test2.txt" , []byte ("hello2" ))
102
201
if err != nil {
103
202
t .Fatalf ("Error creating file: %v" , err )
104
203
}
105
204
106
- err = g .WriteFileInWorkspace (context .Background (), id , "test1/test2.txt" , []byte ("hello-2" ), CreateFileInWorkspaceOptions {MustNotExist : true })
107
- if err == nil {
108
- t .Fatalf ("Expected error creating file that must not exist" )
109
- }
110
-
111
- err = g .WriteFileInWorkspace (context .Background (), id , "test1/test3.txt" , []byte ("hello3" ), CreateFileInWorkspaceOptions {WithoutCreate : true })
112
- if err == nil {
113
- t .Fatalf ("Expected error creating file that doesn't exist" )
205
+ err = g .WriteFileInWorkspace (context .Background (), id , "test1/test3.txt" , []byte ("hello3" ))
206
+ if err != nil {
207
+ t .Fatalf ("Error creating file: %v" , err )
114
208
}
115
209
116
210
err = g .WriteFileInWorkspace (context .Background (), id , ".hidden.txt" , []byte ("hidden" ))
@@ -124,49 +218,33 @@ func TestLsComplexWorkspace(t *testing.T) {
124
218
t .Fatalf ("Error listing files: %v" , err )
125
219
}
126
220
127
- if content .ID != id {
128
- t .Errorf ("Unexpected ID: %s" , content .ID )
129
- }
130
-
131
- if content .Path != "" {
132
- t .Errorf ("Unexpected path: %s" , content .Path )
133
- }
134
-
135
- if content .FileName != "" {
136
- t .Errorf ("Unexpected filename: %s" , content .FileName )
137
- }
138
-
139
- if len (content .Children ) != 3 {
140
- t .Errorf ("Unexpected number of files: %d" , len (content .Children ))
221
+ if len (content ) != 4 {
222
+ t .Errorf ("Unexpected number of files: %d" , len (content ))
141
223
}
142
224
143
225
// List files in subdirectory
144
- content , err = g .ListFilesInWorkspace (context .Background (), id , ListFilesInWorkspaceOptions {SubDir : "test1" })
226
+ content , err = g .ListFilesInWorkspace (context .Background (), id , ListFilesInWorkspaceOptions {Prefix : "test1" })
145
227
if err != nil {
146
228
t .Fatalf ("Error listing files: %v" , err )
147
229
}
148
230
149
- if len (content . Children ) != 1 {
150
- t .Errorf ("Unexpected number of files: %d" , len (content . Children ))
231
+ if len (content ) != 2 {
232
+ t .Errorf ("Unexpected number of files: %d" , len (content ))
151
233
}
152
234
153
- // Exclude hidden files
154
- content , err = g .ListFilesInWorkspace (context .Background (), id , ListFilesInWorkspaceOptions { ExcludeHidden : true } )
235
+ // Remove all files with test1 prefix
236
+ err = g .RemoveAllWithPrefix (context .Background (), id , "test1" )
155
237
if err != nil {
156
- t .Fatalf ("Error listing files: %v" , err )
238
+ t .Fatalf ("Error removing files: %v" , err )
157
239
}
158
240
159
- if len (content .Children ) != 2 {
160
- t .Errorf ("Unexpected number of files when listing without hidden: %d" , len (content .Children ))
161
- }
162
-
163
- // List non-recursive
164
- content , err = g .ListFilesInWorkspace (context .Background (), id , ListFilesInWorkspaceOptions {NonRecursive : true })
241
+ // List files in subdirectory
242
+ content , err = g .ListFilesInWorkspace (context .Background (), id )
165
243
if err != nil {
166
244
t .Fatalf ("Error listing files: %v" , err )
167
245
}
168
246
169
- if len (content . Children ) != 1 {
170
- t .Errorf ("Unexpected number of files when listing non-recursive : %d" , len (content . Children ))
247
+ if len (content ) != 2 {
248
+ t .Errorf ("Unexpected number of files: %d" , len (content ))
171
249
}
172
250
}
0 commit comments