license-acceptance 1.0.5 → 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 (31) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +17 -1
  3. data/config/product_info.toml +23 -1
  4. data/lib/license_acceptance/acceptor.rb +54 -37
  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 +8 -6
  10. data/lib/license_acceptance/product_reader.rb +11 -7
  11. data/lib/license_acceptance/strategy/argument.rb +17 -16
  12. data/lib/license_acceptance/strategy/environment.rb +11 -10
  13. data/lib/license_acceptance/strategy/file.rb +10 -12
  14. data/lib/license_acceptance/strategy/prompt.rb +26 -26
  15. data/lib/license_acceptance/strategy/provided_value.rb +8 -5
  16. data/lib/license_acceptance/version.rb +1 -1
  17. metadata +14 -134
  18. data/Gemfile.lock +0 -94
  19. data/Rakefile +0 -6
  20. data/spec/license_acceptance/acceptor_spec.rb +0 -293
  21. data/spec/license_acceptance/cli_flags/mixlib_cli_spec.rb +0 -14
  22. data/spec/license_acceptance/cli_flags/thor_spec.rb +0 -14
  23. data/spec/license_acceptance/config_spec.rb +0 -111
  24. data/spec/license_acceptance/product_reader_spec.rb +0 -155
  25. data/spec/license_acceptance/product_spec.rb +0 -15
  26. data/spec/license_acceptance/strategy/argument_spec.rb +0 -82
  27. data/spec/license_acceptance/strategy/environment_spec.rb +0 -76
  28. data/spec/license_acceptance/strategy/file_spec.rb +0 -127
  29. data/spec/license_acceptance/strategy/prompt_spec.rb +0 -100
  30. data/spec/license_acceptance/strategy/provided_value_spec.rb +0 -55
  31. data/spec/spec_helper.rb +0 -25
