github.com/jpbruinsslot/dot@v0.0.0-20231229172555-797f05ba0642/file_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 ) 10 11 func TestSyncFiles(t *testing.T) { 12 } 13 14 func TestTrackFile(t *testing.T) { 15 } 16 17 func TestUntrackFile(t *testing.T) { 18 } 19 20 // Test if MakeAndMoveToDir will be able to move a directory, we wil also 21 // test it when there is a file inside the directory 22 func TestMakeAndMoveToDirDirectory(t *testing.T) { 23 // create, and change to current working 24 // directory to temporary directory 25 tempDir, err := ioutil.TempDir("", "") 26 if err != nil { 27 t.Error(err) 28 } 29 os.Chdir(tempDir) 30 31 // create temporary directory with a temporary file, also create a 32 // temporary directory in which the first one is going to be placed, 33 // we will end up with: 34 // 35 // tempDir 36 // |_ dirOne 37 // |_ tempFile 38 // |_ dirTwo 39 dirOne, err := ioutil.TempDir(tempDir, "dirOne") 40 dirTwo, err := ioutil.TempDir(tempDir, "dirTwo") 41 42 // create a temporary file in dirOne 43 tempFile, err := ioutil.TempFile(dirOne, "tempfile") 44 if err != nil { 45 t.Error(err) 46 } 47 48 // we want the base of dirOne 49 baseDirOne := filepath.Base(dirOne) 50 51 // we want the file name of the tempFile 52 _, fileNameTempFile := filepath.Split(tempFile.Name()) 53 54 // set destination (/dirTwo/dirOne/) 55 dst := fmt.Sprintf("%s/%s", dirTwo, baseDirOne) 56 57 // move dirOne inside dirTwo 58 err = MakeAndMoveToDir(dirOne, dst) 59 if err != nil { 60 t.Error(err) 61 } 62 63 // we should have: 64 // tempDir 65 // |_ dirTwo 66 // |_ dirOne 67 // |_ tempFile 68 69 // check if dirOne is present in dirTwo 70 resultDir := fmt.Sprintf("%s/%s", dirTwo, baseDirOne) 71 if _, err = os.Stat(resultDir); err != nil { 72 t.Error(err) 73 } 74 75 // check if file is also present in the correct dir 76 resultFile := fmt.Sprintf("%s/%s/%s", dirTwo, baseDirOne, fileNameTempFile) 77 if _, err = os.Stat(resultFile); err != nil { 78 t.Error(err) 79 } 80 81 // check if directory is gone from source 82 if _, err = os.Stat(dirOne); err == nil { 83 t.Error(err) 84 } 85 } 86 87 // Test if MakeAndMoveToDir will be able to move a file 88 func TestMakeAndMoveToDirFile(t *testing.T) { 89 // create, and change to current working 90 // directory to temporary directory 91 tempDir, err := ioutil.TempDir("", "") 92 if err != nil { 93 t.Error(err) 94 } 95 os.Chdir(tempDir) 96 97 // create temporary directory with a temporary file, also create a 98 // temporary directory in which the first one is going to be placed, 99 // we will end up with: 100 // 101 // tempDir 102 // |_ dirOne 103 // | |_ tempFile 104 // |_ dirTwo 105 dirOne, err := ioutil.TempDir(tempDir, "dirOne") 106 dirTwo, err := ioutil.TempDir(tempDir, "dirTwo") 107 108 // create a temporary file which we will move 109 tempFile, err := ioutil.TempFile(dirOne, "tempfile") 110 if err != nil { 111 t.Error(err) 112 } 113 114 // we want the base of dirOne 115 baseDirOne := filepath.Base(dirOne) 116 117 // we want the file name of the tempFile 118 _, fileNameTempFile := filepath.Split(tempFile.Name()) 119 120 // set destination (/dirTwo/dirOne/tempFile) 121 dst := fmt.Sprintf("%s/%s/%s", dirTwo, baseDirOne, fileNameTempFile) 122 123 // move the file 124 err = MakeAndMoveToDir(tempFile.Name(), dst) 125 if err != nil { 126 t.Error(err) 127 } 128 129 // we should end up with the following: 130 // tempDir 131 // |_ dirTwo 132 // |_ dirOne 133 // |_ tempFile 134 135 // check if file is at destination 136 result := fmt.Sprintf("%s/%s/%s", dirTwo, baseDirOne, fileNameTempFile) 137 if _, err = os.Stat(result); err != nil { 138 t.Error(err) 139 } 140 141 // check if file is gone from source 142 if _, err = os.Stat(tempFile.Name()); err == nil { 143 t.Error(err) 144 } 145 }