defmastership 1.0.1 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.gitlab-ci.yml +20 -0
  4. data/.rubocop.yml +5 -4
  5. data/Gemfile +1 -0
  6. data/LICENSE +22 -0
  7. data/Rakefile +2 -2
  8. data/bin/defmastership +25 -17
  9. data/cucumber.yml +2 -0
  10. data/defmastership.gemspec +18 -11
  11. data/features/changeref.feature +82 -129
  12. data/features/export.feature +102 -31
  13. data/features/modify.feature +143 -0
  14. data/features/rename_included_files.feature +121 -0
  15. data/features/step_definitions/defmastership_steps.rb +1 -0
  16. data/features/support/env.rb +1 -0
  17. data/lib/defmastership.rb +12 -3
  18. data/lib/defmastership/batch_modifier.rb +33 -0
  19. data/lib/defmastership/{ref_changer.rb → change_ref_line_modifier.rb} +19 -35
  20. data/lib/defmastership/change_ref_modifier.rb +15 -0
  21. data/lib/defmastership/comment_filter.rb +1 -0
  22. data/lib/defmastership/constants.rb +15 -1
  23. data/lib/defmastership/csv_formatter.rb +19 -18
  24. data/lib/defmastership/csv_formatter_body.rb +12 -6
  25. data/lib/defmastership/csv_formatter_header.rb +12 -10
  26. data/lib/defmastership/definition.rb +12 -0
  27. data/lib/defmastership/definition_parser.rb +46 -0
  28. data/lib/defmastership/document.rb +44 -75
  29. data/lib/defmastership/filters.rb +30 -0
  30. data/lib/defmastership/line_modifier_base.rb +29 -0
  31. data/lib/defmastership/modifier_base.rb +29 -0
  32. data/lib/defmastership/rename_included_files_line_modifier.rb +126 -0
  33. data/lib/defmastership/rename_included_files_modifier.rb +30 -0
  34. data/lib/defmastership/version.rb +2 -1
  35. data/spec/spec_helper.rb +2 -0
  36. data/spec/unit/defmastership/batch_modifier_spec.rb +115 -0
  37. data/spec/unit/defmastership/{ref_changer_spec.rb → change_ref_line_modifier_spec.rb} +49 -26
  38. data/spec/unit/defmastership/change_ref_modifier_spec.rb +76 -0
  39. data/spec/unit/defmastership/comment_filter_spec.rb +9 -4
  40. data/spec/unit/defmastership/csv_formatter_body_spec.rb +62 -37
  41. data/spec/unit/defmastership/csv_formatter_header_spec.rb +47 -22
  42. data/spec/unit/defmastership/csv_formatter_spec.rb +67 -105
  43. data/spec/unit/defmastership/definition_parser_spec.rb +63 -0
  44. data/spec/unit/defmastership/definition_spec.rb +31 -4
  45. data/spec/unit/defmastership/document_spec.rb +113 -35
  46. data/spec/unit/defmastership/rename_included_files_line_modifier_spec.rb +203 -0
  47. data/spec/unit/defmastership/rename_included_files_modifier_spec.rb +67 -0
  48. data/spec/unit/defmastership_spec.rb +1 -0
  49. metadata +44 -17
  50. data/Gemfile.lock +0 -140
  51. data/lib/defmastership/batch_changer.rb +0 -40
  52. data/lib/defmastership/project_ref_changer.rb +0 -27
  53. data/spec/unit/defmastership/batch_changer_spec.rb +0 -108
  54. data/spec/unit/defmastership/project_ref_changer_spec.rb +0 -79
@@ -1,8 +1,11 @@
1
+ # Copyright (c) 2020 Jerome Arbez-Gindre
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require('defmastership')
4
5
 
5
6
  RSpec.describe(DefMastership::Document) do
7
+ subject(:document) { described_class.new }
8
+
6
9
  describe '.new' do
7
10
  it { is_expected.not_to(be(nil)) }
8
11
  it { is_expected.to(have_attributes(definitions: [])) }
@@ -13,8 +16,6 @@ RSpec.describe(DefMastership::Document) do
13
16
  end
14
17
 
15
18
  describe '#parse' do
16
- subject(:document) { described_class.new }
17
-
18
19
  context 'with valid definitions' do
19
20
  let(:definition) { instance_double(DefMastership::Definition, 'definition') }
20
21
 
@@ -30,22 +31,45 @@ RSpec.describe(DefMastership::Document) do
30
31
  let(:input_lines) { ['[define, requirement, TOTO-0001]'] }
