Esempio n. 1
0
File: wkspc.go Progetto: dvln/wkspc
// prepIfNotThere will create the dir if it doesn't exist and store info about
// it in viper (if a name is given), returns the full dir name and any
// error that occurs.  Note that "item" can be "" to indicate no sub dir.
func prepIfNotThere(directory bool, parent, item, name string) (string, error) {
	var err error
	fullPath := ""
	if item != "" {
		fullPath = filepath.Join(parent, item)
	} else {
		fullPath = parent
	}
	if name != "" {
		globs.Set(name, fullPath)
	}
	if fullPath != "" {
		if directory {
			err = dir.CreateIfNotExists(fullPath)
		} else {
			err = file.CreateIfNotExists(fullPath)
		}
	}
	return fullPath, err
}
Esempio n. 2
0
func TestWkspcRootDetermination(t *testing.T) {
	tempFolder, err := ioutil.TempDir("", "dvln-wkspc-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tempFolder)

	fakeWkspcSubDir := filepath.Join(tempFolder, "fake_wkspc_subdir")

	if err := dir.CreateIfNotExists(fakeWkspcSubDir); err != nil {
		t.Fatal(err)
	}
	fileinfo, err := os.Stat(fakeWkspcSubDir)
	if err != nil {
		t.Fatalf("Should have create a folder, got %v", err)
	}
	if !fileinfo.IsDir() {
		t.Fatalf("Should have been a dir, seems it's not")
	}
	exists, err := dir.Exists(fakeWkspcSubDir)
	if err != nil {
		t.Fatal("Folder should have existed but instead an error was returned")
	}
	if !exists {
		t.Fatal("Folder should have existed but Exists() said it did not")
	}

	wsRootFolder := filepath.Join(tempFolder, ".dvln")
	if err := dir.CreateIfNotExists(wsRootFolder); err != nil {
		t.Fatal(err)
	}
	fileinfo, err = os.Stat(wsRootFolder)
	if err != nil {
		t.Fatalf("Should have create a .dvln folder, got %v", err)
	}
	if !fileinfo.IsDir() {
		t.Fatalf("Should have been a dir, seems .dvln is not")
	}
	exists, err = dir.Exists(wsRootFolder)
	if err != nil {
		t.Fatal("Folder .dvln should have existed but instead an error was returned")
	}
	if !exists {
		t.Fatal("Folder .dvln should have existed but Exists() said it did not")
	}

	root, err := RootDir(fakeWkspcSubDir)
	if err != nil {
		t.Fatalf("Search for workspace root from %s should not have returned an error, got %v", fakeWkspcSubDir, err)
	}
	if root == "" {
		t.Fatal("Failed to find the workspace root folder, it should have been found")
	}
	if root != tempFolder {
		t.Fatalf("Found a workspace root but didn't match expected (found: %s, expected: %s)", root, tempFolder)
	}

	privTempFolder := filepath.Join("/", "private", tempFolder)
	root, err = RootDir(tempFolder)
	if err != nil {
		t.Fatalf("Search for a workspace root dir (from the workspace root dir) got unexpected error: %v", err)
	}
	if root == "" {
		t.Fatal("Failed to find the workspace root directory (from that dir), it should have been found")
	}
	if root != tempFolder && root != privTempFolder {
		t.Fatalf("Root found to be %s, should have been set to %s", root, tempFolder)
	}

	os.Chdir(fakeWkspcSubDir)
	root, err = RootDir()
	if err != nil {
		t.Fatalf("Search for workspace root after Chdir into wkspc subdir should not have returned an error, got %v", err)
	}
	if root == "" {
		t.Fatal("After chdir, failed to find the workspace root folder, it should have been found")
	}
	if root != tempFolder && root != privTempFolder {
		t.Fatalf("After chdir, found a workspace root but didn't match expected (found: %s, expected: %s)", root, tempFolder)
	}
}