@@ -260,6 +260,40 @@ func TestNewBucket(t *testing.T) {
260260 })
261261}
262262
263+ func TestIfNotExistHasNoSideEffects (t * testing.T ) {
264+ ctx := context .Background ()
265+ dir := t .TempDir ()
266+ const (
267+ key = "key"
268+ origContents = "hello world"
269+ )
270+
271+ b , err := OpenBucket (dir , nil )
272+ if err != nil {
273+ t .Fatal (err )
274+ }
275+ defer b .Close ()
276+ if err := b .WriteAll (ctx , key , []byte (origContents ), & blob.WriterOptions {
277+ Metadata : map [string ]string {"md" : "a" }}); err != nil {
278+ t .Fatalf ("failed to write blob: %v" , err )
279+ }
280+ if err := b .WriteAll (ctx , key , []byte ("goodbye world" ), & blob.WriterOptions {
281+ IfNotExist : true ,
282+ Metadata : map [string ]string {"md" : "b" }}); err == nil || ! errors .Is (err , gcerrors .ErrFailedPrecondition ) {
283+ t .Fatalf ("expected FailedPrecondition error, got %v" , err )
284+ }
285+ if got , err := b .ReadAll (ctx , key ); err != nil {
286+ t .Errorf ("failed to read blob: %v" , err )
287+ } else if string (got ) != origContents {
288+ t .Errorf ("expected to read original contents, got %q" , string (got ))
289+ }
290+ if attrs , err := b .Attributes (ctx , key ); err != nil {
291+ t .Errorf ("failed to get attributes for blob: %v" , err )
292+ } else if attrs .Metadata ["md" ] != "a" {
293+ t .Errorf ("expected metadata to be unchanged, got %v" , attrs .Metadata )
294+ }
295+ }
296+
263297func TestSignedURLReturnsUnimplementedWithNoURLSigner (t * testing.T ) {
264298 dir := t .TempDir ()
265299
0 commit comments