31
32
 
32
33
  before do
33
- allow(DefMastership::Definition).to(receive(:new).with(
34
- matchdata_including(
35
- type: 'requirement',
36
- reference: 'TOTO-0001'
37
- )
38
- ).and_return(definition))
34
+ allow(DefMastership::Definition).to(
35
+ receive(:new).with(
36
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001')
37
+ ).and_return(definition)
38
+ )
39
39
  document.parse(input_lines)
40
40
  end
41
41
 
42
42
  it do
43
- expect(DefMastership::Definition).to(have_received(:new).with(
44
- matchdata_including(type: 'requirement', reference: 'TOTO-0001')
45
- ))
43
+ expect(DefMastership::Definition).to(
44
+ have_received(:new).with(
45
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001')
46
+ )
47
+ )
46
48
  end
47
49
 
48
50
  it { expect(document).to(have_attributes(definitions: [definition])) }
51
+ it { expect(definition).to(have_received(:labels)) }
52
+ end
53
+
54
+ context 'when simple definition line with explicit checksum' do
55
+ let(:input_lines) { ['[define, requirement, TOTO-0001(ab12)]'] }
56
+
57
+ before do
58
+ allow(DefMastership::Definition).to(
59
+ receive(:new).with(
60
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001', explicit_checksum: 'ab12')
61
+ ).and_return(definition)
62
+ )
63
+ document.parse(input_lines)
64
+ end
65
+
66
+ it do
67
+ expect(DefMastership::Definition).to(
68
+ have_received(:new).with(
69
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001', explicit_checksum: 'ab12')
70
+ )
71
+ )
72
+ end
49
73
  end
50
74
 
51
75
  context 'when complete definition with content' do
@@ -132,11 +156,11 @@ RSpec.describe(DefMastership::Document) do
132
156
  end
133
157
 
134
158
  it do
135
- expect(DefMastership::Definition).to(have_received(:new).with(matchdata_including(
136
- type: 'requirement',
137
- reference: 'TOTO-0001',
138
- labels: 'label1, label2'
139
- )))
159
+ expect(DefMastership::Definition).to(
160
+ have_received(:new).with(
161
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001', labels: 'label1, label2')
162
+ )
163
+ )
140
164
  end
141
165
 
142
166
  it { expect(document.labels).to(eq(Set['bla1', 'bla2'])) }
@@ -157,11 +181,11 @@ RSpec.describe(DefMastership::Document) do
157
181
  end
158
182
 
159
183
  it do
160
- expect(DefMastership::Definition).to(have_received(:new).with(matchdata_including(
161
- type: 'requirement',
162
- reference: 'TOTO-0001',
163
- labels: 'label1,label2'
164
- )))
184
+ expect(DefMastership::Definition).to(
185
+ have_received(:new).with(
186
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001', labels: 'label1,label2')
187
+ )
188
+ )
165
189
  end
166
190
 
167
191
  it { expect(document.labels).to(eq(Set['bla1', 'bla2'])) }
@@ -208,8 +232,10 @@ RSpec.describe(DefMastership::Document) do
208
232
  end
209
233
 
210
234
  before do
211
- allow(definition).to(receive(:add_eref).with(:implements, 'SYSTEM-0012, SYSTEM-0014')
212
- .and_return(definition))
235
+ allow(definition).to(
236
+ receive(:add_eref).with(:implements, 'SYSTEM-0012, SYSTEM-0014')
237
+ .and_return(definition)
238
+ )
213
239
  document.parse(input_lines)
214
240
  end
215
241
 
@@ -288,18 +314,20 @@ RSpec.describe(DefMastership::Document) do
288
314
  end
289
315
 
290
316
  before do
291
- allow(DefMastership::Definition).to(receive(:new).with(matchdata_including(
292
- type: 'requirement',
293
- reference: 'TOTO-0001'
294
- )).and_return(definition))
317
+ allow(DefMastership::Definition).to(
318
+ receive(:new).with(
319
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001')
320
+ ).and_return(definition)
321
+ )
295
322
  document.parse(input_lines)
296
323
  end
297
324
 
298
325
  it do
299
- expect(DefMastership::Definition).to(have_received(:new).with(matchdata_including(
300
- type: 'requirement',
301
- reference: 'TOTO-0001'
302
- )))
326
+ expect(DefMastership::Definition).to(
327
+ have_received(:new).with(
328
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001')
329
+ )
330
+ )
303
331
  end
304
332
  end
305
333
  end
