license-acceptance 0.2.10 → 0.2.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,14 @@
1
1
  require "spec_helper"
2
- require "license_acceptance/prompt_acceptance"
2
+ require "license_acceptance/strategy/prompt"
3
3
  require "license_acceptance/product"
4
4
  require "tty-prompt"
5
5
 
6
- RSpec.describe LicenseAcceptance::PromptAcceptance do
6
+ RSpec.describe LicenseAcceptance::Strategy::Prompt do
7
7
  let(:output) { StringIO.new }
8
8
  let(:config) do
9
9
  instance_double(LicenseAcceptance::Config, output: output)
10
10
  end
11
- let(:acc) { LicenseAcceptance::PromptAcceptance.new(config) }
11
+ let(:acc) { LicenseAcceptance::Strategy::Prompt.new(config) }
12
12
  let(:prompt) { instance_double(TTY::Prompt) }
13
13
  let(:p1) { instance_double(LicenseAcceptance::Product, name: "name", pretty_name: "Pretty Name") }
14
14
  let(:missing_licenses) { [p1] }
@@ -60,7 +60,7 @@ RSpec.describe LicenseAcceptance::PromptAcceptance do
60
60
  describe "when the prompt times out" do
61
61
  it "returns false" do
62
62
  expect(Timeout).to receive(:timeout).twice.and_yield
63
- expect(prompt).to receive(:ask).twice.and_raise(LicenseAcceptance::PromptTimeout)
63
+ expect(prompt).to receive(:ask).twice.and_raise(LicenseAcceptance::Strategy::PromptTimeout)
64
64
  expect(prompt).to receive(:unsubscribe).twice
65
65
  expect(prompt).to receive(:reader).twice
66
66
  msg1 = /Prompt timed out./
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+ require "license_acceptance/strategy/provided_value"
3
+
4
+ RSpec.describe LicenseAcceptance::Strategy::ProvidedValue do
5
+ let(:acc) { LicenseAcceptance::Strategy::ProvidedValue.new(value) }
6
+
7
+ describe "#accepted?" do
8
+ describe "when the value is correct" do
9
+ let(:value) { "accept" }
10
+ it "returns true" do
11
+ expect(acc.accepted?).to eq(true)
12
+ end
13
+ end
14
+
15
+ describe "when the value is incorrect" do
16
+ let(:value) { nil }
17
+ it "returns false" do
18
+ expect(acc.accepted?).to eq(false)
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "#silent?" do
24
+ describe "when the value is correct" do
25
+ let(:value) { "accept-silent" }
26
+ it "returns true" do
27
+ expect(acc.silent?).to eq(true)
28
+ end
29
+ end
30
+
31
+ describe "when the value is incorrect" do
32
+ let(:value) { "accept" }
33
+ it "returns false" do
34
+ expect(acc.silent?).to eq(false)
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "#no_persist?" do
40
+ describe "when the value is correct" do
41
+ let(:value) { "accept-no-persist" }
42
+ it "returns true" do
43
+ expect(acc.no_persist?).to eq(true)
44
+ end
45
+ end
46
+
47
+ describe "when the value is incorrect" do
48
+ let(:value) { "accept-silent" }
49
+ it "returns false" do
50
+ expect(acc.no_persist?).to eq(false)
51
+ end
52
+ end
53
+ end
54
+
55
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: license-acceptance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - tyler-ball
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-16 00:00:00.000000000 Z
11
+ date: 2019-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -191,28 +191,31 @@ files:
191
191
  - Rakefile
192
192
  - config/product_info.toml
193
193
  - lib/license_acceptance/acceptor.rb
194
- - lib/license_acceptance/arg_acceptance.rb
195
194
  - lib/license_acceptance/cli_flags/mixlib_cli.rb
196
195
  - lib/license_acceptance/cli_flags/thor.rb
197
196
  - lib/license_acceptance/config.rb
198
- - lib/license_acceptance/env_acceptance.rb
199
- - lib/license_acceptance/file_acceptance.rb
200
197
  - lib/license_acceptance/logger.rb
201
198
  - lib/license_acceptance/product.rb
202
199
  - lib/license_acceptance/product_reader.rb
203
200
  - lib/license_acceptance/product_relationship.rb
