github.com/informationsea/shellflow@v0.1.3/filelog_test.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "path" 10 "testing" 11 ) 12 13 var filelogFiles = []string{"./examples/hello.c", "./examples/helloprint.c", "./examples/helloprint.h"} 14 var filelogExtraCopyFiles = []string{"./examples/build.sf"} 15 var filelogExpectedSize = []int64{112, 77, 96} 16 var filelogExpectedSha = []string{"9ac20f410e45c1f6f14fd26a7a324e268d89b2139b0aad3d776cc4f709764d4e", "b4dfc8a845621e80c6d7d680a7d4d207b6e854d4596f05885ebad97434128d19", "6b05c842a049f5dc6413d97933e75a59cde4bd3a050ceeec55db75c700af634b"} 17 var filelogExpectedContent = []FileData{[]byte{}, []byte("#include <stdio.h>\n\nvoid printHello(void) {\n printf(\"Hello, world!\\n\");\n}\n"), []byte("#ifndef HELLOPRINT_H_\n#define HELLOPRINT_H_\n\nvoid printHello(void);\n\n#endif /* HELLOPRINT_H_ */\n")} 18 19 var filelogExpectedBase64 = []string{"H4sIAAAAAAAA/wAAAP//AQAA//8AAAAAAAAAAA==", "H4sICKpGHVsAA2hlbGxvcHJpbnQuYwBTzsxLzilNSVWwKS5JyczXy7Dj4irLz0xRKCjKzCvxSM3JydcA8TUVqrkUgAAsnKahBJbRUSjPL8pJUYzJU9K05qrlAgAU26rXTQAAAA==", "H4sICKpGHVsAA2hlbGxvcHJpbnQuaABTzkzLS0lNU/Bw9fHxDwjy9AuJ94jnUgYKZealoolyleVnpigUFGXmlXik5uTka4D4mtZcXMqpeSmZaQr6WqgaFLT0uQAfsGHpYAAAAA=="} 20 21 type TempDir struct { 22 tempDir string 23 originalCwd string 24 } 25 26 func NewTempDir(prefix string) (*TempDir, error) { 27 originalCwd, err := os.Getwd() 28 if err != nil { 29 return nil, err 30 } 31 tmpDir, err := ioutil.TempDir("", prefix) 32 if err != nil { 33 return nil, err 34 } 35 err = os.Chdir(tmpDir) 36 if err != nil { 37 return nil, err 38 } 39 fmt.Printf("%s tmpdir: %s\n", prefix, tmpDir) 40 allCopyFiles := append(filelogFiles, filelogExtraCopyFiles...) 41 for _, v := range allCopyFiles { 42 baseDir := path.Dir(v) 43 err = os.MkdirAll(baseDir, 0700) 44 if err != nil { 45 return nil, err 46 } 47 48 destFile, err := os.OpenFile(v, os.O_CREATE|os.O_WRONLY, 0600) 49 if err != nil { 50 return nil, err 51 } 52 defer destFile.Close() 53 srcFile, err := os.Open(path.Join(originalCwd, v)) 54 if err != nil { 55 return nil, err 56 } 57 defer srcFile.Close() 58 io.Copy(destFile, srcFile) 59 } 60 return &TempDir{tempDir: tmpDir, originalCwd: originalCwd}, nil 61 } 62 63 func (v *TempDir) Close() error { 64 err := os.RemoveAll(v.tempDir) 65 if err != nil { 66 return err 67 } 68 return os.Chdir(v.originalCwd) 69 } 70 71 func checkFileLog(t *testing.T, logs []FileLog, hint string) { 72 for i, v := range filelogFiles { 73 if logs[i].Relpath != v { 74 t.Fatalf("bad relative path: %s", logs[0].Relpath) 75 } 76 if logs[i].AbsPath[0] != '/' { 77 t.Fatalf("bad absolute path: %s", logs[0].AbsPath) 78 } 79 if fmt.Sprintf("%x", logs[i].Sha256Sum) != filelogExpectedSha[i] { 80 t.Fatalf("bad sha256hash for %s : actual: %x / expected: %s", v, logs[i].Sha256Sum, filelogExpectedSha[i]) 81 } 82 if logs[i].Size != filelogExpectedSize[i] { 83 t.Fatalf("bad file size for %s : %d", v, filelogExpectedSize[i]) 84 } 85 //if !reflect.DeepEqual(logs[i].Content, filelogExpectedContent[i]) { 86 // t.Fatalf("%s bad content for %s\n%s\n%s\n%d\n%d\n", hint, v, logs[i].Content, filelogExpectedContent[i], len(logs[i].Content), len(filelogExpectedContent[i])) 87 //} 88 if logs[i].IsDir != false { 89 t.Fatalf("bad is dir result for %s", v) 90 } 91 } 92 } 93 94 func TestFileLog(t *testing.T) { 95 ClearCache() 96 // create temporary directory, copy files and change current directory 97 tmp, err := NewTempDir("filelog") 98 if err != nil { 99 t.Fatalf("error: %s", err.Error()) 100 } 101 102 // validation start 103 logs, err := CreateFileLog(filelogFiles, false, 100) 104 if err != nil { 105 t.Fatalf("error: %s", err.Error()) 106 } 107 checkFileLog(t, logs, "first check") 108 109 byteData, err := json.Marshal(logs) 110 if err != nil { 111 t.Fatalf("error: %s", err.Error()) 112 } 113 114 //jsonResult := make([]fileLogForJSON, 0) 115 //json.Unmarshal(byteData, &jsonResult) 116 117 /* 118 for i, v := range filelogFiles { 119 if jsonResult[i].Relpath != v { 120 t.Fatalf("bad relative path: %s", logs[0].relpath) 121 } 122 if jsonResult[i].Abspath[0] != '/' { 123 t.Fatalf("bad absolute path: %s", jsonResult[0].Abspath) 124 } 125 if fmt.Sprintf("%s", jsonResult[i].Sha256sum) != filelogExpectedSha[i] { 126 t.Fatalf("bad sha256hash for %s / actual:%s / expected: %s", v, jsonResult[i].Sha256sum, filelogExpectedSha[i]) 127 } 128 if jsonResult[i].Size != filelogExpectedSize[i] { 129 t.Fatalf("bad file size for %s : %d", v, filelogExpectedSize[i]) 130 } 131 //if jsonResult[i].Content != filelogExpectedBase64[i] { 132 // t.Fatalf("bad content for %s\n%s\n%s", v, jsonResult[i].Content, filelogExpectedContent[i]) 133 //} 134 if jsonResult[i].IsDir != false { 135 t.Fatalf("bad is dir result for %s", v) 136 } 137 } 138 */ 139 140 filelogRead := make([]FileLog, 0) 141 err = json.Unmarshal(byteData, &filelogRead) 142 if err != nil { 143 t.Fatalf("failed to unmarshal: %s", err.Error()) 144 } 145 146 for i, v := range logs { 147 if v.Modified.Unix() != filelogRead[i].Modified.Unix() { 148 t.Fatalf("bad modified data / actual: %s / expected: %s", filelogRead[i].Modified, v.Modified) 149 } 150 151 if v.Modified.UnixNano() != filelogRead[i].Modified.UnixNano() { 152 t.Fatalf("bad modified data / actual: %s / expected: %s", filelogRead[i].Modified, v.Modified) 153 } 154 } 155 156 //fmt.Printf("\"%s\"\n\"%s\"\n\"%s\"\n", filelogRead[0].content, filelogRead[1].content, filelogRead[2].content) 157 158 checkFileLog(t, filelogRead, "json decoded") 159 160 //time.Sleep(1100 * time.Millisecond) 161 //time.Sleep(100 * time.Millisecond) 162 163 for _, v := range filelogRead { 164 if result, err := v.IsChanged(); err != nil || result != false { 165 t.Fatalf("bad is changed result: %s", err) 166 } 167 168 file, err := os.OpenFile(v.Relpath, os.O_APPEND|os.O_WRONLY, 0600) 169 if err != nil { 170 t.Fatalf("error: %s", err.Error()) 171 } 172 file.WriteString("hoge") 173 file.Close() 174 175 ClearCache() 176 177 if result, err := v.IsChanged(); err != nil || result != true { 178 t.Fatalf("bad is changed result: %s / %s", err, v.Relpath) 179 } 180 } 181 defer tmp.Close() 182 }