@@ -364,8 +392,9 @@ RSpec.describe(DefMastership::Document) do
364
392
  end
365
393
 
366
394
  before do
367
- allow(DefMastership::Definition).to(receive(:new)
368
- .and_raise('not a valide definition'))
395
+ allow(DefMastership::Definition).to(
396
+ receive(:new).and_raise('not a valide definition')
397
+ )
369
398
  end
370
399
 
371
400
  it do
@@ -384,8 +413,9 @@ RSpec.describe(DefMastership::Document) do
384
413
  end
385
414
 
386
415
  before do
387
- allow(DefMastership::Definition).to(receive(:new)
388
- .and_raise('not a valide definition'))
416
+ allow(DefMastership::Definition).to(
417
+ receive(:new).and_raise('not a valide definition')
418
+ )
389
419
  end
390
420
 
391
421
  it do
@@ -395,4 +425,52 @@ RSpec.describe(DefMastership::Document) do
395
425
  end
396
426
  end
397
427
  end
428
+
429
+ describe '#wrong_explicit_checksum?' do
430
+ subject(:document) { described_class.new }
431
+
432
+ let(:def1) { instance_double(DefMastership::Definition, 'definition') }
433
+ let(:def2) { instance_double(DefMastership::Definition, 'definition') }
434
+ let(:input_lines) do
435
+ [
436
+ '[define, requirement, TOTO-0001]',
437
+ 'def one',
438
+ '',
439
+ '[define, requirement, TOTO-0002]',
440
+ 'def two'
441
+ ]
442
+ end
443
+
444
+ before do
445
+ allow(DefMastership::Definition).to(receive(:new).twice.and_return(def1, def2))
446
+ allow(def1).to(receive(:labels)).and_return([])
447
+ allow(def2).to(receive(:labels)).and_return([])
448
+ allow(def1).to(receive(:<<).and_return(def1))
449
+ allow(def2).to(receive(:<<).and_return(def2))
450
+ end
451
+
452
+ context 'when no wrong explicit checksum' do
453
+ before do
454
+ allow(def1).to(receive(:wrong_explicit_checksum)).and_return(nil)
455
+ allow(def2).to(receive(:wrong_explicit_checksum)).and_return(nil)
456
+ document.parse(input_lines)
457
+ document.wrong_explicit_checksum?
458
+ end
459
+
460
+ it { expect(def1).to(have_received(:wrong_explicit_checksum)) }
461
+ it { expect(def2).to(have_received(:wrong_explicit_checksum)) }
462
+ it { expect(document.wrong_explicit_checksum?).to(eq(false)) }
463
+ end
464
+
465
+ context 'when one req has wrong explicit checksum' do
466
+ before do
467
+ allow(def1).to(receive(:wrong_explicit_checksum)).and_return(nil)
468
+ allow(def2).to(receive(:wrong_explicit_checksum)).and_return('toto')
469
+ document.parse(input_lines)
470
+ document.wrong_explicit_checksum?
471
+ end
472
+
473
+ it { expect(document.wrong_explicit_checksum?).to(eq(true)) }
474
+ end
475
+ end
398
476
  end
