Skip to content

Commit

Permalink
Fix EnsureDestination tests so it takes a temporary directory as a re…
Browse files Browse the repository at this point in the history
…ference instead of the home directory. Now it can be safely removed after the test is done.
  • Loading branch information
Marcelo committed Jul 24, 2024
1 parent bf4d79e commit fda0c88
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
31 changes: 25 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"reflect"

"github.com/prashantv/gostub"
"github.com/stretchr/testify/mock"
. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -299,42 +300,60 @@ func (cs *ConfigSuite) Test_doAfterSave_addFunctionToList(c *C) {
}

func (cs *ConfigSuite) Test_EnsureDestination_createsEncryptedConfigFile(c *C) {
tempDir, err := os.MkdirTemp("", "test")
c.Assert(err, IsNil)
defer os.RemoveAll(tempDir)

defer gostub.New().Stub(&XdgConfigHome, func() string { return tempDir }).Reset()

a := &ApplicationConfig{
filename: "",
encryptedFile: true,
}

a.EnsureDestination()
fakeEncryptedConfigFile := filepath.Join(SystemConfigDir(), "wahay", "config.axx")

c.Assert(a.filename, Equals, fakeEncryptedConfigFile)
expectedEncryptedConfigFile := filepath.Join(tempDir, "wahay", appEncryptedConfigFile)

c.Assert(a.filename, Equals, expectedEncryptedConfigFile)
}

func (cs *ConfigSuite) Test_EnsureDestination_createsUnencryptedConfigFile(c *C) {
tempDir, err := os.MkdirTemp("", "test")
c.Assert(err, IsNil)
defer os.RemoveAll(tempDir)

defer gostub.New().Stub(&XdgConfigHome, func() string { return tempDir }).Reset()

a := &ApplicationConfig{
filename: "",
encryptedFile: false,
}

a.EnsureDestination()
fakeUnencryptedConfigFile := filepath.Join(SystemConfigDir(), "wahay", "config.json")

c.Assert(a.filename, Equals, fakeUnencryptedConfigFile)
expectedEncryptedConfigFile := filepath.Join(tempDir, "wahay", appConfigFile)

c.Assert(a.filename, Equals, expectedEncryptedConfigFile)
}

func (cs *ConfigSuite) Test_EnsureDestination_changesFileSuffixToAValidEncryptedSuffix(c *C) {
tempDir, err := os.MkdirTemp("", "test")
c.Assert(err, IsNil)
defer os.RemoveAll(tempDir)

defer gostub.New().Stub(&XdgConfigHome, func() string { return tempDir }).Reset()

a := &ApplicationConfig{
filename: "config.json",
encryptedFile: true,
}

a.EnsureDestination()
fakeEncryptedConfigFile := filepath.Join(SystemConfigDir(), "wahay", "config.axx")

c.Assert(a.filename, Equals, fakeEncryptedConfigFile)
expectedEncryptedConfigFile := filepath.Join(tempDir, "wahay", appEncryptedConfigFile)

c.Assert(a.filename, Equals, expectedEncryptedConfigFile)
}

type MockKeySupplier struct {
Expand Down
7 changes: 5 additions & 2 deletions config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ func xdgOrWithHome(env, or string) string {
return os.Getenv(env)
}

// XdgConfigHome returns the standardized XDG Configuration directory
func XdgConfigHome() string {
type XdgConfigHomeFunc func() string

var XdgConfigHome XdgConfigHomeFunc = xdgConfigHome

func xdgConfigHome() string {
return xdgOrWithHome("XDG_CONFIG_HOME", ".config")
}

Expand Down

0 comments on commit fda0c88

Please sign in to comment.