license-acceptance 0.2.10 → 2.1.13

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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +17 -1
  3. data/config/product_info.toml +41 -10
  4. data/lib/license_acceptance/acceptor.rb +107 -50
  5. data/lib/license_acceptance/cli_flags/mixlib_cli.rb +2 -2
  6. data/lib/license_acceptance/cli_flags/thor.rb +3 -3
  7. data/lib/license_acceptance/config.rb +6 -4
  8. data/lib/license_acceptance/logger.rb +1 -1
  9. data/lib/license_acceptance/product.rb +15 -8
  10. data/lib/license_acceptance/product_reader.rb +32 -17
  11. data/lib/license_acceptance/strategy/argument.rb +45 -0
  12. data/lib/license_acceptance/strategy/base.rb +7 -0
  13. data/lib/license_acceptance/strategy/environment.rb +39 -0
  14. data/lib/license_acceptance/strategy/file.rb +101 -0
  15. data/lib/license_acceptance/strategy/prompt.rb +111 -0
  16. data/lib/license_acceptance/strategy/provided_value.rb +31 -0
  17. data/lib/license_acceptance/version.rb +1 -1
  18. metadata +20 -137
  19. data/Gemfile.lock +0 -94
  20. data/Rakefile +0 -6
  21. data/lib/license_acceptance/arg_acceptance.rb +0 -32
  22. data/lib/license_acceptance/env_acceptance.rb +0 -26
  23. data/lib/license_acceptance/file_acceptance.rb +0 -97
  24. data/lib/license_acceptance/prompt_acceptance.rb +0 -106
  25. data/spec/license_acceptance/acceptor_spec.rb +0 -272
  26. data/spec/license_acceptance/arg_acceptance_spec.rb +0 -57
  27. data/spec/license_acceptance/cli_flags/mixlib_cli_spec.rb +0 -14
  28. data/spec/license_acceptance/cli_flags/thor_spec.rb +0 -14
  29. data/spec/license_acceptance/config_spec.rb +0 -111
  30. data/spec/license_acceptance/env_acceptance_spec.rb +0 -65
  31. data/spec/license_acceptance/file_acceptance_spec.rb +0 -121
  32. data/spec/license_acceptance/product_reader_spec.rb +0 -139
  33. data/spec/license_acceptance/product_spec.rb +0 -13
  34. data/spec/license_acceptance/prompt_acceptance_spec.rb +0 -100
  35. data/spec/spec_helper.rb +0 -25
