pax_global_header00006660000000000000000000000064135671417530014526gustar00rootroot0000000000000052 comment=6ed5444b406673d119787019bf2610bdf044254f xdg-1.0.0/000077500000000000000000000000001356714175300123065ustar00rootroot00000000000000xdg-1.0.0/.gitignore000066400000000000000000000000271356714175300142750ustar00rootroot00000000000000*.test *.out .DS_STORE xdg-1.0.0/.travis.yml000066400000000000000000000003221356714175300144140ustar00rootroot00000000000000language: go go: - 1.11.x os: - linux - osx before_install: - go get -t -v ./... script: - go test -v -race -covermode=atomic -coverprofile=coverage.txt after_success: - bash <(curl -s https://codecov.io/bash) xdg-1.0.0/LICENSE000066400000000000000000000027471356714175300133250ustar00rootroot00000000000000BSD 3-Clause License Copyright (c) 2017, OpenPeeDeeP All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. xdg-1.0.0/README.md000066400000000000000000000055101356714175300135660ustar00rootroot00000000000000# XDG [![Build status](https://ci.appveyor.com/api/projects/status/9eoupq9jgsu2p0jv?svg=true)](https://ci.appveyor.com/project/dixonwille/xdg) [![Build Status](https://travis-ci.org/OpenPeeDeeP/xdg.svg?branch=master)](https://travis-ci.org/OpenPeeDeeP/xdg) [![Go Report Card](https://goreportcard.com/badge/github.com/OpenPeeDeeP/xdg)](https://goreportcard.com/report/github.com/OpenPeeDeeP/xdg) [![GoDoc](https://godoc.org/github.com/OpenPeeDeeP/xdg?status.svg)](https://godoc.org/github.com/OpenPeeDeeP/xdg) [![codecov](https://codecov.io/gh/OpenPeeDeeP/xdg/branch/master/graph/badge.svg)](https://codecov.io/gh/OpenPeeDeeP/xdg) A cross platform package that tries to follow [XDG Standard](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) when possible. Since XDG is linux specific, I am only able to follow standards to the T on linux. But for the other operating systems I am finding similar best practice locations for the files. ## Locations Per OS The following table shows what is used if the envrionment variable is not set. If the variable is set then this package uses that. Linux follows the default standards. Mac does when it comes to the home directory but for system wide it uses the standard `/Library/Application Support`. As for Windows, the variable defaults are just other environment variables set up by the operation system. > When creating `XDG` application the `Vendor` and `Application` names are appeneded to the end of the path to keep projects unique. | | Linux(and BSD) | Mac | Windows | | ---: | :---: | :---: | :---: | | `XDG_DATA_DIRS` | [`/usr/local/share`, `/usr/share`] | [`/Library/Application Support`] | `%PROGRAMDATA%` | | `XDG_DATA_HOME` | `~/.local/share` | `~/Library/Application Support` | `%APPDATA%` | | `XDG_CONFIG_DIRS` | [`/etc/xdg`] | [`/Library/Application Support`] | `%PROGRAMDATA%` | | `XDG_CONFIG_HOME` | `~/.config` | `~/Library/Application Support` | `%APPDATA%` | | `XDG_CACHE_HOME` | `~/.cache` | `~/Library/Caches` | `%LOCALAPPDATA%` | ## Notes - This package does not merge files if they exist across different directories. - The `Query` methods search through the system variables, `DIRS`, first (when using environment variables first in the variable has presidence). It then checks home variables, `HOME`. - This package will not create any directories for you. In the standard, it states the following: > If, when attempting to write a file, the destination directory is non-existant an attempt should be made to create it with permission `0700`. If the destination directory exists already the permissions should not be changed. The application should be prepared to handle the case where the file could not be written, either because the directory was non-existant and could not be created, or for any other reason. In such case it may chose to present an error message to the user. xdg-1.0.0/appveyor.yml000066400000000000000000000005361356714175300147020ustar00rootroot00000000000000version: 0.0.1_{build} build: off platform: x64 clone_folder: c:\gopath\src\github.com\OpenPeeDeeP\xdg environment: GOPATH: c:\gopath stack: go 1.11 install: - go get -t -v ./... - cinst codecov before_test: - go vet ./... test_script: - go test -v -race -covermode=atomic -coverprofile=coverage.txt on_success: - codecov -f coverage.txt xdg-1.0.0/go.mod000066400000000000000000000003411356714175300134120ustar00rootroot00000000000000module github.com/OpenPeeDeeP/xdg require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.1 // indirect github.com/stretchr/testify v1.2.2 ) xdg-1.0.0/go.sum000066400000000000000000000012601356714175300134400ustar00rootroot00000000000000github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= xdg-1.0.0/xdg.go000066400000000000000000000112101356714175300134120ustar00rootroot00000000000000// Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package xdg impelements the XDG standard for application file locations. package xdg import ( "os" "path/filepath" "strings" ) var defaulter xdgDefaulter = new(osDefaulter) type xdgDefaulter interface { defaultDataHome() string defaultDataDirs() []string defaultConfigHome() string defaultConfigDirs() []string defaultCacheHome() string } type osDefaulter struct { } //This method is used in the testing suit // nolint: deadcode func setDefaulter(def xdgDefaulter) { defaulter = def } // XDG is information about the currently running application type XDG struct { Vendor string Application string } // New returns an instance of XDG that is used to grab files for application use func New(vendor, application string) *XDG { return &XDG{ Vendor: vendor, Application: application, } } // DataHome returns the location that should be used for user specific data files for this specific application func (x *XDG) DataHome() string { return filepath.Join(DataHome(), x.Vendor, x.Application) } // DataDirs returns a list of locations that should be used for system wide data files for this specific application func (x *XDG) DataDirs() []string { dataDirs := DataDirs() for i, dir := range dataDirs { dataDirs[i] = filepath.Join(dir, x.Vendor, x.Application) } return dataDirs } // ConfigHome returns the location that should be used for user specific config files for this specific application func (x *XDG) ConfigHome() string { return filepath.Join(ConfigHome(), x.Vendor, x.Application) } // ConfigDirs returns a list of locations that should be used for system wide config files for this specific application func (x *XDG) ConfigDirs() []string { configDirs := ConfigDirs() for i, dir := range configDirs { configDirs[i] = filepath.Join(dir, x.Vendor, x.Application) } return configDirs } // CacheHome returns the location that should be used for application cache files for this specific application func (x *XDG) CacheHome() string { return filepath.Join(CacheHome(), x.Vendor, x.Application) } // QueryData looks for the given filename in XDG paths for data files. // Returns an empty string if one was not found. func (x *XDG) QueryData(filename string) string { dirs := x.DataDirs() dirs = append([]string{x.DataHome()}, dirs...) return returnExist(filename, dirs) } // QueryConfig looks for the given filename in XDG paths for config files. // Returns an empty string if one was not found. func (x *XDG) QueryConfig(filename string) string { dirs := x.ConfigDirs() dirs = append([]string{x.ConfigHome()}, dirs...) return returnExist(filename, dirs) } // QueryCache looks for the given filename in XDG paths for cache files. // Returns an empty string if one was not found. func (x *XDG) QueryCache(filename string) string { return returnExist(filename, []string{x.CacheHome()}) } func returnExist(filename string, dirs []string) string { for _, dir := range dirs { _, err := os.Stat(filepath.Join(dir, filename)) if (err != nil && os.IsExist(err)) || err == nil { return filepath.Join(dir, filename) } } return "" } // DataHome returns the location that should be used for user specific data files func DataHome() string { dataHome := os.Getenv("XDG_DATA_HOME") if dataHome == "" { dataHome = defaulter.defaultDataHome() } return dataHome } // DataDirs returns a list of locations that should be used for system wide data files func DataDirs() []string { var dataDirs []string dataDirsStr := os.Getenv("XDG_DATA_DIRS") if dataDirsStr != "" { dataDirs = strings.Split(dataDirsStr, string(os.PathListSeparator)) } if len(dataDirs) == 0 { dataDirs = defaulter.defaultDataDirs() } return dataDirs } // ConfigHome returns the location that should be used for user specific config files func ConfigHome() string { configHome := os.Getenv("XDG_CONFIG_HOME") if configHome == "" { configHome = defaulter.defaultConfigHome() } return configHome } // ConfigDirs returns a list of locations that should be used for system wide config files func ConfigDirs() []string { var configDirs []string configDirsStr := os.Getenv("XDG_CONFIG_DIRS") if configDirsStr != "" { configDirs = strings.Split(configDirsStr, string(os.PathListSeparator)) } if len(configDirs) == 0 { configDirs = defaulter.defaultConfigDirs() } return configDirs } // CacheHome returns the location that should be used for application cache files func CacheHome() string { cacheHome := os.Getenv("XDG_CACHE_HOME") if cacheHome == "" { cacheHome = defaulter.defaultCacheHome() } return cacheHome } xdg-1.0.0/xdg_bsd.go000066400000000000000000000013651356714175300142540ustar00rootroot00000000000000// +build freebsd openbsd netbsd // Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import ( "os" "path/filepath" ) func (o *osDefaulter) defaultDataHome() string { return filepath.Join(os.Getenv("HOME"), ".local", "share") } func (o *osDefaulter) defaultDataDirs() []string { return []string{"/usr/local/share/", "/usr/share/"} } func (o *osDefaulter) defaultConfigHome() string { return filepath.Join(os.Getenv("HOME"), ".config") } func (o *osDefaulter) defaultConfigDirs() []string { return []string{"/etc/xdg"} } func (o *osDefaulter) defaultCacheHome() string { return filepath.Join(os.Getenv("HOME"), ".cache") } xdg-1.0.0/xdg_bsd_test.go000066400000000000000000000030401356714175300153030ustar00rootroot00000000000000// +build freebsd openbsd netbsd // Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import ( "os" "testing" "github.com/stretchr/testify/assert" ) func TestDefaultDataHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/.local/share" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultDataHome() assert.Equal(expected, actual) } func TestDefaultDataDirs(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) expected := []string{"/usr/local/share/", "/usr/share/"} actual := defaulter.defaultDataDirs() assert.Equal(expected, actual) } func TestDefaultConfigHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/.config" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultConfigHome() assert.Equal(expected, actual) } func TestDefaultConfigDirs(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) expected := []string{"/etc/xdg"} actual := defaulter.defaultConfigDirs() assert.Equal(expected, actual) } func TestDefaultCacheHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/.cache" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultCacheHome() assert.Equal(expected, actual) } xdg-1.0.0/xdg_darwin.go000066400000000000000000000014701356714175300147650ustar00rootroot00000000000000// Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import ( "os" "path/filepath" ) func (o *osDefaulter) defaultDataHome() string { return filepath.Join(os.Getenv("HOME"), "Library", "Application Support") } func (o *osDefaulter) defaultDataDirs() []string { return []string{filepath.Join("/Library", "Application Support")} } func (o *osDefaulter) defaultConfigHome() string { return filepath.Join(os.Getenv("HOME"), "Library", "Application Support") } func (o *osDefaulter) defaultConfigDirs() []string { return []string{filepath.Join("/Library", "Application Support")} } func (o *osDefaulter) defaultCacheHome() string { return filepath.Join(os.Getenv("HOME"), "Library", "Caches") } xdg-1.0.0/xdg_darwin_test.go000066400000000000000000000031131356714175300160200ustar00rootroot00000000000000// +build darwin // Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import ( "os" "testing" "github.com/stretchr/testify/assert" ) func TestDefaultDataHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/Library/Application Support" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultDataHome() assert.Equal(expected, actual) } func TestDefaultDataDirs(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) expected := []string{"/Library/Application Support"} actual := defaulter.defaultDataDirs() assert.Equal(expected, actual) } func TestDefaultConfigHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/Library/Application Support" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultConfigHome() assert.Equal(expected, actual) } func TestDefaultConfigDirs(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) expected := []string{"/Library/Application Support"} actual := defaulter.defaultConfigDirs() assert.Equal(expected, actual) } func TestDefaultCacheHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/Library/Caches" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultCacheHome() assert.Equal(expected, actual) } xdg-1.0.0/xdg_linux.go000066400000000000000000000013231356714175300146350ustar00rootroot00000000000000// Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import ( "os" "path/filepath" ) func (o *osDefaulter) defaultDataHome() string { return filepath.Join(os.Getenv("HOME"), ".local", "share") } func (o *osDefaulter) defaultDataDirs() []string { return []string{"/usr/local/share/", "/usr/share/"} } func (o *osDefaulter) defaultConfigHome() string { return filepath.Join(os.Getenv("HOME"), ".config") } func (o *osDefaulter) defaultConfigDirs() []string { return []string{"/etc/xdg"} } func (o *osDefaulter) defaultCacheHome() string { return filepath.Join(os.Getenv("HOME"), ".cache") } xdg-1.0.0/xdg_linux_test.go000066400000000000000000000030171356714175300156760ustar00rootroot00000000000000// +build linux // Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import ( "os" "testing" "github.com/stretchr/testify/assert" ) func TestDefaultDataHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/.local/share" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultDataHome() assert.Equal(expected, actual) } func TestDefaultDataDirs(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) expected := []string{"/usr/local/share/", "/usr/share/"} actual := defaulter.defaultDataDirs() assert.Equal(expected, actual) } func TestDefaultConfigHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/.config" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultConfigHome() assert.Equal(expected, actual) } func TestDefaultConfigDirs(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) expected := []string{"/etc/xdg"} actual := defaulter.defaultConfigDirs() assert.Equal(expected, actual) } func TestDefaultCacheHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) homeDir := "/some/path" expected := homeDir + "/.cache" os.Setenv("HOME", homeDir) // nolint: errcheck actual := defaulter.defaultCacheHome() assert.Equal(expected, actual) } xdg-1.0.0/xdg_test.go000066400000000000000000000233451356714175300144650ustar00rootroot00000000000000// Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import ( "fmt" "os" "path/filepath" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) type mockDefaulter struct { mock.Mock } func (m *mockDefaulter) defaultDataHome() string { args := m.Called() return args.String(0) } func (m *mockDefaulter) defaultDataDirs() []string { args := m.Called() return args.Get(0).([]string) } func (m *mockDefaulter) defaultConfigHome() string { args := m.Called() return args.String(0) } func (m *mockDefaulter) defaultConfigDirs() []string { args := m.Called() return args.Get(0).([]string) } func (m *mockDefaulter) defaultCacheHome() string { args := m.Called() return args.String(0) } const ( MDataHome = iota MDataDirs MConfigHome MConfigDirs MCacheHome ) var getterTestCases = []getterTestCase{ {"DataHome Without", "defaultDataHome", filepath.Clean("/some/path"), true, "XDG_DATA_HOME", "", MDataHome, nil, filepath.Clean("/some/path")}, {"DataDirs Without", "defaultDataDirs", []string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}, true, "XDG_DATA_DIRS", "", MDataDirs, nil, []string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}}, {"ConfigHome Without", "defaultConfigHome", filepath.Clean("/some/path"), true, "XDG_CONFIG_HOME", "", MConfigHome, nil, filepath.Clean("/some/path")}, {"ConfigDirs Without", "defaultConfigDirs", []string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}, true, "XDG_CONFIG_DIRS", "", MConfigDirs, nil, []string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}}, {"CacheHome Without", "defaultCacheHome", filepath.Clean("/some/path"), true, "XDG_CACHE_HOME", "", MCacheHome, nil, filepath.Clean("/some/path")}, {"DataHome With", "defaultDataHome", filepath.Clean("/wrong/path"), false, "XDG_DATA_HOME", filepath.Clean("/some/path"), MDataHome, nil, filepath.Clean("/some/path")}, {"DataDirs With", "defaultDataDirs", []string{filepath.Clean("/wrong/path"), filepath.Clean("/some/other/wrong")}, false, "XDG_DATA_DIRS", strings.Join([]string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}, string(os.PathListSeparator)), MDataDirs, nil, []string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}}, {"ConfigHome With", "defaultConfigHome", filepath.Clean("/wrong/path"), false, "XDG_CONFIG_HOME", filepath.Clean("/some/path"), MConfigHome, nil, filepath.Clean("/some/path")}, {"ConfigDirs With", "defaultConfigDirs", []string{filepath.Clean("/wrong/path"), filepath.Clean("/some/other/wrong")}, false, "XDG_CONFIG_DIRS", strings.Join([]string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}, string(os.PathListSeparator)), MConfigDirs, nil, []string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}}, {"CacheHome With", "defaultCacheHome", filepath.Clean("/wrong/path"), false, "XDG_CACHE_HOME", filepath.Clean("/some/path"), MCacheHome, nil, filepath.Clean("/some/path")}, {"DataHome App Without", "defaultDataHome", filepath.Clean("/some/path"), true, "XDG_DATA_HOME", "", MDataHome, New("OpenPeeDeeP", "XDG"), filepath.Clean("/some/path/OpenPeeDeeP/XDG")}, {"DataDirs App Without", "defaultDataDirs", []string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}, true, "XDG_DATA_DIRS", "", MDataDirs, New("OpenPeeDeeP", "XDG"), []string{filepath.Clean("/some/path/OpenPeeDeeP/XDG"), filepath.Clean("/some/other/path/OpenPeeDeeP/XDG")}}, {"ConfigHome App Without", "defaultConfigHome", filepath.Clean("/some/path"), true, "XDG_CONFIG_HOME", "", MConfigHome, New("OpenPeeDeeP", "XDG"), filepath.Clean("/some/path/OpenPeeDeeP/XDG")}, {"ConfigDirs App Without", "defaultConfigDirs", []string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}, true, "XDG_CONFIG_DIRS", "", MConfigDirs, New("OpenPeeDeeP", "XDG"), []string{filepath.Clean("/some/path/OpenPeeDeeP/XDG"), filepath.Clean("/some/other/path/OpenPeeDeeP/XDG")}}, {"CacheHome App Without", "defaultCacheHome", filepath.Clean("/some/path"), true, "XDG_CACHE_HOME", "", MCacheHome, New("OpenPeeDeeP", "XDG"), filepath.Clean("/some/path/OpenPeeDeeP/XDG")}, {"DataHome App With", "defaultDataHome", filepath.Clean("/wrong/path"), false, "XDG_DATA_HOME", filepath.Clean("/some/path"), MDataHome, New("OpenPeeDeeP", "XDG"), filepath.Clean("/some/path/OpenPeeDeeP/XDG")}, {"DataDirs App With", "defaultDataDirs", []string{filepath.Clean("/wrong/path"), filepath.Clean("/some/other/wrong")}, false, "XDG_DATA_DIRS", strings.Join([]string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}, string(os.PathListSeparator)), MDataDirs, New("OpenPeeDeeP", "XDG"), []string{filepath.Clean("/some/path/OpenPeeDeeP/XDG"), filepath.Clean("/some/other/path/OpenPeeDeeP/XDG")}}, {"ConfigHome App With", "defaultConfigHome", filepath.Clean("/wrong/path"), false, "XDG_CONFIG_HOME", filepath.Clean("/some/path"), MConfigHome, New("OpenPeeDeeP", "XDG"), filepath.Clean("/some/path/OpenPeeDeeP/XDG")}, {"ConfigDirs App With", "defaultConfigDirs", []string{filepath.Clean("/wrong/path"), filepath.Clean("/some/other/wrong")}, false, "XDG_CONFIG_DIRS", strings.Join([]string{filepath.Clean("/some/path"), filepath.Clean("/some/other/path")}, string(os.PathListSeparator)), MConfigDirs, New("OpenPeeDeeP", "XDG"), []string{filepath.Clean("/some/path/OpenPeeDeeP/XDG"), filepath.Clean("/some/other/path/OpenPeeDeeP/XDG")}}, {"CacheHome App With", "defaultCacheHome", filepath.Clean("/wrong/path"), false, "XDG_CACHE_HOME", filepath.Clean("/some/path"), MCacheHome, New("OpenPeeDeeP", "XDG"), filepath.Clean("/some/path/OpenPeeDeeP/XDG")}, } type getterTestCase struct { name string mokedMethod string mockedReturn interface{} calledMocked bool env string envVal string method int xdgApp *XDG expected interface{} } func TestXDG_Getters(t *testing.T) { for _, tc := range getterTestCases { t.Run(tc.name, func(t *testing.T) { assert := assert.New(t) mockDef := new(mockDefaulter) mockDef.On(tc.mokedMethod).Return(tc.mockedReturn) setDefaulter(mockDef) os.Setenv(tc.env, tc.envVal) // nolint: errcheck actual := computeActual(tc) if tc.calledMocked { mockDef.AssertExpectations(t) } else { mockDef.AssertNotCalled(t, tc.mokedMethod) } assert.Equal(tc.expected, actual) }) } } // nolint: gocyclo func computeActual(tc getterTestCase) interface{} { var actual interface{} switch tc.method { case MDataHome: if tc.xdgApp != nil { actual = tc.xdgApp.DataHome() } else { actual = DataHome() } case MDataDirs: if tc.xdgApp != nil { actual = tc.xdgApp.DataDirs() } else { actual = DataDirs() } case MConfigHome: if tc.xdgApp != nil { actual = tc.xdgApp.ConfigHome() } else { actual = ConfigHome() } case MConfigDirs: if tc.xdgApp != nil { actual = tc.xdgApp.ConfigDirs() } else { actual = ConfigDirs() } case MCacheHome: if tc.xdgApp != nil { actual = tc.xdgApp.CacheHome() } else { actual = CacheHome() } } return actual } const ( QData = iota QConfig QCache ) var ( root = "testingFolder" fileTypes = []string{"data", "config", "cache"} fileLoc = []string{"home", "dirs"} ) type queryTestCase struct { name string xdgApp *XDG queryType int filename string expected string } var queryTestCases = []queryTestCase{ {"Data Dirs", New("OpenPeeDeeP", "XDG"), QData, "XDG_DATA_DIRS.txt", filepath.Clean("/data/dirs/OpenPeeDeeP/XDG/XDG_DATA_DIRS.txt")}, {"Data Home", New("OpenPeeDeeP", "XDG"), QData, "XDG_DATA_HOME.txt", filepath.Clean("/data/home/OpenPeeDeeP/XDG/XDG_DATA_HOME.txt")}, {"Data DNE", New("OpenPeeDeeP", "XDG"), QData, "XDG_CONFIG_HOME.txt", ""}, {"Config Dirs", New("OpenPeeDeeP", "XDG"), QConfig, "XDG_CONFIG_DIRS.txt", filepath.Clean("/config/dirs/OpenPeeDeeP/XDG/XDG_CONFIG_DIRS.txt")}, {"Config Home", New("OpenPeeDeeP", "XDG"), QConfig, "XDG_CONFIG_HOME.txt", filepath.Clean("/config/home/OpenPeeDeeP/XDG/XDG_CONFIG_HOME.txt")}, {"Config DNE", New("OpenPeeDeeP", "XDG"), QConfig, "XDG_DATA_HOME.txt", ""}, {"Cache Home", New("OpenPeeDeeP", "XDG"), QCache, "XDG_CACHE_HOME.txt", filepath.Clean("/cache/home/OpenPeeDeeP/XDG/XDG_CACHE_HOME.txt")}, {"Cache DNE", New("OpenPeeDeeP", "XDG"), QCache, "XDG_CACHE_DIRS.txt", ""}, } func TestXDG_Query(t *testing.T) { for _, tc := range queryTestCases { t.Run(tc.name, func(t *testing.T) { defer teardownQueryData() //nolint: errcheck standupQueryData(tc) //nolint: errcheck assert := assert.New(t) actual := computeQuery(tc) assert.Equal(tc.expected, actual) }) } } func computeQuery(tc queryTestCase) string { var actual string switch tc.queryType { case QData: actual = tc.xdgApp.QueryData(tc.filename) case QCache: actual = tc.xdgApp.QueryCache(tc.filename) case QConfig: actual = tc.xdgApp.QueryConfig(tc.filename) } rootAbs, _ := filepath.Abs(root) actual = strings.Replace(actual, rootAbs, "", 1) return actual } func standupQueryData(tc queryTestCase) error { for _, t := range fileTypes { for _, l := range fileLoc { path, err := filepath.Abs(filepath.Join(root, t, l)) if err != nil { return err } if err = os.MkdirAll(filepath.Join(path, tc.xdgApp.Vendor, tc.xdgApp.Application), 0777); err != nil { return err } envVar := fmt.Sprintf("XDG_%s_%s", strings.ToUpper(t), strings.ToUpper(l)) if err = os.Setenv(envVar, path); err != nil { return err } file, err := os.OpenFile(filepath.Join(path, tc.xdgApp.Vendor, tc.xdgApp.Application, envVar+".txt"), os.O_CREATE|os.O_RDONLY, 0666) if err != nil { return err } defer file.Close() //nolint: errcheck } } return nil } func teardownQueryData() error { return os.RemoveAll(root) } xdg-1.0.0/xdg_windows.go000066400000000000000000000011721356714175300151720ustar00rootroot00000000000000// Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import "os" func (o *osDefaulter) defaultDataHome() string { return os.Getenv("APPDATA") } func (o *osDefaulter) defaultDataDirs() []string { return []string{os.Getenv("PROGRAMDATA")} } func (o *osDefaulter) defaultConfigHome() string { return os.Getenv("APPDATA") } func (o *osDefaulter) defaultConfigDirs() []string { return []string{os.Getenv("PROGRAMDATA")} } func (o *osDefaulter) defaultCacheHome() string { return os.Getenv("LOCALAPPDATA") } xdg-1.0.0/xdg_windows_test.go000066400000000000000000000032161356714175300162320ustar00rootroot00000000000000// +build windows // Copyright (c) 2017, OpenPeeDeeP. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package xdg import ( "os" "testing" "github.com/stretchr/testify/assert" ) func TestDefaultDataHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) appData := "/some/path" expected := appData os.Setenv("APPDATA", appData) // nolint: errcheck actual := defaulter.defaultDataHome() assert.Equal(expected, actual) } func TestDefaultDataDirs(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) programData := "/some/path" expected := []string{programData} os.Setenv("PROGRAMDATA", programData) // nolint: errcheck actual := defaulter.defaultDataDirs() assert.Equal(expected, actual) } func TestDefaultConfigHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) appData := "/some/path" expected := appData os.Setenv("APPDATA", appData) // nolint: errcheck actual := defaulter.defaultConfigHome() assert.Equal(expected, actual) } func TestDefaultConfigDirs(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) programData := "/some/path" expected := []string{programData} os.Setenv("PROGRAMDATA", programData) // nolint: errcheck actual := defaulter.defaultConfigDirs() assert.Equal(expected, actual) } func TestDefaultCacheHome(t *testing.T) { setDefaulter(new(osDefaulter)) assert := assert.New(t) appData := "/some/path" expected := appData os.Setenv("LOCALAPPDATA", appData) // nolint: errcheck actual := defaulter.defaultCacheHome() assert.Equal(expected, actual) }