defmastership 1.0.2 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.gitlab-ci.yml +20 -0
  4. data/.rubocop.yml +6 -8
  5. data/Gemfile +1 -0
  6. data/LICENSE +22 -0
  7. data/Rakefile +2 -2
  8. data/bin/defmastership +37 -24
  9. data/cucumber.yml +2 -0
  10. data/defmastership.gemspec +17 -10
  11. data/features/changeref.feature +82 -129
  12. data/features/checksum.feature +244 -0
  13. data/features/export.feature +49 -31
  14. data/features/modify.feature +143 -0
  15. data/features/rename_included_files.feature +121 -0
  16. data/features/step_definitions/defmastership_steps.rb +1 -0
  17. data/features/support/env.rb +1 -0
  18. data/lib/defmastership.rb +15 -3
  19. data/lib/defmastership/batch_modifier.rb +33 -0
  20. data/lib/defmastership/{ref_changer.rb → change_ref_line_modifier.rb} +19 -35
  21. data/lib/defmastership/change_ref_modifier.rb +15 -0
  22. data/lib/defmastership/comment_filter.rb +1 -0
  23. data/lib/defmastership/constants.rb +15 -1
  24. data/lib/defmastership/csv_formatter.rb +19 -18
  25. data/lib/defmastership/csv_formatter_body.rb +12 -6
  26. data/lib/defmastership/csv_formatter_header.rb +12 -10
  27. data/lib/defmastership/definition.rb +12 -0
  28. data/lib/defmastership/definition_parser.rb +46 -0
  29. data/lib/defmastership/document.rb +54 -75
  30. data/lib/defmastership/filters.rb +30 -0
  31. data/lib/defmastership/line_modifier_base.rb +29 -0
  32. data/lib/defmastership/modifier_base.rb +29 -0
  33. data/lib/defmastership/rename_included_files_line_modifier.rb +126 -0
  34. data/lib/defmastership/rename_included_files_modifier.rb +15 -0
  35. data/lib/defmastership/update_def_checksum_line_modifier.rb +39 -0
  36. data/lib/defmastership/update_def_checksum_modifier.rb +21 -0
  37. data/lib/defmastership/version.rb +2 -1
  38. data/spec/spec_helper.rb +2 -0
  39. data/spec/unit/defmastership/batch_modifier_spec.rb +115 -0
  40. data/spec/unit/defmastership/{ref_changer_spec.rb → change_ref_line_modifier_spec.rb} +49 -26
  41. data/spec/unit/defmastership/change_ref_modifier_spec.rb +76 -0
  42. data/spec/unit/defmastership/comment_filter_spec.rb +9 -4
  43. data/spec/unit/defmastership/csv_formatter_body_spec.rb +62 -37
  44. data/spec/unit/defmastership/csv_formatter_header_spec.rb +47 -22
  45. data/spec/unit/defmastership/csv_formatter_spec.rb +67 -105
  46. data/spec/unit/defmastership/definition_parser_spec.rb +63 -0
  47. data/spec/unit/defmastership/definition_spec.rb +31 -4
  48. data/spec/unit/defmastership/document_spec.rb +170 -35
  49. data/spec/unit/defmastership/rename_included_files_line_modifier_spec.rb +203 -0
  50. data/spec/unit/defmastership/rename_included_files_modifier_spec.rb +67 -0
  51. data/spec/unit/defmastership/update_def_checksum_line_modifier_spec.rb +68 -0
  52. data/spec/unit/defmastership/update_def_checksum_modifier_spec.rb +75 -0
  53. data/spec/unit/defmastership_spec.rb +1 -0
  54. metadata +50 -18
  55. data/Gemfile.lock +0 -137
  56. data/lib/defmastership/batch_changer.rb +0 -40
  57. data/lib/defmastership/project_ref_changer.rb +0 -27
  58. data/spec/unit/defmastership/batch_changer_spec.rb +0 -108
  59. data/spec/unit/defmastership/project_ref_changer_spec.rb +0 -79
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2020 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require('defmastership')
5
+
6
+ RSpec.describe(DefMastership::DefinitionParser) do
7
+ subject(:parser) { described_class.new(callback_object) }
8
+
9
+ let(:callback_object) { instance_double('callback_object') }
10
+
11
+ before do
12
+ allow(callback_object).to(receive(:add_new_definition).with(any_args))
13
+ allow(callback_object).to(receive(:add_line).with(any_args))
14
+ end
15
+
16
+ describe 'transitions' do
17
+ it { is_expected.to(have_state(:idle)) }
18
+
19
+ it { is_expected.to(transition_from(:idle).to(:wait_content).on_event(:new_definition, :whatever)) }
20
+
21
+ it { is_expected.to(transition_from(:wait_content).to(:in_block).on_event(:block_delimiter, :whatever)) }
22
+ it { is_expected.to(transition_from(:in_block).to(:idle).on_event(:block_delimiter, :whatever)) }
23
+ it { is_expected.to(transition_from(:idle).to(:idle).on_event(:block_delimiter, :whatever)) }
24
+ it { is_expected.to(transition_from(:single_para).to(:idle).on_event(:block_delimiter, :whatever)) }
25
+
26
+ it { is_expected.to(transition_from(:wait_content).to(:single_para).on_event(:new_line, :whatever)) }
27
+ it { is_expected.to(transition_from(:single_para).to(:single_para).on_event(:new_line, :whatever)) }
28
+ it { is_expected.to(transition_from(:in_block).to(:in_block).on_event(:new_line, :whatever)) }
29
+ it { is_expected.to(transition_from(:idle).to(:idle).on_event(:new_line, :whatever)) }
30
+
31
+ it { is_expected.to(transition_from(:wait_content).to(:idle).on_event(:empty_line, :whatever)) }
32
+ it { is_expected.to(transition_from(:single_para).to(:idle).on_event(:empty_line, :whatever)) }
33
+ it { is_expected.to(transition_from(:idle).to(:idle).on_event(:empty_line, :whatever)) }
34
+ it { is_expected.to(transition_from(:in_block).to(:in_block).on_event(:empty_line, :whatever)) }
35
+ end
36
+
37
+ describe 'actions' do
38
+ it do
39
+ parser.new_definition(:whatever)
40
+ expect(callback_object).to(have_received(:add_new_definition).with(:whatever))
41
+ end
42
+
43
+ it do
44
+ parser.new_definition(:whatever)
45
+ parser.new_line(:other_whatever)
46
+ expect(callback_object).to(have_received(:add_line).with(:other_whatever))
47
+ end
48
+
49
+ it do
50
+ parser.new_definition(:whatever)
51
+ parser.new_line(:other_whatever)
52
+ parser.new_line(:other_whatever2)
53
+ expect(callback_object).to(have_received(:add_line).with(:other_whatever2))
54
+ end
55
+
56
+ it do
57
+ parser.new_definition(:whatever)
58
+ parser.block_delimiter(:other_whatever)
59
+ parser.new_line(:other_whatever2)
60
+ expect(callback_object).to(have_received(:add_line).with(:other_whatever2))
61
+ end
62
+ end
63
+ end
@@ -1,10 +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::Definition) do
6
7
  describe '.new' do
