import_test.go 2.5 KB

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