@@ -13,69 +13,79 @@ func Test_cleanPtySequences(t *testing.T) {
1313 cursorPos int
1414 want []byte
1515 wantCursorPos int
16+ wantCleanUpto int
1617 }{
1718 {
1819 "Window title, cursor after" ,
1920 []byte ("\u001B ]0;C:\\ Users\\ RUNNER~1\\ AppData\\ Local\\ Temp\\ 2642502767\\ cache\\ 94dd3fa4\\ exec\\ python3.exe\u0007 Hello" ),
2021 86 , // First two characters of Hello
2122 []byte ("Hello" ),
2223 2 ,
24+ 5 ,
2325 },
2426 {
2527 "Window title, cursor preceding" ,
2628 []byte ("Hello\u001B ]0;C:\\ Users\\ RUNNER~1\\ AppData\\ Local\\ Temp\\ 2642502767\\ cache\\ 94dd3fa4\\ exec\\ python3.exe\u0007 World" ),
2729 1 , // First two characters of Hello
2830 []byte ("HelloWorld" ),
2931 1 ,
32+ 10 ,
3033 },
3134 {
3235 "Window title, cursor on top" ,
3336 []byte ("Hello\u001B ]0;C:\\ Users\\ RUNNER~1\\ AppData\\ Local\\ Temp\\ 2642502767\\ cache\\ 94dd3fa4\\ exec\\ python3.exe\u0007 World" ),
3437 10 , // Inside title escape sequence
3538 []byte ("HelloWorld" ),
3639 4 ,
40+ 10 ,
3741 },
3842 {
3943 "Backspace character" ,
4044 []byte ("Foo \u0008 Bar" ),
4145 7 , // End of string
4246 []byte ("FooBar" ),
4347 5 ,
48+ 6 ,
4449 },
4550 {
4651 "Backspace character, cursor on top of backspace" ,
4752 []byte ("Foo \u0008 Bar" ),
4853 5 , // End of string
4954 []byte ("FooBar" ),
5055 3 ,
56+ 6 ,
5157 },
5258 {
5359 "Cursor position preceding cleaned sequence" ,
5460 []byte ("Foo\u001B [1mBar" ), // \u001B[1m = bold
5561 2 , // End of "Foo"
5662 []byte ("FooBar" ),
5763 2 ,
64+ 6 ,
5865 },
5966 {
6067 "Cursor position succeeding cleaned sequence" ,
6168 []byte ("Foo\u001B [1mBar" ), // \u001B[1m = bold
6269 9 , // End of "Bar"
6370 []byte ("FooBar" ),
6471 5 ,
72+ 6 ,
6573 },
6674 {
6775 "Cursor position on top of cleaned sequence" ,
6876 []byte ("Foo\u001B [1mBar" ), // \u001B[1m = bold
6977 4 , // Unicode code point
7078 []byte ("FooBar" ),
7179 2 ,
80+ 6 ,
7281 },
7382 {
7483 "Negative cursor position" ,
7584 []byte ("Foo\u001B [1mBar" ), // \u001B[1m = bold
7685 - 10 , // End of "Foo"
7786 []byte ("FooBar" ),
7887 - 10 ,
88+ 6 ,
7989 },
8090 {
8191 // Running on ANSI escape codes obviously is not the intent, but without being able to easily identify
@@ -85,6 +95,7 @@ func Test_cleanPtySequences(t *testing.T) {
8595 165 ,
8696 []byte ("25h 25l █ Installing Runtime (Unconfigured)25h 25l █25h 25l █ Installing Runtime Environment25h 25l Setting Up Runtime \n Resolving Dependencies |25h" ),
8797 159 ,
98+ 158 ,
8899 },
89100 {
90101 "Escape at first character" ,
@@ -94,41 +105,47 @@ func Test_cleanPtySequences(t *testing.T) {
94105 // Since the cleaner handles an absolute cursor position against relative output, we can't determine start
95106 // of output and so we return a negative
96107 - 1 ,
108+ 3 ,
97109 },
98110 {
99111 "Cursor character (NOT position)" ,
100112 []byte ("foo\u001B [?25hbar" ),
101113 0 ,
102114 []byte ("foobar" ),
103115 0 ,
116+ 6 ,
104117 },
105118 {
106119 "Home key" ,
107120 []byte ("\x1b [Hfoo" ),
108121 0 ,
109122 []byte ("foo" ),
110123 - 1 ,
124+ 3 ,
111125 },
112126 {
113127 "Home key with Window title following" ,
114128 []byte ("\x1b [H\x1b ]0;C:\\ Windows\\ System32\\ cmd.exe\a foo" ),
115129 0 ,
116130 []byte ("foo" ),
117131 - 1 ,
132+ 3 ,
118133 },
119134 {
120135 "Alert / bell character" ,
121136 []byte ("\a P\x1b [?25lython 3.9.5" ),
122137 0 ,
123138 []byte ("Python 3.9.5" ),
124139 - 1 ,
140+ 12 ,
125141 },
126142 {
127143 "Unterminated escape sequence" ,
128- []byte ("\x1b [?25" ),
144+ []byte ("foo \x1b [?25" ),
129145 0 ,
130- []byte ("\x1b [?25" ),
131- - 1 ,
146+ []byte ("foo\x1b [?25" ),
147+ 0 ,
148+ 3 ,
132149 },
133150 }
134151 for _ , tt := range tests {
@@ -139,9 +156,10 @@ func Test_cleanPtySequences(t *testing.T) {
139156 if tt .wantCursorPos > len (tt .want ) {
140157 t .Fatal ("Wanted cursor position cannot be larger than wanted output" )
141158 }
142- cleaned , cursorPos := cleanPtySnapshot (tt .b , tt .cursorPos , false )
143- assert .Equal (t , string (tt .want ), string (cleaned ))
144- assert .Equal (t , tt .wantCursorPos , cursorPos )
159+ cleaned , cursorPos , cleanUptoPos := cleanPtySnapshot (tt .b , tt .cursorPos , false )
160+ assert .Equal (t , string (tt .want ), string (cleaned ), "wanted output" )
161+ assert .Equal (t , tt .wantCursorPos , cursorPos , "wanted cursor position" )
162+ assert .Equal (t , tt .wantCleanUpto , cleanUptoPos , "wanted clean upto position" )
145163 })
146164 }
147165}
0 commit comments