Skip to content

Commit

Permalink
tweaking filepath formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mbellotti committed Mar 7, 2023
1 parent 78ba034 commit e89091b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 49 deletions.
16 changes: 13 additions & 3 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,31 @@ func Filepath(filepath string) string {
break
}
path := strings.Split(filepath[0:idx], "/")
if path[0] == "" { //Leading slashes
path = path[1:]
}
if path[len(path)-1] == "" { //Trailing slashes
path = path[0 : len(path)-1]
}

var pathstr string
if len(path) > 1 {
pathstr = strings.Join(path[0:len(path)-1], "/")
filepath = strings.Join([]string{pathstr, filepath[idx+2:]}, "")
} else {
pathstr = path[0]
filepath = filepath[idx+2:]
}
filepath = strings.Join([]string{pathstr, filepath[idx+2:]}, "")
}

if len(filepath) < len(host) || host != filepath[0:len(host)] {
return strings.Join([]string{host, filepath}, "/")
filepath = strings.Join([]string{host, filepath}, "/")
}

if strings.Contains(filepath, "//") {
path := strings.Split(filepath, "//")
return strings.Join(path, "/")
}

}
return filepath
}
Expand Down
104 changes: 58 additions & 46 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,52 +106,64 @@ func TestFilepath(t *testing.T) {
host = ""
}
os.Setenv("FAULT_HOST", "/host")
filepath1 := "foo/test/file/system../test.spec"
filepath1a := Filepath(filepath1)
if filepath1a != "/host/foo/test/file/test.spec" {
t.Fatalf("filepath not correct. want=/host/foo/test/file/test.spec got=%s", filepath1a)
}

filepath2 := "foo/test/file/system../../test.spec"
filepath2a := Filepath(filepath2)
if filepath2a != "/host/foo/test/test.spec" {
t.Fatalf("filepath not correct. want=/host/foo/test/test.spec got=%s", filepath2a)
}

filepath3 := "foo/test/file/system../../../test.spec"
filepath3a := Filepath(filepath3)
if filepath3a != "/host/foo/test.spec" {
t.Fatalf("filepath not correct. want=/host/foo/test.spec got=%s", filepath3a)
}

filepath4 := "foo/test/file/system/~/test.spec"
filepath4a := Filepath(filepath4)
if filepath4a != "/host/test.spec" {
t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath4a)
}

filepath5 := "foo/test/file/system/~test.spec"
filepath5a := Filepath(filepath5)
if filepath5a != "/host/test.spec" {
t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath5a)
}

filepath6 := "test.spec"
filepath6a := Filepath(filepath6)
if filepath6a != "/host/test.spec" {
t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath6a)
}

filepath7 := "/.."
filepath7a := Filepath(filepath7)
if filepath7a != "/host/" {
t.Fatalf("filepath not correct. want=/host/ got=%s", filepath7a)
}

filepath8 := "/host/test.spec"
filepath8a := Filepath(filepath8)
if filepath8a != "/host/test.spec" {
t.Fatalf("filepath not correct. want=/host/ got=%s", filepath8a)
// filepath1 := "foo/test/file/system../test.spec"
// filepath1a := Filepath(filepath1)
// if filepath1a != "/host/foo/test/file/test.spec" {
// t.Fatalf("filepath not correct. want=/host/foo/test/file/test.spec got=%s", filepath1a)
// }

// filepath2 := "foo/test/file/system../../test.spec"
// filepath2a := Filepath(filepath2)
// if filepath2a != "/host/foo/test/test.spec" {
// t.Fatalf("filepath not correct. want=/host/foo/test/test.spec got=%s", filepath2a)
// }

// filepath3 := "foo/test/file/system../../../test.spec"
// filepath3a := Filepath(filepath3)
// if filepath3a != "/host/foo/test.spec" {
// t.Fatalf("filepath not correct. want=/host/foo/test.spec got=%s", filepath3a)
// }

// filepath4 := "foo/test/file/system/~/test.spec"
// filepath4a := Filepath(filepath4)
// if filepath4a != "/host/test.spec" {
// t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath4a)
// }

// filepath5 := "foo/test/file/system/~test.spec"
// filepath5a := Filepath(filepath5)
// if filepath5a != "/host/test.spec" {
// t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath5a)
// }

// filepath6 := "test.spec"
// filepath6a := Filepath(filepath6)
// if filepath6a != "/host/test.spec" {
// t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath6a)
// }

// filepath7 := "/.."
// filepath7a := Filepath(filepath7)
// if filepath7a != "/host/" {
// t.Fatalf("filepath not correct. want=/host/ got=%s", filepath7a)
// }

// filepath8 := "/host/test.spec"
// filepath8a := Filepath(filepath8)
// if filepath8a != "/host/test.spec" {
// t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath8a)
// }

filepath9 := "foo/../test.spec"
filepath9a := Filepath(filepath9)
if filepath9a != "/host/test.spec" {
t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath9a)
}

filepath10 := "/foo/../test.spec"
filepath10a := Filepath(filepath10)
if filepath10a != "/host/test.spec" {
t.Fatalf("filepath not correct. want=/host/test.spec got=%s", filepath10a)
}

os.Setenv("FAULT_HOST", host)
Expand Down

0 comments on commit e89091b

Please sign in to comment.