ttytest2 0.9.6 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 196acc1f485c7ad416fbdbe07c94f0829bfb53a57c852a85bf79d72655f4debb
4
- data.tar.gz: a2afc1f836dff32a2e2aa53a90137ce9bd768a20907855defae05da66af821e4
3
+ metadata.gz: 4e35165fcf1f2e3702b3d1027de2579a0264acef5e042ac273399affec6aa2d8
4
+ data.tar.gz: 8900407b6a21d719ac075c133a1cf9a9c386910836fe971f33ee12a1cb1323f6
5
5
  SHA512:
6
- metadata.gz: 762f9246cadb92d30ef2d8535b135cb9d72270d8e24bc100c8d18e18ecdd949e35560a7ff6fdc3b413a7dffbacbef56cf712c0b459b4cc3f8f7797dbfeca61d3
7
- data.tar.gz: 2e099fda420602c981073d9cb39d9be307e261086ac8682ec4b213b6a061eded99a9ccff73df9e6c16a9157676fc4dea03a33d834d643fcfdf63631090807156
6
+ metadata.gz: 70bbb5f89d0fa13fa2b1f0d35be80e1946239316bcbc4810b315e62a31e6f4f78aa919ebbcc3f213192bcc1e601bcb9415de2f57712a49b46b32d2fe805de7cc
7
+ data.tar.gz: 4dae391082d500a4c7f6aa8f3d78d69e917527991ece395f3b444b5f3e28d1becbadcad83261299a78cc44013f0127771cd359a7f0703f8f4a966978e0924af5
@@ -1,18 +1,18 @@
1
- name: Test
2
-
3
- on: [push]
4
-
5
- jobs:
6
- test:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/checkout@v4
10
- - name: Set up Ruby 3.2.3
11
- uses: ruby/setup-ruby@v1
12
- with:
13
- ruby-version: 3.2.3
14
- bundler-cache: true
15
- - name: Build and test with Rake
16
- run: |
17
- bundle install --jobs 4 --retry 3
18
- bundle exec rake
1
+ name: Test
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v4
10
+ - name: Set up Ruby 3.2.3
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.2.3
14
+ bundler-cache: true
15
+ - name: Build and test with Rake
16
+ run: |
17
+ bundle install --jobs 4 --retry 3
18
+ bundle exec rake
data/.gitignore CHANGED
@@ -1,5 +1,5 @@
1
- Gemfile.lock
2
- pkg/
3
- .yardoc/
4
- doc/
5
- *.gem
1
+ Gemfile.lock
2
+ pkg/
3
+ .yardoc/
4
+ doc/
5
+ *.gem
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- gemspec
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/README.md CHANGED
@@ -1,201 +1,225 @@
1
- # ttytest2
2
-
3
- ttytest2 is an acceptance test framework for interactive console applications. It's like [capybara](https://github.com/teamcapybara/capybara) for the terminal.
4
-
5
- A drop-in replacement for [ttytest](https://github.com/jhawthorn/ttytest), because I had some features I needed for my own project.
6
-
7
- It works by running commands inside a tmux session, capturing the pane, and comparing the content.
8
-
9
- The assertions will wait a specified amount of time (default 2 seconds) for the expected content to appear.
10
-
11
- [![Gem Version](https://badge.fury.io/rb/ttytest2.svg?icon=si%3Arubygems)](https://badge.fury.io/rb/ttytest2)
12
-
13
- ## Table of Contents
14
-
15
- 1. [Minimum Requirements](#minimum-requirements)
16
- 2. [Usage](#usage)
17
- 3. [Example for Canonical CLI or Shell](#example-for-canonical-cli-or-shell)
18
- 4. [Example for Noncanonical CLI or Shell](#example-for-noncanonical-cli-or-shell)
19
- 5. [Assertions](#assertions)
20
- 6. [Output](#output)
21
- 7. [Output Helpers](#output-helpers)
22
- 8. [Troubleshooting](#troubleshooting)
23
- 9. [Constants](#constants)
24
- 10. [Tips](#tips)
25
- 11. [Docker](#docker)
26
- 12. [Contributing](#contributing)
27
- 13. [License](#license)
28
-
29
- ## Minimum Requirements
30
-
31
- * tmux >= 1.8
32
- * Ruby >= 3.2.3
33
-
34
- ## Usage
35
-
36
- More documentation available at [ttytest2 docs](https://www.rubydoc.info/gems/ttytest2).
37
-
38
- There are more examples in the examples folder.
39
-
40
- ### Example for Canonical CLI or Shell
41
-
42
- Most people should use send_keys, if you are writing or working with a noncanonical shell/CLI, you will probably know it! Most shell/CLI applications are canonical.
43
-
44
- ``` ruby
45
- require 'ttytest'
46
-
47
- @tty = TTYtest.new_terminal(%{PS1='$ ' /bin/sh}, width: 80, height: 24)
48
- @tty.assert_row(0, '$')
49
- @tty.assert_cursor_position(x: 2, y: 0)
50
-
51
- @tty.send_keys(%{echo "Hello, world"\n})
52
-
53
- @tty.assert_contents <<TTY
54
- $ echo "Hello, world"
55
- Hello, world
56
- $
57
- TTY
58
- @tty.assert_cursor_position(x: 2, y: 2)
59
-
60
- @tty.print_rows # => ["$ echo \"Hello, world\"", "Hello, world", "$", "", "", "", ...]
61
-
62
- @tty.print # prints out the contents of the terminal
63
- ```
64
-
65
- ### Example for Noncanonical CLI or Shell
66
-
67
- If you are working with a noncanonical shell, you need to use send_keys_one_at_a_time to have your shell/CLI process the input correctly.
68
-
69
- Also useful if you need to send input one character at a time for whatever reason.
70
-
71
- 'Multi-character' characters like '\n' need to be sent with send-keys, though.
72
-
73
- ``` ruby
74
- require 'ttytest'
75
-
76
- @tty = TTYtest.new_terminal(%{PS1='$ ' /bin/noncanonical-sh}, width: 80, height: 24)
77
- @tty.assert_row_starts_with(0, ENV['USER'])
78
- @tty.assert_row_ends_with(0, '$')
79
-
80
- @tty.send_keys_one_at_a_time('ls')
81
- @tty.assert_row_ends_with(0, 'ls')
82
- @tty.send_keys(%(\n)) # make sure to use send_keys for 'multi-character' characters like \n, \r, \t, etc.
83
-
84
- @tty.send_keys_one_at_a_time('ps')
85
- @tty.assert_row_ends_with(1, 'ps')
86
- @tty.send_keys(TTYtest:NEWLINE) # can use constants instead
87
-
88
-
89
- @tty.assert_row_starts_with(2, ENV['USER'])
90
- @tty.assert_row_ends_with(2, '$')
91
- @tty.send_newline # an alternative to the 2 above methods to send \n to the terminal
92
-
93
- puts "\n#{@tty.capture}" # prints out the contents of the terminal, equivalent to @tty.print
94
- ```
95
-
96
- ### Assertions
97
-
98
- The main way to use TTYtest is through assertions. When called on a `TTYtest::Terminal`, each of these will be retried (for up to 2 seconds).
99
-
100
- Available assertions:
101
-
102
- * `assert_row(row_number, expected_text)`
103
- * `assert_row_at(row_number, column_start_position, column_end_position, expected_text)`
104
- * `assert_row_like(row_number, expected_text)`
105
- * `assert_row_starts_with(row_number, expected_text)`
106
- * `assert_row_ends_with(row_number, expected_text)`
107
- * `assert_cursor_position(x: x, y: y)`
108
- * `assert_cursor_visible`
109
- * `assert_cursor_hidden`
110
- * `assert_contents(lines_of_terminal)`
111
-
112
- ### Output
113
-
114
- You can send output to the terminal with the following calls.
115
-
116
- * `send_keys(output) # for canonical shells/CLI's (or multi-character keys for noncanonical shells/CLI's)`
117
- * `send_keys_one_at_a_time(output) # for noncanonical shells/CLI's`
118
- * `send_keys_exact(output) # for sending tmux specific keys (DC for delete, Escape for ESC, etc.)`
119
-
120
- ### Output Helpers
121
-
122
- Helper functions to make sending output easier! They use the methods above under 'Sending Output' section under the hood.
123
-
124
- * `send_newline` # equivalent to @tty.send_keys(%(\n))
125
- * `send_newlines(number_of_times)` # equivalent to calling send_newline number_of_times
126
- * `send_backspace` # equivalent to @tty.send_keys(TTYtest::BACKSPACE)
127
- * `send_backspaces(number_of_times)` # equivalent to calling send_backspace number_of_times
128
- * `send_delete` # equivalent to calling send_keys_exact(%(DC))
129
- * `send_deletes` # equivalent to calling send_delete number_of_times
130
- * `send_right_arrow`
131
- * `send_right_arrows(number_of_times)`
132
- * `send_left_arrow`
133
- * `send_left_arrows(number_of_times)`
134
- * `send_up_arrow`
135
- * `send_up_arrows(number_of_times)`
136
- * `send_down_arrow`
137
- * `send_down_arrows(number_of_times)`
138
- * `send_home` # simulate pressing the Home key
139
- * `send_end` # simulate pressing the End key
140
- * `send_clear` # clear the screen by sending clear ascii code
141
-
142
- ### Troubleshooting
143
-
144
- You can use the method rows to get all rows of the terminal as an array, of use the method capture to get the contents of the terminal window. This can be useful when troubleshooting.
145
-
146
- ``` ruby
147
- # you can use @tty.rows to access the entire pane, split by line into an array.
148
- p @tty.rows # prints out the contents of the terminal as a array.
149
- @tty.print_rows # equivalent to above, just for ease of use.
150
-
151
- # you can use @tty.capture to access the entire pane.
152
- p "\n#{@tty.capture}" # prints out the contents of the terminal
153
- @tty.print # equivalent to above, just for ease of use.
154
- ```
155
-
156
- ### Constants
157
-
158
- There are some commonly used keys available as constants to make interacting with your shell/CLI easy.
159
-
160
- ``` ruby
161
- TTYtest::BACKSPACE
162
- TTYtest::TAB
163
- TTYtest::CTRLF
164
- TTYtest::CTRLC
165
- TTYtest::CTRLD
166
- TTYtest::ESCAPE
167
-
168
- TTYtest::UP_ARROW
169
- TTYtest::DOWN_ARROW
170
- TTYtest::RIGHT_ARROW
171
- TTYtest::LEFT_ARROW
172
-
173
- TTYtest::CLEAR # clear the screen
174
- ```
175
-
176
- ### Tips
177
-
178
- If you are using ttyest2 to test your CLI, using sh is easier than bash because you don't have to worry about user, current working directory, etc. as shown in the examples above.
179
-
180
- If you are using ttytest2 to test your shell, using assertions like assert_row_like, assert_row_starts_with, and assert_row_ends_with are going to be extremely helpful, especially if trying to test your shell in different environments or using a docker container.
181
-
182
- ## Docker
183
-
184
- Easy to use from Docker. Add this to your dockerfile to get started.
185
-
186
- ``` dockerfile
187
- RUN apt update && \
188
- apt install gcc make ruby tmux -y && \
189
- gem install ttytest2
190
-
191
- # add this if you have issues
192
- # ENV RUBYOPT="-KU -E utf-8:utf-8"
193
- ```
194
-
195
- ## Contributing
196
-
197
- Bug reports and pull requests are welcome on GitHub at [ttytest2](https://github.com/a-eski/ttytest2).
198
-
199
- ## License
200
-
201
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
1
+ # ttytest2
2
+
3
+ ttytest2 is an acceptance test framework for interactive console applications. It's like [capybara](https://github.com/teamcapybara/capybara) for the terminal.
4
+
5
+ A drop-in replacement for [ttytest](https://github.com/jhawthorn/ttytest), because I had some features I needed for my own project.
6
+
7
+ It works by running commands inside a tmux session, capturing the pane, and comparing the content.
8
+
9
+ The assertions will wait a specified amount of time (default 2 seconds) for the expected content to appear.
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/ttytest2.svg?icon=si%3Arubygems)](https://badge.fury.io/rb/ttytest2)
12
+
13
+ ## Table of Contents
14
+
15
+ 1. [Minimum Requirements](#minimum-requirements)
16
+ 2. [Usage](#usage)
17
+ 3. [Example for Canonical CLI or Shell](#example-for-canonical-cli-or-shell)
18
+ 4. [Example for Noncanonical CLI or Shell](#example-for-noncanonical-cli-or-shell)
19
+ 5. [Assertions](#assertions)
20
+ 6. [Output](#output)
21
+ 7. [Output Helpers](#output-helpers)
22
+ 8. [Troubleshooting](#troubleshooting)
23
+ 9. [Constants](#constants)
24
+ 10. [Tips](#tips)
25
+ 11. [Docker](#docker)
26
+ 12. [Contributing](#contributing)
27
+ 13. [License](#license)
28
+
29
+ ## Minimum Requirements
30
+
31
+ * tmux >= 1.8
32
+ * Ruby >= 3.2.3
33
+
34
+ ## Usage
35
+
36
+ More documentation available at [ttytest2 docs](https://www.rubydoc.info/gems/ttytest2).
37
+
38
+ There are more examples in the examples folder.
39
+
40
+ ### Example for Canonical CLI or Shell
41
+
42
+ Most people should use send_keys, if you are writing or working with a noncanonical shell/CLI, you will probably know it! Most shell/CLI applications are canonical.
43
+
44
+ ``` ruby
45
+ require 'ttytest'
46
+
47
+ @tty = TTYtest.new_terminal(%{PS1='$ ' /bin/sh}, width: 80, height: 24)
48
+ @tty.assert_row(0, '$')
49
+ @tty.assert_cursor_position(x: 2, y: 0)
50
+
51
+ @tty.send_keys(%{echo "Hello, world"\n})
52
+
53
+ @tty.assert_contents <<TTY
54
+ $ echo "Hello, world"
55
+ Hello, world
56
+ $
57
+ TTY
58
+ @tty.assert_cursor_position(x: 2, y: 2)
59
+
60
+ @tty.assert_contents_at(0, 0, '$ echo "Hello, world"')
61
+
62
+ @tty.assert_row_starts_with(0, '$ echo')
63
+ @tty.assert_row_ends_with(0, '"Hello, world"')
64
+ @tty.assert_row_starts_with(1, 'Hello')
65
+ @tty.assert_row_ends_with(1, ', world')
66
+
67
+ @tty.print_rows # => ["$ echo \"Hello, world\"", "Hello, world", "$", "", "", "", ...]
68
+
69
+ @tty.print # prints out the contents of the terminal
70
+ ```
71
+
72
+ ### Example for Noncanonical CLI or Shell
73
+
74
+ If you are working with a noncanonical shell, you need to use send_keys_one_at_a_time to have your shell/CLI process the input correctly.
75
+
76
+ Also useful if you need to send input one character at a time for whatever reason.
77
+
78
+ 'Multi-character' characters like '\n' need to be sent with send-keys, though.
79
+
80
+ ``` ruby
81
+ require 'ttytest'
82
+
83
+ @tty = TTYtest.new_terminal(%{PS1='$ ' /bin/noncanonical-sh}, width: 80, height: 24)
84
+ @tty.assert_row_starts_with(0, ENV['USER'])
85
+ @tty.assert_row_ends_with(0, '$')
86
+
87
+ @tty.send_keys_one_at_a_time('ls')
88
+ @tty.assert_row_ends_with(0, 'ls')
89
+ @tty.send_newline # builtins for common outputs line newline
90
+
91
+ @tty.send_keys_one_at_a_time('ps')
92
+ @tty.assert_row_ends_with(1, 'ps')
93
+ @tty.send_keys(TTYtest:NEWLINE) # can use constants instead
94
+
95
+ @tty.assert_row_starts_with(2, ENV['USER'])
96
+ @tty.assert_row_ends_with(2, '$')
97
+ @tty.send_newline
98
+
99
+ puts "\n#{@tty.capture}" # prints out the contents of the terminal, equivalent to @tty.print
100
+ ```
101
+
102
+ ### Assertions
103
+
104
+ The main way to use TTYtest is through assertions. When called on a `TTYtest::Terminal`, each of these will be retried (for up to 2 seconds).
105
+
106
+ Available assertions:
107
+
108
+ * `assert_row(row_number, expected_text)`
109
+ * `assert_row_at(row_number, column_start_position, column_end_position, expected_text)`
110
+ * `assert_row_like(row_number, expected_text)`
111
+ * `assert_row_starts_with(row_number, expected_text)`
112
+ * `assert_row_ends_with(row_number, expected_text)`
113
+ * `assert_cursor_position(x: x, y: y)`
114
+ * `assert_cursor_visible`
115
+ * `assert_cursor_hidden`
116
+ * `assert_contents(lines_of_terminal)`
117
+ * `assert_contents_at(row_start, row_end, expected_text)`
118
+
119
+ ### Output
120
+
121
+ You can send output to the terminal with the following calls.
122
+
123
+ * `send_keys(output) # for canonical shells/CLI's (or multi-character keys for noncanonical shells/CLI's)`
124
+ * `send_keys_one_at_a_time(output) # for noncanonical shells/CLI's`
125
+ * `send_keys_exact(output) # for sending tmux specific keys (DC for delete, Escape for ESC, etc.)`
126
+
127
+ ### Output Helpers
128
+
129
+ Helper functions to make sending output easier! They use the methods above under 'Sending Output' section under the hood.
130
+
131
+ * `send_newline` # equivalent to @tty.send_keys(%(\n))
132
+ * `send_newlines(number_of_times)` # equivalent to calling send_newline number_of_times
133
+ * `send_backspace` # equivalent to @tty.send_keys(TTYtest::BACKSPACE)
134
+ * `send_backspaces(number_of_times)` # equivalent to calling send_backspace number_of_times
135
+ * `send_delete` # equivalent to calling send_keys_exact(%(DC))
136
+ * `send_deletes` # equivalent to calling send_delete number_of_times
137
+ * `send_right_arrow`
138
+ * `send_right_arrows(number_of_times)`
139
+ * `send_left_arrow`
140
+ * `send_left_arrows(number_of_times)`
141
+ * `send_up_arrow`
142
+ * `send_up_arrows(number_of_times)`
143
+ * `send_down_arrow`
144
+ * `send_down_arrows(number_of_times)`
145
+ * `send_home` # simulate pressing the Home key
146
+ * `send_end` # simulate pressing the End key
147
+ * `send_clear` # clear the screen by sending clear ascii code
148
+
149
+ ### Configurables
150
+
151
+ Currently the only configuration for ttytest2 is max wait time.
152
+
153
+ Max wait time represents the amount of time in seconds that ttytest2 will keep retrying an assertion before failing.
154
+
155
+ You can configure max wait time as shown below.
156
+
157
+ ``` ruby
158
+ @tty = TTYtest::new_terminal('')
159
+ @tty.max_wait_time = 1 # sets the max wait time to 1 second
160
+
161
+ @tty.assert_row(0, 'echo Hello, world') # this assertion would fail after 1 second
162
+ ```
163
+
164
+ ### Troubleshooting
165
+
166
+ You can use the method rows to get all rows of the terminal as an array, of use the method capture to get the contents of the terminal window. This can be useful when troubleshooting.
167
+
168
+ ``` ruby
169
+ @tty = TTYtest::new_terminal('')
170
+
171
+ # you can use @tty.rows to access the entire pane, split by line into an array.
172
+ p @tty.rows # prints out the contents of the terminal as a array.
173
+ @tty.print_rows # equivalent to above, just for ease of use.
174
+
175
+ # you can use @tty.capture to access the entire pane.
176
+ p "\n#{@tty.capture}" # prints out the contents of the terminal
177
+ @tty.print # equivalent to above, just for ease of use.
178
+ ```
179
+
180
+ ### Constants
181
+
182
+ There are some commonly used keys available as constants to make interacting with your shell/CLI easy.
183
+
184
+ ``` ruby
185
+ TTYtest::BACKSPACE
186
+ TTYtest::TAB
187
+ TTYtest::CTRLF
188
+ TTYtest::CTRLC
189
+ TTYtest::CTRLD
190
+ TTYtest::ESCAPE
191
+
192
+ TTYtest::UP_ARROW
193
+ TTYtest::DOWN_ARROW
194
+ TTYtest::RIGHT_ARROW
195
+ TTYtest::LEFT_ARROW
196
+
197
+ TTYtest::CLEAR # clear the screen
198
+ ```
199
+
200
+ ### Tips
201
+
202
+ If you are using ttyest2 to test your CLI, using sh is easier than bash because you don't have to worry about user, current working directory, etc. as shown in the examples above.
203
+
204
+ If you are using ttytest2 to test your shell, using assertions like assert_row_like, assert_row_starts_with, and assert_row_ends_with are going to be extremely helpful, especially if trying to test your shell in different environments or using a docker container.
205
+
206
+ ## Docker
207
+
208
+ Easy to use from Docker. Add this to your dockerfile to get started.
209
+
210
+ ``` dockerfile
211
+ RUN apt update && \
212
+ apt install gcc make ruby tmux -y && \
213
+ gem install ttytest2
214
+
215
+ # add this if you have issues
216
+ # ENV RUBYOPT="-KU -E utf-8:utf-8"
217
+ ```
218
+
219
+ ## Contributing
220
+
221
+ Bug reports and pull requests are welcome on GitHub at [ttytest2](https://github.com/a-eski/ttytest2).
222
+
223
+ ## License
224
+
225
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,21 +1,21 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rake/testtask'
5
- require 'yard'
6
-
7
- Rake::TestTask.new(:test) do |t|
8
- t.libs << 'test'
9
- t.libs << 'lib'
10
- t.test_files = FileList['test/**/*_test.rb']
11
- end
12
-
13
- YARD::Rake::YardocTask.new do |t|
14
- t.files = ['lib/**/*.rb']
15
- end
16
-
17
- task :console do
18
- system 'irb -Ilib -rttytest'
19
- end
20
-
21
- task default: :test
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+ require 'yard'
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.libs << 'lib'
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ end
12
+
13
+ YARD::Rake::YardocTask.new do |t|
14
+ t.files = ['lib/**/*.rb']
15
+ end
16
+
17
+ task :console do
18
+ system 'irb -Ilib -rttytest'
19
+ end
20
+
21
+ task default: :test