Skip to content

Commit aa865a5

Browse files
authored
Merge pull request #101 from adrg/update-search-runtime-file
Update xdg.SearchRuntimeFile to also look in the temporary directory
2 parents 2335a68 + 71a81ec commit aa865a5

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ func main() {
207207
// ConfigFile takes one parameter which must contain the name of the file,
208208
// but it can also contain a set of parent directories. If the directories
209209
// don't exist, they will be created relative to the base config directory.
210+
// It is recommended for files to be saved inside an application directory
211+
// relative to the base directory rather than directly inside the base
212+
// directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`).
210213
configFilePath, err := xdg.ConfigFile("appname/config.yaml")
211214
if err != nil {
212215
log.Fatal(err)

base_dirs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@ func (bd baseDirectories) searchCacheFile(relPath string) (string, error) {
7878
}
7979

8080
func (bd baseDirectories) searchRuntimeFile(relPath string) (string, error) {
81-
return pathutil.Search(relPath, []string{bd.runtime})
81+
return pathutil.Search(relPath, pathutil.Unique([]string{bd.runtime, os.TempDir()}))
8282
}

doc.go

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ XDG Base Directory
4848
// ConfigFile takes one parameter which must contain the name of the file,
4949
// but it can also contain a set of parent directories. If the directories
5050
// don't exist, they will be created relative to the base config directory.
51+
// It is recommended for files to be saved inside an application directory
52+
// relative to the base directory rather than directly inside the base
53+
// directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`).
5154
configFilePath, err := xdg.ConfigFile("appname/config.yaml")
5255
if err != nil {
5356
log.Fatal(err)

xdg.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,11 @@ func SearchCacheFile(relPath string) (string, error) {
202202

203203
// SearchRuntimeFile searches for the specified file in the runtime search path.
204204
// The relPath parameter must contain the name of the runtime file, and
205-
// optionally, a set of parent directories (e.g. appname/app.pid). If the
206-
// file cannot be found, an error specifying the searched path is returned.
205+
// optionally, a set of parent directories (e.g. appname/app.pid). The runtime
206+
// file is also searched in the operating system's temporary directory in order
207+
// to cover cases in which the runtime base directory does not exist or is not
208+
// accessible. If the file cannot be found, an error specifying the searched
209+
// paths is returned.
207210
func SearchRuntimeFile(relPath string) (string, error) {
208211
return baseDirs.searchRuntimeFile(relPath)
209212
}

xdg_test.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func TestInvalidPaths(t *testing.T) {
202202

203203
func TestNonExistentRuntimeDir(t *testing.T) {
204204
var (
205+
runtimeFiles = []string{"app.pid", "appname/app.pid"}
205206
envRuntimeDirVar = "XDG_RUNTIME_DIR"
206207
originalRuntimeDir = xdg.RuntimeDir
207208
nonExistentRuntimeDir = filepath.Join(xdg.Home, "runtime")
@@ -212,7 +213,18 @@ func TestNonExistentRuntimeDir(t *testing.T) {
212213
xdg.Reload()
213214
require.Equal(t, nonExistentRuntimeDir, xdg.RuntimeDir)
214215

215-
p, err := xdg.RuntimeFile("app.pid")
216-
require.NoError(t, err)
217-
require.Equal(t, filepath.Clean(os.TempDir()), filepath.Dir(p))
216+
for _, runtimeFile := range runtimeFiles {
217+
suggestedPath, err := xdg.RuntimeFile(runtimeFile)
218+
require.NoError(t, err)
219+
require.Equal(t, true, strings.HasPrefix(suggestedPath, os.TempDir()))
220+
221+
f, err := os.Create(suggestedPath)
222+
require.NoError(t, err)
223+
require.NoError(t, f.Close())
224+
defer os.Remove(suggestedPath)
225+
226+
foundPath, err := xdg.SearchRuntimeFile(runtimeFile)
227+
require.NoError(t, err)
228+
require.Equal(t, suggestedPath, foundPath)
229+
}
218230
}

0 commit comments

Comments
 (0)