@@ -57,8 +57,8 @@ func TestUpdateCommandShowsSuccessMessage(t *testing.T) {
5757 runSilentUpdateCheck = func (context.Context ) {}
5858 runUpdateCommand = func (context.Context , updateCommandOptions ) (updater.UpdateResult , error ) {
5959 return updater.UpdateResult {
60- CurrentVersion : "v0 .1.0" ,
61- LatestVersion : "v0 .2.1" ,
60+ CurrentVersion : "\x1b [31mv0 .1.0\x1b [0m " ,
61+ LatestVersion : "\x1b [32mv0 .2.1\x1b [0m \t " ,
6262 Updated : true ,
6363 }, nil
6464 }
@@ -74,6 +74,12 @@ func TestUpdateCommandShowsSuccessMessage(t *testing.T) {
7474 if got := stdout .String (); got == "" || ! bytes .Contains (stdout .Bytes (), []byte ("Updated successfully" )) {
7575 t .Fatalf ("unexpected output: %q" , got )
7676 }
77+ if strings .Contains (stdout .String (), "\x1b " ) {
78+ t .Fatalf ("expected sanitized output without ANSI sequence, got %q" , stdout .String ())
79+ }
80+ if ! strings .Contains (stdout .String (), "v0.1.0 -> v0.2.1" ) {
81+ t .Fatalf ("unexpected output: %q" , stdout .String ())
82+ }
7783}
7884
7985func TestUpdateCommandShowsUnknownLatestWhenLatestVersionEmpty (t * testing.T ) {
@@ -102,6 +108,35 @@ func TestUpdateCommandShowsUnknownLatestWhenLatestVersionEmpty(t *testing.T) {
102108 }
103109}
104110
111+ func TestUpdateCommandSanitizesLatestVersionInUpToDateMessage (t * testing.T ) {
112+ originalRunner := runUpdateCommand
113+ originalPreload := runGlobalPreload
114+ originalSilentCheck := runSilentUpdateCheck
115+ t .Cleanup (func () { runUpdateCommand = originalRunner })
116+ t .Cleanup (func () { runGlobalPreload = originalPreload })
117+ t .Cleanup (func () { runSilentUpdateCheck = originalSilentCheck })
118+
119+ runGlobalPreload = func (context.Context ) error { return nil }
120+ runSilentUpdateCheck = func (context.Context ) {}
121+ runUpdateCommand = func (context.Context , updateCommandOptions ) (updater.UpdateResult , error ) {
122+ return updater.UpdateResult {Updated : false , LatestVersion : "\x1b [31mv0.2.1\x1b [0m\t \n " }, nil
123+ }
124+
125+ command := NewRootCommand ()
126+ var stdout bytes.Buffer
127+ command .SetOut (& stdout )
128+ command .SetArgs ([]string {"update" })
129+ if err := command .ExecuteContext (context .Background ()); err != nil {
130+ t .Fatalf ("ExecuteContext() error = %v" , err )
131+ }
132+ if strings .Contains (stdout .String (), "\x1b " ) {
133+ t .Fatalf ("expected sanitized output without ANSI sequence, got %q" , stdout .String ())
134+ }
135+ if ! strings .Contains (stdout .String (), "latest: v0.2.1" ) {
136+ t .Fatalf ("unexpected output: %q" , stdout .String ())
137+ }
138+ }
139+
105140func TestUpdateCommandReturnsRunnerError (t * testing.T ) {
106141 originalRunner := runUpdateCommand
107142 originalPreload := runGlobalPreload
@@ -216,3 +251,12 @@ func TestDefaultUpdateCommandRunnerReturnsUnderlyingError(t *testing.T) {
216251 t .Fatalf ("expected underlying error %v, got %v" , expected , err )
217252 }
218253}
254+
255+ func TestDisplayVersionForTerminal (t * testing.T ) {
256+ if got := displayVersionForTerminal ("\x1b [31mv0.2.1\x1b [0m\t " ); got != "v0.2.1" {
257+ t .Fatalf ("displayVersionForTerminal() = %q, want %q" , got , "v0.2.1" )
258+ }
259+ if got := displayVersionForTerminal (" \n \t " ); got != "unknown" {
260+ t .Fatalf ("displayVersionForTerminal() empty = %q, want %q" , got , "unknown" )
261+ }
262+ }
0 commit comments