github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/filesystem/normalize_test.go (about) 1 package filesystem 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "os/user" 8 "path/filepath" 9 "runtime" 10 "strings" 11 "testing" 12 ) 13 14 // TestTildeNotPathSeparator ensures that ~ is not considered a path separator 15 // on the platform. This is essentially guaranteed, but since we rely on this 16 // behavior, it's best to have an explicit check of it. 17 func TestTildeNotPathSeparator(t *testing.T) { 18 if os.IsPathSeparator('~') { 19 t.Fatal("tilde considered path separator") 20 } 21 } 22 23 func TestTildeExpandHome(t *testing.T) { 24 // Compute the path to the user's home directory. 25 homeDirectory, err := os.UserHomeDir() 26 if err != nil { 27 t.Fatal("unable to compute home directory:", err) 28 } 29 30 // Perform expansion. 31 expanded, err := tildeExpand("~") 32 if err != nil { 33 t.Fatal("tilde expansion failed:", err) 34 } 35 36 // Ensure that the result matches the expected values. 37 if expanded != homeDirectory { 38 t.Error("tilde-expanded path does not match expected") 39 } 40 } 41 42 func TestTildeExpandHomeSlash(t *testing.T) { 43 // Compute the path to the user's home directory. 44 homeDirectory, err := os.UserHomeDir() 45 if err != nil { 46 t.Fatal("unable to compute home directory:", err) 47 } 48 49 // Perform expansion. 50 expanded, err := tildeExpand("~/") 51 if err != nil { 52 t.Fatal("tilde expansion failed:", err) 53 } 54 55 // Ensure that the result matches the expected values. 56 if expanded != homeDirectory { 57 t.Error("tilde-expanded path does not match expected") 58 } 59 } 60 61 func TestTildeExpandHomeBackslash(t *testing.T) { 62 // Set expectations. 63 expectFailure := runtime.GOOS != "windows" 64 65 // Compute the path to the user's home directory. 66 homeDirectory, err := os.UserHomeDir() 67 if err != nil { 68 t.Fatal("unable to compute home directory:", err) 69 } 70 71 // Perform expansion. 72 expanded, err := tildeExpand("~\\") 73 if expectFailure && err == nil { 74 t.Error("tilde expansion succeeded unexpectedly") 75 } else if !expectFailure && err != nil { 76 t.Fatal("tilde expansion failed:", err) 77 } 78 79 // Bail if we're done. 80 if expectFailure { 81 return 82 } 83 84 // Ensure that the result matches the expected values. 85 if expanded != homeDirectory { 86 t.Error("tilde-expanded path does not match expected") 87 } 88 } 89 90 // currentUsername is a utility wrapper around user.Current for Windows systems, 91 // where the Username field will be of the form DOMAIN\username. 92 func currentUsername() (string, error) { 93 // Grab the user. 94 user, err := user.Current() 95 if err != nil { 96 return "", fmt.Errorf("unable to get current user: %w", err) 97 } 98 99 // If we're on a POSIX system, we're done. 100 if runtime.GOOS != "windows" { 101 return user.Username, nil 102 } 103 104 // If we're on Windows, there may be a DOMAIN\ prefix on the username. 105 if index := strings.IndexByte(user.Username, '\\'); index >= 0 { 106 if index == len(user.Username) { 107 return "", errors.New("domain extends to end of username") 108 } 109 return user.Username[index+1:], nil 110 } 111 return user.Username, nil 112 } 113 114 func TestTildeExpandLookup(t *testing.T) { 115 // Compute the path to the user's home directory. 116 homeDirectory, err := os.UserHomeDir() 117 if err != nil { 118 t.Fatal("unable to compute home directory:", err) 119 } 120 121 // Grab the current username. 122 username, err := currentUsername() 123 if err != nil { 124 t.Fatal("unable to look up current username:", err) 125 } 126 127 // Perform expansion. 128 expanded, err := tildeExpand("~" + username) 129 if err != nil { 130 t.Fatal("tilde expansion failed:", err) 131 } 132 133 // Ensure that the result matches the expected values. 134 if expanded != homeDirectory { 135 t.Error("tilde-expanded path does not match expected") 136 } 137 } 138 139 func TestTildeExpandLookupSlash(t *testing.T) { 140 // Compute the path to the user's home directory. 141 homeDirectory, err := os.UserHomeDir() 142 if err != nil { 143 t.Fatal("unable to compute home directory:", err) 144 } 145 146 // Grab the current username. 147 username, err := currentUsername() 148 if err != nil { 149 t.Fatal("unable to look up current username:", err) 150 } 151 152 // Perform expansion. 153 expanded, err := tildeExpand(fmt.Sprintf("~%s/", username)) 154 if err != nil { 155 t.Fatal("tilde expansion failed:", err) 156 } 157 158 // Ensure that the result matches the expected values. 159 if expanded != homeDirectory { 160 t.Error("tilde-expanded path does not match expected") 161 } 162 } 163 164 func TestTildeExpandLookupBackslash(t *testing.T) { 165 // Set expectations. 166 expectFailure := runtime.GOOS != "windows" 167 168 // Compute the path to the user's home directory. 169 homeDirectory, err := os.UserHomeDir() 170 if err != nil { 171 t.Fatal("unable to compute home directory:", err) 172 } 173 174 // Grab the current username. 175 username, err := currentUsername() 176 if err != nil { 177 t.Fatal("unable to look up current username:", err) 178 } 179 180 // Perform expansion. 181 expanded, err := tildeExpand(fmt.Sprintf("~%s\\", username)) 182 if expectFailure && err == nil { 183 t.Error("tilde expansion succeeded unexpectedly") 184 } else if !expectFailure && err != nil { 185 t.Fatal("tilde expansion failed:", err) 186 } 187 188 // Bail if we're done. 189 if expectFailure { 190 return 191 } 192 193 // Ensure that the result matches the expected values. 194 if expanded != homeDirectory { 195 t.Error("tilde-expanded path does not match expected") 196 } 197 } 198 199 func TestNormalizeHome(t *testing.T) { 200 // Compute the path to the user's home directory. 201 homeDirectory, err := os.UserHomeDir() 202 if err != nil { 203 t.Fatal("unable to compute home directory:", err) 204 } 205 206 // Compute a path relative to the home directory. 207 normalized, err := Normalize("~/somepath") 208 if err != nil { 209 t.Fatal("unable to normalize path:", err) 210 } 211 212 // Ensure that it's what we expect. 213 if normalized != filepath.Join(homeDirectory, "somepath") { 214 t.Error("normalized path does not match expected") 215 } 216 }