Skip to content

Commit

Permalink
refactor: get rid of mitchellh/go-homedir package (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Dec 24, 2024
1 parent 7998011 commit 4ca2c11
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 1 addition & 2 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/fatih/color"
"github.com/mgechev/revive/config"
"github.com/mgechev/revive/revivelib"
"github.com/mitchellh/go-homedir"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -128,7 +127,7 @@ func buildDefaultConfigPath() string {
configFileName := "revive.toml"
configDirFile := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), configFileName)

if homeDir, err := homedir.Dir(); err == nil {
if homeDir, err := os.UserHomeDir(); err == nil {
homeDirFile = filepath.Join(homeDir, configFileName)
}

Expand Down
16 changes: 12 additions & 4 deletions cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package cli
import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/mitchellh/go-homedir"
"github.com/spf13/afero"
)

func TestMain(m *testing.M) {
os.Unsetenv("HOME")
os.Unsetenv("USERPROFILE")
os.Unsetenv("XDG_CONFIG_HOME")
AppFs = afero.NewMemMapFs()
homedir.DisableCache = true
os.Exit(m.Run())
}

Expand All @@ -32,7 +32,7 @@ func TestXDGConfigDirIsPreferredFirst(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", xdgDirPath)

afero.WriteFile(AppFs, filepath.Join(homeDirPath, "revive.toml"), []byte("\n"), 0644)
t.Setenv("HOME", homeDirPath)
setHome(t, homeDirPath)

got := buildDefaultConfigPath()
want := filepath.Join(xdgDirPath, "revive.toml")
Expand All @@ -48,7 +48,7 @@ func TestHomeConfigDir(t *testing.T) {
AppFs.MkdirAll(homeDirPath, 0755)

afero.WriteFile(AppFs, filepath.Join(homeDirPath, "revive.toml"), []byte("\n"), 0644)
t.Setenv("HOME", homeDirPath)
setHome(t, homeDirPath)

got := buildDefaultConfigPath()
want := filepath.Join(homeDirPath, "revive.toml")
Expand All @@ -58,6 +58,14 @@ func TestHomeConfigDir(t *testing.T) {
}
}

func setHome(t *testing.T, dir string) {
homeEnv := "HOME"
if runtime.GOOS == "windows" {
homeEnv = "USERPROFILE"
}
t.Setenv(homeEnv, dir)
}

func TestXDGConfigDir(t *testing.T) {
t.Cleanup(func() { AppFs = afero.NewMemMapFs() })
xdgDirPath := filepath.FromSlash("/tmp-iofs/xdg/config")
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/fatih/structtag v1.2.0
github.com/hashicorp/go-version v1.7.0
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/afero v1.11.0
golang.org/x/mod v0.22.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6T
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0=
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down

0 comments on commit 4ca2c11

Please # to comment.