format_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package tool
  11. import (
  12. "bytes"
  13. "flag"
  14. "fmt"
  15. "io/ioutil"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "testing"
  20. "devt.de/krotik/common/errorutil"
  21. "devt.de/krotik/common/fileutil"
  22. )
  23. const formatTestDir = "formattest"
  24. func setupFormatTestDir() {
  25. if res, _ := fileutil.PathExists(formatTestDir); res {
  26. os.RemoveAll(formatTestDir)
  27. }
  28. err := os.Mkdir(formatTestDir, 0770)
  29. if err != nil {
  30. fmt.Print("Could not create test directory:", err.Error())
  31. os.Exit(1)
  32. }
  33. }
  34. func tearDownFormatTestDir() {
  35. err := os.RemoveAll(formatTestDir)
  36. if err != nil {
  37. fmt.Print("Could not remove test directory:", err.Error())
  38. }
  39. }
  40. func TestFormat(t *testing.T) {
  41. setupFormatTestDir()
  42. defer tearDownFormatTestDir()
  43. out := bytes.Buffer{}
  44. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
  45. flag.CommandLine.SetOutput(&out)
  46. osArgs = []string{"foo", "bar", "-help"}
  47. if err := Format(); err != nil {
  48. t.Error("Unexpected result:", err)
  49. return
  50. }
  51. if !strings.Contains(out.String(), "Root directory for ECAL files") {
  52. t.Error("Unexpected output:", out.String())
  53. return
  54. }
  55. myfile := filepath.Join(formatTestDir, "myfile.ecal")
  56. myfile2 := filepath.Join(formatTestDir, "myfile.eca")
  57. myfile3 := filepath.Join(formatTestDir, "myinvalidfile.ecal")
  58. originalContent := "if a == 1 { b := 1 }"
  59. err := ioutil.WriteFile(myfile, []byte(originalContent), 0777)
  60. errorutil.AssertOk(err)
  61. err = ioutil.WriteFile(myfile2, []byte(originalContent), 0777)
  62. errorutil.AssertOk(err)
  63. err = ioutil.WriteFile(myfile3, []byte(originalContent[5:]), 0777)
  64. errorutil.AssertOk(err)
  65. out = bytes.Buffer{}
  66. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
  67. flag.CommandLine.SetOutput(&out)
  68. osArgs = []string{"foo", "bar", "-dir", formatTestDir}
  69. if err := Format(); err != nil {
  70. t.Error("Unexpected result:", err)
  71. return
  72. }
  73. if out.String() != `Formatting all .ecal files in formattest
  74. Could not format formattest/myinvalidfile.ecal: Parse error in formattest/myinvalidfile.ecal: Term cannot start an expression (==) (Line:1 Pos:1)
  75. ` {
  76. t.Error("Unexpected output:", out.String())
  77. return
  78. }
  79. myfileContent, err := ioutil.ReadFile(myfile)
  80. errorutil.AssertOk(err)
  81. if string(myfileContent) != `if a == 1 {
  82. b := 1
  83. }
  84. ` {
  85. t.Error("Unexpected result:", string(myfileContent))
  86. return
  87. }
  88. myfileContent, err = ioutil.ReadFile(myfile2)
  89. errorutil.AssertOk(err)
  90. if string(myfileContent) != originalContent {
  91. t.Error("Unexpected result:", string(myfileContent))
  92. return
  93. }
  94. }