-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added module that adds 'tt' and 'tarantool' (installed using 'tt') to the current environment. Closes #338
- Loading branch information
1 parent
24ecbdf
commit b2d1358
Showing
7 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/tarantool/tt/cli/cmdcontext" | ||
"github.com/tarantool/tt/cli/env" | ||
"github.com/tarantool/tt/cli/modules" | ||
) | ||
|
||
// NewEnvCmd creates env command. | ||
func NewEnvCmd() *cobra.Command { | ||
var envCmd = &cobra.Command{ | ||
Use: "env", | ||
Short: "Add current environment binaries location to the PATH variable", | ||
Long: "Add current environment binaries location to the PATH variable.\n" + | ||
"Also sets TARANTOOL_DIR variable.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, | ||
internalEnvModule, args) | ||
handleCmdErr(cmd, err) | ||
}, | ||
} | ||
|
||
return envCmd | ||
} | ||
|
||
// internalEnvModule is a default env module. | ||
func internalEnvModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { | ||
var err error | ||
_, err = fmt.Print(env.CreateEnvString(cliOpts)) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package env | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/tarantool/tt/cli/config" | ||
) | ||
|
||
// CreateEnvString generates environment variables for 'tarantool' and 'tt' installed using 'tt'. | ||
func CreateEnvString(cliOpts *config.CliOpts) string { | ||
binDir := cliOpts.Env.BinDir | ||
path := os.Getenv("PATH") | ||
if !strings.Contains(path, binDir) { | ||
path = binDir + ":" + path | ||
} | ||
|
||
tarantoolDir := filepath.Join(cliOpts.Env.IncludeDir, "include") | ||
|
||
return fmt.Sprintf("export %s=%s\nexport %s=%s\n", "PATH", path, "TARANTOOL_DIR", tarantoolDir) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package env | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tarantool/tt/cli/configure" | ||
) | ||
|
||
func Test_CreateEnvString(t *testing.T) { | ||
cliOpts := configure.GetDefaultCliOpts() | ||
cliOpts.Env.BinDir = "foo/bin/" | ||
cliOpts.Env.IncludeDir = "bar/include/" | ||
assert.Contains(t, CreateEnvString(cliOpts), | ||
"\nexport TARANTOOL_DIR=bar/include/include\n") | ||
assert.Contains(t, CreateEnvString(cliOpts), | ||
"export PATH=foo/bin/:") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
import re | ||
import subprocess | ||
|
||
from utils import config_name | ||
|
||
|
||
def test_env_output(tt_cmd, tmpdir): | ||
configPath = os.path.join(tmpdir, config_name) | ||
# Create test config | ||
with open(configPath, 'w') as f: | ||
f.write('env:\n bin_dir:\n inc_dir:\n') | ||
binDir = str(tmpdir + "/bin") | ||
tarantoolDir = "TARANTOOL_DIR=" + str(tmpdir + "/include/include") | ||
|
||
env_cmd = [tt_cmd, "env"] | ||
instance_process = subprocess.Popen( | ||
env_cmd, | ||
cwd=tmpdir, | ||
stderr=subprocess.STDOUT, | ||
stdout=subprocess.PIPE, | ||
text=True | ||
) | ||
|
||
# Check that the process shutdowned correctly. | ||
instance_process_rc = instance_process.wait() | ||
assert instance_process_rc == 0 | ||
|
||
# Check output | ||
output = instance_process.stdout.read() | ||
assert re.search(tarantoolDir, output) | ||
assert re.search(binDir, output) |