hub_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package csconfig
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestLoadHub(t *testing.T) {
  10. hubFullPath, err := filepath.Abs("./hub")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. dataFullPath, err := filepath.Abs("./data")
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. configDirFullPath, err := filepath.Abs("./tests")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. tests := []struct {
  27. name string
  28. Input *Config
  29. expectedResult *Hub
  30. err string
  31. }{
  32. {
  33. name: "basic valid configuration",
  34. Input: &Config{
  35. ConfigPaths: &ConfigurationPaths{
  36. ConfigDir: "./tests",
  37. DataDir: "./data",
  38. HubDir: "./hub",
  39. HubIndexFile: "./hub/.index.json",
  40. },
  41. },
  42. expectedResult: &Hub{
  43. ConfigDir: configDirFullPath,
  44. DataDir: dataFullPath,
  45. HubDir: hubFullPath,
  46. HubIndexFile: hubIndexFileFullPath,
  47. },
  48. },
  49. {
  50. name: "no data dir",
  51. Input: &Config{
  52. ConfigPaths: &ConfigurationPaths{
  53. ConfigDir: "./tests",
  54. HubDir: "./hub",
  55. HubIndexFile: "./hub/.index.json",
  56. },
  57. },
  58. expectedResult: nil,
  59. },
  60. {
  61. name: "no configuration path",
  62. Input: &Config{},
  63. expectedResult: nil,
  64. },
  65. }
  66. for idx, test := range tests {
  67. err := test.Input.LoadHub()
  68. if err == nil && test.err != "" {
  69. fmt.Printf("TEST '%s': NOK\n", test.name)
  70. t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
  71. } else if test.err != "" {
  72. if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
  73. fmt.Printf("TEST '%s': NOK\n", test.name)
  74. t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
  75. test.err,
  76. fmt.Sprintf("%s", err))
  77. }
  78. }
  79. isOk := assert.Equal(t, test.expectedResult, test.Input.Hub)
  80. if !isOk {
  81. t.Fatalf("TEST '%s': NOK", test.name)
  82. } else {
  83. fmt.Printf("TEST '%s': OK\n", test.name)
  84. }
  85. }
  86. }