|
| 1 | +package platform |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestIsDirWritable(t *testing.T) { |
| 10 | + dir := t.TempDir() |
| 11 | + |
| 12 | + if !IsDirWritable(dir) { |
| 13 | + t.Errorf("existing temp dir %q should be writable", dir) |
| 14 | + } |
| 15 | + |
| 16 | + // A not-yet-existing subdirectory whose parent is writable should count as |
| 17 | + // writable (we can create it). |
| 18 | + nested := filepath.Join(dir, "a", "b", "c") |
| 19 | + if !IsDirWritable(nested) { |
| 20 | + t.Errorf("nested path under writable parent %q should be writable", nested) |
| 21 | + } |
| 22 | + |
| 23 | + // A path whose ancestor is a file (not a directory) is not writable. |
| 24 | + fileParent := filepath.Join(dir, "afile") |
| 25 | + if err := os.WriteFile(fileParent, []byte("x"), 0o644); err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + if IsDirWritable(filepath.Join(fileParent, "sub")) { |
| 29 | + t.Error("path under a regular file should not be writable") |
| 30 | + } |
| 31 | + |
| 32 | + if IsDirWritable("") { |
| 33 | + t.Error("empty dir should not be writable") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestSelectBinDir(t *testing.T) { |
| 38 | + writable := t.TempDir() |
| 39 | + |
| 40 | + // A non-writable candidate: under a regular file. |
| 41 | + blocker := filepath.Join(t.TempDir(), "file") |
| 42 | + if err := os.WriteFile(blocker, []byte("x"), 0o644); err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + badCandidate := filepath.Join(blocker, "bin") |
| 46 | + |
| 47 | + got, err := SelectBinDir([]string{badCandidate, writable}) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("SelectBinDir: %v", err) |
| 50 | + } |
| 51 | + if got != writable { |
| 52 | + t.Errorf("SelectBinDir picked %q, want %q", got, writable) |
| 53 | + } |
| 54 | + |
| 55 | + if _, err := SelectBinDir([]string{badCandidate}); err == nil { |
| 56 | + t.Error("SelectBinDir with only bad candidates: expected error") |
| 57 | + } |
| 58 | + if _, err := SelectBinDir(nil); err == nil { |
| 59 | + t.Error("SelectBinDir with no candidates: expected error") |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestIsOnPATH(t *testing.T) { |
| 64 | + tests := []struct { |
| 65 | + name string |
| 66 | + osID OS |
| 67 | + dir string |
| 68 | + pathEnv string |
| 69 | + want bool |
| 70 | + }{ |
| 71 | + { |
| 72 | + name: "unix present", |
| 73 | + osID: Linux, |
| 74 | + dir: "/home/jane/.local/bin", |
| 75 | + pathEnv: "/usr/bin:/home/jane/.local/bin:/bin", |
| 76 | + want: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "unix trailing slash normalized", |
| 80 | + osID: Linux, |
| 81 | + dir: "/home/jane/.local/bin/", |
| 82 | + pathEnv: "/usr/bin:/home/jane/.local/bin", |
| 83 | + want: true, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "unix absent", |
| 87 | + osID: MacOS, |
| 88 | + dir: "/opt/homebrew/bin", |
| 89 | + pathEnv: "/usr/bin:/bin", |
| 90 | + want: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "unix case sensitive", |
| 94 | + osID: Linux, |
| 95 | + dir: "/Home/Jane/bin", |
| 96 | + pathEnv: "/home/jane/bin", |
| 97 | + want: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "windows case-insensitive and slash-agnostic", |
| 101 | + osID: Windows, |
| 102 | + dir: `C:\Program Files\php-debugger\bin`, |
| 103 | + pathEnv: `C:\Windows;c:/program files/PHP-DEBUGGER/BIN`, |
| 104 | + want: true, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "windows absent", |
| 108 | + osID: Windows, |
| 109 | + dir: `C:\a\bin`, |
| 110 | + pathEnv: `C:\b\bin;C:\c\bin`, |
| 111 | + want: false, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "empty dir", |
| 115 | + osID: Linux, |
| 116 | + dir: "", |
| 117 | + pathEnv: "/usr/bin", |
| 118 | + want: false, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "empty path", |
| 122 | + osID: Linux, |
| 123 | + dir: "/usr/bin", |
| 124 | + pathEnv: "", |
| 125 | + want: false, |
| 126 | + }, |
| 127 | + } |
| 128 | + for _, tt := range tests { |
| 129 | + t.Run(tt.name, func(t *testing.T) { |
| 130 | + if got := IsOnPATH(tt.osID, tt.dir, tt.pathEnv); got != tt.want { |
| 131 | + t.Errorf("IsOnPATH(%s, %q, %q) = %v, want %v", tt.osID, tt.dir, tt.pathEnv, got, tt.want) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments