add more tests

This commit is contained in:
Vladyslav Yurchenko
2026-01-29 15:22:26 +02:00
parent ecf43ff38c
commit 14324ff374
2 changed files with 138 additions and 2 deletions
+4 -2
View File
@@ -313,6 +313,8 @@ func loadConfigFiles(configPaths []string, specifiedConfig bool) ([]kong.Resolve
return resolvers, filePassthrough, nil
}
var GLOBAL_DEFAULT_PATH = "/etc/percona-toolkit/percona-toolkit.conf"
// getDefaultPaths returns the default configuration file paths for a tool.
// Returns paths in order of precedence (lowest to highest):
// 1. /etc/percona-toolkit/percona-toolkit.conf
@@ -323,13 +325,13 @@ func getDefaultPaths(toolName string) []string {
u, err := user.Current()
if err != nil {
return []string{
"/etc/percona-toolkit/percona-toolkit.conf",
GLOBAL_DEFAULT_PATH,
fmt.Sprintf("/etc/percona-toolkit/%s.conf", toolName),
}
}
return []string{
"/etc/percona-toolkit/percona-toolkit.conf",
GLOBAL_DEFAULT_PATH,
fmt.Sprintf("/etc/percona-toolkit/%s.conf", toolName),
filepath.Join(u.HomeDir, ".percona-toolkit.conf"),
filepath.Join(u.HomeDir, fmt.Sprintf(".%s.conf", toolName)),
+134
View File
@@ -2,6 +2,7 @@ package config
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/user"
@@ -433,6 +434,139 @@ cycles=2
}
}
func TestCmdWithArgs(t *testing.T) {
tests := []struct {
name string
args []string
cli any
wantJson string
}{
{
name: "cmd_one_arg",
args: []string{"test-cmd", "file.txt"},
cli: &struct {
TestCmd struct {
Paths []string `arg:"" name:"paths"`
} `cmd:"" name:"test-cmd"`
}{},
wantJson: `{"TestCmd":{"Paths":["file.txt"]}}`,
},
{
name: "cmd_one_path_arg",
args: []string{"test-cmd", "tests/logs/upgrade/node1.log"},
cli: &struct {
TestCmd struct {
Paths []string `arg:"" name:"paths"`
} `cmd:"" name:"test-cmd"`
}{},
wantJson: `{"TestCmd":{"Paths":["tests/logs/upgrade/node1.log"]}}`,
},
{
name: "cmd_many_arg",
args: []string{"test-cmd", "file.txt", "file2.txt", "file3.txt"},
cli: &struct {
TestCmd struct {
Paths []string `arg:"" name:"paths"`
} `cmd:"" name:"test-cmd"`
}{},
wantJson: `{"TestCmd":{"Paths":["file.txt","file2.txt","file3.txt"]}}`,
},
}
for _, test := range tests {
os.Args = []string{test.name}
os.Args = append(os.Args, test.args...)
t.Log(os.Args)
_, _, err := Setup(test.name, test.cli)
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(test.cli)
if err != nil {
t.Fatal(err)
}
if string(data) != test.wantJson {
t.Errorf("got %s, want %s", string(data), test.wantJson)
}
}
}
func TestCmdWithArgsAndConfig(t *testing.T) {
tests := []struct {
name string
args []string
cli any
config string
wantJson string
}{
{
name: "cmd_one_arg",
args: []string{"test-cmd", "file.txt"},
config: `no-version`,
cli: &struct {
TestCmd struct {
Paths []string `arg:"" name:"paths"`
} `cmd:"" name:"test-cmd"`
Version bool `negatable:"" default:"true" name:"version"`
}{},
wantJson: `{"TestCmd":{"Paths":["file.txt"]},"Version":false}`,
},
{
name: "cmd_one_arg",
args: []string{"test-cmd", "file.txt"},
config: `test-list=a,b,c`,
cli: &struct {
TestCmd struct {
Paths []string `arg:"" name:"paths"`
} `cmd:"" name:"test-cmd"`
TestList []string `name:"test-list"`
}{},
wantJson: `{"TestCmd":{"Paths":["file.txt"]},"TestList":["a","b","c"]}`,
},
{
name: "cmd_one_arg",
args: []string{"test-cmd", "file.txt"},
config: `test-list=a,b,c
limit=123`,
cli: &struct {
TestCmd struct {
Paths []string `arg:"" name:"paths"`
Limit int `name:"limit"`
} `cmd:"" name:"test-cmd"`
TestList []string `name:"test-list"`
}{},
wantJson: `{"TestCmd":{"Paths":["file.txt"],"Limit":123},"TestList":["a","b","c"]}`,
},
}
var oldGlobalDefaultPath = GLOBAL_DEFAULT_PATH
defer func() {
GLOBAL_DEFAULT_PATH = oldGlobalDefaultPath
}()
for _, test := range tests {
tmpDir := t.TempDir()
tmpConf := filepath.Join(tmpDir, "test.conf")
os.WriteFile(tmpConf, []byte(test.config), 0644)
GLOBAL_DEFAULT_PATH = tmpConf
os.Args = []string{test.name}
os.Args = append(os.Args, test.args...)
t.Log(os.Args)
_, _, err := Setup(test.name, test.cli)
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(test.cli)
if err != nil {
t.Fatal(err)
}
if string(data) != test.wantJson {
t.Errorf("got %s, want %s", string(data), test.wantJson)
}
}
}
func TestParseConfigFlag(t *testing.T) {
tests := []struct {
name string