Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix lint #398

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/protoc-gen-go-test/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"

"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/gotomicro/ego/internal/tools"
orderedmap "github.com/wk8/go-ordered-map"
"golang.org/x/tools/imports"

"github.com/gotomicro/ego/internal/tools"
)

func checkAndMerge(f *file) ([]byte, error) {
origBytes, err := ioutil.ReadFile(f.orig)
origBytes, err := os.ReadFile(f.orig)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("read origFile fail, %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/protoc-gen-go-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"path/filepath"
Expand All @@ -30,7 +30,7 @@ func main() {
if len(os.Args) > 1 {
exit(fmt.Errorf("unknown argument %q (this program should be run by protoc, not directly)", os.Args[1]))
}
in, err := ioutil.ReadAll(os.Stdin)
in, err := io.ReadAll(os.Stdin)
if err != nil {
exit(err)
}
Expand Down
61 changes: 27 additions & 34 deletions core/econf/conf_api_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package econf

import (
"io/ioutil"
"log"
"net/url"
"os"
Expand All @@ -14,7 +13,6 @@ import (

"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type mockDataSource struct {
Expand Down Expand Up @@ -72,11 +70,11 @@ func TestWatchFile(t *testing.T) {
v, configFile, cleanup, wg := newWithConfigFile(t)
defer cleanup()
_, err := os.Stat(configFile)
require.NoError(t, err)
assert.NoError(t, err)
t.Logf("test config file: %s\n", configFile)
// when overwriting the file and waiting for the custom change notification handler to be triggered
err = os.WriteFile(configFile, []byte(`foo= "baz"`), 0640)
require.Nil(t, err)
err1 := os.WriteFile(configFile, []byte(`foo= "baz"`), 0640)
assert.NoError(t, err1)
// wg.Wait()
wg.Done()
// then the config value should have changed
Expand All @@ -92,35 +90,32 @@ func TestWatchFile(t *testing.T) {
// when link to another `config.toml` file
dataDir2 := path.Join(watchDir, "data2")
err := os.Mkdir(dataDir2, 0777)
require.Nil(t, err)
assert.NoError(t, err)
configFile2 := path.Join(dataDir2, "config.toml")
err = os.WriteFile(configFile2, []byte(`foo= "baz"`), 0640)
require.Nil(t, err)
err1 := os.WriteFile(configFile2, []byte(`foo= "baz"`), 0640)
assert.NoError(t, err1)
// change the symlink using the `ln -sfn` command
err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
require.Nil(t, err)
err2 := exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
assert.NoError(t, err2)
wg.Wait()
// then
require.Nil(t, err)
assert.Equal(t, "baz", v.Get("foo"))
})
}

func newWithConfigFile(t *testing.T) (*Configuration, string, func(), *sync.WaitGroup) {
watchDir, err := ioutil.TempDir("", "")
require.Nil(t, err)
var watchDir = os.TempDir()
configFile := path.Join(watchDir, "config.toml")
err = os.WriteFile(configFile, []byte(`foo= "baz"`), 0640)
require.Nil(t, err)
err := os.WriteFile(configFile, []byte(`foo= "baz"`), 0640)
assert.NoError(t, err)
content, err := os.ReadFile(configFile)
if err != nil {
log.Panicf("Error: %v\n", err)
}
t.Logf("Content of configFile: %v\n", string(content))

cleanup := func() {
err1 := os.RemoveAll(watchDir)
assert.NoError(t, err1)
os.RemoveAll(watchDir)
}

v := New()
Expand All @@ -138,34 +133,32 @@ func newWithConfigFile(t *testing.T) (*Configuration, string, func(), *sync.Wait
wg.Done()
})

err = v.LoadFromDataSource(provider, toml.Unmarshal)
assert.Nil(t, err)
err1 := v.LoadFromDataSource(provider, toml.Unmarshal)
assert.NoError(t, err1)
assert.Equal(t, "baz", v.Get("foo"))
return v, configFile, cleanup, wg
}

func newWithSymlinkedConfigFile(t *testing.T) (*Configuration, string, string, func(), *sync.WaitGroup) {
watchDir, err := ioutil.TempDir("", "")
require.Nil(t, err)
watchDir := os.TempDir()
dataDir1 := path.Join(watchDir, "data1")
err = os.Mkdir(dataDir1, 0777)
require.Nil(t, err)
err := os.Mkdir(dataDir1, 0777)
assert.NoError(t, err)
realConfigFile := path.Join(dataDir1, "config.toml")
t.Logf("Real config file location: %s\n", realConfigFile)
err = os.WriteFile(realConfigFile, []byte(`foo= "baz"`), 0640)
require.Nil(t, err)
err1 := os.WriteFile(realConfigFile, []byte(`foo= "baz"`), 0640)
assert.NoError(t, err1)
cleanup := func() {
err1 := os.RemoveAll(watchDir)
assert.NoError(t, err1)
os.RemoveAll(watchDir)
}
// now, symlink the tm `data1` dir to `data` in the baseDir
err = os.Symlink(dataDir1, path.Join(watchDir, "data"))
require.Nil(t, err)
err2 := os.Symlink(dataDir1, path.Join(watchDir, "data"))
assert.NoError(t, err2)

// and link the `<watchdir>/datadir1/config.toml` to `<watchdir>/config.toml`
configFile := path.Join(watchDir, "config.toml")
err = os.Symlink(path.Join(watchDir, "data", "config.toml"), configFile)
require.Nil(t, err)
err3 := os.Symlink(path.Join(watchDir, "data", "config.toml"), configFile)
assert.NoError(t, err3)
t.Logf("Config file location: %s\n", path.Join(watchDir, "config.toml"))

v := New()
Expand All @@ -182,8 +175,8 @@ func newWithSymlinkedConfigFile(t *testing.T) (*Configuration, string, string, f
}
wg.Done()
})
err = v.LoadFromDataSource(provider, toml.Unmarshal)
require.Nil(t, err)
require.Equal(t, "bar", v.Get("foo"))
err4 := v.LoadFromDataSource(provider, toml.Unmarshal)
assert.NoError(t, err4)
assert.Equal(t, "bar", v.Get("foo"))
return v, watchDir, configFile, cleanup, wg
}
122 changes: 122 additions & 0 deletions core/econf/file/file_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,135 @@
package file

import (
"os"
"os/exec"
"path"
"runtime"
"sync"
"sync/atomic"
"testing"

"github.com/stretchr/testify/assert"

"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/econf/manager"
)

func TestWatchFile(t *testing.T) {
if runtime.GOOS == "linux" {
t.Skip("Skip test on Linux ...")
}
t.Run("file content changed", func(t *testing.T) {
// given a `config.yaml` file being watched
v, configFile, cleanup, wg := newWithConfigFile(t)
defer cleanup()
_, err := os.Stat(configFile)
assert.NoError(t, err)
t.Logf("test config file: %s\n", configFile)
// when overwriting the file and waiting for the custom change notification handler to be triggered
err1 := os.WriteFile(configFile, []byte("foo: baz\n"), 0640)
assert.NoError(t, err1)
wg.Wait()
// then the config value should have changed
assert.Equal(t, "baz", v.Get("foo"))
})

t.Run("link to real file changed (Kubernetes)", func(t *testing.T) {
// skip if not executed on Linux
if runtime.GOOS != "linux" {
t.Skipf("Skipping test as symlink replacements don't work on non-linux environment...")
}

v, watchDir, _, _, wg := newWithSymlinkedConfigFile(t)
// defer cleanup()
// when link to another `config.yaml` file
dataDir2 := path.Join(watchDir, "data2")
err := os.Mkdir(dataDir2, 0777)
assert.NoError(t, err)
configFile2 := path.Join(dataDir2, "config.yaml")
err1 := os.WriteFile(configFile2, []byte("foo: baz\n"), 0640)
assert.NoError(t, err1)
// change the symlink using the `ln -sfn` command
err3 := exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
assert.NoError(t, err3)
wg.Wait()
assert.Equal(t, "baz", v.Get("foo"))
})
}

func newWithConfigFile(t *testing.T) (*econf.Configuration, string, func(), *sync.WaitGroup) {
watchDir := os.TempDir()
configFile := path.Join(watchDir, "config.yaml")
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0640)
assert.NoError(t, err)
cleanup := func() {
os.RemoveAll(watchDir)
}
v := econf.New()
provider, parser, tag, err1 := manager.NewDataSource(configFile, true)
assert.NoError(t, err1)

wg := &sync.WaitGroup{}
wg.Add(2)
var init int64
v.OnChange(func(configuration *econf.Configuration) {
if atomic.CompareAndSwapInt64(&init, 0, 1) {
t.Logf("config init")
} else {
t.Logf("config file changed")
}
wg.Done()
})
err2 := v.LoadFromDataSource(provider, parser, econf.WithTagName(tag))
assert.NoError(t, err2)
assert.Equal(t, "bar", v.Get("foo"))
return v, configFile, cleanup, wg
}

