pkg_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package depth
  2. import (
  3. "go/build"
  4. "sort"
  5. "testing"
  6. )
  7. func TestPkg_CleanName(t *testing.T) {
  8. tests := []struct {
  9. input string
  10. expected string
  11. }{
  12. {"strings", "strings"},
  13. {"net/http", "net/http"},
  14. {"github.com/KyleBanks/depth", "github.com/KyleBanks/depth"},
  15. {"C", ""},
  16. {"golang_org/x/anything", "vendor/golang_org/x/anything"},
  17. }
  18. for _, tt := range tests {
  19. p := Pkg{Name: tt.input}
  20. out := p.cleanName()
  21. if out != tt.expected {
  22. t.Fatalf("Unexpected cleanName, expected=%v, got=%v", tt.expected, out)
  23. }
  24. }
  25. }
  26. func TestPkg_AddDepImportSeen(t *testing.T) {
  27. var m MockImporter
  28. var tr Tree
  29. tr.Importer = m
  30. testName := "test"
  31. testSrcDir := "src/testing"
  32. var expectedIm build.ImportMode
  33. p := Pkg{Tree: &tr}
  34. m.ImportFn = func(name, srcDir string, im build.ImportMode) (*build.Package, error) {
  35. if name != testName {
  36. t.Fatalf("Unexpected name provided, expected=%v, got=%v", testName, name)
  37. }
  38. if srcDir != testSrcDir {
  39. t.Fatalf("Unexpected srcDir provided, expected=%v, got=%v", testSrcDir, srcDir)
  40. }
  41. if im != expectedIm {
  42. t.Fatalf("Unexpected ImportMode provided, expected=%v, got=%v", expectedIm, im)
  43. }
  44. return &build.Package{}, nil
  45. }
  46. // Hasn't seen the import
  47. p.addDep(m, testName, testSrcDir, false)
  48. // Has seen the import
  49. expectedIm = build.FindOnly
  50. p.addDep(m, testName, testSrcDir, false)
  51. }
  52. func TestByInternalAndName(t *testing.T) {
  53. pkgs := []Pkg{
  54. Pkg{Internal: true, Name: "net/http"},
  55. Pkg{Internal: false, Name: "github.com/KyleBanks/depth"},
  56. Pkg{Internal: true, Name: "strings"},
  57. Pkg{Internal: false, Name: "github.com/KyleBanks/commuter"},
  58. }
  59. expected := []string{"net/http", "strings", "github.com/KyleBanks/commuter", "github.com/KyleBanks/depth"}
  60. sort.Sort(byInternalAndName(pkgs))
  61. for i, e := range expected {
  62. if pkgs[i].Name != e {
  63. t.Fatalf("Unexpected Pkg at index %v, expected=%v, got=%v", i, e, pkgs[i].Name)
  64. }
  65. }
  66. }