@@ -1,65 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/env_acceptance"
3
-
4
- RSpec.describe LicenseAcceptance::EnvAcceptance do
5
- let(:acc) { LicenseAcceptance::EnvAcceptance.new }
6
-
7
- describe "when passed an accept value" do
8
- describe "#accepted?" do
9
- it "returns true if the env contains the correct key and value" do
10
- env = {"CHEF_LICENSE" => "accept"}
11
- expect(acc.accepted?(env)).to eq(true)
12
- end
13
-
14
- it "returns false if the env has a key but nil value" do
15
- env = {"CHEF_LICENSE" => nil}
16
- expect(acc.accepted?(env)).to eq(false)
17
- end
18
-
19
- it "returns false if the env has a key but incorrect value" do
20
- env = {"CHEF_LICENSE" => "foo"}
21
- expect(acc.accepted?(env)).to eq(false)
22
- end
23
- end
24
-
25
- describe "#silent?" do
26
- it "returns true if the env contains the correct key and value" do
27
- env = {"CHEF_LICENSE" => "accept-silent"}
28
- expect(acc.silent?(env)).to eq(true)
29
- end
30
-
31
- it "returns false if the env has a key but nil value" do
32
- env = {"CHEF_LICENSE" => nil}
33
- expect(acc.silent?(env)).to eq(false)
34
- end
35
-
36
- it "returns false if the env has a key but incorrect value" do
37
- env = {"CHEF_LICENSE" => "foo"}
38
- expect(acc.silent?(env)).to eq(false)
39
- env = {"CHEF_LICENSE" => "accept"}
40
- expect(acc.silent?(env)).to eq(false)
41
- end
42
- end
43
-
44
- end
45
-
46
- describe "#no_persist?" do
47
- it "returns true if the env contains the correct key and value" do
48
- env = {"CHEF_LICENSE" => "accept-no-persist"}
49
- expect(acc.no_persist?(env)).to eq(true)
50
- end
51
-
52
- it "returns false if the env has a key but nil value" do
53
- env = {"CHEF_LICENSE" => nil}
54
- expect(acc.no_persist?(env)).to eq(false)
55
- end
56
-
57
- it "returns false if the env has a key but incorrect value" do
58
- env = {"CHEF_LICENSE" => "foo"}
59
- expect(acc.no_persist?(env)).to eq(false)
60
- env = {"CHEF_LICENSE" => "accept"}
61
- expect(acc.no_persist?(env)).to eq(false)
62
- end
63
- end
64
-
65
- end
@@ -1,121 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/config"
3
- require "license_acceptance/file_acceptance"
4
- require "license_acceptance/product_relationship"
5
- require "license_acceptance/product"
6
-
7
- RSpec.describe LicenseAcceptance::FileAcceptance do
8
- let(:dir1) { "/dir1" }
9
- let(:dir2) { "/dir2" }
10
- let(:dir3) { "/dir3" }
11
- let(:config) do
12
- instance_double(LicenseAcceptance::Config, license_locations: [dir1, dir2], persist_location: dir3)
13
- end
14
- let(:acc) { LicenseAcceptance::FileAcceptance.new(config) }
15
- let(:p1_name) { "chef_client" }
16
- let(:p1_filename) { "p1_filename" }
17
- let(:p1) { instance_double(LicenseAcceptance::Product, name: p1_name, filename: p1_filename) }
18
- let(:version) { "0.1.0" }
19
- let(:product_relationship) { instance_double(LicenseAcceptance::ProductRelationship, parent: p1, children: [], parent_version: version) }
20
- let(:mode) { File::WRONLY | File::CREAT | File::EXCL }
21
-
22
- describe "#check" do
23
- describe "when there is an existing license file" do
24
- it "returns an empty missing product list" do
25
- expect(File).to receive(:exist?).with(File.join(dir1, p1_filename)).and_return(true)
26
- expect(acc.accepted?(product_relationship)).to eq([])
27
- end
28
- end
29
-
30
- describe "when there is not an existing license file" do
31
- it "returns the product in the missing product list" do
32
- expect(File).to receive(:exist?).with(File.join(dir1, p1_filename)).and_return(false)
33
- expect(File).to receive(:exist?).with(File.join(dir2, p1_filename)).and_return(false)
34
- expect(acc.accepted?(product_relationship)).to eq([p1])
35
- end
36
- end
37
-
38
- describe "#persist" do
39
- let(:file) { double("file") }
40
-
41
- it "stores a single license without children" do
42
- expect(Dir).to receive(:exist?).with(dir3).and_return(true)
43
- expect(File).to receive(:open).with(File.join(dir3, p1_filename), mode).and_yield(file)
44
- expect(file).to receive(:<<) do |yaml|
45
- yaml = YAML.load(yaml)
46
- expect(yaml["name"]).to eq(p1_name)
47
- expect(yaml["accepting_product"]).to eq(p1_name)
48
- expect(yaml["accepting_product_version"]).to eq(version)
49
- end
50
- expect(acc.persist(product_relationship, [p1])).to eq([])
51
- end
52
-
53
- describe "when license has children" do
54
- let(:p2_name) { "inspec" }
55
- let(:p2_filename) { "p2_filename" }
56
- let(:p2) { instance_double(LicenseAcceptance::Product, name: p2_name, filename: p2_filename) }
57
- let(:product_relationship) {
58
- instance_double(
59
- LicenseAcceptance::ProductRelationship,
60
- parent: p1,
61
- children: [p2],
62
- parent_version: version
63
- )
64
- }
65
-
66
- it "stores a license file for all" do
67
- expect(Dir).to receive(:exist?).with(dir3).and_return(true)
68
- expect(File).to receive(:open).with(File.join(dir3, p1_filename), mode).and_yield(file)
69
- expect(file).to receive(:<<) do |yaml|
70
- yaml = YAML.load(yaml)
71
- expect(yaml["name"]).to eq(p1_name)
72
- expect(yaml["accepting_product"]).to eq(p1_name)
73
- expect(yaml["accepting_product_version"]).to eq(version)
74
- end
75
- expect(File).to receive(:open).with(File.join(dir3, p2_filename), mode).and_yield(file)
76
- expect(file).to receive(:<<) do |yaml|
77
- yaml = YAML.load(yaml)
78
- expect(yaml["name"]).to eq(p2_name)
79
- expect(yaml["accepting_product"]).to eq(p1_name)
80
- expect(yaml["accepting_product_version"]).to eq(version)
81
- end
82
- expect(acc.persist(product_relationship, [p1, p2])).to eq([])
83
- end
84
-
85
- describe "when parent is already persisted" do
86
- it "only stores a license file for the child" do
87
- expect(Dir).to receive(:exist?).with(dir3).and_return(true)
88
- expect(File).to receive(:open).once.with(File.join(dir3, p2_filename), mode).and_yield(file)
89
- expect(file).to receive(:<<) do |yaml|
90
- yaml = YAML.load(yaml)
91
- expect(yaml["name"]).to eq(p2_name)
92
- expect(yaml["accepting_product"]).to eq(p1_name)
93
- expect(yaml["accepting_product_version"]).to eq(version)
94
- end
95
- expect(acc.persist(product_relationship, [p2])).to eq([])
96
- end
97
- end
98
- end
99
-
100
- describe "when the folder cannot be created" do
101
- let(:err) { StandardError.new("foo") }
102
- it "returns the error" do
103
- expect(Dir).to receive(:exist?).with(dir3).and_return(false)
104
- expect(FileUtils).to receive(:mkdir_p).and_raise(err)
105
- expect(File).to_not receive(:open)
106
- expect(acc.persist(product_relationship, [p1])).to eq([err])
107
- end
108
- end
109
-
110
- describe "when the file cannot be written" do
111
- let(:err) { StandardError.new("bar") }
112
- it "returns the error" do
113
- expect(Dir).to receive(:exist?).with(dir3).and_return(true)
114
- expect(File).to receive(:open).with(File.join(dir3, p1_filename), mode).and_raise(err)
115
- expect(acc.persist(product_relationship, [p1])).to eq([err])
116
- end
117
- end
118
- end
119
-
120
- end
121
- end
@@ -1,139 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/product_reader"
3
- require "license_acceptance/product_relationship"
4
-
5
- RSpec.describe LicenseAcceptance::ProductReader do
6
- let(:reader) { LicenseAcceptance::ProductReader.new }
7
- let(:version) { "0.1.0" }
8
- let(:location) { "location" }
9
-
10
- let(:p1) { {"name" => "p1", "pretty_name" => "P1", "filename" => "f1"} }
11
- let(:p2) { {"name" => "p2", "pretty_name" => "P2", "filename" => "f2"} }
12
- # defined the `==` operator on Product for ease of comparison
13
- let(:product1) { LicenseAcceptance::Product.new(p1["name"], p1["pretty_name"], p1["filename"]) }
14
- let(:product2) { LicenseAcceptance::Product.new(p2["name"], p2["pretty_name"], p2["filename"]) }
15
- let(:r1) { {p1 => p2} }
16
- let(:toml) { {"products" => [p1, p2], "relationships" => {"p1" => ["p2"]}} }
17
-
18
- describe "#read" do
19
- it "reads products and relationships" do
20
- expect(reader).to receive(:get_location).and_return(location)
21
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return(toml)
22
- reader.read
23
- expect(reader.products).to eq({
24
- "p1" => product1,
25
- "p2" => product2
26
- })
27
- expect(reader.relationships.size).to eq(1)
28
- expect(reader.relationships.first).to eq([product1, [product2]])
29
- end
30
-
31
- describe "with an empty file" do
32
- it "raises a InvalidProductInfo error" do
33
- expect(reader).to receive(:get_location).and_return(location)
34
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return({})
35
-
36
- expect { reader.read }.to raise_error(LicenseAcceptance::InvalidProductInfo)
37
- end
38
- end
39
-
40
- describe "with an unknown parent" do
41
- let(:toml) { {"products" => [p1, p2], "relationships" => {"p3" => ["p2"]}} }
42
-
43
- it "raises a UnknownParent error" do
44
- expect(reader).to receive(:get_location).and_return(location)
45
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return(toml)
46
-
47
- expect { reader.read }.to raise_error(LicenseAcceptance::UnknownParent)
48
- end
49
- end
50
-
51
- describe "with a relationship of nil children" do
52
- let(:toml) { {"products" => [p1], "relationships" => {"p1" => nil}} }
53
-
54
- it "raises a NoChildRelationships error" do
55
- expect(reader).to receive(:get_location).and_return(location)
56
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return(toml)
57
-
58
- expect { reader.read }.to raise_error(LicenseAcceptance::NoChildRelationships)
59
- end
60
- end
61
-
62
- describe "with a relationship of empty children" do
63
- let(:toml) { {"products" => [p1], "relationships" => {"p1" => []}} }
64
-
65
- it "raises a NoChildRelationships error" do
66
- expect(reader).to receive(:get_location).and_return(location)
67
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return(toml)
68
-
69
- expect { reader.read }.to raise_error(LicenseAcceptance::NoChildRelationships)
70
- end
71
- end
72
-
73
- describe "with a relationship of non-array children" do
74
- let(:toml) { {"products" => [p1], "relationships" => {"p1" => "p2"}} }
75
-
76
- it "raises a NoChildRelationships error" do
77
- expect(reader).to receive(:get_location).and_return(location)
78
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return(toml)
79
-
80
- expect { reader.read }.to raise_error(LicenseAcceptance::NoChildRelationships)
81
- end
82
- end
83
-
84
- describe "with an unknown child" do
85
- let(:toml) { {"products" => [p1, p2], "relationships" => {"p1" => ["p2", "p3"]}} }
86
-
87
- it "raises a UnknownChild error" do
88
- expect(reader).to receive(:get_location).and_return(location)
89
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return(toml)
90
-
91
- expect { reader.read }.to raise_error(LicenseAcceptance::UnknownChild)
92
- end
93
- end
94
- end
95
-
96
- describe "::lookup" do
97
- before do
98
- expect(reader).to receive(:get_location).and_return(location)
99
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return(toml)
100
- reader.read
101
- end
102
-
103
- it "returns a ProductRelationship instance successfully" do
104
- expect(reader.lookup("p1", version)).to be_an_instance_of(LicenseAcceptance::ProductRelationship) do |instance|
105
- expect(instance.parent_product).to eq(product1)
106
- expect(instance.children).to eq([prouct2])
107
- expect(instance.version).to eq(version)
108
- end
109
- end
110
-
111
- describe "when called on an unknown product" do
112
- it "raises an UnknownProduct error" do
113
- expect { reader.lookup("DNE", nil) }.to raise_error(LicenseAcceptance::UnknownProduct)
114
- end
115
- end
116
-
117
- let(:nonya) { LicenseAcceptance::Product.new("nonya", "NonYa", "nofile") }
118
- describe "when called on a product with no relationship" do
119
- before do
120
- reader.products = { "nonya" => nonya }
121
- end
122
-
123
- it "returns the product" do
124
- expect(reader.lookup('nonya', version)).to be_an_instance_of(LicenseAcceptance::ProductRelationship) do |instance|
125
- expect(instance.parent_product).to eq(nonya)
126
- expect(instance.children).to eq([])
127
- expect(instance.version).to eq(version)
128
- end
129
- end
130
- end
131
-
132
- describe "when called with an invalid parent version type" do
133
- it "raises an ProductVersionTypeError error" do
134
- expect { reader.lookup("p1", 1) }.to raise_error(LicenseAcceptance::ProductVersionTypeError)
135
- end
136
- end
137
- end
138
-
139
- end
@@ -1,13 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/product"
3
-
4
- RSpec.describe LicenseAcceptance::Product do
5
- let(:instance) { LicenseAcceptance::Product.new("name", "Pretty Name", "filename") }
6
-
7
- it "can lookup the product attributes" do
8
- expect(instance.name).to eq("name")
9
- expect(instance.pretty_name).to eq("Pretty Name")
10
- expect(instance.filename).to eq("filename")
11
- end
12
-
13
- end
@@ -1,100 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/prompt_acceptance"
3
- require "license_acceptance/product"
4
- require "tty-prompt"
5
-
6
- RSpec.describe LicenseAcceptance::PromptAcceptance do
7
- let(:output) { StringIO.new }
8
- let(:config) do
9
- instance_double(LicenseAcceptance::Config, output: output)
10
- end
11
- let(:acc) { LicenseAcceptance::PromptAcceptance.new(config) }
12
- let(:prompt) { instance_double(TTY::Prompt) }
13
- let(:p1) { instance_double(LicenseAcceptance::Product, name: "name", pretty_name: "Pretty Name") }
14
- let(:missing_licenses) { [p1] }
15
-
16
- before do
17
- expect(TTY::Prompt).to receive(:new).at_least(:once).and_return(prompt)
18
- end
19
-
20
- describe "when the user accepts" do
21
- it "returns true" do
22
- expect(prompt).to receive(:ask).and_return("yes")
23
- msg1 = /License that need accepting:\n \* #{p1.pretty_name}/m
24
- msg2 = /product license persisted\./
25
- b = Proc.new { [] }
26
- expect(acc.request(missing_licenses, &b)).to eq(true)
27
- expect(output.string).to match(msg1)
28
- expect(output.string).to match(msg2)
29
- end
30
-
31
- describe "when there are multiple products" do
32
- let(:p2) { instance_double(LicenseAcceptance::Product, name: "other_name", pretty_name: "Other") }
33
- let(:missing_licenses) { [p1, p2] }
34
- it "returns true" do
35
- expect(prompt).to receive(:ask).and_return("yes")
36
- msg1 = /Licenses that need accepting:\n \* #{p1.pretty_name}\n \* #{p2.pretty_name}/m
37
- msg2 = /product licenses persisted\./
38
- msg3 = /2 product licenses\nmust be accepted/m
39
- b = Proc.new { [] }
40
- expect(acc.request(missing_licenses, &b)).to eq(true)
41
- expect(output.string).to match(msg1)
42
- expect(output.string).to match(msg2)
43
- expect(output.string).to match(msg3)
44
- end
45
- end
46
-
47
- describe "when the callback returns an error" do
48
- it "returns true" do
49
- expect(prompt).to receive(:ask).and_return("yes")
50
- msg1 = /License that need accepting:\n \* #{p1.pretty_name}/m
51
- msg2 = /Could not persist acceptance:/
52
- b = Proc.new { [StandardError.new("foo")] }
53
- expect(acc.request(missing_licenses, &b)).to eq(true)
54
- expect(output.string).to match(msg1)
55
- expect(output.string).to match(msg2)
56
- end
57
- end
58
- end
59
-
60
- describe "when the prompt times out" do
61
- it "returns false" do
62
- expect(Timeout).to receive(:timeout).twice.and_yield
63
- expect(prompt).to receive(:ask).twice.and_raise(LicenseAcceptance::PromptTimeout)
64
- expect(prompt).to receive(:unsubscribe).twice
65
- expect(prompt).to receive(:reader).twice
66
- msg1 = /Prompt timed out./
67
- b = Proc.new { [] }
68
- expect(acc.request(missing_licenses, &b)).to eq(false)
69
- expect(output.string).to match(msg1)
70
- end
71
- end
72
-
73
- describe "when the user declines twice" do
74
- it "returns false" do
75
- expect(prompt).to receive(:ask).twice.and_return("no")
76
- msg1 = /License that need accepting:\n \* #{p1.pretty_name}/m
77
- msg2 = /product license persisted\./
78
- b = Proc.new { raise "should not be called" }
79
- expect(acc.request(missing_licenses, &b)).to eq(false)
80
- expect(output.string).to match(msg1)
81
- expect(output.string).to_not match(msg2)
82
- end
83
- end
84
-
85
- describe "when the user declines once then accepts" do
86
- it "returns true" do
87
- expect(prompt).to receive(:ask).and_return("no")
88
- expect(prompt).to receive(:ask).and_return("yes")
89
- msg1 = /License that need accepting:\n \* #{p1.pretty_name}/m
90
- msg2 = /product license persisted\./
91
- msg3 = /If you do not accept this license you will\nnot be able to use Chef products/m
92
- b = Proc.new { [] }
93
- expect(acc.request(missing_licenses, &b)).to eq(true)
94
- expect(output.string).to match(msg1)
95
- expect(output.string).to match(msg2)
96
- expect(output.string).to match(msg3)
97
- end
98
- end
99
-
100
- end
data/spec/spec_helper.rb DELETED
@@ -1,25 +0,0 @@
1
- require "bundler/setup"
2
- require "license_acceptance/logger"
3
- require "logger"
4
-
5
- RSpec.configure do |config|
6
- # Enable flags like --only-failures and --next-failure
7
- config.example_status_persistence_file_path = ".rspec_status"
8
-
9
- # Disable RSpec exposing methods globally on `Module` and `main`
10
- config.disable_monkey_patching!
11
-
12
- config.filter_run :focus => true
13
- config.run_all_when_everything_filtered = true
14
- config.mock_with :rspec do |mocks|
15
- mocks.verify_partial_doubles = true
16
- end
17
-
18
- config.expect_with :rspec do |c|
19
- c.syntax = :expect
20
- end
21
-
22
- config.before(:all) do
23
- LicenseAcceptance::Logger.initialize(::Logger.new(IO::NULL))
24
- end
25
- end