codeberg.org/anaseto/gofrundis@v0.14.0/cmd/frundis/gofrundis_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "os/exec" 8 "path" 9 "strings" 10 "syscall" 11 "testing" 12 13 "codeberg.org/anaseto/gofrundis/exporter/latex" 14 "codeberg.org/anaseto/gofrundis/exporter/markdown" 15 "codeberg.org/anaseto/gofrundis/exporter/mom" 16 "codeberg.org/anaseto/gofrundis/exporter/tpl" 17 "codeberg.org/anaseto/gofrundis/exporter/xhtml" 18 "codeberg.org/anaseto/gofrundis/frundis" 19 ) 20 21 func TestMain(m *testing.M) { 22 err := os.Setenv("FRUNDIS", "ok") 23 if err != nil { 24 fmt.Fprint(os.Stderr, err) 25 } 26 err = os.Chdir("../../testdata") 27 if err != nil { 28 fmt.Fprint(os.Stderr, err) 29 os.Exit(1) 30 } 31 err = os.Setenv("FRUNDISLIB", "data/includes") 32 if err != nil { 33 fmt.Fprint(os.Stderr, err) 34 } 35 os.Exit(m.Run()) 36 } 37 38 func TestWarnings(t *testing.T) { 39 doWarnings(t, "warnings.frundis", "xhtml") 40 } 41 42 func TestWarningsNbsp(t *testing.T) { 43 doWarnings(t, "warnings-nbsp.frundis", "xhtml") 44 } 45 46 func TestWarningsEpub(t *testing.T) { 47 doWarnings(t, "warnings-epub.frundis", "epub") 48 } 49 50 func doWarnings(t *testing.T, path, format string) { 51 err := os.RemoveAll(".gofrundis_warnings_test") 52 if err != nil { 53 t.Fatal(err) 54 } 55 exp := xhtml.NewExporter( 56 &xhtml.Options{ 57 Format: format, 58 OutputFile: ".gofrundis_warnings_test", 59 Werror: io.Discard, 60 //Werror: os.Stderr, 61 AllInOneFile: true}) 62 err = frundis.ProcessFrundisSource(exp, path, false) 63 if err != nil { 64 t.Fatal(err) 65 } 66 } 67 68 func TestFragments(t *testing.T) { 69 dataDir, err := os.Open("data") 70 if err != nil { 71 t.Fatalf("Error reading data: %v", err) 72 } 73 defer func() { 74 err := dataDir.Close() 75 if err != nil { 76 t.Fatalf("Error closing data: %v", err) 77 } 78 }() 79 names, err := dataDir.Readdirnames(-1) 80 if err != nil { 81 t.Logf("Could not read directory: %v", err) 82 } 83 for _, f := range names { 84 if b, _ := path.Match("*.frundis", f); !b { 85 continue 86 } 87 fullPath := path.Join("data", f) 88 for _, format := range []string{"latex", "mom", "xhtml", "markdown"} { 89 t.Run(fullPath+"-"+format, func(t *testing.T) { 90 doFile(t, fullPath, format, false) 91 }) 92 } 93 } 94 t.Run("tpl.frundis", func(t *testing.T) { 95 doFile(t, "tpl.frundis", "xhtml", true) 96 }) 97 } 98 99 func TestStandalones(t *testing.T) { 100 dataDir, err := os.Open("data-dirs") 101 if err != nil { 102 t.Fatalf("Error reading data-dirs: %v", err) 103 } 104 defer func() { 105 err = dataDir.Close() 106 if err != nil { 107 t.Fatalf("Error closing data-dirs: %v", err) 108 } 109 }() 110 names, err := dataDir.Readdirnames(-1) 111 if err != nil { 112 t.Logf("Could not read directory: %v", err) 113 } 114 for _, f := range names { 115 if b, _ := path.Match("*.frundis", f); !b { 116 continue 117 } 118 fullPath := path.Join("data-dirs", f) 119 if b, _ := path.Match("*-epub*", f); b { 120 t.Run(fullPath+" "+"*-epub", func(t *testing.T) { 121 doStandalone(t, fullPath, "epub", false) 122 }) 123 continue 124 } 125 if b, _ := path.Match("*-xhtml*", f); b { 126 t.Run(fullPath+" "+"*-xhtml", func(t *testing.T) { 127 doStandalone(t, fullPath, "xhtml", false) 128 }) 129 t.Run(fullPath+" "+"*-xhtml", func(t *testing.T) { 130 doStandalone(t, fullPath, "xhtml", true) 131 }) 132 continue 133 } 134 if b, _ := path.Match("*-latex*", f); b { 135 t.Run(fullPath+" "+"*-latex", func(t *testing.T) { 136 doStandalone(t, fullPath, "latex", true) 137 }) 138 continue 139 } 140 t.Run(fullPath+" "+"xhtml", func(t *testing.T) { 141 doStandalone(t, fullPath, "xhtml", false) 142 }) 143 for _, format := range []string{"xhtml", "latex", "mom"} { 144 t.Run(fullPath+" "+format, func(t *testing.T) { 145 doStandalone(t, fullPath, format, true) 146 }) 147 } 148 } 149 } 150 151 var outputFile = ".gofrundistest.out" 152 var outputDir = ".gofrundistestdir.out" 153 154 func doFile(t *testing.T, file string, format string, tplmode bool) { 155 name := strings.TrimSuffix(file, ".frundis") 156 suffix := strings.Replace(format, "xhtml", "html", -1) 157 suffix = strings.Replace(suffix, "latex", "tex", -1) 158 var exp frundis.Exporter 159 switch format { 160 case "xhtml": 161 if tplmode { 162 exp = tpl.NewExporter(&tpl.Options{ 163 OutputFile: outputFile, 164 Format: format}) 165 } else { 166 exp = xhtml.NewExporter( 167 &xhtml.Options{ 168 Format: "xhtml", 169 OutputFile: outputFile, 170 AllInOneFile: true}) 171 } 172 case "latex": 173 exp = latex.NewExporter(&latex.Options{OutputFile: outputFile}) 174 case "markdown": 175 exp = markdown.NewExporter(&markdown.Options{OutputFile: outputFile}) 176 case "mom": 177 exp = mom.NewExporter(&mom.Options{OutputFile: outputFile}) 178 } 179 err := frundis.ProcessFrundisSource(exp, file, true) 180 ref := name + "." + suffix 181 if err != nil { 182 t.Fatal(err) 183 } 184 cmd := exec.Command("diff", "-u", ref, outputFile) 185 _, e := os.Stat(ref) 186 var diff []byte 187 if e != nil { 188 diff = []byte("Reference file does not exist yet.\n") 189 } else { 190 var err error 191 diff, err = cmd.CombinedOutput() 192 if err != nil { 193 switch err := err.(type) { 194 case *exec.ExitError: 195 s := err.Sys().(syscall.WaitStatus) 196 if s.ExitStatus() >= 2 { 197 t.Fatalf("Error executing command: %s: %s", strings.Join(cmd.Args, " "), string(diff)) 198 } 199 default: 200 t.Fatal(err) 201 } 202 } 203 } 204 if !(string(diff) == "") { 205 t.Error(string(diff)) 206 } 207 } 208 209 func doStandalone(t *testing.T, file string, format string, toFile bool) { 210 var suffix string 211 switch format { 212 case "epub": 213 suffix = "-epub" 214 case "xhtml": 215 if toFile { 216 suffix = ".html" 217 } else { 218 suffix = "-html" 219 } 220 case "latex": 221 suffix = ".tex" 222 case "mom": 223 suffix = ".mom" 224 default: 225 t.Fatalf("internal error:unknown format: %s", format) 226 } 227 name := strings.TrimSuffix(file, ".frundis") 228 info, err := os.Stat(outputDir) 229 if err == nil { 230 if info.IsDir() { 231 err = os.RemoveAll(outputDir) 232 if err != nil { 233 t.Fatalf("removing outputDir: %v", err) 234 } 235 } else { 236 os.Remove(outputDir) 237 } 238 } 239 var exp frundis.Exporter 240 switch format { 241 case "xhtml", "epub": 242 exp = xhtml.NewExporter( 243 &xhtml.Options{ 244 Format: format, 245 OutputFile: outputDir, 246 Standalone: true, 247 AllInOneFile: toFile}) 248 case "latex": 249 exp = latex.NewExporter( 250 &latex.Options{ 251 OutputFile: outputDir, 252 Standalone: true}) 253 case "markdown": 254 exp = markdown.NewExporter(&markdown.Options{OutputFile: outputFile}) 255 case "mom": 256 exp = mom.NewExporter( 257 &mom.Options{ 258 OutputFile: outputDir, 259 Standalone: true}) 260 } 261 err = frundis.ProcessFrundisSource(exp, file, false) 262 if err != nil { 263 t.Fatal(err) 264 } 265 ref := name + suffix 266 _, e := os.Stat(ref) 267 var diff []byte 268 if e != nil { 269 diff = []byte("Reference file does not exist yet\n") 270 } else { 271 cmd := exec.Command("diff", "-ru", ref, outputDir) 272 var err error 273 diff, err = cmd.CombinedOutput() 274 if err != nil { 275 switch err := err.(type) { 276 case *exec.ExitError: 277 s := err.Sys().(syscall.WaitStatus) 278 if s.ExitStatus() >= 2 { 279 t.Error("^^^^^^^ DIFF ERROR ^^^^^^^^^^^^^^^^^") 280 t.Error(string(diff)) 281 t.Error("^^^^^^^ END OF ERROR ^^^^^^^^^") 282 t.FailNow() 283 } 284 default: 285 t.Fatal(err) 286 } 287 } 288 } 289 if !(string(diff) == "") { 290 t.Error(string(diff)) 291 } 292 }