Check tty flag to set default terminal in Env

First, all the defaults for TERM=xterm were removed from c/common, then accordingly the same will be added if encountered a set tty flag.

Signed-off-by: Chetan Giradkar <cgiradka@redhat.com>
This commit is contained in:
Chetan Giradkar
2023-07-20 20:21:04 +01:00
parent cd5ce63724
commit 53d44a65e5
4 changed files with 16 additions and 3 deletions

1
pkg/env/env.go vendored
View File

@ -32,7 +32,6 @@ const whiteSpaces = " \t"
func DefaultEnvVariables() map[string]string {
return map[string]string{
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TERM": "xterm",
"container": "podman",
}
}

View File

@ -130,6 +130,12 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
defaultEnvs = envLib.Join(envLib.DefaultEnvVariables(), envLib.Join(defaultEnvs, envs))
}
// add default terminal to env if tty flag is set
_, ok := defaultEnvs["TERM"]
if s.Terminal && !ok {
defaultEnvs["TERM"] = "xterm"
}
for _, e := range s.EnvMerge {
processedWord, err := imagebuilder.ProcessWord(e, envLib.Slice(defaultEnvs))
if err != nil {

View File

@ -818,9 +818,18 @@ EOF
@test "podman run defaultenv" {
run_podman run --rm $IMAGE printenv
assert "$output" =~ "TERM=xterm" "env includes TERM"
assert "$output" !~ "TERM=" "env doesn't include TERM by default"
assert "$output" =~ "container=podman" "env includes container=podman"
run_podman 1 run -t=false --rm $IMAGE printenv TERM
assert "$output" == "" "env doesn't include TERM"
run_podman run -t=true --rm $IMAGE printenv TERM # uses CRLF terminators
assert "$output" == $'xterm\r' "env includes default TERM"
run_podman run -t=false -e TERM=foobar --rm $IMAGE printenv TERM
assert "$output" == "foobar" "env includes TERM"
run_podman run --unsetenv=TERM --rm $IMAGE printenv
assert "$output" =~ "container=podman" "env includes container=podman"
assert "$output" != "TERM" "unwanted TERM environment variable despite --unsetenv=TERM"

View File

@ -372,7 +372,6 @@ _EOF
is "$output" bin "expect container within pod to run as the bin user"
run_podman inspect --format "{{ .Config.Env }}" test_pod-test
is "$output" ".*PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.*" "expect PATH to be set"
is "$output" ".*TERM=xterm.*" "expect TERM to be set"
is "$output" ".*container=podman.*" "expect container to be set"
run_podman stop -a -t 0