204
- - lib/license_acceptance/prompt_acceptance.rb
201
+ - lib/license_acceptance/strategy/argument.rb
202
+ - lib/license_acceptance/strategy/base.rb
203
+ - lib/license_acceptance/strategy/environment.rb
204
+ - lib/license_acceptance/strategy/file.rb
205
+ - lib/license_acceptance/strategy/prompt.rb
206
+ - lib/license_acceptance/strategy/provided_value.rb
205
207
  - lib/license_acceptance/version.rb
206
208
  - spec/license_acceptance/acceptor_spec.rb
207
- - spec/license_acceptance/arg_acceptance_spec.rb
208
209
  - spec/license_acceptance/cli_flags/mixlib_cli_spec.rb
209
210
  - spec/license_acceptance/cli_flags/thor_spec.rb
210
211
  - spec/license_acceptance/config_spec.rb
211
- - spec/license_acceptance/env_acceptance_spec.rb
212
- - spec/license_acceptance/file_acceptance_spec.rb
213
212
  - spec/license_acceptance/product_reader_spec.rb
214
213
  - spec/license_acceptance/product_spec.rb
215
- - spec/license_acceptance/prompt_acceptance_spec.rb
214
+ - spec/license_acceptance/strategy/argument_spec.rb
215
+ - spec/license_acceptance/strategy/environment_spec.rb
216
+ - spec/license_acceptance/strategy/file_spec.rb
217
+ - spec/license_acceptance/strategy/prompt_spec.rb
218
+ - spec/license_acceptance/strategy/provided_value_spec.rb
216
219
  - spec/spec_helper.rb
217
220
  homepage: https://github.com/chef/license-acceptance/
218
221
  licenses:
@@ -226,7 +229,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
226
229
  requirements:
227
230
  - - ">="
228
231
  - !ruby/object:Gem::Version
229
- version: '0'
232
+ version: '2.4'
230
233
  required_rubygems_version: !ruby/object:Gem::Requirement
231
234
  requirements:
232
235
  - - ">="
