From 53d44a65e582def91a63b538a956dc6a4f7b4974 Mon Sep 17 00:00:00 2001 From: Chetan Giradkar Date: Thu, 20 Jul 2023 20:21:04 +0100 Subject: [PATCH] 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 --- pkg/env/env.go | 1 - pkg/specgen/generate/container.go | 6 ++++++ test/system/030-run.bats | 11 ++++++++++- test/system/700-play.bats | 1 - 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/env/env.go b/pkg/env/env.go index bd4bed0f21..80b13ed684 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -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", } } diff --git a/pkg/specgen/generate/container.go b/pkg/specgen/generate/container.go index c43d61f821..a9ce054006 100644 --- a/pkg/specgen/generate/container.go +++ b/pkg/specgen/generate/container.go @@ -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 { diff --git a/test/system/030-run.bats b/test/system/030-run.bats index 9bcfc3e2d0..e1e47d4c8a 100644 --- a/test/system/030-run.bats +++ b/test/system/030-run.bats @@ -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" diff --git a/test/system/700-play.bats b/test/system/700-play.bats index bc2f69a0e6..2c3b0b6cf0 100644 --- a/test/system/700-play.bats +++ b/test/system/700-play.bats @@ -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