@@ -0,0 +1,203 @@
1
+ # Copyright (c) 2021 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require('defmastership')
5
+
6
+ RSpec.describe(DefMastership::RenameIncludedFilesLineModifier) do
7
+ subject(:includeschanger) { described_class.new }
8
+
9
+ describe '.new' do
10
+ it { is_expected.not_to(be(nil)) }
11
+ it { is_expected.to(have_attributes(from_regexp: '')) }
12
+ it { is_expected.to(have_attributes(to_template: '')) }
13
+ it { is_expected.to(have_attributes(changes: [])) }
14
+ it { expect { includeschanger.user_defined_attribute }.to(raise_error(NoMethodError)) }
15
+ end
16
+
17
+ describe '.from_config' do
18
+ subject(:includeschanger) do
19
+ described_class.from_config(
20
+ from_regexp: 'Whatever.+',
21
+ to_template: 'Whatever'
22
+ )
23
+ end
24
+
25
+ it { is_expected.not_to(be(nil)) }
26
+ it { is_expected.to(have_attributes(from_regexp: 'Whatever.+')) }
27
+ it { is_expected.to(have_attributes(to_template: 'Whatever')) }
28
+ end
29
+
30
+ describe '#replace' do
31
+ context 'when NOT valid include' do
32
+ before do
33
+ includeschanger.from_config(
34
+ from_regexp: 'orig',
35
+ cancel_if_match: 'REF',
36
+ to_template: 'dest'
37
+ )
38
+ allow(File).to(receive(:rename))
39
+ end
40
+
41
+ context 'when the include statement do not match' do
42
+ before do
43
+ ['[define,requirement,REFERENCE]', '--'].each do |line|
44
+ includeschanger.replace(line)
45
+ end
46
+ end
47
+
48
+ it do
49
+ expect(includeschanger.replace('badinclude::orig[]'))
50
+ .to(eq('badinclude::orig[]'))
51
+ end
52
+
53
+ it do
54
+ includeschanger.replace('badinclude::orig[]')
55
+ expect(includeschanger).to(have_attributes(changes: []))
56
+ end
57
+ end
58
+
59
+ context 'when the include is not in a definition' do
60
+ before do
61
+ ['[define,requirement,REFERENCE]', '--', 'text', '--'].each do |line|
62
+ includeschanger.replace(line)
63
+ end
64
+ end
65
+
66
+ it do
67
+ expect(includeschanger.replace('include::orig[]'))
68
+ .to(eq('include::orig[]'))
69
+ end
70
+
71
+ it do
72
+ includeschanger.replace('include::orig[]')
73
+ expect(includeschanger).to(have_attributes(changes: []))
74
+ end
75
+ end
76
+
77
+ context 'when the cancel regexp is met' do
78
+ before do
79
+ ['[define,requirement,REFERENCE]', '--'].each do |line|
80
+ includeschanger.replace(line)
81
+ end
82
+ end
83
+
84
+ it do
85
+ expect(includeschanger.replace('include::REFERENCE_orig[]'))
86
+ .to(eq('include::REFERENCE_orig[]'))
87
+ end
88
+
89
+ it do
90
+ includeschanger.replace('include::REFERENCE_orig[]')
91
+ expect(includeschanger).to(have_attributes(changes: []))
92
+ end
93
+ end
94
+ end
95
+
96
+ context 'when valid include' do
97
+ before do
98
+ allow(File).to(receive(:rename))
99
+ end
100
+
101
+ context 'when really simple rule' do
102
+ before do
103
+ includeschanger.from_config(
104
+ from_regexp: 'orig',
105
+ cancel_if_match: 'REF',
106
+ to_template: 'dest'
107
+ )
108
+ includeschanger.replace('[define,requirement,REFERENCE]')
109
+ includeschanger.replace('--')
110
+ end
111
+
112
+ it do
113
+ expect(includeschanger.replace('include::orig[]'))
114
+ .to(eq('include::dest[]'))
115
+ end
116
+
117
+ it do
118
+ includeschanger.replace('include::orig[]')
119
+ expect(File).to(have_received(:rename).with('orig', 'dest'))
120
+ end
121
+
122
+ it do
123
+ includeschanger.replace('include::orig[]')
124
+ expect(includeschanger).to(have_attributes(changes: [%w[orig dest]]))
125
+ end
126
+
127
+ it do
128
+ expect(includeschanger.replace('include::toto/orig[]'))
129
+ .to(eq('include::toto/dest[]'))
130
+ end
131
+
132
+ it do
133
+ includeschanger.replace('include::toto/orig[]')
134
+ expect(File).to(have_received(:rename).with('toto/orig', 'toto/dest'))
135
+ end
136
+
137
+ it do
138
+ includeschanger.replace('include::toto/orig[]')
139
+ expect(includeschanger).to(have_attributes(changes: [%w[toto/orig toto/dest]]))
140
+ end
141
+ end
142
+
143
+ context 'when complex from_regexp' do
144
+ before do
145
+ includeschanger.from_config(
146
+ from_regexp: '(?<origin>.*\.extension)',
147
+ to_template: '%<reference>s_%<origin>s'
148
+ )
149
+ includeschanger.replace('[define,requirement,REF]')
150
+ end
151
+
152
+ it do
153
+ expect(includeschanger.replace('include::any_path/one_file.extension[]'))
154
+ .to(eq('include::any_path/REF_one_file.extension[]'))
155
+ end
156
+
157
+ it do
158
+ includeschanger.replace('include::any_path/one_file.extension[]')
159
+ expect(File).to(have_received(:rename).with('any_path/one_file.extension', 'any_path/REF_one_file.extension'))
160
+ end
161
+
162
+ it do
163
+ changes = [%w[any_path/one_file.extension any_path/REF_one_file.extension]]
164
+ includeschanger.replace('include::any_path/one_file.extension[]')
165
+ expect(includeschanger).to(have_attributes(changes: changes))
166
+ end
167
+ end
168
+
169
+ context 'when path with variable' do
170
+ before do
171
+ includeschanger.from_config(
172
+ from_regexp: 'orig',
173
+ to_template: 'dest'
174
+ )
175
+ includeschanger.replace(':any: one')
176
+ includeschanger.replace(':path: two')
177
+ includeschanger.replace('[define,requirement,REFERENCE]')
178
+ includeschanger.replace('--')
179
+ end
180
+
181
+ it do
182
+ expect(includeschanger.replace('include::{any}/orig[]'))
183
+ .to(eq('include::{any}/dest[]'))
184
+ end
185
+
186
+ it do
187
+ includeschanger.replace('include::{any}/orig[]')
188
+ expect(File).to(have_received(:rename).with('one/orig', 'one/dest'))
189
+ end
190
+
191
+ it do
192
+ includeschanger.replace('include::{any}_{path}/orig[]')
193
+ expect(File).to(have_received(:rename).with('one_two/orig', 'one_two/dest'))
194
+ end
195
+
196
+ it do
197
+ includeschanger.replace('include::{any}_{path}/orig[]')
198
+ expect(includeschanger).to(have_attributes(changes: [%w[one_two/orig one_two/dest]]))
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,67 @@
1
+ # Copyright (c) 2021 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require('defmastership')
5
+
6
+ RSpec.describe(DefMastership::RenameIncludedFilesModifier) do
7
+ subject(:modifier) do
8
+ described_class.new(
9
+ 'fake config'
10
+ )
11
+ end
12
+
13
+ let(:adoc_texts) do
14
+ {
15
+ 'file1.adoc' => "file1 line1\nfile1 line2",
16
+ 'file2.adoc' => "file2 line1\nfile2 line2"
17
+ }
18
+ end
19
+
20
+ describe '.new' do
21
+ it { is_expected.not_to(be(nil)) }
22
+ it { is_expected.to(have_attributes(config: 'fake config')) }
23
+ it { is_expected.to(have_attributes(changes: [])) }
24
+ end
25
+
26
+ describe '#do_modifications' do
27
+ let(:line_modifier) { instance_double(DefMastership::RenameIncludedFilesLineModifier, 'lineModifier') }
28
+
29
+ before do
30
+ allow(DefMastership::RenameIncludedFilesLineModifier).to(
31
+ receive(:from_config).with('fake config').and_return(line_modifier)
32
+ )
33
+ allow(line_modifier).to(receive(:replace).with("file1 line1\n").and_return("new file1 line1\n"))
34
+ allow(line_modifier).to(receive(:replace).with('file1 line2').and_return('new file1 line2'))
35
+ allow(line_modifier).to(receive(:replace).with("file2 line1\n").and_return("new file2 line1\n"))
36
+ allow(line_modifier).to(receive(:replace).with('file2 line2').and_return('new file2 line2'))
37
+ allow(line_modifier).to(receive(:changes).and_return([%w[from1 to1], %w[from2 to2]]))
38
+ allow(line_modifier).to(receive(:config).and_return('fake config'))
39
+ end
40
+
41
+ context 'when detailed expectations' do
42
+ before { modifier.do_modifications(adoc_texts) }
43
+
44
+ it do
45
+ expect(DefMastership::RenameIncludedFilesLineModifier).to(
46
+ have_received(:from_config).with('fake config')
47
+ )
48
+ end
49
+
50
+ it { expect(line_modifier).to(have_received(:replace).with("file1 line1\n")) }
51
+ it { expect(line_modifier).to(have_received(:replace).with('file1 line2')) }
52
+ it { expect(line_modifier).to(have_received(:replace).with("file2 line1\n")) }
53
+ it { expect(line_modifier).to(have_received(:replace).with('file2 line2')) }
54
+ it { expect(line_modifier).to(have_received(:changes)) }
55
+ it { is_expected.to(have_attributes(config: 'fake config')) }
56
+ it { is_expected.to(have_attributes(changes: [%w[from1 to1], %w[from2 to2]])) }
57
+ end
58
+
59
+ it do
60
+ expected_adoc = {
61
+ 'file1.adoc' => "new file1 line1\nnew file1 line2",
62
+ 'file2.adoc' => "new file2 line1\nnew file2 line2"
63
+ }
64
+ expect(modifier.do_modifications(adoc_texts)).to(eq(expected_adoc))
65
+ end
66
+ end
67
+ end