github.com/dnutiu/simplFT@v0.0.0-20181023061248-df4b14cdce57/server/path_test.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func TestMakePathFromStringStack(t *testing.T) {
    11  	st := MakeStringStack(5)
    12  
    13  	st.Push("first")
    14  	st.Push("folder two")
    15  	st.Push("trinity")
    16  
    17  	BasePath = "./"
    18  
    19  	path := MakePathFromStringStack(st)
    20  	expected := fmt.Sprintf("%s%s/%s/%s/", BasePath, "first", "folder two", "trinity")
    21  
    22  	if path != expected {
    23  		t.Errorf("TestMakePathFromStringStack: Returned an invalid path! Want %s Got %s", expected, path)
    24  	}
    25  
    26  }
    27  
    28  func TestMakePathFromStringStack_StackIsEmpty(t *testing.T) {
    29  	st := MakeStringStack(1)
    30  	path := MakePathFromStringStack(st)
    31  
    32  	expected := BasePath
    33  	if BasePath[len(BasePath)-1] != '/' {
    34  		expected += "/"
    35  	}
    36  
    37  	if path != expected {
    38  		t.Errorf("TestMakePathFromStringStack: Returned an invalid path!")
    39  	}
    40  }
    41  
    42  func TestChangeDirectory(t *testing.T) {
    43  	st := MakeStringStack(1)
    44  	err := os.Chdir(BasePath)
    45  	if err != nil {
    46  		t.Errorf("TestChangeDirectory: Step1: %s", err.Error())
    47  	}
    48  
    49  	dirName := string(rand.Intn(10000))
    50  
    51  	err = os.Mkdir(dirName, os.FileMode(0522))
    52  	if err != nil {
    53  		t.Errorf("TestChangeDirectory: Step2: %s", err.Error())
    54  	}
    55  
    56  	err = ChangeDirectory(st, dirName)
    57  	if err != nil {
    58  		t.Errorf("TestChangeDirectory: Step3: %s", err.Error())
    59  	}
    60  
    61  	err = os.Chdir(BasePath)
    62  	if err != nil {
    63  		t.Errorf("TestChangeDirectory: Step4: %s", err.Error())
    64  	}
    65  
    66  	err = os.Remove(dirName)
    67  	if err != nil {
    68  		t.Errorf("TestChangeDirectory: Step5: %s", err.Error())
    69  	}
    70  }
    71  
    72  func TestChangeDirectory_InvalidDirectoryIsNotInStack(t *testing.T) {
    73  	st := MakeStringStack(1)
    74  	err := os.Chdir(BasePath)
    75  	if err != nil {
    76  		t.Errorf("TestChangeDirectory_InvalidDirectoryIsNotInStack: Step1: %s", err.Error())
    77  	}
    78  
    79  	dirName := string(rand.Intn(10000))
    80  
    81  	// ignore no such directory error
    82  	ChangeDirectory(st, dirName)
    83  	if !st.IsEmpty() && st.Top() == dirName {
    84  		t.Errorf("TestChangeDirectory: Stack is corrupted because invalid directory remained in stack")
    85  	}
    86  
    87  }
    88  
    89  func TestChangeDirectoryToPrevious_StackIsEmpty(t *testing.T) {
    90  	st := MakeStringStack(1)
    91  	err := ChangeDirectoryToPrevious(st)
    92  	if err != ErrAlreadyAtBaseDirectory {
    93  		t.Error("TestChangeDirectoryToPrevious: Stack is empty and no error occured!")
    94  	}
    95  }