@@ -1,111 +0,0 @@
1
- require "spec_helper"
2
- require "climate_control"
3
- require "license_acceptance/config"
4
- require "license_acceptance/product_relationship"
5
-
6
- RSpec.describe LicenseAcceptance::Config do
7
- let(:opts) { {} }
8
- let(:config) { LicenseAcceptance::Config.new(opts) }
9
-
10
- it "loads correctly with default values" do
11
- config
12
- end
13
-
14
- describe "with overwritten values" do
15
- let(:output) { StringIO.new }
16
- let(:logger) { "logger" }
17
- let(:license_locations) { [] }
18
- let(:persist_location) { "foo" }
19
- let(:persist) { false }
20
- let(:opts) { { output: output, logger: logger, license_locations: license_locations, persist_location: persist_location, persist: persist } }
21
-
22
- it "loads correctly" do
23
- expect(config.output).to eq(output)
24
- expect(config.logger).to eq(logger)
25
- expect(config.license_locations).to eq(license_locations)
26
- expect(config.persist_location).to eq("foo")
27
- expect(config.persist).to eq(false)
28
- end
29
- end
30
-
31
- describe "#default_license_locations and #default_persist_location" do
32
- before do
33
- expect(Process).to receive(:uid).and_return(uid)
34
- end
35
-
36
- describe "when platform is Windows" do
37
- before do
38
- stub_const("RUBY_PLATFORM", "mingw")
39
- end
40
-
41
- describe "when user is Administrator" do
42
- let(:uid) { 0 }
43
-
44
- it "returns the default value" do
45
- ClimateControl.modify HOMEDRIVE: "C:" do
46
- expect(config.license_locations).to eq(["C:/chef/accepted_licenses/"])
47
- expect(config.persist_location).to eq("C:/chef/accepted_licenses/")
48
- end
49
- end
50
- end
51
-
52
- describe "when user is not Administrator" do
53
- let(:uid) { 1000 }
54
-
55
- it "returns the default USERPROFILE value" do
56
- ClimateControl.modify HOMEDRIVE: "C:", USERPROFILE: "C:/Users/foo", HOME: nil do
57
- expect(Dir).to receive(:exist?).with("C:/Users/foo").and_return(true)
58
- expect(config.license_locations).to eq([
59
- "C:/chef/accepted_licenses/",
60
- "C:/Users/foo/.chef/accepted_licenses/"
61
- ])
62
- expect(config.persist_location).to eq("C:/Users/foo/.chef/accepted_licenses/")
63
- end
64
- end
65
-
66
- it "returns the default HOMEDRIVE + HOMEPATH value" do
67
- ClimateControl.modify HOMEDRIVE: "C:", USERPROFILE: "C:/Users/bar", HOME: nil do
68
- expect(Dir).to receive(:exist?).with("C:/Users/bar").and_return(true)
69
- expect(config.license_locations).to eq([
70
- "C:/chef/accepted_licenses/",
71
- "C:/Users/bar/.chef/accepted_licenses/"
72
- ])
73
- expect(config.persist_location).to eq("C:/Users/bar/.chef/accepted_licenses/")
74
- end
75
- end
76
- end
77
-
78
- end
79
-
80
- describe "when platform is non-Windows" do
81
- before do
82
- stub_const("RUBY_PLATFORM", "darwin")
83
- end
84
-
85
- describe "when user is root" do
86
- let(:uid) { 0 }
87
-
88
- it "returns the default value" do
89
- expect(config.license_locations).to eq(["/etc/chef/accepted_licenses/"])
90
- expect(config.persist_location).to eq("/etc/chef/accepted_licenses/")
91
- end
92
- end
93
-
94
- describe "when user is not root" do
95
- let(:uid) { 1000 }
96
-
97
- it "returns the default user value" do
98
- ClimateControl.modify HOME: "/Users/foo" do
99
- expect(config.license_locations).to eq([
100
- "/etc/chef/accepted_licenses/",
101
- "/Users/foo/.chef/accepted_licenses/"
102
- ])
103
- expect(config.persist_location).to eq("/Users/foo/.chef/accepted_licenses/")
104
- end
105
- end
106
- end
107
-
108
- end
109
- end
110
-
111
- end
@@ -1,155 +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) { {"id" => "p1", "pretty_name" => "P1", "filename" => "f1", "mixlib_name" => "p1m", "license_required_version" => "p1v"} }
11
- let(:p2) { {"id" => "p2", "pretty_name" => "P2", "filename" => "f2", "mixlib_name" => "p2m", "license_required_version" => "p2v"} }
12
- # defined the `==` operator on Product for ease of comparison
13
- let(:product1) { LicenseAcceptance::Product.new(p1["id"], p1["pretty_name"], p1["filename"], p1["mixlib_name"], p1["license_required_version"]) }
14
- let(:product2) { LicenseAcceptance::Product.new(p2["id"], p2["pretty_name"], p2["filename"], p2["mixlib_name"], p2["license_required_version"]) }
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", "no_mixlib", "no_version") }
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
- describe "::lookup_by_mixlib" do
140
- before do
141
- expect(reader).to receive(:get_location).and_return(location)
142
- expect(Tomlrb).to receive(:load_file).with(location, symbolize_keys: false).and_return(toml)
143
- reader.read
144
- end
145
-
146
- it "returns a Product successfully" do
147
- expect(reader.lookup_by_mixlib("p1m")).to eq(product1)
148
- end
149
-
150
- it "returns nil for an unknown product" do
151
- expect(reader.lookup_by_mixlib("foo")).to eq(nil)
152
- end
153
- end
154
-
155
- end
@@ -1,15 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/product"
3
-
4
- RSpec.describe LicenseAcceptance::Product do
5
- let(:instance) { LicenseAcceptance::Product.new("id", "Pretty Name", "filename", "mixlib_name", "version") }
6
-
7
- it "can lookup the product attributes" do
8
- expect(instance.id).to eq("id")
9
- expect(instance.pretty_name).to eq("Pretty Name")
10
- expect(instance.filename).to eq("filename")
11
- expect(instance.mixlib_name).to eq("mixlib_name")
12
- expect(instance.license_required_version).to eq("version")
13
- end
14
-
15
- end
@@ -1,82 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/strategy/argument"
3
-
4
- RSpec.describe LicenseAcceptance::Strategy::Argument do
5
- let(:acc) { LicenseAcceptance::Strategy::Argument.new(argv) }
6
-
7
- describe "#accepted?" do
8
- describe "when value is space seperated" do
9
- let(:argv) { ["--chef-license", "accept"] }
10
- it "returns true if the args contain the required flag with spaces" do
11
- expect(acc.accepted?).to eq(true)
12
- end
13
- end
14
-
15
- describe "when the value is equal seperated" do
16
- let(:argv) { ["--chef-license=accept"] }
17
- it "returns true if the args contain the required flag with equal" do
18
- expect(acc.accepted?).to eq(true)
19
- end
20
- end
21
-
22
- [ ["--chef-license"], ["--chef-license=foo"], ["--chef-license", "foo"] ].each do |v|
23
- describe "when the value is #{v}" do
24
- let(:argv) { v }
25
- it "returns false if the args do not contain the required value" do
26
- expect(acc.accepted?).to eq(false)
27
- end
28
- end
29
- end
30
- end
31
-
32
- describe "#silent?" do
33
- describe "when value is space seperated" do
34
- let(:argv) { ["--chef-license", "accept-silent"] }
35
- it "returns true if the args contain the required flag with spaces" do
36
- expect(acc.silent?).to eq(true)
37
- end
38
- end
39
-
40
- describe "when the value is equal seperated" do
41
- let(:argv) { ["--chef-license=accept-silent"] }
42
- it "returns true if the args contain the required flag with equal" do
43
- expect(acc.silent?).to eq(true)
44
- end
45
- end
46
-
47
- [ ["--chef-license"], ["--chef-license=accept"], ["--chef-license", "accept"] ].each do |v|
48
- describe "when the value is #{v}" do
49
- let(:argv) { v }
50
- it "returns false if the args do not contain the required value" do
51
- expect(acc.silent?).to eq(false)
52
- end
53
- end
54
- end
55
- end
56
-
57
- describe "#no_persist?" do
58
- describe "when value is space seperated" do
59
- let(:argv) { ["--chef-license", "accept-no-persist"] }
60
- it "returns true if the args contain the required flag with spaces" do
61
- expect(acc.no_persist?).to eq(true)
62
- end
63
- end
64
-
65
- describe "when the value is equal seperated" do
66
- let(:argv) { ["--chef-license=accept-no-persist"] }
67
- it "returns true if the args contain the required flag with equal" do
68
- expect(acc.no_persist?).to eq(true)
69
- end
70
- end
71
-
72
- [ ["--chef-license"], ["--chef-license=accept"], ["--chef-license", "accept"] ].each do |v|
73
- describe "when the value is #{v}" do
74
- let(:argv) { v }
75
- it "returns false if the args do not contain the required value" do
76
- expect(acc.no_persist?).to eq(false)
77
- end
78
- end
79
- end
80
- end
81
-
82
- end
@@ -1,76 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/strategy/environment"
3
-
4
- RSpec.describe LicenseAcceptance::Strategy::Environment do
5
- let(:acc) { LicenseAcceptance::Strategy::Environment.new(env) }
6
-
7
- describe "#accepted?" do
8
- describe "when the environment contains the correct key and value" do
9
- let(:env) { {"CHEF_LICENSE" => "accept"} }
10
- it "returns true" do
11
- expect(acc.accepted?).to eq(true)
12
- end
13
- end
14
-
15
- describe "when the env has a key but nil value" do
16
- let(:env) { {"CHEF_LICENSE" => nil} }
17
- it "returns false" do
18
- expect(acc.accepted?).to eq(false)
19
- end
20
- end
21
-
22
- describe "when the env has a key but incorrect value" do
23
- let(:env) { {"CHEF_LICENSE" => "foo"} }
24
- it "returns false" do
25
- expect(acc.accepted?).to eq(false)
26
- end
27
- end
28
- end
29
-
30
- describe "#silent?" do
31
- describe "when the environment contains the correct key and value" do
32
- let(:env) { {"CHEF_LICENSE" => "accept-silent"} }
33
- it "returns true" do
34
- expect(acc.silent?).to eq(true)
35
- end
36
- end
37
-
38
- describe "when the env has a key but nil value" do
39
- let(:env) { {"CHEF_LICENSE" => nil} }
40
- it "returns false" do
41
- expect(acc.silent?).to eq(false)
42
- end
43
- end
44
-
45
- describe "when the env has a key but incorrect value" do
46
- let(:env) { {"CHEF_LICENSE" => "accept"} }
47
- it "returns false" do
48
- expect(acc.silent?).to eq(false)
49
- end
50
- end
51
- end
52
-
53
- describe "#no_persist?" do
54
- describe "when the environment contains the correct key and value" do
55
- let(:env) { {"CHEF_LICENSE" => "accept-no-persist"} }
56
- it "returns true" do
57
- expect(acc.no_persist?).to eq(true)
58
- end
59
- end
60
-
61
- describe "when the env has a key but nil value" do
62
- let(:env) { {"CHEF_LICENSE" => nil} }
63
- it "returns false" do
64
- expect(acc.no_persist?).to eq(false)
65
- end
66
- end
67
-
68
- describe "when the env has a key but incorrect value" do
69
- let(:env) { {"CHEF_LICENSE" => "accept-silent"} }
70
- it "returns false" do
71
- expect(acc.no_persist?).to eq(false)
72
- end
73
- end
74
- end
75
-
76
- end
@@ -1,127 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/config"
3
- require "license_acceptance/strategy/file"
4
- require "license_acceptance/product_relationship"
5
- require "license_acceptance/product"
6
-
7
- RSpec.describe LicenseAcceptance::Strategy::File 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::Strategy::File.new(config) }
15
- let(:p1_id) { "foo" }
16
- let(:p1_filename) { "p1_filename" }
17
- let(:p1_pretty) { "Pretty Name" }
18
- let(:p1) { instance_double(LicenseAcceptance::Product, id: p1_id, filename: p1_filename, pretty_name: p1_pretty) }
19
- let(:version) { "0.1.0" }
20
- let(:product_relationship) { instance_double(LicenseAcceptance::ProductRelationship, parent: p1, children: [], parent_version: version) }
21
- let(:mode) { File::WRONLY | File::CREAT | File::EXCL }
22
-
23
- describe "#check" do
24
- describe "when there is an existing license file" do
25
- it "returns an empty missing product list" do
26
- expect(File).to receive(:exist?).with(File.join(dir1, p1_filename)).and_return(true)
27
- expect(acc.accepted?(product_relationship)).to eq([])
28
- end
29
- end
30
-
31
- describe "when there is not an existing license file" do
32
- it "returns the product in the missing product list" do
33
- expect(File).to receive(:exist?).with(File.join(dir1, p1_filename)).and_return(false)
34
- expect(File).to receive(:exist?).with(File.join(dir2, p1_filename)).and_return(false)
35
- expect(acc.accepted?(product_relationship)).to eq([p1])
36
- end
37
- end
38
-
39
- describe "#persist" do
40
- let(:file) { double("file") }
41
-
42
- it "stores a single license without children" do
43
- expect(Dir).to receive(:exist?).with(dir3).and_return(true)
44
- expect(File).to receive(:open).with(File.join(dir3, p1_filename), mode).and_yield(file)
45
- expect(file).to receive(:<<) do |yaml|
46
- yaml = YAML.load(yaml)
47
- expect(yaml["id"]).to eq(p1_id)
48
- expect(yaml["name"]).to eq(p1_pretty)
49
- expect(yaml["accepting_product"]).to eq(p1_id)
50
- expect(yaml["accepting_product_version"]).to eq(version)
51
- end
52
- expect(acc.persist(product_relationship, [p1])).to eq([])
53
- end
54
-
55
- describe "when license has children" do
56
- let(:p2_id) { "bar" }
57
- let(:p2_filename) { "p2_filename" }
58
- let(:p2_pretty) { "Other Pretty Name" }
59
- let(:p2) { instance_double(LicenseAcceptance::Product, id: p2_id, filename: p2_filename, pretty_name: p2_pretty) }
60
- let(:product_relationship) {
61
- instance_double(
62
- LicenseAcceptance::ProductRelationship,
63
- parent: p1,
64
- children: [p2],
65
- parent_version: version
66
- )
67
- }
68
-
69
- it "stores a license file for all" do
70
- expect(Dir).to receive(:exist?).with(dir3).and_return(true)
71
- expect(File).to receive(:open).with(File.join(dir3, p1_filename), mode).and_yield(file)
72
- expect(file).to receive(:<<) do |yaml|
73
- yaml = YAML.load(yaml)
74
- expect(yaml["id"]).to eq(p1_id)
75
- expect(yaml["name"]).to eq(p1_pretty)
76
- expect(yaml["accepting_product"]).to eq(p1_id)
77
- expect(yaml["accepting_product_version"]).to eq(version)
78
- end
79
- expect(File).to receive(:open).with(File.join(dir3, p2_filename), mode).and_yield(file)
80
- expect(file).to receive(:<<) do |yaml|
81
- yaml = YAML.load(yaml)
82
- expect(yaml["id"]).to eq(p2_id)
83
- expect(yaml["name"]).to eq(p2_pretty)
84
- expect(yaml["accepting_product"]).to eq(p1_id)
85
- expect(yaml["accepting_product_version"]).to eq(version)
86
- end
87
- expect(acc.persist(product_relationship, [p1, p2])).to eq([])
88
- end
89
-
90
- describe "when parent is already persisted" do
91
- it "only stores a license file for the child" do
92
- expect(Dir).to receive(:exist?).with(dir3).and_return(true)
93
- expect(File).to receive(:open).once.with(File.join(dir3, p2_filename), mode).and_yield(file)
94
- expect(file).to receive(:<<) do |yaml|
95
- yaml = YAML.load(yaml)
96
- expect(yaml["id"]).to eq(p2_id)
97
- expect(yaml["name"]).to eq(p2_pretty)
98
- expect(yaml["accepting_product"]).to eq(p1_id)
99
- expect(yaml["accepting_product_version"]).to eq(version)
100
- end
101
- expect(acc.persist(product_relationship, [p2])).to eq([])
102
- end
103
- end
104
- end
105
-
106
- describe "when the folder cannot be created" do
107
- let(:err) { StandardError.new("foo") }
108
- it "returns the error" do
109
- expect(Dir).to receive(:exist?).with(dir3).and_return(false)
110
- expect(FileUtils).to receive(:mkdir_p).and_raise(err)
111
- expect(File).to_not receive(:open)
112
- expect(acc.persist(product_relationship, [p1])).to eq([err])
113
- end
114
- end
115
-
116
- describe "when the file cannot be written" do
117
- let(:err) { StandardError.new("bar") }
118
- it "returns the error" do
119
- expect(Dir).to receive(:exist?).with(dir3).and_return(true)
120
- expect(File).to receive(:open).with(File.join(dir3, p1_filename), mode).and_raise(err)
121
- expect(acc.persist(product_relationship, [p1])).to eq([err])
122
- end
123
- end
124
- end
125
-
126
- end
127
- end
@@ -1,100 +0,0 @@
1
- require "spec_helper"
2
- require "license_acceptance/strategy/prompt"
3
- require "license_acceptance/product"
4
- require "tty-prompt"
5
-
6
- RSpec.describe LicenseAcceptance::Strategy::Prompt do
7
- let(:output) { StringIO.new }
8
- let(:config) do
9
- instance_double(LicenseAcceptance::Config, output: output)
10
- end
11
- let(:acc) { LicenseAcceptance::Strategy::Prompt.new(config) }
12
- let(:prompt) { instance_double(TTY::Prompt) }
13
- let(:p1) { instance_double(LicenseAcceptance::Product, id: "foo", 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, id: "bar", 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::Strategy::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