fixblobs_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package server
  2. import (
  3. "io/fs"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "slices"
  8. "strings"
  9. "testing"
  10. )
  11. func TestFixBlobs(t *testing.T) {
  12. cases := []struct {
  13. path []string
  14. want []string
  15. }{
  16. {path: []string{"sha256-1234"}, want: []string{"sha256-1234"}},
  17. {path: []string{"sha256:1234"}, want: []string{"sha256-1234"}},
  18. {path: []string{"sha259:5678"}, want: []string{"sha259:5678"}},
  19. {path: []string{"sha256:abcd"}, want: []string{"sha256-abcd"}},
  20. {path: []string{"x/y/sha256:abcd"}, want: []string{"x/y/sha256-abcd"}},
  21. {path: []string{"x:y/sha256:abcd"}, want: []string{"x:y/sha256-abcd"}},
  22. {path: []string{"x:y/sha256:abcd"}, want: []string{"x:y/sha256-abcd"}},
  23. {path: []string{"x:y/sha256:abcd", "sha256:1234"}, want: []string{"x:y/sha256-abcd", "sha256-1234"}},
  24. {path: []string{"x:y/sha256:abcd", "sha256-1234"}, want: []string{"x:y/sha256-abcd", "sha256-1234"}},
  25. }
  26. for _, tt := range cases {
  27. t.Run(strings.Join(tt.path, "|"), func(t *testing.T) {
  28. hasColon := slices.ContainsFunc(tt.path, func(s string) bool { return strings.Contains(s, ":") })
  29. if hasColon && runtime.GOOS == "windows" {
  30. t.Skip("skipping test on windows")
  31. }
  32. rootDir := t.TempDir()
  33. for _, path := range tt.path {
  34. fullPath := filepath.Join(rootDir, path)
  35. fullDir, _ := filepath.Split(fullPath)
  36. t.Logf("creating dir %s", fullDir)
  37. if err := os.MkdirAll(fullDir, 0o755); err != nil {
  38. t.Fatal(err)
  39. }
  40. t.Logf("writing file %s", fullPath)
  41. if err := os.WriteFile(fullPath, nil, 0o644); err != nil {
  42. t.Fatal(err)
  43. }
  44. }
  45. if err := fixBlobs(rootDir); err != nil {
  46. t.Fatal(err)
  47. }
  48. got := slurpFiles(os.DirFS(rootDir))
  49. slices.Sort(tt.want)
  50. slices.Sort(got)
  51. if !slices.Equal(got, tt.want) {
  52. t.Fatalf("got = %v, want %v", got, tt.want)
  53. }
  54. })
  55. }
  56. }
  57. func slurpFiles(fsys fs.FS) []string {
  58. var sfs []string
  59. fn := func(path string, d fs.DirEntry, err error) error {
  60. if err != nil {
  61. return err
  62. }
  63. if d.IsDir() {
  64. return nil
  65. }
  66. sfs = append(sfs, path)
  67. return nil
  68. }
  69. if err := fs.WalkDir(fsys, ".", fn); err != nil {
  70. panic(err)
  71. }
  72. return sfs
  73. }