func newWithSymlinkedConfigFile(t *testing.T) (*econf.Configuration, string, string, func(), *sync.WaitGroup) {
watchDir := os.TempDir()
dataDir1 := path.Join(watchDir, "data1")
err := os.Mkdir(dataDir1, 0777)
assert.NoError(t, err)
realConfigFile := path.Join(dataDir1, "config.yaml")
t.Logf("Real config file location: %s\n", realConfigFile)
err1 := os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0640)
assert.NoError(t, err1)
cleanup := func() {
os.RemoveAll(watchDir)
}
// now, symlink the tm `data1` dir to `data` in the baseDir
err2 := os.Symlink(dataDir1, path.Join(watchDir, "data"))
assert.NoError(t, err2)

// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
configFile := path.Join(watchDir, "config.yaml")
err3 := os.Symlink(path.Join(watchDir, "data", "config.yaml"), configFile)
assert.NoError(t, err3)

t.Logf("Config file location: %s\n", path.Join(watchDir, "config.yaml"))

v := econf.New()
provider, parser, tag, err4 := manager.NewDataSource(configFile, true)
assert.NoError(t, err4)

wg := &sync.WaitGroup{}
wg.Add(2)
var init int64
v.OnChange(func(configuration *econf.Configuration) {
if atomic.CompareAndSwapInt64(&init, 0, 1) {
t.Logf("config init")
} else {
t.Logf("config file changed")
}
wg.Done()
})
err5 := v.LoadFromDataSource(provider, parser, econf.WithTagName(tag))
assert.NoError(t, err5)
assert.Equal(t, "bar", v.Get("foo"))
return v, watchDir, configFile, cleanup, wg
}

