Exemple #1
0
func TestFileOrDirExists(t *testing.T) {
	tmpDir := createTmpDir(t)
	defer os.RemoveAll(tmpDir)

	fpath := filepath.Join(tmpDir, "test.txt")
	dpath := filepath.Join(tmpDir, "test")
	os.Create(fpath)
	os.Mkdir(dpath, 0766)

	if !fval.Exists(fpath) {
		t.Errorf("Did not indicate that existing file %s exists", fpath)
	}
	if !fval.Exists(dpath) {
		t.Errorf("Did not indicate that existing directory %s exists", dpath)
	}

	os.Remove(fpath)
	os.Remove(dpath)

	if fval.FileExists(fpath) {
		t.Errorf("Did indicate that non existing file %s exists", fpath)
	}
	if fval.FileExists(dpath) {
		t.Errorf("Did indicate that non existing directory %s exists", dpath)
	}
}
Exemple #2
0
func Example() {
	tmpDir, err := ioutil.TempDir("", "go-arg-test")
	if err != nil {
		fmt.Println("TempDir error")
		return
	}
	defer os.RemoveAll(tmpDir)

	os.Args = make([]string, 3)
	os.Args[0] = "example"
	os.Args[1] = filepath.Join(tmpDir, "input")
	os.Args[2] = filepath.Join(tmpDir, "output")

	fmt.Println(fval.FileExists(os.Args[1]))
	os.Create(os.Args[1])
	fmt.Println(fval.FileExists(os.Args[1]))

	exists, err := fval.DirExistsOrCreate(os.Args[2], 0766)
	fmt.Println(exists)
	fmt.Println(err)

	fmt.Println(fval.Exists(os.Args[1]))
	fmt.Println(fval.Exists(os.Args[2]))

	// Output:
	// false
	// true
	// true
	// <nil>
	// true
	// true
}