Browse Source

pkg/fileutil: test "Exist" on directory

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
Gyuho Lee 7 years ago
parent
commit
b8bf42cc5a
3 changed files with 30 additions and 1 deletions
  1. 16 0
      pkg/fileutil/doc.go
  2. 1 1
      pkg/fileutil/fileutil.go
  3. 13 0
      pkg/fileutil/fileutil_test.go

+ 16 - 0
pkg/fileutil/doc.go

@@ -0,0 +1,16 @@
+// Copyright 2018 The etcd Authors
+//
+// 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 fileutil implements utility functions related to files and paths.
+package fileutil

+ 1 - 1
pkg/fileutil/fileutil.go

@@ -12,7 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-// Package fileutil implements utility functions related to files and paths.
 package fileutil
 
 import (
@@ -93,6 +92,7 @@ func CreateDirAll(dir string) error {
 	return err
 }
 
+// Exist returns true if a file or directory exists.
 func Exist(name string) bool {
 	_, err := os.Stat(name)
 	return err == nil

+ 13 - 0
pkg/fileutil/fileutil_test.go

@@ -15,8 +15,10 @@
 package fileutil
 
 import (
+	"fmt"
 	"io"
 	"io/ioutil"
+	"math/rand"
 	"os"
 	"os/user"
 	"path/filepath"
@@ -24,6 +26,7 @@ import (
 	"runtime"
 	"strings"
 	"testing"
+	"time"
 )
 
 func TestIsDirWriteable(t *testing.T) {
@@ -104,6 +107,16 @@ func TestCreateDirAll(t *testing.T) {
 }
 
 func TestExist(t *testing.T) {
+	fdir := filepath.Join(os.TempDir(), fmt.Sprint(time.Now().UnixNano()+rand.Int63n(1000)))
+	os.RemoveAll(fdir)
+	if err := os.Mkdir(fdir, 0666); err != nil {
+		t.Skip(err)
+	}
+	defer os.RemoveAll(fdir)
+	if !Exist(fdir) {
+		t.Fatalf("expected Exist true, got %v", Exist(fdir))
+	}
+
 	f, err := ioutil.TempFile(os.TempDir(), "fileutil")
 	if err != nil {
 		t.Fatal(err)