file_system_vpath_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "path/filepath"
  4. "runtime"
  5. "strings"
  6. "testing"
  7. )
  8. /*
  9. virtualDirname feeds the "Location" row of the file properties dialog.
  10. It must stay lexical and slash based. filepath.Dir looks correct on Linux
  11. but mangles a virtual path on Windows, which is the regression this guards.
  12. */
  13. func TestVirtualDirname(t *testing.T) {
  14. tests := []struct {
  15. name string
  16. vpath string
  17. want string
  18. }{
  19. {"nested file", "user:/cluster/hello_world.job.agi", "user:/cluster"},
  20. {"nested folder", "user:/Desktop/Photos", "user:/Desktop"},
  21. {"file at root of vroot", "user:/a.txt", "user:"},
  22. {"deep path", "tmp:/a/b/c/d.txt", "tmp:/a/b/c"},
  23. {"name with plus and space", "user:/My +Stuff/a b.mkv", "user:/My +Stuff"},
  24. {"other vroot id", "cluster:/shared/report.pdf", "cluster:/shared"},
  25. }
  26. for _, tt := range tests {
  27. t.Run(tt.name, func(t *testing.T) {
  28. got := virtualDirname(tt.vpath)
  29. if got != tt.want {
  30. t.Errorf("virtualDirname(%q) = %q, want %q", tt.vpath, got, tt.want)
  31. }
  32. if strings.HasPrefix(got, "./") {
  33. t.Errorf("virtualDirname(%q) = %q, must not carry a \"./\" prefix", tt.vpath, got)
  34. }
  35. if strings.Contains(got, "\\") {
  36. t.Errorf("virtualDirname(%q) = %q, must stay slash separated", tt.vpath, got)
  37. }
  38. })
  39. }
  40. }
  41. // Documents why filepath.Dir cannot be used on a virtual path. It is the exact
  42. // call this fix replaced, and it only misbehaves on Windows.
  43. func TestFilepathDirIsUnsafeForVirtualPathOnWindows(t *testing.T) {
  44. const vpath = "user:/cluster/hello_world.job.agi"
  45. got := filepath.ToSlash(filepath.Dir(vpath))
  46. if runtime.GOOS == "windows" {
  47. if !strings.HasPrefix(got, "./") {
  48. t.Skipf("filepath.Dir no longer prefixes %q with \"./\" on this Go version (got %q); virtualDirname stays correct either way", vpath, got)
  49. }
  50. if virtualDirname(vpath) == got {
  51. t.Errorf("virtualDirname must not reproduce filepath.Dir's %q", got)
  52. }
  53. return
  54. }
  55. if got != virtualDirname(vpath) {
  56. t.Errorf("on %s filepath.Dir and virtualDirname should agree, got %q vs %q", runtime.GOOS, got, virtualDirname(vpath))
  57. }
  58. }