haveapi-client 0.31.2 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9262c30e31afb7945acb49f137c5f9c58c0f76a51b754a0cab7cc7554524c865
4
- data.tar.gz: f8a38d4a8907b0e3f83327dce2c3009a125fc946d876949cfe80cbfccac369c9
3
+ metadata.gz: baae52475f71928ac2df1122fd12e2b83cb85c47059aba9e51e257cf67a9fb5d
4
+ data.tar.gz: c5b47290a8ea55093bbc513ddb85ca51febcf55ed08159fad7b3ea1663b1aeb3
5
5
  SHA512:
6
- metadata.gz: f5834a948d75f51f9b85df7cfb1bdf5132e4691ac352e7be44854e5faf23b59c64761e8d44a1eac44613c53cf2e65ebd3f3f622d28aa4ba1ea19bb5268433798
7
- data.tar.gz: a8082e60b055d31619b75bb66f0e24d5dd4a2ac628c7949b133b63712b50ee755868005e3ad4c6f06469a2f80714934d4d110315b0cf9a14cc15169c085a275e
6
+ metadata.gz: b24eef27ca7e9004d0cc8090cd972316f94f1339e0b781f0c10b5f9591401cc864cf0ed7c0de9257bcaee7d034f94a079459a5cf79edcd49ae9c60596a5af5b0
7
+ data.tar.gz: bdaa388b1e40bc44a2555a23474b2d8f5f5a7ec85b6ce8bf3b102b40d761b24bae696b0d3614d7ad08722aea3628f49bfe936a2bb624273967e7f51ba3a9635f
@@ -1,3 +1,5 @@
1
+ require 'shellwords'
2
+
1
3
  module HaveAPI::CLI
2
4
  module ExampleFormatter
3
5
  def self.format_examples(cli, action, out = $>)
@@ -5,7 +7,7 @@ module HaveAPI::CLI
5
7
  out << ' > ' << example[:title] << ":\n" unless example[:title].empty?
6
8
 
7
9
  # request
8
- out << "$ #{$0} #{action.resource_path.join('.')} #{action.name}"
10
+ out << "$ #{Shellwords.escape($0)} #{Shellwords.escape(action.resource_path.join('.'))} #{Shellwords.escape(action.name.to_s)}"
9
11
 
10
12
  params = example[:request]
11
13
 
@@ -36,15 +38,15 @@ module HaveAPI::CLI
36
38
 
37
39
  case desc[:type]
38
40
  when 'Boolean'
39
- value ? "--#{option}" : "--no-#{option}"
41
+ Shellwords.escape(value ? "--#{option}" : "--no-#{option}")
40
42
 
41
43
  else
42
- "--#{option} #{example_value(value)}"
44
+ "#{Shellwords.escape("--#{option}")} #{example_value(value)}"
43
45
  end
44
46
  end
45
47
 
46
48
  def self.example_value(v)
47
- v.is_a?(String) && (v.empty? || v.index(' ')) ? "\"#{v}\"" : v
49
+ Shellwords.escape(v.to_s)
48
50
  end
49
51
  end
50
52
  end
@@ -1,6 +1,6 @@
1
1
  module HaveAPI
2
2
  module Client
3
3
  PROTOCOL_VERSION = '2.0'.freeze
4
- VERSION = '0.31.2'.freeze
4
+ VERSION = '0.32.0'.freeze
5
5
  end
6
6
  end
