Add file utility:

* ToAbsolutePath method added; not very well tested, since I don't know the environment the code will run on and the test code would be to prone to errors.
This commit is contained in:
Marcel Schramm 2019-10-12 19:52:17 +02:00
parent da72f303b4
commit 017f5e1f39
No known key found for this signature in database
GPG Key ID: 05971054C70EEDC7
3 changed files with 131 additions and 0 deletions

43
util/files/files.go Normal file
View File

@ -0,0 +1,43 @@
package files
import (
"net/url"
"os/user"
"path/filepath"
"strings"
)
// ToAbsolutePath handles different kind of paths and makes an absolute path
// out of them. Consider the following three inputs:
// file:///home/marcel/test.txt%C3%A4
// ./test.txtä
// ~/test.txtä
// Those will be turned into (assuming that our current working directory
// is /home/marcel:
// /home/marcel/test.txtä
// However, this method doesn't check for file existence.
func ToAbsolutePath(input string) (string, error) {
var resolvedPath string
if strings.HasPrefix(input, "~") {
currentUser, userResolveError := user.Current()
if userResolveError != nil {
return "", userResolveError
}
resolvedPath = filepath.Join(currentUser.HomeDir, strings.TrimPrefix(input, "~"))
} else {
resolvedPath = strings.TrimPrefix(input, "file://")
var unescapeError error
resolvedPath, unescapeError = url.PathUnescape(resolvedPath)
if unescapeError != nil {
return "", unescapeError
}
}
resolvedPath, resolveError := filepath.Abs(resolvedPath)
if resolveError != nil {
return "", resolveError
}
return resolvedPath, nil
}

45
util/files/files_test.go Normal file
View File

@ -0,0 +1,45 @@
//+build !windows
package files
import (
"testing"
)
func TestToAbsolutePath(t *testing.T) {
tests := []struct {
name string
input string
want string
wantErr bool
}{
{
name: "absolute path",
input: "/test.txt",
want: "/test.txt",
wantErr: false,
}, {
name: "absolute path; file uri",
input: "file:///te%20st.txt",
want: "/te st.txt",
wantErr: false,
}, {
name: "absolute path; contains Escaped characters",
input: "/te%20st.txt",
want: "/te st.txt",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToAbsolutePath(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ToAbsolutePath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ToAbsolutePath() got = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -0,0 +1,43 @@
package files
import (
"testing"
)
func TestToAbsolutePath(t *testing.T) {
tests := []struct {
name string
input string
want string
wantErr bool
}{
{
name: "absolute path",
input: "C:/test.txt",
want: "C:/test.txt",
wantErr: false,
}, {
name: "absolute path; file uri",
input: "file://C:/te%20st.txt",
want: "C:/te st.txt",
wantErr: false,
}, {
name: "absolute path; contains Escaped characters",
input: "C:/te%20st.txt",
want: "C:/te st.txt",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToAbsolutePath(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ToAbsolutePath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ToAbsolutePath() got = %v, want %v", got, tt.want)
}
})
}
}