github.com/dvln/wkspc@v0.0.0-20150922010055-31dce5e9918e/wkspc_test.go (about)

     1  // Copyright © 2015 Erik Brady <brady@dvln.org>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Package test: wkspc
    15  //      Basic testing for the 'wkspc' package
    16  
    17  package wkspc
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/dvln/util/dir"
    26  )
    27  
    28  func TestWkspcRootDetermination(t *testing.T) {
    29  	tempFolder, err := ioutil.TempDir("", "dvln-wkspc-test")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	defer os.RemoveAll(tempFolder)
    34  
    35  	fakeWkspcSubDir := filepath.Join(tempFolder, "fake_wkspc_subdir")
    36  
    37  	if err := dir.CreateIfNotExists(fakeWkspcSubDir); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	fileinfo, err := os.Stat(fakeWkspcSubDir)
    41  	if err != nil {
    42  		t.Fatalf("Should have create a folder, got %v", err)
    43  	}
    44  	if !fileinfo.IsDir() {
    45  		t.Fatalf("Should have been a dir, seems it's not")
    46  	}
    47  	exists, err := dir.Exists(fakeWkspcSubDir)
    48  	if err != nil {
    49  		t.Fatal("Folder should have existed but instead an error was returned")
    50  	}
    51  	if !exists {
    52  		t.Fatal("Folder should have existed but Exists() said it did not")
    53  	}
    54  
    55  	wsRootFolder := filepath.Join(tempFolder, ".dvln")
    56  	if err := dir.CreateIfNotExists(wsRootFolder); err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	fileinfo, err = os.Stat(wsRootFolder)
    60  	if err != nil {
    61  		t.Fatalf("Should have create a .dvln folder, got %v", err)
    62  	}
    63  	if !fileinfo.IsDir() {
    64  		t.Fatalf("Should have been a dir, seems .dvln is not")
    65  	}
    66  	exists, err = dir.Exists(wsRootFolder)
    67  	if err != nil {
    68  		t.Fatal("Folder .dvln should have existed but instead an error was returned")
    69  	}
    70  	if !exists {
    71  		t.Fatal("Folder .dvln should have existed but Exists() said it did not")
    72  	}
    73  
    74  	root, err := RootDir(fakeWkspcSubDir)
    75  	if err != nil {
    76  		t.Fatalf("Search for workspace root from %s should not have returned an error, got %v", fakeWkspcSubDir, err)
    77  	}
    78  	if root == "" {
    79  		t.Fatal("Failed to find the workspace root folder, it should have been found")
    80  	}
    81  	if root != tempFolder {
    82  		t.Fatalf("Found a workspace root but didn't match expected (found: %s, expected: %s)", root, tempFolder)
    83  	}
    84  
    85  	privTempFolder := filepath.Join("/", "private", tempFolder)
    86  	root, err = RootDir(tempFolder)
    87  	if err != nil {
    88  		t.Fatalf("Search for a workspace root dir (from the workspace root dir) got unexpected error: %v", err)
    89  	}
    90  	if root == "" {
    91  		t.Fatal("Failed to find the workspace root directory (from that dir), it should have been found")
    92  	}
    93  	if root != tempFolder && root != privTempFolder {
    94  		t.Fatalf("Root found to be %s, should have been set to %s", root, tempFolder)
    95  	}
    96  
    97  	os.Chdir(fakeWkspcSubDir)
    98  	root, err = RootDir()
    99  	if err != nil {
   100  		t.Fatalf("Search for workspace root after Chdir into wkspc subdir should not have returned an error, got %v", err)
   101  	}
   102  	if root == "" {
   103  		t.Fatal("After chdir, failed to find the workspace root folder, it should have been found")
   104  	}
   105  	if root != tempFolder && root != privTempFolder {
   106  		t.Fatalf("After chdir, found a workspace root but didn't match expected (found: %s, expected: %s)", root, tempFolder)
   107  	}
   108  }