@@ -0,0 +1,86 @@
1
+ require 'fileutils'
2
+ require 'open3'
3
+ require 'stringio'
4
+ require 'tmpdir'
5
+ require_relative '../lib/haveapi/cli'
6
+
7
+ RSpec.describe HaveAPI::CLI::ExampleFormatter do
8
+ def format_request(request, program: 'haveapi-cli', resources: ['widget'], action_name: 'update',
9
+ parameters: { name: { type: 'String' } }, title: '', response: {}, receiver: nil)
10
+ action = Object.new
11
+ action.define_singleton_method(:examples) { [{ title: title, request: request, response: response }] }
12
+ action.define_singleton_method(:resource_path) { resources }
13
+ action.define_singleton_method(:name) { action_name }
14
+ action.define_singleton_method(:param_description) { |_direction, name| parameters[name] }
15
+ action.define_singleton_method(:namespace) { |_direction| :widget }
16
+ cli = Object.new
17
+ cli.define_singleton_method(:format_output) { |*args| receiver&.call(*args) }
18
+ out = StringIO.new
19
+ saved = $0
20
+ begin
21
+ $0 = program
22
+ described_class.format_examples(cli, action, out)
23
+ ensure
24
+ $0 = saved
25
+ end
26
+ out.string
27
+ end
28
+
29
+ def capture_arguments(source, directory, program: 'haveapi-cli')
30
+ executable = File.join(directory, program)
31
+ File.write(executable, "#!/bin/sh\nprintf '%s\\0' \"$@\" > \"$CAPTURE\"\n")
32
+ File.chmod(0o755, executable)
33
+ capture = File.join(directory, 'arguments')
34
+ stdout, stderr, status = Open3.capture3(
35
+ { 'PATH' => "#{directory}:#{ENV.fetch('PATH')}", 'CAPTURE' => capture },
36
+ 'sh', '-c', source.delete_prefix('$ '), chdir: directory
37
+ )
38
+ expect(status).to be_success, "#{stdout}\n#{stderr}"
39
+ File.binread(capture).force_encoding(Encoding::UTF_8).split("\0", -1)[0...-1]
40
+ end
41
+
42
+ it 'keeps the reported CLI-help request substitution literal', :aggregate_failures do
43
+ Dir.mktmpdir('haveapi-cli-help-') do |directory|
44
+ marker = File.join(directory, 'request-value-executed')
45
+ payload = "$(printf request-value-confirmed > #{marker})"
46
+ source = format_request({ name: payload })
47
+ argv = capture_arguments(source, directory)
48
+ expect(File.exist?(marker)).to be(false)
49
+ expect(argv).to eq(['widget', 'update', '--', '--name', payload])
50
+ end
51
+ end
52
+
53
+ it 'preserves every complete CLI-help token and existing conversions without evaluation' do
54
+ Dir.mktmpdir('haveapi-cli-help-') do |directory|
55
+ hostile = "space\tline\n雪'\"\\$value;`touch unintended`$(touch unintended)"
56
+ program = "cli #{hostile}"
57
+ option = "odd_name #{hostile}"
58
+ values = { option => hostile, empty: '', zero: 0, nil_value: nil, list: [false, 0, nil], object: { key: false },
59
+ enabled_flag: true, disabled_flag: false, nil_flag: nil, at_file: '@example', escaped_at: '@@example', unknown: 'filtered' }
60
+ parameters = values.except(:unknown).to_h { |key, _| [key, { type: 'String' }] }
61
+ %i[enabled_flag disabled_flag nil_flag].each { |key| parameters[key] = { type: 'Boolean' } }
62
+ source = format_request(values, program: program, resources: [hostile, 'nested'], action_name: hostile, parameters: parameters)
63
+ argv = capture_arguments(source, directory, program: program)
64
+ expect(argv).to eq(["#{hostile}.nested", hostile, '--', "--#{option.tr('_', '-')}", hostile,
65
+ '--empty', '', '--zero', '0', '--nil-value', '', '--list', values[:list].to_s,
66
+ '--object', values[:object].to_s, '--enabled-flag', '--no-disabled-flag', '--no-nil-flag',
67
+ '--at-file', '@example', '--escaped-at', '@@example'])
68
+ expect(File.exist?(File.join(directory, 'unintended'))).to be(false)
69
+ end
70
+ end
71
+
72
+ it 'keeps absent inputs, filtering, title and response output routing intact' do
73
+ saved = $0
74
+ calls = []
75
+ receiver = proc do |action, response, out|
76
+ calls << [action.name, response, out.class]
77
+ out << "formatted response\n"
78
+ end
79
+ source = format_request(nil, title: 'Visible title', response: { id: 0 }, receiver: receiver)
80
+ expect(source).to eq(" > Visible title:\n$ haveapi-cli widget update\nformatted response\n")
81
+ expect(calls).to eq([['update', { widget: { id: 0 } }, StringIO]])
82
+ expect(format_request({})).to eq("$ haveapi-cli widget update\n")
83
+ expect(format_request({ unknown: 'filtered' })).to eq("$ haveapi-cli widget update --\n")
84
+ expect($0).to eq(saved)
85
+ end
86
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haveapi-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.31.2
4
+ version: 0.32.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Skokan
@@ -159,6 +159,7 @@ files:
159
159
  - lib/restclient_ext/resource.rb
160
160
  - spec/action_security_spec.rb
161
161
  - spec/authentication_token_security_spec.rb
162
+ - spec/cli_example_formatter_spec.rb
162
163
  - spec/cli_i18n_spec.rb
163
164
  - spec/cli_security_spec.rb
164
165
  - spec/description_method_security_spec.rb