import_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 util
  11. import (
  12. "fmt"
  13. "io/ioutil"
  14. "os"
  15. "path/filepath"
  16. "strings"
  17. "testing"
  18. "devt.de/krotik/common/fileutil"
  19. )
  20. const importTestDir = "importtest"
  21. func TestImportLocater(t *testing.T) {
  22. if res, _ := fileutil.PathExists(importTestDir); res {
  23. os.RemoveAll(importTestDir)
  24. }
  25. err := os.Mkdir(importTestDir, 0770)
  26. if err != nil {
  27. t.Error("Could not create test dir:", err)
  28. return
  29. }
  30. defer func() {
  31. // Teardown
  32. if err := os.RemoveAll(importTestDir); err != nil {
  33. t.Error("Could not create test dir:", err)
  34. return
  35. }
  36. }()
  37. err = os.Mkdir(filepath.Join(importTestDir, "test1"), 0770)
  38. if err != nil {
  39. t.Error("Could not create test dir:", err)
  40. return
  41. }
  42. codecontent := "\na := 1 + 1\n"
  43. ioutil.WriteFile(filepath.Join(importTestDir, "test1", "myfile.ecal"),
  44. []byte(codecontent), 0770)
  45. fil := &FileImportLocator{importTestDir}
  46. res, err := fil.Resolve(filepath.Join("..", "t"))
  47. expectedError := fmt.Sprintf("Import path is outside of code root: ..%vt",
  48. string(os.PathSeparator))
  49. if res != "" || err == nil || err.Error() != expectedError {
  50. t.Error("Unexpected result:", res, err)
  51. return
  52. }
  53. res, err = fil.Resolve(filepath.Join("..", importTestDir, "x"))
  54. if res != "" || err == nil || !strings.HasPrefix(err.Error(), "Could not import path") {
  55. t.Error("Unexpected result:", res, err)
  56. return
  57. }
  58. res, err = fil.Resolve(filepath.Join("..", importTestDir, "x"))
  59. if res != "" || err == nil || !strings.HasPrefix(err.Error(), "Could not import path") {
  60. t.Error("Unexpected result:", res, err)
  61. return
  62. }
  63. res, err = fil.Resolve(filepath.Join("test1", "myfile.ecal"))
  64. if res != codecontent || err != nil {
  65. t.Error("Unexpected result:", res, err)
  66. return
  67. }
  68. mil := &MemoryImportLocator{make(map[string]string)}
  69. mil.Files["foo"] = "bar"
  70. mil.Files["test"] = "test1"
  71. res, err = mil.Resolve("xxx")
  72. if res != "" || err == nil || err.Error() != "Could not find import path: xxx" {
  73. t.Error("Unexpected result:", res, err)
  74. return
  75. }
  76. res, err = mil.Resolve("foo")
  77. if res != "bar" || err != nil {
  78. t.Error("Unexpected result:", res, err)
  79. return
  80. }
  81. res, err = mil.Resolve("test")
  82. if res != "test1" || err != nil {
  83. t.Error("Unexpected result:", res, err)
  84. return
  85. }
  86. }