github.com/inabyte/embed@v1.0.1/generate_test.go (about) 1 package embed 2 3 // Copyright 2020 Inabyte Inc. All rights reserved. 4 // Use of this source code is governed by a MIT-style 5 // license that can be found in the LICENSE.md file. 6 7 import ( 8 "fmt" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "testing" 13 "time" 14 ) 15 16 func TestGenerate(t *testing.T) { 17 18 base, err := createFs() 19 20 if len(base) > 0 { 21 defer os.RemoveAll(base) 22 } 23 24 if err != nil { 25 t.Fatalf("unable to cerate fs %v", err) 26 } 27 28 config := New() 29 config.Output = filepath.Join(base, "assets", "files") 30 fpath := filepath.Join(base, "www") 31 l := len(fpath) - 4 32 fpath = fpath[:l] + PrefixMarker + fpath[l:] 33 config.Files = []string{fpath, filepath.Join(base, "single", "settings.html")} 34 config.Ignore = `\.go$` 35 config.Include = `\.html$` 36 37 for _, v := range []struct { 38 name string 39 hasErr bool 40 doFunc func() func() 41 }{ 42 { 43 name: "No files", 44 hasErr: true, 45 doFunc: func() func() { 46 files := config.Files 47 config.Files = nil 48 return func() { config.Files = files } 49 }, 50 }, 51 { 52 name: "Bad Int", 53 hasErr: true, 54 doFunc: func() func() { 55 config.ModifyTime = "bad int" 56 return func() { config.ModifyTime = "" } 57 }, 58 }, 59 { 60 name: "Duplicates", 61 hasErr: true, 62 doFunc: func() func() { 63 files := config.Files 64 config.Files = append(config.Files, filepath.Join(base, "repeat", "settings.html")) 65 return func() { config.Files = files } 66 }, 67 }, 68 { 69 name: "Success", 70 doFunc: func() func() { 71 return func() {} 72 }, 73 }, 74 { 75 name: "Fixed Time", 76 doFunc: func() func() { 77 config.ModifyTime = fmt.Sprintf("%d", time.Now().Unix()) 78 return func() { config.ModifyTime = "" } 79 }, 80 }, 81 { 82 name: "Binary", 83 doFunc: func() func() { 84 config.Binary = true 85 return func() { config.Binary = false } 86 }, 87 }, 88 { 89 name: "Binary Relative", 90 doFunc: func() func() { 91 oldOutput := config.Output 92 config.Output = filepath.Join("assets", "files") 93 old, _ := os.Getwd() 94 os.Chdir(base) 95 config.Binary = true 96 return func() { config.Output = oldOutput; config.Binary = false; os.Chdir(old) } 97 }, 98 }, 99 { 100 name: "Go", 101 doFunc: func() func() { 102 config.Go = true 103 return func() { config.Go = false } 104 }, 105 }, 106 } { 107 t.Run(v.name, func(t *testing.T) { 108 post := v.doFunc() 109 err := config.Generate() 110 post() 111 if err == nil { 112 if v.hasErr { 113 t.Errorf("Generate did not return an error") 114 } 115 } else { 116 if !v.hasErr { 117 t.Errorf("Generate returned unexpected error %v", err) 118 } 119 } 120 }) 121 } 122 } 123 124 func createFs() (string, error) { 125 base, err := ioutil.TempDir("", "generate-test") 126 127 for _, v := range []struct { 128 path string 129 content []byte 130 }{ 131 {"www/index.html", []byte("<html></html>")}, 132 {"www/scripts/init.js", []byte("var data")}, 133 {"www/code/process.go", []byte("package process")}, 134 {"single/settings.html", []byte("<html></html>")}, 135 {"repeat/settings.html", []byte("<html></html>")}, 136 } { 137 if err == nil { 138 path := filepath.Join(base, v.path) 139 140 if err == nil { 141 err = os.MkdirAll(filepath.Dir(path), os.ModePerm) 142 } 143 if err == nil { 144 err = ioutil.WriteFile(path, v.content, os.ModePerm) 145 } 146 } 147 } 148 149 return base, err 150 }