From 3859406a88f47d39a2a473aed0506341d982b865 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 21 Mar 2024 17:13:04 +0100 Subject: [PATCH 1/3] Handle detection of upgraded shell --- internal/stack/shellinit.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/stack/shellinit.go b/internal/stack/shellinit.go index 2e22e1a693..a3db128a6e 100644 --- a/internal/stack/shellinit.go +++ b/internal/stack/shellinit.go @@ -109,8 +109,16 @@ func helpText(shell string) string { func getShellName(exe string) string { shell := filepath.Base(exe) - // NOTE: remove .exe extension from executable names present in Windows - shell = strings.TrimSuffix(shell, ".exe") + cleanSuffixes := []string{ + // Remove .exe extension from executable names present in Windows. + ".exe", + // Remove " (deleted)", that can appear here if the shell process has been + // replaced by an upgrade in Linux while the terminal was open. + " (deleted)", + } + for _, suffix := range cleanSuffixes { + shell = strings.TrimSuffix(shell, suffix) + } return shell } From 1a55c50303b2bbab1933871b5901a591c95c4bd7 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 21 Mar 2024 17:37:40 +0100 Subject: [PATCH 2/3] Add test cases --- internal/stack/shellinit_internal_test.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/stack/shellinit_internal_test.go b/internal/stack/shellinit_internal_test.go index 80c9114cad..27cfc1f8f1 100644 --- a/internal/stack/shellinit_internal_test.go +++ b/internal/stack/shellinit_internal_test.go @@ -5,6 +5,7 @@ package stack import ( + "path/filepath" "strings" "testing" @@ -44,13 +45,22 @@ func Test_getShellName(t *testing.T) { type args struct { exe string } - tests := []struct { + type testCase struct { name string args args want string - }{ - {"linux exec", args{exe: "bash"}, "bash"}, - {"windows exec", args{exe: "cmd.exe"}, "cmd"}, + } + var tests []testCase + if filepath.Separator == '\\' { + tests = []testCase{ + {"windows exec", args{exe: `C:\Windows\System32\cmd.exe`}, "cmd"}, + } + } else { + tests = []testCase{ + {"linux exec", args{exe: "/usr/bin/bash"}, "bash"}, + {"linux upgraded exec", args{exe: "/usr/bin/bash (deleted)"}, "bash"}, + {"windows exec", args{exe: `C:/Windows/System32/cmd.exe`}, "cmd"}, + } } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 05d534e85e0c3b2071bba6abcc0c4c964cd83303 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 21 Mar 2024 17:41:09 +0100 Subject: [PATCH 3/3] Keep old test case for windows --- internal/stack/shellinit_internal_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/stack/shellinit_internal_test.go b/internal/stack/shellinit_internal_test.go index 27cfc1f8f1..cedc3a8b25 100644 --- a/internal/stack/shellinit_internal_test.go +++ b/internal/stack/shellinit_internal_test.go @@ -53,12 +53,14 @@ func Test_getShellName(t *testing.T) { var tests []testCase if filepath.Separator == '\\' { tests = []testCase{ + {"windows relative path", args{exe: `cmd.exe`}, "cmd"}, // Not sure if this case is real. {"windows exec", args{exe: `C:\Windows\System32\cmd.exe`}, "cmd"}, } } else { tests = []testCase{ {"linux exec", args{exe: "/usr/bin/bash"}, "bash"}, {"linux upgraded exec", args{exe: "/usr/bin/bash (deleted)"}, "bash"}, + {"windows relative path", args{exe: `cmd.exe`}, "cmd"}, // Not sure if this case is real. {"windows exec", args{exe: `C:/Windows/System32/cmd.exe`}, "cmd"}, } }