Skip to content

Commit

Permalink
Merge pull request #22 from adrg/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
adrg authored Sep 8, 2021
2 parents f33a497 + 5a48d5c commit 40e0bf5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
21 changes: 11 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,21 @@ func xdgPaths(name string, defaultPaths ...string) []string {
}

func uniquePaths(paths []string) []string {
var uniq []string
registry := map[string]struct{}{}
var (
uniq []string
registry = map[string]struct{}{}
)

for _, p := range paths {
dir := expandPath(p, Home)
if dir == "" || !filepath.IsAbs(dir) {
continue
}
if _, ok := registry[dir]; ok {
continue
}
if dir != "" && filepath.IsAbs(dir) {
if _, ok := registry[dir]; ok {
continue
}

registry[dir] = struct{}{}
uniq = append(uniq, dir)
registry[dir] = struct{}{}
uniq = append(uniq, dir)
}
}

return uniq
Expand Down
25 changes: 20 additions & 5 deletions xdg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func TestBaseDirFuncs(t *testing.T) {
pathFunc: xdg.ConfigFile,
searchFunc: xdg.SearchConfigFile,
},
{
relPaths: []string{"app.state", "appname/app.state"},
pathFunc: xdg.StateFile,
searchFunc: xdg.SearchStateFile,
},
{
relPaths: []string{"app.cache", "appname/app.cache"},
pathFunc: xdg.CacheFile,
Expand All @@ -70,11 +75,6 @@ func TestBaseDirFuncs(t *testing.T) {
pathFunc: xdg.RuntimeFile,
searchFunc: xdg.SearchRuntimeFile,
},
{
relPaths: []string{"app.state", "appname/app.state"},
pathFunc: xdg.StateFile,
searchFunc: xdg.SearchStateFile,
},
}

// Test base directories for regular files.
Expand Down Expand Up @@ -167,3 +167,18 @@ func testBaseDirsSymlinks(t *testing.T, inputs []*testInputData) {
}
}
}

func TestInvalidPaths(t *testing.T) {
inputs := map[string]func(string) (string, error){
"\000/app.data": xdg.DataFile,
"appname\000/app.yaml": xdg.ConfigFile,
"appname/\000/app.state": xdg.StateFile,
"\000appname/app.cache": xdg.CacheFile,
"\000/appname/app.pid": xdg.RuntimeFile,
}

for inputPath, xdgFunc := range inputs {
_, err := xdgFunc(inputPath)
require.Error(t, err)
}
}

0 comments on commit 40e0bf5

Please sign in to comment.