github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/pkg/util/validation/check_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestFileIsExist(t *testing.T) {
    11  
    12  	dir, err := ioutil.TempDir("", "TestTempFile_BadDir")
    13  	if err != nil {
    14  		t.Fatalf("%v", err)
    15  	}
    16  	defer os.RemoveAll(dir)
    17  
    18  	ef, err := ioutil.TempFile(dir, "CheckFileIsExist")
    19  	if err == nil {
    20  		if !FileIsExist(ef.Name()) {
    21  			t.Fatalf("file %v should exist", ef.Name())
    22  		}
    23  	}
    24  
    25  	nonexistentDir := filepath.Join(dir, "_not_exists_")
    26  	nf, err := ioutil.TempFile(nonexistentDir, "foo")
    27  	if err == nil {
    28  		if FileIsExist(nf.Name()) {
    29  			t.Fatalf("file %v should not exist", nf.Name())
    30  		}
    31  	}
    32  }