@@ -1,32 +0,0 @@
1
- module LicenseAcceptance
2
- class ArgAcceptance
3
-
4
- def accepted?(argv)
5
- look_for_value(argv, "accept")
6
- end
7
-
8
- def silent?(argv)
9
- look_for_value(argv, "accept-silent")
10
- end
11
-
12
- def no_persist?(argv)
13
- look_for_value(argv, "accept-no-persist")
14
- end
15
-
16
- private
17
-
18
- def look_for_value(argv, sought)
19
- if argv.include?("--chef-license=#{sought}")
20
- return true
21
- end
22
- i = argv.index("--chef-license")
23
- unless i.nil?
24
- val = argv[i+1]
25
- if val != nil && val.downcase == sought
26
- return true
27
- end
28
- end
29
- return false
30
- end
31
- end
32
- end
@@ -1,26 +0,0 @@
1
- module LicenseAcceptance
2
- class EnvAcceptance
3
-
4
- def accepted?(env)
5
- look_for_value(env, "accept")
6
- end
7
-
8
- def silent?(env)
9
- look_for_value(env, "accept-silent")
10
- end
11
-
12
- def no_persist?(env)
13
- look_for_value(env, "accept-no-persist")
14
- end
15
-
16
- private
17
-
18
- def look_for_value(env, sought)
19
- if env['CHEF_LICENSE'] && env['CHEF_LICENSE'].downcase == sought
20
- return true
21
- end
22
- return false
23
- end
24
-
25
- end
26
- end
@@ -1,97 +0,0 @@
1
- require 'date'
2
- require 'yaml'
3
- require 'fileutils'
4
- require 'etc'
5
- require "license_acceptance/logger"
6
-
7
- module LicenseAcceptance
8
- class FileAcceptance
9
- include Logger
10
-
11
- attr_reader :config
12
-
13
- def initialize(config)
14
- @config = config
15
- end
16
-
17
- INVOCATION_TIME = DateTime.now.freeze
18
-
19
- # For all the given products in the product set, search all possible locations for the
20
- # license acceptance files.
21
- def accepted?(product_relationship)
22
- searching = [product_relationship.parent] + product_relationship.children
23
- missing_licenses = searching.clone
24
- logger.debug("Searching for the following licenses: #{missing_licenses.map(&:name)}")
25
-
26
- searching.each do |product|
27
- found = false
28
- config.license_locations.each do |loc|
29
- f = File.join(loc, product.filename)
30
- if File.exist?(f)
31
- found = true
32
- logger.debug("Found license #{product.filename} at #{f}")
33
- missing_licenses.delete(product)
34
- break
35
- end
36
- end
37
- break if missing_licenses.empty?
38
- end
39
- logger.debug("Missing licenses remaining: #{missing_licenses.map(&:name)}")
40
- missing_licenses
41
- end
42
-
43
- def persist(product_relationship, missing_licenses)
44
- parent = product_relationship.parent
45
- parent_version = product_relationship.parent_version
46
- root_dir = config.persist_location
47
-
48
- if !Dir.exist?(root_dir)
49
- begin
50
- FileUtils.mkdir_p(root_dir)
51
- rescue StandardError => e
52
- msg = "Could not create license directory #{root_dir}"
53
- logger.info "#{msg}\n\t#{e.message}\n\t#{e.backtrace.join("\n\t")}"
54
- return [e]
55
- end
56
- end
57
-
58
- errs = []
59
- if missing_licenses.include?(parent)
60
- err = persist_license(root_dir, parent, parent, parent_version)
61
- errs << err unless err.nil?
62
- end
63
- product_relationship.children.each do |child|
64
- if missing_licenses.include?(child)
65
- err = persist_license(root_dir, child, parent, parent_version)
66
- errs << err unless err.nil?
67
- end
68
- end
69
- return errs
70
- end
71
-
72
- private
73
-
74
- def persist_license(folder_path, product, parent, parent_version)
75
- path = File.join(folder_path, product.filename)
76
- logger.info("Persisting a license for #{product.name} at path #{path}")
77
- File.open(path, File::WRONLY | File::CREAT | File::EXCL) do |license_file|
78
- contents = {
79
- name: product.name,
80
- date_accepted: INVOCATION_TIME.iso8601,
81
- accepting_product: parent.name,
82
- accepting_product_version: parent_version,
83
- user: Etc.getlogin,
84
- file_format: 1,
85
- }
86
- contents = Hash[contents.map { |k, v| [k.to_s, v] }]
87
- license_file << YAML.dump(contents)
88
- end
89
- return nil
90
- rescue StandardError => e
91
- msg = "Could not persist license to #{path}"
92
- logger.info "#{msg}\n\t#{e.message}\n\t#{e.backtrace.join("\n\t")}"
93
- return e
94
- end
95
-
96
- end
97
- end
@@ -1,106 +0,0 @@
1
- require 'tty-prompt'
2
- require 'pastel'
3
- require "license_acceptance/logger"
4
- require "timeout"
5
-
6
- module LicenseAcceptance
7
- class PromptAcceptance
8
- include Logger
9
-
10
- attr_reader :output
11
-
12
- def initialize(config)
13
- @output = config.output
14
- end
15
-
16
- WIDTH = 50.freeze
17
- PASTEL = Pastel.new
18
- BORDER = "+---------------------------------------------+".freeze
19
- YES = PASTEL.green.bold("yes")
20
- CHECK = PASTEL.green("✔")
21
-
22
- def request(missing_licenses, &persist_callback)
23
- logger.debug("Requesting a license for #{missing_licenses.map(&:name)}")
24
- c = missing_licenses.size
25
- s = c > 1 ? "s": ""
26
-
27
- acceptance_question = "Do you accept the #{c} product license#{s} (#{YES}/no)?"
28
- output.puts <<~EOM
29
- #{BORDER}
30
- Chef License Acceptance
31
-
32
- Before you can continue, #{c} product license#{s}
33
- must be accepted. View the license at
34
- https://www.chef.io/end-user-license-agreement/
35
-
36
- License#{s} that need accepting:
37
- * #{missing_licenses.map(&:pretty_name).join("\n * ")}
38
-
39
- #{acceptance_question}
40
-
41
- EOM
42
-
43
- if ask(output, c, s, persist_callback)
44
- output.puts BORDER
45
- return true
46
- end
47
-
48
- output.puts <<~EOM
49
-
50
- If you do not accept this license you will
51
- not be able to use Chef products.
52
-
53
- #{acceptance_question}
54
-
55
- EOM
56
-
57
- answer = ask(output, c, s, persist_callback)
58
- if answer != "yes"
59
- output.puts BORDER
60
- end
61
- return answer
62
- end
63
-
64
- private
65
-
66
- def ask(output, c, s, persist_callback)
67
- logger.debug("Attempting to request interactive prompt on TTY")
68
- prompt = TTY::Prompt.new(track_history: false, active_color: :bold, interrupt: :exit, output: output)
69
-
70
- answer = "no"
71
- begin
72
- Timeout::timeout(60, PromptTimeout) do
73
- answer = prompt.ask(">") do |q|
74
- q.modify :down, :trim
75
- q.required true
76
- q.messages[:required?] = "You must enter 'yes' or 'no'"
77
- q.validate /^\s*(yes|no)\s*$/i
78
- q.messages[:valid?] = "You must enter 'yes' or 'no'"
79
- end
80
- end
81
- rescue PromptTimeout
82
- prompt.unsubscribe(prompt.reader)
83
- output.puts "Prompt timed out. Use non-interactive flags or enter an answer within 60 seconds."
84
- end
85
-
86
- if answer == "yes"
87
- output.puts
88
- output.puts "Persisting #{c} product license#{s}..."
89
- errs = persist_callback.call
90
- if errs.empty?
91
- output.puts "#{CHECK} #{c} product license#{s} persisted.\n\n"
92
- else
93
- output.puts <<~EOM
94
- #{CHECK} #{c} product license#{s} accepted.
95
- Could not persist acceptance:\n\t* #{errs.map(&:message).join("\n\t* ")}
96
- EOM
97
- end
98
- return true
99
- end
100
- return false
101
- end
102
-
103
- end
104
-
105
- class PromptTimeout < StandardError; end
106
- end
@@ -1,57 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/arg_acceptance"
3
-
4
- RSpec.describe LicenseAcceptance::ArgAcceptance do
5
- let(:acc) { LicenseAcceptance::ArgAcceptance.new }
6
-
7
- describe "with an accept option" do
8
- describe "#accepted?" do
9
- it "returns true if the args contain the required flag with spaces" do
10
- expect(acc.accepted?(["--chef-license", "accept"])).to eq(true)
11
- end
12
-
13
- it "returns true if the args contain the required flag with equal" do
14
- expect(acc.accepted?(["--chef-license=accept"])).to eq(true)
15
- end
16
-
17
- it "returns false if the args do not contain the required value" do
18
- expect(acc.accepted?(["--chef-license"])).to eq(false)
19
- expect(acc.accepted?(["--chef-license=foo"])).to eq(false)
20
- expect(acc.accepted?(["--chef-license", "foo"])).to eq(false)
21
- end
22
- end
23
-
24
- describe "#silent?" do
25
- it "returns true if the args contain the required flag with spaces" do
26
- expect(acc.silent?(["--chef-license", "accept-silent"])).to eq(true)
27
- end
28
-
29
- it "returns true if the args contain the required flag with equal" do
30
- expect(acc.silent?(["--chef-license=accept-silent"])).to eq(true)
31
- end
32
-
33
- it "returns false if the args do not contain the required value" do
34
- expect(acc.silent?(["--chef-license"])).to eq(false)
35
- expect(acc.silent?(["--chef-license=accept"])).to eq(false)
36
- expect(acc.silent?(["--chef-license", "accept"])).to eq(false)
37
- end
38
- end
39
- end
40
-
41
- describe "#no_persist?" do
42
- it "returns true if the args contain the required flag with spaces" do
43
- expect(acc.no_persist?(["--chef-license", "accept-no-persist"])).to eq(true)
44
- end
45
-
46
- it "returns true if the args contain the required flag with equal" do
47
- expect(acc.no_persist?(["--chef-license=accept-no-persist"])).to eq(true)
48
- end
49
-
50
- it "returns false if the args do not contain the required value" do
51
- expect(acc.no_persist?(["--chef-license"])).to eq(false)
52
- expect(acc.no_persist?(["--chef-license=accept"])).to eq(false)
53
- expect(acc.no_persist?(["--chef-license", "accept"])).to eq(false)
54
- end
55
- end
56
-
57
- end