github.com/cryptix/massren@v1.0.1/main_test.go (about) 1 package main 2 3 import ( 4 "errors" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 "time" 11 ) 12 13 var createdTempFiles []string 14 15 func setup(t *testing.T) { 16 pwd, err := os.Getwd() 17 if err != nil { 18 t.Fatal(err) 19 } 20 homeDir_ = pwd + "/homedirtest" 21 err = os.MkdirAll(homeDir_, 0700) 22 if err != nil { 23 t.Fatal(err) 24 } 25 } 26 27 func teardown(t *testing.T) { 28 for _, filePath := range createdTempFiles { 29 os.Remove(filePath) 30 } 31 } 32 33 func createRandomTempFiles() []string { 34 var output []string 35 for i := 0; i < 10; i++ { 36 filePath := fmt.Sprintf("%s/testfile%d", tempFolder(), i) 37 ioutil.WriteFile(filePath, []byte("testing"), 0700) 38 output = append(output, filePath) 39 createdTempFiles = append(createdTempFiles, filePath) 40 } 41 return output 42 } 43 44 func Test_stringHash(t *testing.T) { 45 if len(stringHash("aaaa")) != 32 { 46 t.Error("hash should be 32 characters long") 47 } 48 49 if stringHash("abcd") == stringHash("efgh") || stringHash("") == stringHash("ijkl") { 50 t.Error("hashes should be different") 51 } 52 } 53 54 func Test_configFolder(t *testing.T) { 55 setup(t) 56 defer teardown(t) 57 58 configFolder := configFolder() 59 stat, err := os.Stat(configFolder) 60 if err != nil { 61 t.Error(err) 62 } 63 if !stat.IsDir() { 64 t.Error("config folder is not a directory: %s", configFolder) 65 } 66 } 67 68 func Test_watchFile(t *testing.T) { 69 setup(t) 70 defer teardown(t) 71 72 filePath := tempFolder() + "watchtest" 73 ioutil.WriteFile(filePath, []byte("testing"), 0700) 74 doneChan := make(chan bool) 75 76 go func(doneChan chan bool) { 77 defer func() { 78 doneChan <- true 79 }() 80 err := watchFile(filePath) 81 if err != nil { 82 t.Error(err) 83 } 84 }(doneChan) 85 86 time.Sleep(500 * time.Millisecond) 87 ioutil.WriteFile(filePath, []byte("testing change"), 0700) 88 89 <-doneChan 90 } 91 92 func fileListsAreEqual(files1 []string, files2 []string) error { 93 if len(files1) != len(files2) { 94 return errors.New("file count is different") 95 } 96 97 for _, f1 := range files1 { 98 found := false 99 for _, f2 := range files2 { 100 if filepath.Base(f1) == filepath.Base(f2) { 101 found = true 102 } 103 } 104 if !found { 105 return errors.New("file names do not match") 106 } 107 } 108 109 return nil 110 } 111 112 func Test_filePathsFromArgs(t *testing.T) { 113 setup(t) 114 defer teardown(t) 115 116 tempFiles := createRandomTempFiles() 117 args := []string{ 118 tempFolder() + "/*", 119 } 120 121 filePaths, err := filePathsFromArgs(args) 122 if err != nil { 123 t.Fatal(err) 124 } 125 126 err = fileListsAreEqual(filePaths, tempFiles) 127 if err != nil { 128 t.Error(err) 129 } 130 131 // If no argument is provided, the function should default to "*" 132 // in the current dir. 133 134 currentDir, err := os.Getwd() 135 if err != nil { 136 panic(err) 137 } 138 139 err = os.Chdir(tempFolder()) 140 if err != nil { 141 panic(err) 142 } 143 144 args = []string{} 145 filePaths, err = filePathsFromArgs(args) 146 if err != nil { 147 t.Fatal(err) 148 } 149 150 err = fileListsAreEqual(filePaths, tempFiles) 151 if err != nil { 152 t.Error(err) 153 } 154 155 os.Chdir(currentDir) 156 }