Skip to content

Commit

Permalink
Merge pull request #101 from adrg/update-search-runtime-file
Browse files Browse the repository at this point in the history
Update xdg.SearchRuntimeFile to also look in the temporary directory
  • Loading branch information
adrg authored Oct 31, 2024
2 parents 2335a68 + 71a81ec commit aa865a5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ func main() {
// ConfigFile takes one parameter which must contain the name of the file,
// but it can also contain a set of parent directories. If the directories
// don't exist, they will be created relative to the base config directory.
// It is recommended for files to be saved inside an application directory
// relative to the base directory rather than directly inside the base
// directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`).
configFilePath, err := xdg.ConfigFile("appname/config.yaml")
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion base_dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ func (bd baseDirectories) searchCacheFile(relPath string) (string, error) {
}

func (bd baseDirectories) searchRuntimeFile(relPath string) (string, error) {
return pathutil.Search(relPath, []string{bd.runtime})
return pathutil.Search(relPath, pathutil.Unique([]string{bd.runtime, os.TempDir()}))
}
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ XDG Base Directory
// ConfigFile takes one parameter which must contain the name of the file,
// but it can also contain a set of parent directories. If the directories
// don't exist, they will be created relative to the base config directory.
// It is recommended for files to be saved inside an application directory
// relative to the base directory rather than directly inside the base
// directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`).
configFilePath, err := xdg.ConfigFile("appname/config.yaml")
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 5 additions & 2 deletions xdg.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,11 @@ func SearchCacheFile(relPath string) (string, error) {

// SearchRuntimeFile searches for the specified file in the runtime search path.
// The relPath parameter must contain the name of the runtime file, and
// optionally, a set of parent directories (e.g. appname/app.pid). If the
// file cannot be found, an error specifying the searched path is returned.
// optionally, a set of parent directories (e.g. appname/app.pid). The runtime
// file is also searched in the operating system's temporary directory in order
// to cover cases in which the runtime base directory does not exist or is not
// accessible. If the file cannot be found, an error specifying the searched
// paths is returned.
func SearchRuntimeFile(relPath string) (string, error) {
return baseDirs.searchRuntimeFile(relPath)
}
18 changes: 15 additions & 3 deletions xdg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func TestInvalidPaths(t *testing.T) {

func TestNonExistentRuntimeDir(t *testing.T) {
var (
runtimeFiles = []string{"app.pid", "appname/app.pid"}
envRuntimeDirVar = "XDG_RUNTIME_DIR"
originalRuntimeDir = xdg.RuntimeDir
nonExistentRuntimeDir = filepath.Join(xdg.Home, "runtime")
Expand All @@ -212,7 +213,18 @@ func TestNonExistentRuntimeDir(t *testing.T) {
xdg.Reload()
require.Equal(t, nonExistentRuntimeDir, xdg.RuntimeDir)

p, err := xdg.RuntimeFile("app.pid")
require.NoError(t, err)
require.Equal(t, filepath.Clean(os.TempDir()), filepath.Dir(p))
for _, runtimeFile := range runtimeFiles {
suggestedPath, err := xdg.RuntimeFile(runtimeFile)
require.NoError(t, err)
require.Equal(t, true, strings.HasPrefix(suggestedPath, os.TempDir()))

f, err := os.Create(suggestedPath)
require.NoError(t, err)
require.NoError(t, f.Close())
defer os.Remove(suggestedPath)

foundPath, err := xdg.SearchRuntimeFile(runtimeFile)
require.NoError(t, err)
require.Equal(t, suggestedPath, foundPath)
}
}

0 comments on commit aa865a5

Please sign in to comment.