Skip to content

Commit

Permalink
Add CMT_HOME (or remove it?) (cometbft#983)
Browse files Browse the repository at this point in the history
Closes cometbft#982

Added `CMT_HOME` everywhere `CMTHOME` is used.

### Notes to reviewers

This could be fixed the opposite way, by removing the only reference to `CMT_HOME` in the code, and also the reference in `UPGRADING.md` (two lines of code).

However, the reference in `UPGRADING.md`, which is part of our documentation, is already present in `v0.34.x` (not in `v0.37.x` though!). That's why this PR introduces `CMT_HOME` to work in equal conditions as `CMTHOME`.

If reviewers lean toward removing `CMT_HOME` from the doc in `v0.34.x` (and unreleased `v0.38.x` and `main`), I can do it easily.


---

#### PR checklist

- [x] Tests written/updated
- [ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog)
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments
  • Loading branch information
sergio-mena authored Jun 19, 2023
1 parent c0a5715 commit b7be568
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ on instead of `~/.tendermint`.
### Environment variables

The environment variable prefixes have now changed from `TM` to `CMT`. For
example, `TMHOME` or `TM_HOME` become `CMTHOME` or `CMT_HOME`.
example, `TMHOME` becomes `CMTHOME`.

We have implemented a fallback check in case `TMHOME` is still set and `CMTHOME`
is not, but you will start to see a warning message in the logs if the old
Expand Down
58 changes: 25 additions & 33 deletions cmd/cometbft/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,12 @@ import (
cmtos "github.com/cometbft/cometbft/libs/os"
)

var defaultRoot = os.ExpandEnv("$HOME/.some/test/dir")

// clearConfig clears env vars, the given root dir, and resets viper.
func clearConfig(dir string) {
if err := os.Unsetenv("CMTHOME"); err != nil {
panic(err)
}
if err := os.Unsetenv("CMT_HOME"); err != nil {
panic(err)
}
if err := os.Unsetenv("TMHOME"); err != nil {
// XXX: Deprecated.
panic(err)
}
if err := os.Unsetenv("TM_HOME"); err != nil {
// XXX: Deprecated.
panic(err)
}
func clearConfig(t *testing.T, dir string) {
os.Clearenv()
err := os.RemoveAll(dir)
require.NoError(t, err)

if err := os.RemoveAll(dir); err != nil {
panic(err)
}
viper.Reset()
config = cfg.DefaultConfig()
}
Expand All @@ -56,34 +40,39 @@ func testRootCmd() *cobra.Command {
return rootCmd
}

func testSetup(args []string, env map[string]string) error {
clearConfig(defaultRoot)
func testSetup(t *testing.T, root string, args []string, env map[string]string) error {
clearConfig(t, root)

rootCmd := testRootCmd()
cmd := cli.PrepareBaseCmd(rootCmd, "CMT", defaultRoot)
cmd := cli.PrepareBaseCmd(rootCmd, "CMT", root)

// run with the args and env
args = append([]string{rootCmd.Use}, args...)
return cli.RunWithArgs(cmd, args, env)
}

func TestRootHome(t *testing.T) {
newRoot := filepath.Join(defaultRoot, "something-else")
tmpDir := os.TempDir()
root := filepath.Join(tmpDir, "adir")
newRoot := filepath.Join(tmpDir, "something-else")
defer clearConfig(t, root)
defer clearConfig(t, newRoot)

cases := []struct {
args []string
env map[string]string
root string
}{
{nil, nil, defaultRoot},
{nil, nil, root},
{[]string{"--home", newRoot}, nil, newRoot},
{nil, map[string]string{"TMHOME": newRoot}, newRoot}, // XXX: Deprecated.
{nil, map[string]string{"CMTHOME": newRoot}, newRoot},
}

for i, tc := range cases {
idxString := strconv.Itoa(i)
idxString := "idx: " + strconv.Itoa(i)

err := testSetup(tc.args, tc.env)
err := testSetup(t, root, tc.args, tc.env)
require.Nil(t, err, idxString)

assert.Equal(t, tc.root, config.RootDir, idxString)
Expand Down Expand Up @@ -115,8 +104,10 @@ func TestRootFlagsEnv(t *testing.T) {

for i, tc := range cases {
idxString := strconv.Itoa(i)

err := testSetup(tc.args, tc.env)
root := filepath.Join(os.TempDir(), "adir2_"+idxString)
idxString = "idx: " + idxString
defer clearConfig(t, root)
err := testSetup(t, root, tc.args, tc.env)
require.Nil(t, err, idxString)

assert.Equal(t, tc.logLevel, config.LogLevel, idxString)
Expand Down Expand Up @@ -144,10 +135,11 @@ func TestRootConfig(t *testing.T) {

for i, tc := range cases {
idxString := strconv.Itoa(i)
clearConfig(defaultRoot)

root := filepath.Join(os.TempDir(), "adir3_"+idxString)
idxString = "idx: " + idxString
defer clearConfig(t, root)
// XXX: path must match cfg.defaultConfigPath
configFilePath := filepath.Join(defaultRoot, "config")
configFilePath := filepath.Join(root, "config")
err := cmtos.EnsureDir(configFilePath, 0o700)
require.Nil(t, err)

Expand All @@ -157,7 +149,7 @@ func TestRootConfig(t *testing.T) {
require.Nil(t, err)

rootCmd := testRootCmd()
cmd := cli.PrepareBaseCmd(rootCmd, "CMT", defaultRoot)
cmd := cli.PrepareBaseCmd(rootCmd, "CMT", root)

// run with the args and env
tc.args = append([]string{rootCmd.Use}, tc.args...)
Expand Down

0 comments on commit b7be568

Please sign in to comment.