warp-dir 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,118 +0,0 @@
1
- require 'rspec/expectations'
2
- require 'warp/dir/app/cli'
3
- require 'rspec/expectations'
4
-
5
- module Warp
6
- module Dir
7
- module CLIHelper
8
-
9
- def run_command!(arguments)
10
- argv = arguments.is_a?(Array) ? arguments : arguments.split(' ')
11
- cli = Warp::Dir::App::CLI.new(argv)
12
- cli.run
13
- end
14
-
15
- def validate!(arguments, yield_before_validation: false)
16
- argv = arguments.is_a?(Array) ? arguments : arguments.split(' ')
17
- cli = Warp::Dir::App::CLI.new(argv)
18
- if yield_before_validation
19
- yield(cli) if block_given?
20
- end
21
- cli.validate
22
- unless yield_before_validation
23
- yield(cli) if block_given?
24
- end
25
- cli.run
26
- end
27
-
28
- def output_matches(output, expected)
29
- if expected.is_a?(Regexp)
30
- expected.match(output)
31
- elsif expected.is_a?(String)
32
- output.include?(expected)
33
- else
34
- nil
35
- end
36
- end
37
- end
38
- end
39
- end
40
-
41
- RSpec::Matchers.define :output do |*expectations|
42
- include Warp::Dir::CLIHelper
43
- match do |actual|
44
- @response = nil
45
- @command = "wd #{actual.is_a?(Array) ? actual.join(' ') : actual}"
46
- expectations.all? do |expected|
47
- @response = run_command!(actual)
48
- @response.messages.any? { |m| output_matches(m, expected) }
49
- end
50
- end
51
- failure_message do |actual|
52
- "#{@command} was supposed to produce something matching or containing:\nexpected: '#{expected}',\n actual: #{@response.messages}"
53
- end
54
- match_when_negated do |actual|
55
- @response = nil
56
- @command = "wd #{actual.is_a?(Array) ? actual.join(' ') : actual}"
57
- expectations.none? do |expected|
58
- @response = run_command!(actual)
59
- @response.messages.any? { |m| output_matches(m, expected) }
60
- end
61
- end
62
- failure_message_when_negated do |actual|
63
- "expected #{actual} not to contain #{expected}, got #{@response}"
64
- end
65
- end
66
-
67
- RSpec::Matchers.define :validate do |expected|
68
- include Warp::Dir::CLIHelper
69
- match do |actual|
70
- if expected == true || expected == false
71
- yield_before_validation = expected
72
- end
73
- expected = block_arg
74
- if expected.is_a?(Proc)
75
- begin
76
- @response = validate!(actual, yield_before_validation: yield_before_validation) do |cli|
77
- expected.call(cli)
78
- end
79
- rescue Exception => e
80
- STDERR.puts(e.inspect)
81
- STDERR.puts(e.backtrace.join("\n"))
82
- raise
83
- end
84
- else
85
- raise TypeError.new('Expected must be a block')
86
- end
87
- end
88
- failure_message do |actual|
89
- "expected #{actual} to validate that the block evaluates to true"
90
- end
91
-
92
- end
93
-
94
- RSpec::Matchers.define :exit_with do |expected|
95
- include Warp::Dir::CLIHelper
96
-
97
- match do |actual|
98
- response = run_command!(actual)
99
- response.code == expected
100
- end
101
- match_when_negated do |actual|
102
- response = run_command!(actual)
103
- response.code != expected
104
- end
105
- end
106
-
107
- # RSpec::Matchers.define :eval_to_true_after_validate do |expected|
108
- # match do |actual|
109
- # expected_type = expected.is_a?(Symbol) ?
110
- # Warp::Dir::App::Response::RETURN_TYPE[expected_type] :
111
- # expected
112
- # response = run_and_yield(expected)
113
- # response.type == expected_type
114
- # end
115
- # failure_message_for_should_not do |actual|
116
- # "expected #{expected} to produce return type #{}"
117
- # end
118
- # end
@@ -1,239 +0,0 @@
1
- require 'spec_helper'
2
- require 'support/cli_expectations'
3
- require 'warp/dir'
4
- require 'warp/dir/config'
5
- require 'warp/dir/app/cli'
6
- require 'pp'
7
- require 'fileutils'
8
-
9
- RSpec.describe Warp::Dir::App::CLI do
10
- include_context :fixture_file
11
- include_context :initialized_store
12
-
13
- let(:config_args) { ['--config', config.warprc] }
14
- let(:warprc) { config_args.join(' ') }
15
-
16
- describe 'when parsing argument list' do
17
- let(:cli) { Warp::Dir::App::CLI.new(argv) }
18
- before do
19
- cli.config = config
20
- end
21
-
22
- describe 'with suffix flags' do
23
- subject { cli.send(:extract_suffix_flags, argv) }
24
- describe 'and with at leats two arguments' do
25
- let(:argv) { 'command argument --flag1 --flag2 -- --suffix1 --suffix2 suffix-argument'.split(' ')}
26
- it 'extracts them well' do
27
- should eql(%w(--suffix1 --suffix2 suffix-argument))
28
- end
29
- end
30
- end
31
-
32
- describe 'with only one argument' do
33
- let(:result) { cli.send(:shift_non_flag_commands) }
34
-
35
- describe "that's a list command" do
36
- let(:argv) { %w(list --verbose) }
37
-
38
- it 'should assign the command' do
39
- expect(cli.argv).to eql(%w(list --verbose))
40
- expect(result[:command]).to eql(:list)
41
- expect(cli.argv).to eql(['--verbose'])
42
- end
43
- end
44
-
45
- describe "that's a warp point" do
46
- let(:argv) { %w(awesome-point) }
47
-
48
- it 'should default to the :warp command' do
49
- expect(result[:command]).to eql(:warp)
50
- expect(result[:point]).to eql(:'awesome-point')
51
- expect(cli.argv).to be_empty
52
- end
53
- end
54
- end
55
-
56
- describe 'with two command args' do
57
- let(:argv) { %w(add mypoint) }
58
- let(:result) { cli.send(:shift_non_flag_commands) }
59
-
60
- it 'should interpret as a command and a point' do
61
- expect(result[:command]).to eql(:add)
62
- expect(result[:point]).to eql(:mypoint)
63
- expect(cli.argv).to be_empty
64
- end
65
- end
66
- end
67
-
68
- describe 'when parsing flags' do
69
- describe 'and found --help' do
70
- let(:argv) { ['--help', *config_args] }
71
- it 'should print the help message' do
72
- expect(argv).to output(/<point>/, /Usage:/)
73
- expect(argv).not_to output(/^cd /)
74
- end
75
- it 'should exit with zero status' do
76
- expect(argv).to exit_with(0)
77
- end
78
- end
79
-
80
- describe 'and a flag is no found' do
81
- let(:argv) { [ '--boo mee --moo', *config_args ] }
82
- it 'should report invalid option' do
83
- expect(argv).to output( /unknown option/)
84
- end
85
- end
86
-
87
- describe 'when an exception error occurs' do
88
- let(:argv) { [ %w(boo dkk --debug), *config_args ].flatten }
89
- context 'and --debug is given' do
90
- it 'should print backtrace' do
91
- expect(argv.join(' ')).to eql('boo dkk --debug --config /tmp/warprc')
92
- expect(STDERR).to receive(:puts).twice
93
- expect(argv).to validate(false) { |cli|
94
- expect(cli.config.debug).to be_truthy
95
- }
96
- end
97
- end
98
- end
99
- end
100
-
101
- describe 'when running command' do
102
- describe 'without a parameter' do
103
- describe 'such as list' do
104
- let(:argv) { ['list', *config_args] }
105
-
106
- it 'should return listing of all points' do
107
- expect("list #{warprc}").to output %r{log -> /var/log}
108
- expect("list #{warprc}").to output %r{tmp -> /tmp}
109
- end
110
-
111
- it 'should exit with zero status' do
112
- expect("list #{warprc}").to exit_with(0)
113
- end
114
- end
115
- end
116
-
117
- describe 'with a point arg, such as ' do
118
- let(:wp_name) { store.last.name }
119
- let(:wp_path) { store.last.path }
120
- let(:warp_args) { "#{wp_name} #{warprc}" }
121
-
122
- describe 'warp <point>' do
123
- it "should return response with a 'cd' to a warp point" do
124
- warp_point = wp_name
125
- expect(warp_args).to validate { |cli|
126
- expect(cli.config.point).to eql(warp_point)
127
- expect(cli.config.command).to eql(:warp)
128
- expect(cli.store[warp_point]).to eql(Warp::Dir::Point.new(wp_name, wp_path))
129
- }
130
- expect(warp_args).to output("cd #{wp_path}")
131
- end
132
-
133
- describe 'when using --command' do
134
- let(:warp_args) { "--command warp -p #{wp_name} #{warprc}"}
135
- it "should return response with a 'cd' to a warp point" do
136
- warp_point = wp_name
137
- expect(warp_args).to validate { |cli|
138
- expect(cli.config.point).to eql(warp_point.to_s)
139
- expect(cli.config.command).to eql(:warp)
140
- expect(cli.store[warp_point]).to eql(Warp::Dir::Point.new(wp_name, wp_path))
141
- }
142
- expect(warp_args).to output("cd #{wp_path}")
143
- end
144
-
145
- end
146
- end
147
-
148
- describe 'remove <point>' do
149
- let(:warp_args) { "remove #{wp_name} #{warprc}" }
150
-
151
- it 'should show that point is removed ' do
152
- expect(warp_args).to output(/has been removed/)
153
- end
154
- it 'should change warp point count ' do
155
- expect(store.size).to eq(2)
156
- expect {
157
- expect(warp_args).to validate { |cli|
158
- expect(cli.config.point).to eql(point.name)
159
- expect(cli.config.command).to eql(:remove)
160
- }
161
- store.restore!
162
- }.to change(store, :size).by(-1)
163
- expect(store.size).to eq(1)
164
- end
165
- end
166
-
167
- describe 'add <point>' do
168
- context 'when point exists' do
169
- context 'without --force flag' do
170
- let(:warp_args) { "add #{wp_name} #{warprc}" }
171
-
172
- it 'should show error without' do
173
- expect(warp_args).to output(/already exists/)
174
- expect(warp_args).to exit_with(1)
175
- end
176
- end
177
- context 'with --force' do
178
- let(:warp_args) { "add #{wp_name} #{warprc} --force" }
179
- it 'should overwrite existing point' do
180
-
181
- expect(Warp::Dir.pwd).to_not eql(wp_path)
182
-
183
- existing_point = store[wp_name]
184
- expect(existing_point).to be_kind_of(Warp::Dir::Point)
185
- expect(existing_point.path).to eql(wp_path)
186
-
187
- expect {
188
- response = expect(warp_args).to validate { |cli|
189
- expect(cli.config.point).to eql(point.name)
190
- expect(cli.config.command).to eql(:add)
191
- expect(cli.store[point.name]).to_not be_nil
192
- }
193
- expect(response.type).to eql(Warp::Dir::App::Response::INFO), response.message
194
- store.restore!
195
- updated_point = store[wp_name]
196
- expect(updated_point.relative_path).to eql(Warp::Dir::pwd)
197
- }.to_not change(store, :size)
198
-
199
- end
200
- end
201
- end
202
- end
203
-
204
- describe 'ls <point>' do
205
- context 'no flags' do
206
- let(:warp_args) { "ls #{wp_name} #{warprc}" }
207
- it 'should default to -al' do
208
- expect(warp_args).to output(%r{total \d+\n}, %r{ warprc\n})
209
- end
210
- end
211
- context '-- -l' do
212
- let(:warp_args) { "ls #{wp_name} #{warprc} -- -l" }
213
- it 'should extract flags' do
214
- expect(warp_args).to validate { |cli|
215
- expect(cli.flags).to eql(['-l'])
216
- }
217
- end
218
- it 'should ls folder in long format' do
219
- expect(warp_args).to output(%r{total \d+\n}, %r{ warprc\n})
220
- end
221
- end
222
- context '-- -1' do
223
- let(:warp_args) { "ls #{wp_name} #{warprc} -- -1" }
224
- it 'should not list directory in long format' do
225
- expect(warp_args).not_to output(%r{total \d+\n}, %r{ warprc\n})
226
- end
227
- end
228
- context '-- -elf' do
229
- let(:warp_args) { "ls #{wp_name} #{warprc} -- -alF" }
230
- [%r{total \d+\n}, %r{ warprc\n}].each do |reg|
231
- it "should list directory and match #{reg}" do
232
- expect(warp_args).to output(reg)
233
- end
234
- end
235
- end
236
- end
237
- end
238
- end
239
- end
@@ -1,131 +0,0 @@
1
- require 'spec_helper'
2
- require 'warp/dir/app/response'
3
-
4
- RSpec.describe Warp::Dir::App::Response do
5
- let(:response_class) { ::Warp::Dir::App::Response }
6
- let(:response_instance) { response_class.new }
7
- let(:stream) { double }
8
-
9
- before do
10
- expect(Warp::Dir).to receive(:eval_context?).at_most(100).times.and_return(true)
11
- end
12
- after do
13
- response_instance.messages.clear
14
- response_instance.instance_variable_set(:@type, nil)
15
- end
16
-
17
- describe 'class and type' do
18
- it 'should be able to create response_instance' do
19
- expect(response_instance).to be_kind_of(response_class)
20
- end
21
-
22
- it 'should have constants of correct type' do
23
- expect(Warp::Dir::App::Response::INFO).to be_kind_of(Warp::Dir::App::Response::Type)
24
- end
25
- end
26
-
27
- describe 'accessors' do
28
- it 'should allow setting type, code and messages' do
29
- response_instance.type(Warp::Dir::App::Response::INFO)
30
- response_instance.message('Hi')
31
- response_instance.code(200)
32
- expect(response_instance.type).to eql(Warp::Dir::App::Response::INFO)
33
- expect(response_instance.message).to eql('Hi')
34
- expect(response_instance.code).to eql(200)
35
- end
36
- end
37
-
38
- describe '#configure' do
39
- describe 'given a response_instance object' do
40
- before do
41
- response_instance.instance_variable_set(:@type, nil)
42
- response_instance.configure do
43
- type Warp::Dir::App::Response::INFO
44
- message 'Hello'
45
- message 'World'
46
- code 255
47
- end
48
- end
49
-
50
- describe '#messages' do
51
- it 'should concatenate when merged' do
52
- expect(response_instance.messages).to eql(%w(Hello World))
53
- expect(response_instance.message).to eql('HelloWorld')
54
- end
55
- end
56
- describe '#type' do
57
- it 'be equal to the lookup result' do
58
- expect(response_instance.type).to eql(Warp::Dir::App::Response::RETURN_TYPE[:success])
59
- expect(response_instance.type).to eql(Warp::Dir::App::Response::INFO)
60
- end
61
- end
62
- describe '#code' do
63
- it 'should be as it was configured' do
64
- expect(response_instance.code).to eql(255)
65
- end
66
- end
67
- end
68
- end
69
-
70
-
71
- describe '#exit' do
72
- let(:exit_code) { 199 }
73
- let(:fake_type) { Warp::Dir::App::Response::Type.new(exit_code, stream) }
74
- before do
75
- Warp::Dir::App::Response.enable_exit!
76
- end
77
- after do
78
- Warp::Dir::App::Response.disable_exit!
79
- end
80
-
81
- describe 'without specifying type' do
82
- it 'should throw an exception' do
83
- expect { response_instance.exit! }.to raise_error(ArgumentError)
84
- end
85
- end
86
-
87
- describe 'when type is user output' do
88
- before do
89
- response_instance.type(fake_type)
90
- end
91
-
92
- it 'should properly format messages for eval' do
93
- expect(stream).to receive(:printf).with(%Q{printf -- 'Hello\\n';}).once
94
- expect(stream).to receive(:printf).with(%Q{printf -- 'World\\n';}).once
95
- response_instance.configure do
96
- message 'Hello'
97
- message 'World'
98
- end
99
- response_instance.print
100
- end
101
- it 'should properly exit via Kernel' do
102
- expect(Kernel).to receive(:exit).with(exit_code).once
103
- response_instance.configure do
104
- code 199
105
- end
106
- response_instance.exit!
107
- end
108
- end
109
-
110
- describe 'when type is shell' do
111
- before do
112
- @stream = Warp::Dir::App::Response::SHELL.stream
113
- Warp::Dir::App::Response::SHELL.stream = stream
114
- end
115
- after do
116
- Warp::Dir::App::Response::SHELL.stream = @stream
117
- end
118
-
119
- it 'should return proper exit code for shell commands' do
120
- response_instance.type(Warp::Dir::App::Response::SHELL)
121
- response_instance.configure do
122
- message 'ls -al'
123
- code 1231 # this should be ignored
124
- end
125
- expect(stream).to receive(:printf).with('ls -al;').once
126
- expect(Kernel).to receive(:exit).with(2).once
127
- response_instance.print.exit!
128
- end
129
- end
130
- end
131
- end