Beispiel #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)
	}
}
Beispiel #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
}
Beispiel #3
0
func TestDirPurgeAndCreate(t *testing.T) {
	tmpDir := createTmpDir(t)
	defer os.RemoveAll(tmpDir)

	path := filepath.Join(tmpDir, "test")
	os.Mkdir(path, 0766)
	fpath := filepath.Join(path, "test2")
	os.Create(fpath)
	if !fval.FileExists(fpath) {
		t.Errorf("File %s does not exist", fpath)
	}
	ok, err := fval.DirPurgeAndCreate(path, 0766)
	if !ok {
		t.Errorf("Directory %s was not deleted and created", path)
	}
	if err != nil {
		t.Errorf("Error occured %s", err)
	}
	if fval.FileExists(fpath) {
		t.Errorf("File %s exist", fpath)
	}
}
Beispiel #4
0
func TestFileExists(t *testing.T) {
	tmpDir := createTmpDir(t)
	defer os.RemoveAll(tmpDir)

	path := filepath.Join(tmpDir, "test")
	os.Create(path)

	if !fval.FileExists(path) {
		t.Errorf("Did not indicate that existing file %s exists", path)
	}

	os.Remove(path)

	if fval.FileExists(path) {
		t.Errorf("Did indicate that non existing file %s exists", path)
	}

	os.Mkdir(path, 0766)

	if fval.FileExists(path) {
		t.Errorf("Did indicate that directory %s is a file", path)
	}
}
Beispiel #5
0
func TestDirPurgeAndCreateError(t *testing.T) {
	tmpDir := createTmpDir(t)
	defer os.RemoveAll(tmpDir)

	// Use same way of testing error to os.RemoveAll as stdlib

	// Determine if we should run the following test.
	testit := true
	if runtime.GOOS == "windows" {
		// Chmod is not supported under windows.
		testit = false
	} else {
		// Test fails as root.
		testit = os.Getuid() != 0
	}
	if testit {
		path := filepath.Join(tmpDir, "test")
		fpath := filepath.Join(path, ".file")
		os.Mkdir(path, 0766)
		os.Create(fpath)

		if err := os.Chmod(path, 0); err != nil {
			t.Fatalf("Chmod %q 0: %s", path, err)
		}

		ok, err := fval.DirPurgeAndCreate(path, 0766)
		if ok {
			t.Errorf("Directory should not have been created")
		}
		if err == nil {
			t.Errorf("Error should not have been nil")
		}
		if fval.FileExists(fpath) {
			t.Errorf("File %s should not exist", fpath)
		}
	}
}