Skip to content

Commit

Permalink
refactor: Remove convery and assert (#75)
Browse files Browse the repository at this point in the history
* refactor: Remove convery and assert

Signed-off-by: Xuanwo <github@xuanwo.io>

* format code

Signed-off-by: Xuanwo <github@xuanwo.io>

---------

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored Aug 4, 2024
1 parent 9a916f3 commit cbe0365
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 341 deletions.
11 changes: 0 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ module github.com/Xuanwo/go-locale
go 1.17

require (
github.com/smartystreets/goconvey v1.7.2
github.com/stretchr/testify v1.9.0
golang.org/x/sys v0.22.0
golang.org/x/text v0.16.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/smartystreets/assertions v1.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
29 changes: 0 additions & 29 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.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/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
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/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand All @@ -32,7 +10,6 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down Expand Up @@ -78,15 +55,9 @@ golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
50 changes: 26 additions & 24 deletions locale_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var detectors = []detector{
// - global AppleLanguages
//
// ref:
// - Apple Developer Guide: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html
// - Homebrew: https://github.com/Homebrew/brew/pull/7940
// - Apple Developer Guide: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html
// - Homebrew: https://github.com/Homebrew/brew/pull/7940
func detectViaDefaultsSystem() ([]string, error) {
// Read user's apple locale setting.
m, err := parseDefaultsSystemAppleLocale("-g")
Expand Down Expand Up @@ -73,28 +73,30 @@ func parseDefaultsSystemAppleLocale(domain string) ([]string, error) {
// Output should be like:
//
// (
// en,
// ja,
// fr,
// de,
// es,
// it,
// pt,
// "pt-PT",
// nl,
// sv,
// nb,
// da,
// fi,
// ru,
// pl,
// "zh-Hans",
// "zh-Hant",
// ko,
// ar,
// cs,
// hu,
// tr
//
// en,
// ja,
// fr,
// de,
// es,
// it,
// pt,
// "pt-PT",
// nl,
// sv,
// nb,
// da,
// fi,
// ru,
// pl,
// "zh-Hans",
// "zh-Hant",
// ko,
// ar,
// cs,
// hu,
// tr
//
// )
func parseDefaultsSystemAppleLanguages(domain string) ([]string, error) {
cmd := exec.Command("defaults", "read", domain, "AppleLanguages")
Expand Down
20 changes: 8 additions & 12 deletions locale_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package locale

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestDetectViaUserDefaultsSystem(t *testing.T) {
Convey("detect via user defaults system", t, func() {
langs, err := detectViaDefaultsSystem()
langs, err := detectViaDefaultsSystem()

t.Logf("langs: %v", langs)
Convey("The error should not be nil", func() {
So(err, ShouldBeNil)
})
Convey("The langs should not be empty", func() {
So(langs, ShouldNotBeEmpty)
})
})
t.Logf("langs: %v", langs)
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}
if len(langs) == 0 {
t.Error("Expected non-empty langs, got empty slice")
}
}
144 changes: 60 additions & 84 deletions locale_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,95 +7,71 @@ import (
"os"
"path"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestGetLocaleConfPath(t *testing.T) {
Convey("get locale conf path", t, func() {
// Make sure env has clear before current test.
setupEnv()

Reset(func() {
// Reset all env after every Convey.
setupEnv()
})

Convey("When user set XDG_CONFIG_HOME", func() {
tmpDir := setupLocaleConf("locale.conf")
Reset(func() {
_ = os.RemoveAll(tmpDir)
})

err := os.Setenv("XDG_CONFIG_HOME", tmpDir)
if err != nil {
t.Error(err)
}

fp := getLocaleConfPath()

Convey("The path should be equal", func() {
So(fp, ShouldEqual, path.Join(tmpDir, "locale.conf"))
})
})

Convey("When user set HOME", func() {
tmpDir := setupLocaleConf(".config/locale.conf")
Reset(func() {
_ = os.RemoveAll(tmpDir)
})

err := os.Setenv("HOME", tmpDir)
if err != nil {
t.Error(err)
}

fp := getLocaleConfPath()

Convey("The path should be equal", func() {
So(fp, ShouldEqual, path.Join(tmpDir, ".config/locale.conf"))
})
})

Convey("When fallback to system level locale.conf", func() {
var localeExist bool
_, err := os.Stat("/etc/locale.conf")
if err == nil {
localeExist = true
}
func TestXDGConfigHome(t *testing.T) {
setupEnv()
tmpDir := setupLocaleConf("locale.conf")
defer os.RemoveAll(tmpDir)

err := os.Setenv("XDG_CONFIG_HOME", tmpDir)
if err != nil {
t.Fatal(err)
}

fp := getLocaleConfPath()
expected := path.Join(tmpDir, "locale.conf")
if fp != expected {
t.Errorf("Expected path %s, got %s", expected, fp)
}
}

fp := getLocaleConfPath()
func TestHOME(t *testing.T) {
setupEnv()
tmpDir := setupLocaleConf(".config/locale.conf")
defer os.RemoveAll(tmpDir)

err := os.Setenv("HOME", tmpDir)
if err != nil {
t.Fatal(err)
}

fp := getLocaleConfPath()
expected := path.Join(tmpDir, ".config/locale.conf")
if fp != expected {
t.Errorf("Expected path %s, got %s", expected, fp)
}
}

Convey("The path should be equal", func() {
So(fp == "/etc/locale.conf", ShouldEqual, localeExist)
})
})
})
func TestFallbackToSystem(t *testing.T) {
setupEnv()
var localeExist bool
_, err := os.Stat("/etc/locale.conf")
if err == nil {
localeExist = true
}

fp := getLocaleConfPath()
if (fp == "/etc/locale.conf") != localeExist {
t.Errorf("Expected path to be /etc/locale.conf: %v, got: %s", localeExist, fp)
}
}

func TestDetectViaLocaleConf(t *testing.T) {
Convey("detect via locale conf", t, func() {
setupEnv()
Reset(func() {
setupEnv()
})

tmpDir := setupLocaleConf("locale.conf")
Reset(func() {
_ = os.RemoveAll(tmpDir)
})
err := os.Setenv("XDG_CONFIG_HOME", tmpDir)
if err != nil {
t.Error(err)
}

lang, err := detectViaLocaleConf()

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The lang should not be empty", func() {
So(lang, ShouldNotBeEmpty)
})
})
setupEnv()
tmpDir := setupLocaleConf("locale.conf")
defer os.RemoveAll(tmpDir)

err := os.Setenv("XDG_CONFIG_HOME", tmpDir)
if err != nil {
t.Fatal(err)
}

lang, err := detectViaLocaleConf()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(lang) == 0 {
t.Error("Expected non-empty lang, got empty string")
}
}
Loading

0 comments on commit cbe0365

Please sign in to comment.