forked from mirror/afero
Added test cases for `Mkdir` & `MkdirAll` to cover empty folder names
This commit is contained in:
parent
78acf3b074
commit
13ee679c8e
|
@ -146,6 +146,10 @@ type writerMock struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *writerMock) Write(p []byte) (n int, err error) {
|
func (w *writerMock) Write(p []byte) (n int, err error) {
|
||||||
|
if w.name == "" {
|
||||||
|
return 0, gcsfs.ErrEmptyObjectName
|
||||||
|
}
|
||||||
|
|
||||||
if w.file == nil {
|
if w.file == nil {
|
||||||
w.file, err = w.fs.Create(w.name)
|
w.file, err = w.fs.Create(w.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -157,6 +161,9 @@ func (w *writerMock) Write(p []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *writerMock) Close() error {
|
func (w *writerMock) Close() error {
|
||||||
|
if w.name == "" {
|
||||||
|
return gcsfs.ErrEmptyObjectName
|
||||||
|
}
|
||||||
if w.file == nil {
|
if w.file == nil {
|
||||||
var err error
|
var err error
|
||||||
if strings.HasSuffix(w.name, "/") {
|
if strings.HasSuffix(w.name, "/") {
|
||||||
|
|
144
gcs_test.go
144
gcs_test.go
|
@ -712,76 +712,96 @@ func TestGcsGlob(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGcsMkdir(t *testing.T) {
|
func TestGcsMkdir(t *testing.T) {
|
||||||
dirName := filepath.Join(bucketName, "a-test-dir")
|
t.Run("empty", func(t *testing.T) {
|
||||||
var err error
|
emptyDirName := bucketName
|
||||||
|
|
||||||
err = gcsAfs.Mkdir(dirName, 0755)
|
err := gcsAfs.Mkdir(emptyDirName, 0755)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
t.Fatal("failed to create a folder with error", err)
|
t.Fatal("did not fail upon creation of an empty folder")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
t.Run("success", func(t *testing.T) {
|
||||||
|
dirName := filepath.Join(bucketName, "a-test-dir")
|
||||||
|
var err error
|
||||||
|
|
||||||
info, err := gcsAfs.Stat(dirName)
|
err = gcsAfs.Mkdir(dirName, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("failed to get info", err)
|
t.Fatal("failed to create a folder with error", err)
|
||||||
}
|
}
|
||||||
if !info.IsDir() {
|
|
||||||
t.Fatalf("%s: not a dir", dirName)
|
|
||||||
}
|
|
||||||
if !info.Mode().IsDir() {
|
|
||||||
t.Errorf("%s: mode is not directory", dirName)
|
|
||||||
}
|
|
||||||
|
|
||||||
if info.Mode() != os.ModeDir|0755 {
|
info, err := gcsAfs.Stat(dirName)
|
||||||
t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", dirName, info.Mode())
|
if err != nil {
|
||||||
}
|
t.Fatal("failed to get info", err)
|
||||||
|
}
|
||||||
|
if !info.IsDir() {
|
||||||
|
t.Fatalf("%s: not a dir", dirName)
|
||||||
|
}
|
||||||
|
if !info.Mode().IsDir() {
|
||||||
|
t.Errorf("%s: mode is not directory", dirName)
|
||||||
|
}
|
||||||
|
|
||||||
err = gcsAfs.Remove(dirName)
|
if info.Mode() != os.ModeDir|0755 {
|
||||||
if err != nil {
|
t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", dirName, info.Mode())
|
||||||
t.Fatalf("could not delete the folder %s after the test with error: %s", dirName, err)
|
}
|
||||||
}
|
|
||||||
|
err = gcsAfs.Remove(dirName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not delete the folder %s after the test with error: %s", dirName, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGcsMkdirAll(t *testing.T) {
|
func TestGcsMkdirAll(t *testing.T) {
|
||||||
dirName := filepath.Join(bucketName, "a/b/c")
|
t.Run("empty", func(t *testing.T) {
|
||||||
|
emptyDirName := bucketName
|
||||||
|
|
||||||
err := gcsAfs.MkdirAll(dirName, 0755)
|
err := gcsAfs.MkdirAll(emptyDirName, 0755)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
t.Fatal(err)
|
t.Fatal("did not fail upon creation of an empty folder")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
t.Run("success", func(t *testing.T) {
|
||||||
|
dirName := filepath.Join(bucketName, "a/b/c")
|
||||||
|
|
||||||
info, err := gcsAfs.Stat(filepath.Join(bucketName, "a"))
|
err := gcsAfs.MkdirAll(dirName, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !info.Mode().IsDir() {
|
|
||||||
t.Errorf("%s: mode is not directory", filepath.Join(bucketName, "a"))
|
|
||||||
}
|
|
||||||
if info.Mode() != os.ModeDir|0755 {
|
|
||||||
t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", filepath.Join(bucketName, "a"), info.Mode())
|
|
||||||
}
|
|
||||||
info, err = gcsAfs.Stat(filepath.Join(bucketName, "a/b"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !info.Mode().IsDir() {
|
|
||||||
t.Errorf("%s: mode is not directory", filepath.Join(bucketName, "a/b"))
|
|
||||||
}
|
|
||||||
if info.Mode() != os.ModeDir|0755 {
|
|
||||||
t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", filepath.Join(bucketName, "a/b"), info.Mode())
|
|
||||||
}
|
|
||||||
info, err = gcsAfs.Stat(dirName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !info.Mode().IsDir() {
|
|
||||||
t.Errorf("%s: mode is not directory", dirName)
|
|
||||||
}
|
|
||||||
if info.Mode() != os.ModeDir|0755 {
|
|
||||||
t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", dirName, info.Mode())
|
|
||||||
}
|
|
||||||
|
|
||||||
err = gcsAfs.RemoveAll(filepath.Join(bucketName, "a"))
|
info, err := gcsAfs.Stat(filepath.Join(bucketName, "a"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to remove the folder %s with error: %s", filepath.Join(bucketName, "a"), err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if !info.Mode().IsDir() {
|
||||||
|
t.Errorf("%s: mode is not directory", filepath.Join(bucketName, "a"))
|
||||||
|
}
|
||||||
|
if info.Mode() != os.ModeDir|0755 {
|
||||||
|
t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", filepath.Join(bucketName, "a"), info.Mode())
|
||||||
|
}
|
||||||
|
info, err = gcsAfs.Stat(filepath.Join(bucketName, "a/b"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !info.Mode().IsDir() {
|
||||||
|
t.Errorf("%s: mode is not directory", filepath.Join(bucketName, "a/b"))
|
||||||
|
}
|
||||||
|
if info.Mode() != os.ModeDir|0755 {
|
||||||
|
t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", filepath.Join(bucketName, "a/b"), info.Mode())
|
||||||
|
}
|
||||||
|
info, err = gcsAfs.Stat(dirName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !info.Mode().IsDir() {
|
||||||
|
t.Errorf("%s: mode is not directory", dirName)
|
||||||
|
}
|
||||||
|
if info.Mode() != os.ModeDir|0755 {
|
||||||
|
t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", dirName, info.Mode())
|
||||||
|
}
|
||||||
|
|
||||||
|
err = gcsAfs.RemoveAll(filepath.Join(bucketName, "a"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to remove the folder %s with error: %s", filepath.Join(bucketName, "a"), err)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue