Skip to content

Commit

Permalink
drop google-cloud-go-testing
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
  • Loading branch information
sagikazarmark committed Jan 9, 2025
1 parent 77b5f5d commit ee4067b
Show file tree
Hide file tree
Showing 14 changed files with 600 additions and 78 deletions.
2 changes: 1 addition & 1 deletion gcsfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"sort"
"syscall"

"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/spf13/afero/gcsfs/internal/stiface"

"cloud.google.com/go/storage"

Expand Down
2 changes: 1 addition & 1 deletion gcsfs/file_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"os"
"syscall"

"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/spf13/afero/gcsfs/internal/stiface"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion gcsfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"syscall"
"time"

"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/spf13/afero/gcsfs/internal/stiface"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion gcsfs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"time"

"cloud.google.com/go/storage"
"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/spf13/afero"
"github.com/spf13/afero/gcsfs/internal/stiface"

"google.golang.org/api/option"
)
Expand Down
2 changes: 1 addition & 1 deletion gcsfs/gcs_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"strings"

"cloud.google.com/go/storage"
"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/spf13/afero"
"github.com/spf13/afero/gcsfs/internal/stiface"
"google.golang.org/api/iterator"
)

Expand Down
2 changes: 1 addition & 1 deletion gcsfs/gcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"golang.org/x/oauth2/google"

"cloud.google.com/go/storage"
"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/spf13/afero"
"github.com/spf13/afero/gcsfs/internal/stiface"
)