func TestParse(t *testing.T) {
cases := []struct {
in string
Expand Down
17 changes: 7 additions & 10 deletions core/econf/options_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package econf

import (
"io/ioutil"
"os"
"path"
"testing"

"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWithTagName(t *testing.T) {
watchDir, err := ioutil.TempDir("", "")
require.Nil(t, err)
watchDir := os.TempDir()
configFile := path.Join(watchDir, "config.toml")
err = os.WriteFile(configFile, []byte(`foo= "baz"`), 0640)
require.Nil(t, err)
err := os.WriteFile(configFile, []byte(`foo= "baz"`), 0640)
assert.NoError(t, err)
defer func() {
os.RemoveAll(configFile)
}()
v := New()
provider := newMockDataSource(configFile, true)

err = v.LoadFromDataSource(provider, toml.Unmarshal, WithTagName("toml"), WithWeaklyTypedInput(true))
require.Nil(t, err)
err1 := v.LoadFromDataSource(provider, toml.Unmarshal, WithTagName("toml"), WithWeaklyTypedInput(true))
assert.NoError(t, err1)
assert.Equal(t, "toml", GetOptionTagName())
assert.Equal(t, true, GetOptionWeaklyTypedInput())

err = v.LoadFromDataSource(provider, toml.Unmarshal, WithSquash(true))
require.Nil(t, err)
err2 := v.LoadFromDataSource(provider, toml.Unmarshal, WithSquash(true))
assert.NoError(t, err2)
}
Loading
Loading