7
- context 'without labels' do
8
+ context 'when minimal' do
8
9
  subject do
9
10
  described_class.new(
10
11
  type: 'req',
@@ -21,12 +22,13 @@ RSpec.describe(DefMastership::Definition) do
21
22
  it { is_expected.to(have_attributes(iref: [])) }
22
23
  it { is_expected.to(have_attributes(attributes: {})) }
23
24
  it { is_expected.to(have_attributes(labels: Set.new)) }
25
+ it { is_expected.to(have_attributes(wrong_explicit_checksum: nil)) }
24
26
  end
25
27
 
26
28
  context 'with labels' do
27
29
  subject do
28
30
  described_class.new(
29
- gtype: 'req',
31
+ type: 'req',
30
32
  reference: 'TUTU-001',
31
33
  labels: 'something'
32
34
  )
@@ -34,6 +36,26 @@ RSpec.describe(DefMastership::Definition) do
34
36
 
35
37
  it { is_expected.to(have_attributes(labels: Set['something'])) }
36
38
  end
39
+
40
+ context 'with explicit_checksum' do
41
+ subject(:definition) do
42
+ described_class.new(
43
+ type: 'req',
44
+ reference: 'TUTU-001',
45
+ explicit_checksum: '8cc259e6'
46
+ )
47
+ end
48
+
49
+ it do
50
+ definition << 'def value with a checksum != 8cc259e6'
51
+ expect(definition).to(have_attributes(wrong_explicit_checksum: '8cc259e6'))
52
+ end
53
+
54
+ it do
55
+ definition << 'def value with a good checksum'
56
+ expect(definition).to(have_attributes(wrong_explicit_checksum: nil))
57
+ end
58
+ end
37
59
  end
38
60
 
39
61
  describe '#<<' do
@@ -49,7 +71,7 @@ RSpec.describe(DefMastership::Definition) do
49
71
  expect(definition.value).to(eq('first line'))
50
72
  end
51
73
 
52
- it 'store line when added' do
74
+ it 'stores line when added' do
53
75
  definition << 'first line'
54
76
  expect(definition.lines).to(eq(['first line']))
55
77
  end
@@ -59,10 +81,15 @@ RSpec.describe(DefMastership::Definition) do
59
81
  expect(definition.value).to(eq("first line\nsecond line"))
60
82
  end
61
83
 
62
- it 'store each line when added' do
84
+ it 'stores each line when added' do
63
85
  definition << 'first line' << 'second line'
64
86
  expect(definition.lines).to(eq(['first line', 'second line']))
65
87
  end
88
+
89
+ it 'calculates sha256 of value' do
90
+ definition << 'first line' << 'second line'
91
+ expect(definition.sha256).to(eq('beb0535a'))
92
+ end
66
93
  end
67
94
 
68
95
  describe '#add_eref' do
@@ -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,109 @@ RSpec.describe(DefMastership::Document) do
395
425
  end
396
426
  end
397
427
  end
428
+
429
+ describe '#parse_file_with_preprocessor' do
430
+ let(:definition) { instance_double(DefMastership::Definition, 'definition') }
431
+ let(:input_lines) { ['[define, requirement, TOTO-0001]'] }
432
+ let(:adoc_doc) { instance_double(Asciidoctor::Document, 'adoc_doc') }
433
+ let(:adoc_reader) { instance_double(Asciidoctor::Reader, 'adoc_reader') }
434
+
435
+ before do
436
+ allow(Asciidoctor).to(
437
+ receive(:load_file).with('the_file.adoc', { parse: false, safe: :unsafe }).and_return(adoc_doc)
438
+ )
439
+ allow(adoc_doc).to(receive(:reader).and_return(adoc_reader))
440
+ allow(adoc_reader).to(receive(:read_lines).and_return(input_lines))
441
+ allow(DefMastership::Definition).to(receive(:new).and_return(definition))
442
+ allow(definition).to(receive(:<<).and_return(definition))
443
+ allow(definition).to(receive(:labels).and_return(Set.new))
444
+ document.parse_file_with_preprocessor('the_file.adoc')
445
+ end
446
+
447
+ it { expect(Asciidoctor).to(have_received(:load_file).with('the_file.adoc', { parse: false, safe: :unsafe })) }
448
+ it { expect(adoc_doc).to(have_received(:reader).with(no_args)) }
449
+ it { expect(adoc_reader).to(have_received(:read_lines).with(no_args)) }
450
+
451
+ it do
452
+ expect(DefMastership::Definition).to(
453
+ have_received(:new).with(
454
+ matchdata_including(type: 'requirement', reference: 'TOTO-0001')
455
+ )
456
+ )
457
+ end
458
+ end
459
+
460
+ describe '#wrong_explicit_checksum?' do
461
+ let(:def1) { instance_double(DefMastership::Definition, 'definition') }
462
+ let(:def2) { instance_double(DefMastership::Definition, 'definition') }
463
+ let(:input_lines) do
464
+ [
465
+ '[define, requirement, TOTO-0001]',
466
+ 'def one',
467
+ '',
468
+ '[define, requirement, TOTO-0002]',
469
+ 'def two'
470
+ ]
471
+ end
472
+
473
+ before do
474
+ allow(DefMastership::Definition).to(receive(:new).twice.and_return(def1, def2))
475
+ allow(def1).to(receive(:labels)).and_return([])
476
+ allow(def2).to(receive(:labels)).and_return([])
477
+ allow(def1).to(receive(:<<).and_return(def1))
478
+ allow(def2).to(receive(:<<).and_return(def2))
479
+ end
480
+
481
+ context 'when no wrong explicit checksum' do
482
+ before do
483
+ allow(def1).to(receive(:wrong_explicit_checksum)).and_return(nil)
484
+ allow(def2).to(receive(:wrong_explicit_checksum)).and_return(nil)
485
+ document.parse(input_lines)
486
+ document.wrong_explicit_checksum?
487
+ end
488
+
489
+ it { expect(def1).to(have_received(:wrong_explicit_checksum)) }
490
+ it { expect(def2).to(have_received(:wrong_explicit_checksum)) }
491
+ it { expect(document.wrong_explicit_checksum?).to(eq(false)) }
492
+ end
493
+
494
+ context 'when one req has wrong explicit checksum' do
495
+ before do
496
+ allow(def1).to(receive(:wrong_explicit_checksum)).and_return(nil)
497
+ allow(def2).to(receive(:wrong_explicit_checksum)).and_return('toto')
498
+ document.parse(input_lines)
499
+ document.wrong_explicit_checksum?
500
+ end
501
+
502
+ it { expect(document.wrong_explicit_checksum?).to(eq(true)) }
503
+ end
504
+ end
505
+
506
+ describe '#ref_to_def?' do
507
+ let(:def1) { instance_double(DefMastership::Definition, 'definition') }
508
+ let(:def2) { instance_double(DefMastership::Definition, 'definition') }
509
+ let(:input_lines) do
510
+ [
511
+ '[define, requirement, TOTO-0001]',
512
+ 'def one',
513
+ '',
514
+ '[define, requirement, TOTO-0002(1234)]',
515
+ 'def two'
516
+ ]
517
+ end
518
+
519
+ before do
520
+ allow(DefMastership::Definition).to(receive(:new).twice.and_return(def1, def2))
521
+ allow(def1).to(receive(:labels)).and_return([])
522
+ allow(def2).to(receive(:labels)).and_return([])
523
+ allow(def1).to(receive(:<<).and_return(def1))
524
+ allow(def2).to(receive(:<<).and_return(def2))
525
+ allow(def1).to(receive(:reference).and_return('TOTO-0001'))
526
+ allow(def2).to(receive(:reference).and_return('TOTO-0002'))
527
+ document.parse(input_lines)
528
+ end
529
+
530
+ it { expect(document.ref_to_def('TOTO-0001')).to(eq(def1)) }
531
+ it { expect(document.ref_to_def('TOTO-0002')).to(eq(def2)) }
532
+ end
398
533
  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