const (
Expand Down
4 changes: 4 additions & 0 deletions gcsfs/internal/stiface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copy of [google-cloud-go-testing](https://github.com/googleapis/google-cloud-go-testing)

This is a temporary copy of the [google-cloud-go-testing](https://github.com/googleapis/google-cloud-go-testing) library.
The library is deprecated and the code was copied here to drop it as a dependency (allowing to upgrade other library dependencies).
176 changes: 176 additions & 0 deletions gcsfs/internal/stiface/adapters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package stiface

import (
"context"

"cloud.google.com/go/storage"
)

// AdaptClient adapts a storage.Client so that it satisfies the Client
// interface.
func AdaptClient(c *storage.Client) Client {
return client{c}
}

type (
client struct{ *storage.Client }
bucketHandle struct{ *storage.BucketHandle }
objectHandle struct{ *storage.ObjectHandle }
bucketIterator struct{ *storage.BucketIterator }
objectIterator struct{ *storage.ObjectIterator }
reader struct{ *storage.Reader }
writer struct{ *storage.Writer }
copier struct{ *storage.Copier }
composer struct{ *storage.Composer }
aclHandle struct{ *storage.ACLHandle }
)

func (client) embedToIncludeNewMethods() {}
func (bucketHandle) embedToIncludeNewMethods() {}
func (objectHandle) embedToIncludeNewMethods() {}
func (bucketIterator) embedToIncludeNewMethods() {}
func (objectIterator) embedToIncludeNewMethods() {}
func (writer) embedToIncludeNewMethods() {}
func (reader) embedToIncludeNewMethods() {}
func (copier) embedToIncludeNewMethods() {}
func (composer) embedToIncludeNewMethods() {}
func (aclHandle) embedToIncludeNewMethods() {}

func (c client) Bucket(name string) BucketHandle {
return bucketHandle{c.Client.Bucket(name)}
}

func (c client) Buckets(ctx context.Context, projectID string) BucketIterator {
return bucketIterator{c.Client.Buckets(ctx, projectID)}
}

func (b bucketHandle) Object(name string) ObjectHandle {
return objectHandle{b.BucketHandle.Object(name)}
}

func (b bucketHandle) If(conds storage.BucketConditions) BucketHandle {
return bucketHandle{b.BucketHandle.If(conds)}
}

func (b bucketHandle) Objects(ctx context.Context, q *storage.Query) ObjectIterator {
return objectIterator{b.BucketHandle.Objects(ctx, q)}
}

func (b bucketHandle) DefaultObjectACL() ACLHandle {
return aclHandle{b.BucketHandle.DefaultObjectACL()}
}

func (b bucketHandle) ACL() ACLHandle {
return aclHandle{b.BucketHandle.ACL()}
}

func (b bucketHandle) UserProject(projectID string) BucketHandle {
return bucketHandle{b.BucketHandle.UserProject(projectID)}
}

func (bi bucketIterator) SetPrefix(s string) {
bi.BucketIterator.Prefix = s
}

func (o objectHandle) ACL() ACLHandle {
return aclHandle{o.ObjectHandle.ACL()}
}

func (o objectHandle) Generation(gen int64) ObjectHandle {
return objectHandle{o.ObjectHandle.Generation(gen)}
}

func (o objectHandle) If(conds storage.Conditions) ObjectHandle {
return objectHandle{o.ObjectHandle.If(conds)}
}

func (o objectHandle) Key(encryptionKey []byte) ObjectHandle {
return objectHandle{o.ObjectHandle.Key(encryptionKey)}
}

func (o objectHandle) ReadCompressed(compressed bool) ObjectHandle {
return objectHandle{o.ObjectHandle.ReadCompressed(compressed)}
}

func (o objectHandle) NewReader(ctx context.Context) (Reader, error) {
r, err := o.ObjectHandle.NewReader(ctx)
if err != nil {
return nil, err
}
return reader{r}, nil
}

func (o objectHandle) NewRangeReader(ctx context.Context, offset, length int64) (Reader, error) {
r, err := o.ObjectHandle.NewRangeReader(ctx, offset, length)
if err != nil {
return nil, err
}
return reader{r}, nil
}

func (o objectHandle) NewWriter(ctx context.Context) Writer {
return writer{o.ObjectHandle.NewWriter(ctx)}
}

func (o objectHandle) CopierFrom(src ObjectHandle) Copier {
return copier{o.ObjectHandle.CopierFrom(src.(objectHandle).ObjectHandle)}
}

func (o objectHandle) ComposerFrom(srcs ...ObjectHandle) Composer {
objs := make([]*storage.ObjectHandle, len(srcs))
for i, s := range srcs {
objs[i] = s.(objectHandle).ObjectHandle
}
return composer{o.ObjectHandle.ComposerFrom(objs...)}
}

func (w writer) ObjectAttrs() *storage.ObjectAttrs {
return &w.Writer.ObjectAttrs
}

func (w writer) SetChunkSize(s int) {
w.ChunkSize = s
}

func (w writer) SetProgressFunc(f func(int64)) {
w.ProgressFunc = f
}

func (w writer) SetCRC32C(c uint32) {
w.CRC32C = c
w.SendCRC32C = true
}

func (c copier) ObjectAttrs() *storage.ObjectAttrs {
return &c.Copier.ObjectAttrs
}

func (c copier) SetRewriteToken(t string) {
c.RewriteToken = t
}

func (c copier) SetProgressFunc(f func(copiedBytes, totalBytes uint64)) {
c.ProgressFunc = f
}

func (c copier) SetDestinationKMSKeyName(k string) {
c.DestinationKMSKeyName = k
}

func (c composer) ObjectAttrs() *storage.ObjectAttrs {
return &c.Composer.ObjectAttrs
}
34 changes: 34 additions & 0 deletions gcsfs/internal/stiface/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package stiface provides a set of interfaces for the types in
// cloud.google.com/go/storage. These can be used to create mocks or other test
// doubles. The package also provides adapters to enable the types of the
// storage package to implement these interfaces.
//
// We do not recommend using mocks for most testing. Please read
// https://testing.googleblog.com/2013/05/testing-on-toilet-dont-overuse-mocks.html.
//
// Note: This package is in alpha. Some backwards-incompatible changes may occur.
//
// You must embed these interfaces to implement them:
//
// type ClientMock struct {
// stiface.Client
// ...
// }
//
// This ensures that your implementations will not break when methods are added
// to the interfaces.
package stiface
34 changes: 34 additions & 0 deletions gcsfs/internal/stiface/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package stiface_test

import (
"context"

"cloud.google.com/go/storage"
"github.com/spf13/afero/gcsfs/internal/stiface"
)

func Example_AdaptClient() {
ctx := context.Background()
c, err := storage.NewClient(ctx)
if err != nil {
// TODO: Handle error.
}
client := stiface.AdaptClient(c)
w := client.Bucket("my-bucket").Object("my-object").NewWriter(ctx)
w.ObjectAttrs().ContentType = "text/plain"
// TODO: Use w.
}
Loading

0 comments on commit ee4067b

Please sign in to comment.