|
53 | 53 | provider.create |
54 | 54 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo=blah\nfoo2\nfoo3") |
55 | 55 | end |
56 | | - it 'appends the line if no matches are found' do # rubocop:disable RSpec/MultipleExpectations : |
| 56 | + it 'appends the line if no matches are found' do # rubocop:disable RSpec/MultipleExpectations : separating expectations breaks the tests |
57 | 57 | File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } |
58 | 58 | expect(provider.exists?).to be false |
59 | 59 | provider.create |
|
62 | 62 | it 'raises an error with invalid values' do |
63 | 63 | expect { |
64 | 64 | @resource = Puppet::Type::File_line.new( |
65 | | - name: 'foo', |
66 | | - path: tmpfile, |
67 | | - line: 'foo = bar', |
68 | | - match: '^foo\s*=.*$', |
69 | | - replace: 'asgadga', |
| 65 | + name: 'foo', path: tmpfile, line: 'foo = bar', match: '^foo\s*=.*$', replace: 'asgadga', |
70 | 66 | ) |
71 | 67 | }.to raise_error(Puppet::Error, %r{Invalid value "asgadga"\. Valid values are true, false\.}) |
72 | 68 | end |
|
76 | 72 | pending('To be added?') |
77 | 73 | end |
78 | 74 | context 'when matching' do |
| 75 | + # rubocop:disable RSpec/InstanceVariable : replacing before with let breaks the tests, variables need to be altered within it block : multi |
79 | 76 | before :each do |
80 | 77 | @resource = Puppet::Type::File_line.new( |
81 | 78 | name: 'foo', |
|
86 | 83 | @provider = provider_class.new(@resource) |
87 | 84 | end |
88 | 85 | describe 'using match' do |
89 | | - it 'raises an error if more than one line matches, and should not have modified the file' do |
| 86 | + it 'raises an error if more than one line matches, and should not have modified the file' do # rubocop:disable RSpec/MultipleExpectations : multiple expectations required |
90 | 87 | File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } |
91 | | - expect(@provider.exists?).to be(false) |
92 | 88 | expect { @provider.create }.to raise_error(Puppet::Error, %r{More than one line.*matches}) |
93 | 89 | expect(File.read(tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz") |
94 | 90 | end |
95 | 91 |
|
96 | 92 | it 'replaces all lines that matches' do |
97 | | - @resource = Puppet::Type::File_line.new( |
98 | | - name: 'foo', |
99 | | - path: tmpfile, |
100 | | - line: 'foo = bar', |
101 | | - match: '^foo\s*=.*$', |
102 | | - multiple: true, |
103 | | - ) |
| 93 | + @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo = bar', match: '^foo\s*=.*$', multiple: true) |
104 | 94 | @provider = provider_class.new(@resource) |
105 | 95 | File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } |
106 | | - expect(@provider.exists?).to be(false) |
107 | 96 | @provider.create |
108 | 97 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar") |
109 | 98 | end |
110 | 99 |
|
111 | 100 | it 'replaces all lines that match, even when some lines are correct' do |
112 | | - @resource = Puppet::Type::File_line.new( |
113 | | - name: 'neil', |
114 | | - path: tmpfile, |
115 | | - line: "\thard\tcore\t0\n", |
116 | | - match: '^[ \t]hard[ \t]+core[ \t]+.*', |
117 | | - multiple: true, |
118 | | - ) |
| 101 | + @resource = Puppet::Type::File_line.new(name: 'neil', path: tmpfile, line: "\thard\tcore\t0\n", match: '^[ \t]hard[ \t]+core[ \t]+.*', multiple: true) |
119 | 102 | @provider = provider_class.new(@resource) |
120 | 103 | File.open(tmpfile, 'w') { |fh| fh.write("\thard\tcore\t90\n\thard\tcore\t0\n") } |
121 | | - expect(@provider.exists?).to be(false) |
122 | 104 | @provider.create |
123 | 105 | expect(File.read(tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0") |
124 | 106 | end |
125 | 107 |
|
126 | 108 | it 'raises an error with invalid values' do |
127 | 109 | expect { |
128 | 110 | @resource = Puppet::Type::File_line.new( |
129 | | - name: 'foo', |
130 | | - path: tmpfile, |
131 | | - line: 'foo = bar', |
132 | | - match: '^foo\s*=.*$', |
133 | | - multiple: 'asgadga', |
| 111 | + name: 'foo', path: tmpfile, line: 'foo = bar', match: '^foo\s*=.*$', multiple: 'asgadga', |
134 | 112 | ) |
135 | 113 | }.to raise_error(Puppet::Error, %r{Invalid value "asgadga"\. Valid values are true, false\.}) |
136 | 114 | end |
137 | 115 |
|
138 | 116 | it 'replaces a line that matches' do |
139 | 117 | File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2") } |
140 | | - expect(@provider.exists?).to be(false) |
141 | 118 | @provider.create |
142 | 119 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") |
143 | 120 | end |
144 | 121 | it 'adds a new line if no lines match' do |
145 | 122 | File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } |
146 | | - expect(@provider.exists?).to be(false) |
147 | 123 | @provider.create |
148 | 124 | expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n") |
149 | 125 | end |
150 | 126 | it 'does nothing if the exact line already exists' do |
151 | 127 | File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = bar\nfoo2") } |
152 | | - expect(@provider.exists?).to be(true) |
153 | 128 | @provider.create |
154 | 129 | expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") |
155 | 130 | end |
156 | 131 | end |
157 | | - describe 'using match+append_on_no_match' do |
158 | | - context 'when there is a match' do |
159 | | - it 'replaces line' do |
160 | | - @resource = Puppet::Type::File_line.new( |
161 | | - name: 'foo', |
162 | | - path: tmpfile, |
163 | | - line: 'inserted = line', |
164 | | - match: '^foo3$', |
165 | | - append_on_no_match: false, |
166 | | - ) |
167 | | - @provider = provider_class.new(@resource) |
168 | | - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } |
169 | | - expect(@provider.exists?).to be true |
170 | | - expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") |
171 | | - end |
| 132 | + describe 'using match+append_on_no_match - when there is a match' do |
| 133 | + it 'replaces line' do |
| 134 | + @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false) |
| 135 | + @provider = provider_class.new(@resource) |
| 136 | + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } |
| 137 | + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") |
172 | 138 | end |
173 | | - context 'when there is no match' do |
174 | | - it 'does not add line after no matches found' do |
175 | | - @resource = Puppet::Type::File_line.new( |
176 | | - name: 'foo', |
177 | | - path: tmpfile, |
178 | | - line: 'inserted = line', |
179 | | - match: '^foo3$', |
180 | | - append_on_no_match: false, |
181 | | - ) |
182 | | - @provider = provider_class.new(@resource) |
183 | | - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } |
184 | | - expect(@provider.exists?).to be true |
185 | | - expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") |
186 | | - end |
| 139 | + end |
| 140 | + describe 'using match+append_on_no_match - when there is no match' do |
| 141 | + it 'does not add line after no matches found' do |
| 142 | + @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false) |
| 143 | + @provider = provider_class.new(@resource) |
| 144 | + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } |
| 145 | + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") |
187 | 146 | end |
188 | 147 | end |
189 | 148 | end |
| 149 | + # rubocop:enable RSpec/InstanceVariable : replacing before with let breaks the tests : multi |
190 | 150 | context 'when match+replace+append_on_no_match' do |
| 151 | + pending('to do') |
191 | 152 | end |
192 | 153 | context 'when after' do |
193 | 154 | let :resource do |
|
0 commit comments