Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update xdg.SearchRuntimeFile to also look in the temporary directory #101

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()))
adrg marked this conversation as resolved.
Show resolved Hide resolved

f, err := os.Create(suggestedPath)
require.NoError(t, err)
require.NoError(t, f.Close())
defer os.Remove(suggestedPath)
adrg marked this conversation as resolved.
Show resolved Hide resolved

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