depth_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package depth
  2. import (
  3. "go/build"
  4. "testing"
  5. )
  6. type MockImporter struct {
  7. ImportFn func(name, srcDir string, im build.ImportMode) (*build.Package, error)
  8. }
  9. func (m MockImporter) Import(name, srcDir string, im build.ImportMode) (*build.Package, error) {
  10. return m.ImportFn(name, srcDir, im)
  11. }
  12. func TestTree_Resolve(t *testing.T) {
  13. // Fail case, bad package name
  14. var tr Tree
  15. if err := tr.Resolve("name"); err != ErrRootPkgNotResolved {
  16. t.Fatalf("Unexpected error, expected=%v, got=%b", ErrRootPkgNotResolved, err)
  17. }
  18. // Positive case, expect deps
  19. if err := tr.Resolve("strings"); err != nil {
  20. t.Fatal(err)
  21. }
  22. if tr.Root == nil || tr.Root.Name != "strings" {
  23. t.Fatalf("Unexpected Root, expected=%v, got=%v", "strings", tr.Root)
  24. } else if len(tr.Root.Deps) == 0 {
  25. t.Fatal("Expected positive number of Deps")
  26. } else if len(tr.Root.SrcDir) == 0 {
  27. t.Fatal("Expected SrcDir to be populated")
  28. } else if tr.Root.Raw == nil {
  29. t.Fatal("Expected non-nil Raw")
  30. }
  31. // Reuse the same tree and the same package to ensure that the internal pkg cache
  32. // is reset and dependencies are still resolved.
  33. stringsDepCount := len(tr.Root.Deps)
  34. if err := tr.Resolve("strings"); err != nil {
  35. t.Fatal(err)
  36. }
  37. if len(tr.Root.Deps) != stringsDepCount {
  38. t.Fatalf("Unexpected number of Deps, expected=%v, got=%b", stringsDepCount, len(tr.Root.Deps))
  39. }
  40. }
  41. func TestTree_shouldResolveInternal(t *testing.T) {
  42. var pt Tree
  43. pt.Root = &Pkg{}
  44. if pt.shouldResolveInternal(&Pkg{}) {
  45. t.Fatal("Unexpected shouldResolveInternal, should have been false for non-root pkg and default config")
  46. }
  47. pt.ResolveInternal = true
  48. if !pt.shouldResolveInternal(&Pkg{}) {
  49. t.Fatal("Unexpected shouldResolveInternal, should have been true when ResolveInternal = true")
  50. }
  51. pt.ResolveInternal = false
  52. if !pt.shouldResolveInternal(pt.Root) {
  53. t.Fatal("Unexpected shouldResolveInternal, should have been true for root pkg")
  54. }
  55. }
  56. func TestTree_isAtMaxDepth(t *testing.T) {
  57. tests := []struct {
  58. maxDepth int
  59. depth int
  60. expected bool
  61. }{
  62. {0, 0, false},
  63. {0, 10, false},
  64. {1, 0, false},
  65. {1, 1, true},
  66. {1, 10, true},
  67. }
  68. for idx, tt := range tests {
  69. tr := Tree{MaxDepth: tt.maxDepth}
  70. var last *Pkg
  71. for i := 0; i < tt.depth+1; i++ {
  72. p := Pkg{Parent: last}
  73. last = &p
  74. }
  75. maxDepth := tr.isAtMaxDepth(last)
  76. if maxDepth != tt.expected {
  77. t.Fatalf("[%v] Unexpected isAtMaxDepth, expected=%v, got=%v", idx, tt.expected, maxDepth)
  78. }
  79. }
  80. }
  81. func TestTree_hasSeenImport(t *testing.T) {
  82. var tr Tree
  83. if tr.hasSeenImport("name") {
  84. t.Fatalf("Expected false the first time an import name is provided, got=true")
  85. }
  86. if !tr.hasSeenImport("name") {
  87. t.Fatalf("Expected true to be returned after the import name has been seen, got=false")
  88. }
  89. }