diff --git a/cmd/ctrld/cli.go b/cmd/ctrld/cli.go index a5fecf3..22497a7 100644 --- a/cmd/ctrld/cli.go +++ b/cmd/ctrld/cli.go @@ -445,6 +445,8 @@ func initCLI() { func writeConfigFile() error { if cfu := v.ConfigFileUsed(); cfu != "" { defaultConfigFile = cfu + } else if configPath != "" { + defaultConfigFile = configPath } f, err := os.OpenFile(defaultConfigFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0o644)) if err != nil { diff --git a/cmd/ctrld/cli_test.go b/cmd/ctrld/cli_test.go new file mode 100644 index 0000000..23746b7 --- /dev/null +++ b/cmd/ctrld/cli_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_writeConfigFile(t *testing.T) { + tmpdir := t.TempDir() + // simulate --config CLI flag by setting configPath manually. + configPath = filepath.Join(tmpdir, "ctrld.toml") + _, err := os.Stat(configPath) + assert.True(t, os.IsNotExist(err)) + + assert.NoError(t, writeConfigFile()) + + _, err = os.Stat(configPath) + require.NoError(t, err) +}