mod_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package mod
  2. import (
  3. "runtime/debug"
  4. "testing"
  5. )
  6. func TestModuleVersion(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. module string
  10. biContent string
  11. wantVersion string
  12. }{
  13. {
  14. name: "returns empty string if build information not available",
  15. biContent: `
  16. go go1.20.3
  17. path github.com/docker/docker/builder/builder-next/worker
  18. mod github.com/docker/docker (devel)
  19. `,
  20. module: "github.com/moby/buildkit",
  21. wantVersion: "",
  22. },
  23. {
  24. name: "returns the version of buildkit dependency",
  25. biContent: `
  26. go go1.20.3
  27. path github.com/docker/docker/builder/builder-next/worker
  28. mod github.com/docker/docker (devel)
  29. dep github.com/moby/buildkit v0.11.5 h1:JZvvWzulcnA2G4c/gJiSIqKDUoBjctYw2WMuS+XJexU=
  30. `,
  31. module: "github.com/moby/buildkit",
  32. wantVersion: "v0.11.5",
  33. },
  34. {
  35. name: "returns the replaced version of buildkit dependency",
  36. biContent: `
  37. go go1.20.3
  38. path github.com/docker/docker/builder/builder-next/worker
  39. mod github.com/docker/docker (devel)
  40. dep github.com/moby/buildkit v0.11.5 h1:JZvvWzulcnA2G4c/gJiSIqKDUoBjctYw2WMuS+XJexU=
  41. => github.com/moby/buildkit v0.12.0 h1:3YO8J4RtmG7elEgaWMb4HgmpS2CfY1QlaOz9nwB+ZSs=
  42. `,
  43. module: "github.com/moby/buildkit",
  44. wantVersion: "v0.12.0",
  45. },
  46. {
  47. name: "returns the base version of pseudo version",
  48. biContent: `
  49. go go1.20.3
  50. path github.com/docker/docker/builder/builder-next/worker
  51. mod github.com/docker/docker (devel)
  52. dep github.com/moby/buildkit v0.10.7-0.20230306143919-70f2ad56d3e5 h1:JZvvWzulcnA2G4c/gJiSIqKDUoBjctYw2WMuS+XJexU=
  53. `,
  54. module: "github.com/moby/buildkit",
  55. wantVersion: "v0.10.6+70f2ad56d3e5",
  56. },
  57. }
  58. for _, tt := range tests {
  59. tt := tt
  60. t.Run(tt.name, func(t *testing.T) {
  61. bi, err := debug.ParseBuildInfo(tt.biContent)
  62. if err != nil {
  63. t.Fatalf("failed to parse build info: %v", err)
  64. }
  65. if gotVersion := moduleVersion(tt.module, bi); gotVersion != tt.wantVersion {
  66. t.Errorf("moduleVersion() = %v, want %v", gotVersion, tt.wantVersion)
  67. }
  68. })
  69. }
  70. }