defmastership 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2cd7a521f197f1ab374863af89ab7a24c50bc27d5dfef6161bc47e8fff27825f
4
- data.tar.gz: a92ef2ee968e39a48e20fbf2a7d6f3040393c727a422e4f39c935978a29e187f
3
+ metadata.gz: 0341f43f016456f897b3382a1a8356dd7dc5d35b37059bccfd254f5cd9ccf65d
4
+ data.tar.gz: 442ebad69c76c21f8bfb972bd53d7ce83e6431795026f7b4abc760a70b450bf7
5
5
  SHA512:
6
- metadata.gz: ee7d58f8392a43fe5b45e234f9c4965037e2864a37a669c191d33b58016cf7094a99fe29443c3fc60678bab3e591b19188579771d14aa762fba8d4b34b01bd89
7
- data.tar.gz: 526ae0002d2f5d66f2ad555d09bc8cf9786b567098e6bc071a8b119b2cbfe6b664e84eaf3fc68e453ca16f41552a4004f23bab6323ed62cba526322f27e88b15
6
+ metadata.gz: 7106dd56ff07b88ca64e67f3dbd62b46f09a078dff05a955f80a5a1473ca3ff918ff038346a9e598c8355de761e1e5dcc1270db811eadacb875d51de57032b09
7
+ data.tar.gz: 719a47a9bb86aca473565693a645a85b72ade6036a2ed1ee47cdafc00c85640c52a747e2394d59e8f2ed08803406d49f03914a1b5d74026d20ae01de56b8c474
data/bin/defmastership CHANGED
@@ -2,7 +2,6 @@
2
2
  # Copyright (c) 2020 Jerome Arbez-Gindre
3
3
  # frozen_string_literal: true
4
4
 
5
- require('asciidoctor')
6
5
  require('csv')
7
6
  require('defmastership')
8
7
  require('gli')
@@ -52,18 +51,24 @@ module DefMastership
52
51
  arg_name 'asciidoctor_files'
53
52
  command :export, :exp, :e do |c|
54
53
  c.flag(%i[separator sep s], default_value: ',', desc: 'CSV separator')
54
+ c.switch(%i[no-fail], desc: 'Exit succes even in case of wrong explicit checksum')
55
+
55
56
  c.action do |_global_options, options, args|
56
- doc = Asciidoctor.load_file(args[0], safe: :unsafe, parse: false)
57
- # FIXME: also escape ifdef, ifndef, ifeval and endif directives
58
- # FIXME: do this more carefully by reading line by line; if input
59
- # differs by output by leading backslash, restore original line
60
- lines = doc.reader.read_lines
61
57
  my_doc = DefMastership::Document.new
62
- my_doc.parse(lines)
58
+ my_doc.parse_file_with_preprocessor(args[0])
63
59
 
64
60
  output_file = args[0].sub(/\.adoc$/, '.csv')
65
61
 
66
62
  DefMastership::CSVFormatter.new(my_doc, options['separator']).export_to(output_file)
63
+
64
+ if my_doc.wrong_explicit_checksum?
65
+ my_doc.definitions.each do |definition|
66
+ next if definition.wrong_explicit_checksum.nil?
67
+
68
+ warn("WARNING: #{definition.reference} has a wrong explicit checksum (should be #{definition.sha256})")
69
+ end
70
+ exit 1 unless options[:"no-fail"]
71
+ end
67
72
  end
68
73
  end
69
74
 
@@ -0,0 +1,244 @@
1
+ Feature: checksum
2
+ As a Responsible of definitions for a given document
3
+ In order to detect modifications of defintiions
4
+ I want defmastership to export and update checksum in definitions
5
+
6
+ Scenario: Extract one definition with WRONG explicit checksum to CSV
7
+ Given a file named "thedoc.adoc" with:
8
+ """
9
+ [define, requirement, TOTO-0001(babe1234)]
10
+ def one
11
+ """
12
+ When I run `defmastership export thedoc.adoc`
13
+ Then it should fail with:
14
+ """
15
+ WARNING: TOTO-0001 has a wrong explicit checksum (should be d27cb5c3)
16
+ """
17
+ And the file "thedoc.csv" should contain:
18
+ """
19
+ Type,Reference,Value,sha256,Wrong explicit checksum
20
+ requirement,TOTO-0001,def one,d27cb5c3,babe1234
21
+ """
22
+ And the stdout should not contain anything
23
+
24
+ Scenario: Extract two definitions with one WRONG explicit checksum to CSV
25
+ Given a file named "thedoc.adoc" with:
26
+ """
27
+ [define, requirement, TOTO-0001(d27cb5c3)]
28
+ def one
29
+
30
+ [define, requirement, TOTO-0002(babe1234)]
31
+ def two
32
+ """
33
+ When I run `defmastership export thedoc.adoc`
34
+ Then it should fail with:
35
+ """
36
+ WARNING: TOTO-0002 has a wrong explicit checksum (should be b80e1be5)
37
+ """
38
+ Then the file "thedoc.csv" should contain:
39
+ """
40
+ Type,Reference,Value,sha256,Wrong explicit checksum
41
+ requirement,TOTO-0001,def one,d27cb5c3,\"\"
42
+ requirement,TOTO-0002,def two,b80e1be5,babe1234
43
+ """
44
+ And the stdout should not contain anything
45
+
46
+ Scenario: Export not failing even if WRONG explicit checksum
47
+ Given a file named "thedoc.adoc" with:
48
+ """
49
+ [define, requirement, TOTO-0001(d27cb5c3)]
50
+ def one
51
+
52
+ [define, requirement, TOTO-0002(babe1234)]
53
+ def two
54
+ """
55
+ When I successfully run `defmastership export --no-fail thedoc.adoc`
56
+ Then the file "thedoc.csv" should contain:
57
+ """
58
+ Type,Reference,Value,sha256,Wrong explicit checksum
59
+ requirement,TOTO-0001,def one,d27cb5c3,\"\"
60
+ requirement,TOTO-0002,def two,b80e1be5,babe1234
61
+ """
62
+ And the stdout should not contain anything
63
+ And the stderr should contain:
64
+ """
65
+ WARNING: TOTO-0002 has a wrong explicit checksum (should be b80e1be5)
66
+ """
67
+
68
+ Scenario: Extract one definition with explicit checksum to CSV
69
+ Given a file named "thedoc.adoc" with:
70
+ """
71
+ [define, requirement, TOTO-0001(b86dcbde)]
72
+ --
73
+ Exemple of multiline requirement.
74
+ Second line.
75
+ --
76
+ """
77
+ When I successfully run `defmastership export thedoc.adoc`
78
+ Then the file "thedoc.csv" should contain:
79
+ """
80
+ Type,Reference,Value,sha256
81
+ requirement,TOTO-0001,"Exemple of multiline requirement.
82
+ Second line.",b86dcbde
83
+ """
84
+ And the stdout should not contain anything
85
+ And the stderr should not contain anything
86
+
87
+ Scenario: Change one modifier
88
+ Given a file named "modifications.yml" with:
89
+ """
90
+ ---
91
+ :update_requirement_checksum:
92
+ :type: update_def_checksum
93
+ :config:
94
+ :def_type: requirement
95
+ """
96
+ And a file named "thedoc.adoc" with:
97
+ """
98
+ [define, requirement, TOTO-TEMP-XXX1]
99
+ --
100
+ the requirement value.
101
+ --
102
+ """
103
+ When I successfully run `defmastership modify --modifications update_requirement_checksum thedoc.adoc`
104
+ Then the stdout should not contain anything
105
+ And the stderr should not contain anything
106
+ And the file "thedoc.adoc" should contain:
107
+ """
108
+ [define, requirement, TOTO-TEMP-XXX1(244eed18)]
109
+ --
110
+ the requirement value.
111
+ --
112
+ """
113
+
114
+ Scenario: Change one modifier with already set checksum
115
+ Given a file named "modifications.yml" with:
116
+ """
117
+ ---
118
+ :update_requirement_checksum:
119
+ :type: update_def_checksum
120
+ :config:
121
+ :def_type: requirement
122
+ """
123
+ And a file named "thedoc.adoc" with:
124
+ """
125
+ [define, requirement, TOTO-TEMP-XXX1(abcd1234)]
126
+ --
127
+ the requirement value.
128
+ --
129
+ """
130
+ When I successfully run `defmastership modify --modifications update_requirement_checksum thedoc.adoc`
131
+ Then the stdout should not contain anything
132
+ And the stderr should not contain anything
133
+ And the file "thedoc.adoc" should contain:
134
+ """
135
+ [define, requirement, TOTO-TEMP-XXX1(244eed18)]
136
+ --
137
+ the requirement value.
138
+ --
139
+ """
140
+
141
+ Scenario: Checksum is calculated on included file for export
142
+ Given a file named "thedoc.adoc" with:
143
+ """
144
+ [define, requirement, TOTO-0001]
145
+ --
146
+ include::included.txt[]
147
+ --
148
+ """
149
+ And a file named "included.txt" with:
150
+ """
151
+ Exemple of multiline requirement.
152
+ Second line.
153
+ """
154
+ When I successfully run `defmastership export thedoc.adoc`
155
+ Then the file "thedoc.csv" should contain:
156
+ """
157
+ Type,Reference,Value,sha256
158
+ requirement,TOTO-0001,"Exemple of multiline requirement.
159
+ Second line.",b86dcbde
160
+ """
161
+ And the stdout should not contain anything
162
+ And the stderr should not contain anything
163
+
164
+ Scenario: Checksum is calculated on included file in ref modifcation
165
+ Given a file named "modifications.yml" with:
166
+ """
167
+ ---
168
+ :update_requirement_checksum:
169
+ :type: update_def_checksum
170
+ :config:
171
+ :def_type: requirement
172
+ """
173
+ And a file named "thedoc.adoc" with:
174
+ """
175
+ [define, requirement, TOTO-0001]
176
+ --
177
+ include::included.txt[]
178
+ --
179
+ """
180
+ And a file named "included.txt" with:
181
+ """
182
+ Exemple of multiline requirement.
183
+ Second line.
184
+ """
185
+ When I successfully run `defmastership modify --modifications update_requirement_checksum thedoc.adoc`
186
+ Then the file "thedoc.adoc" should contain:
187
+ """
188
+ [define, requirement, TOTO-0001(b86dcbde)]
189
+ --
190
+ include::included.txt[]
191
+ --
192
+ """
193
+ And the stdout should not contain anything
194
+ And the stderr should not contain anything
195
+
196
+ Scenario: Checksum take into account variables in CSV export
197
+ Given a file named "thedoc.adoc" with:
198
+ """
199
+ :variable: multiline
200
+ [define, requirement, TOTO-0001]
201
+ --
202
+ Exemple of {variable} requirement.
203
+ Second line.
204
+ --
205
+ """
206
+ When I successfully run `defmastership export thedoc.adoc`
207
+ Then the file "thedoc.csv" should contain:
208
+ """
209
+ Type,Reference,Value,sha256
210
+ requirement,TOTO-0001,"Exemple of multiline requirement.
211
+ Second line.",b86dcbde
212
+ """
213
+ And the stdout should not contain anything
214
+ And the stderr should not contain anything
215
+
216
+ Scenario: Checksum take into account variables in ref modifcation
217
+ Given a file named "modifications.yml" with:
218
+ """
219
+ ---
220
+ :update_requirement_checksum:
221
+ :type: update_def_checksum
222
+ :config:
223
+ :def_type: requirement
224
+ """
225
+ And a file named "thedoc.adoc" with:
226
+ """
227
+ :variable: multiline
228
+ [define, requirement, TOTO-0001]
229
+ --
230
+ Exemple of {variable} requirement.
231
+ Second line.
232
+ --
233
+ """
234
+ When I successfully run `defmastership modify --modifications update_requirement_checksum thedoc.adoc`
235
+ Then the file "thedoc.adoc" should contain:
236
+ """
237
+ [define, requirement, TOTO-0001(b86dcbde)]
238
+ --
239
+ Exemple of {variable} requirement.
240
+ Second line.
241
+ --
242
+ """
243
+ And the stdout should not contain anything
244
+ And the stderr should not contain anything
@@ -22,59 +22,6 @@ Feature: The extract command
22
22
  And the stdout should not contain anything
23
23
  And the stderr should not contain anything
24
24
 
25
- Scenario: Extract one definition with WRONG explicit checksum to CSV
26
- Given a file named "toto.adoc" with:
27
- """
28
- [define, requirement, TOTO-0001(babe1234)]
29
- def one
30
- """
31
- When I successfully run `defmastership export toto.adoc`
32
- Then the file "toto.csv" should contain:
33
- """
34
- Type,Reference,Value,sha256,Wrong explicit checksum
35
- requirement,TOTO-0001,def one,d27cb5c3,babe1234
36
- """
37
- And the stdout should not contain anything
38
- And the stderr should not contain anything
39
-
40
- Scenario: Extract two definitions with one WRONG explicit checksum to CSV
41
- Given a file named "toto.adoc" with:
42
- """
43
- [define, requirement, TOTO-0001(d27cb5c3)]
44
- def one
45
-
46
- [define, requirement, TOTO-0002(babe1234)]
47
- def two
48
- """
49
- When I successfully run `defmastership export toto.adoc`
50
- Then the file "toto.csv" should contain:
51
- """
52
- Type,Reference,Value,sha256,Wrong explicit checksum
53
- requirement,TOTO-0001,def one,d27cb5c3,\"\"
54
- requirement,TOTO-0002,def two,b80e1be5,babe1234
55
- """
56
- And the stdout should not contain anything
57
- And the stderr should not contain anything
58
-
59
- Scenario: Extract one definition with explicit checksum to CSV
60
- Given a file named "toto.adoc" with:
61
- """
62
- [define, requirement, TOTO-0001(b86dcbde)]
63
- --
64
- Exemple of multiline requirement.
65
- Second line.
66
- --
67
- """
68
- When I successfully run `defmastership export toto.adoc`
69
- Then the file "toto.csv" should contain:
70
- """
71
- Type,Reference,Value,sha256
72
- requirement,TOTO-0001,"Exemple of multiline requirement.
73
- Second line.",b86dcbde
74
- """
75
- And the stdout should not contain anything
76
- And the stderr should not contain anything
77
-
78
25
  Scenario: Extract to CSV with alternative CSV separator
79
26
  Given a file named "toto.adoc" with:
80
27
  """
data/lib/defmastership.rb CHANGED
@@ -22,3 +22,6 @@ require('defmastership/change_ref_line_modifier')
22
22
 
23
23
  require('defmastership/rename_included_files_modifier')
24
24
  require('defmastership/rename_included_files_line_modifier')
25
+
26
+ require('defmastership/update_def_checksum_modifier')
27
+ require('defmastership/update_def_checksum_line_modifier')
@@ -8,8 +8,8 @@ module DefMastership
8
8
  %i[replace_refdef replace_irefs]
9
9
  end
10
10
 
11
- def line_modifier_class
12
- ChangeRefLineModifier
11
+ def new_line_modifier(config, _adoc_texts)
12
+ ChangeRefLineModifier.from_config(config)
13
13
  end
14
14
  end
15
15
  end
@@ -1,6 +1,8 @@
1
1
  # Copyright (c) 2020 Jerome Arbez-Gindre
2
2
  # frozen_string_literal: true
3
3
 
4
+ require('asciidoctor')
5
+
4
6
  module DefMastership
5
7
  # Contains the content of a DefMastership document: mainly definitions
6
8
 
@@ -20,6 +22,22 @@ module DefMastership
20
22
  @definition_parser = DefinitionParser.new(self)
21
23
  end
22
24
 
25
+ def parse(lines)
26
+ lines.reject(&:commented?).each do |line|
27
+ (@in_literal ? FILTERS : FILTERS_IN_LITERAL).each do |filter|
28
+ next unless line.match(filter.regexp)
29
+
30
+ line = generate_event(filter.event, Regexp.last_match, line)
31
+
32
+ break if filter.consumed_line
33
+ end
34
+ end
35
+ end
36
+
37
+ def parse_file_with_preprocessor(adoc_file)
38
+ parse(Asciidoctor.load_file(adoc_file, safe: :unsafe, parse: false).reader.read_lines)
39
+ end
40
+
23
41
  def wrong_explicit_checksum?
24
42
  @definitions.reduce(false) do |res, definition|
25
43
  res || !definition.wrong_explicit_checksum.nil?
@@ -38,6 +56,10 @@ module DefMastership
38
56
  line
39
57
  end
40
58
 
59
+ def ref_to_def(ref)
60
+ @definitions.find { |definition| definition.reference == ref }
61
+ end
62
+
41
63
  def add_line(_match, line)
42
64
  @definitions.last << line
43
65
  line
@@ -96,18 +118,6 @@ module DefMastership
96
118
  new_line
97
119
  end
98
120
 
99
- def parse(lines)
100
- lines.reject(&:commented?).each do |line|
101
- (@in_literal ? FILTERS : FILTERS_IN_LITERAL).each do |filter|
102
- next unless line.match(filter.regexp)
103
-
104
- line = generate_event(filter.event, Regexp.last_match, line)
105
-
106
- break if filter.consumed_line
107
- end
108
- end
109
- end
110
-
111
121
  private
112
122
 
113
123
  def generate_event(event, match, line)
@@ -12,7 +12,7 @@ module DefMastership
12
12
  end
13
13
 
14
14
  def do_modifications(adoc_texts)
15
- line_modifier = line_modifier_class.from_config(@config)
15
+ line_modifier = new_line_modifier(@config, adoc_texts)
16
16
 
17
17
  adoc_texts =
18
18
  replacements.reduce(adoc_texts) do |texts, method|
@@ -4,27 +4,12 @@
4
4
  module DefMastership
5
5
  # Change included filenames
6
6
  class RenameIncludedFilesModifier < ModifierBase
7
- def do_modifications(adoc_texts)
8
- line_modifier = line_modifier_class.from_config(@config)
9
-
10
- adoc_texts =
11
- replacements.reduce(adoc_texts) do |texts, method|
12
- texts.transform_values do |text|
13
- text.lines.map { |line| line_modifier.public_send(method, line) }.join
14
- end
15
- end
16
-
17
- @config = line_modifier.config
18
- @changes = line_modifier.changes
19
- adoc_texts
20
- end
21
-
22
7
  def replacements
23
8
  %i[replace]
24
9
  end
25
10
 
26
- def line_modifier_class
27
- RenameIncludedFilesLineModifier
11
+ def new_line_modifier(config, _adoc_texts)
12
+ RenameIncludedFilesLineModifier.from_config(config)
28
13
  end
29
14
  end
30
15
  end
@@ -0,0 +1,39 @@
1
+ # Copyright (c) 2020 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ module DefMastership
5
+ # modify one line after another
6
+ class UpdateDefChecksumLineModifier < LineModifierBase
7
+ attr_accessor :document
8
+
9
+ def self.from_config(hash)
10
+ new.from_config(hash)
11
+ end
12
+
13
+ def initialize
14
+ super
15
+ @config = {
16
+ def_type: ''
17
+ }
18
+ end
19
+
20
+ def replace(line)
21
+ match = matched?(line)
22
+
23
+ return line unless match
24
+ return line unless match[:type] == def_type
25
+
26
+ explicit_checksum_str = match[:explicit_checksum].nil? ? '' : "(#{match[:explicit_checksum]})"
27
+ line.gsub("#{match[:reference]}#{explicit_checksum_str}") do
28
+ "#{match[:reference]}(#{@document.ref_to_def(match[:reference]).sha256})"
29
+ end
30
+ end
31
+
32
+ def matched?(line)
33
+ return if line.commented?
34
+ return unless line =~ DMRegexp::DEFINITION
35
+
36
+ Regexp.last_match
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ # Copyright (c) 2020 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ module DefMastership
5
+ # Update definition checksum wih calculated one
6
+ class UpdateDefChecksumModifier < ModifierBase
7
+ def replacements
8
+ %i[replace]
9
+ end
10
+
11
+ def new_line_modifier(config, adoc_texts)
12
+ document = Document.new
13
+ adoc_texts.each do |adoc_file, _|
14
+ document.parse_file_with_preprocessor(adoc_file)
15
+ end
16
+ line_modifier = UpdateDefChecksumLineModifier.from_config(config)
17
+ line_modifier.document = document
18
+ line_modifier
19
+ end
20
+ end
21
+ end
@@ -2,6 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module DefMastership
5
- VERSION = '1.0.6'
5
+ VERSION = '1.0.7'
6
6
  public_constant :VERSION
7
7
  end
@@ -426,9 +426,38 @@ RSpec.describe(DefMastership::Document) do
426
426
  end
427
427
  end
428
428
 
429
- describe '#wrong_explicit_checksum?' do
430
- subject(:document) { described_class.new }
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
431
459
 
460
+ describe '#wrong_explicit_checksum?' do
432
461
  let(:def1) { instance_double(DefMastership::Definition, 'definition') }
433
462
  let(:def2) { instance_double(DefMastership::Definition, 'definition') }
434
463
  let(:input_lines) do
@@ -473,4 +502,32 @@ RSpec.describe(DefMastership::Document) do
473
502
  it { expect(document.wrong_explicit_checksum?).to(eq(true)) }
474
503
  end
475
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
476
533
  end
@@ -0,0 +1,68 @@
1
+ # Copyright (c) 2020 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require('defmastership')
5
+
6
+ RSpec.describe(DefMastership::UpdateDefChecksumLineModifier) do
7
+ subject(:linemodifier) { described_class.new }
8
+
9
+ describe '.new' do
10
+ it { is_expected.not_to(be(nil)) }
11
+ it { is_expected.to(have_attributes(def_type: '')) }
12
+ it { is_expected.to(have_attributes(changes: [])) }
13
+ it { expect { linemodifier.user_defined_attribute }.to(raise_error(NoMethodError)) }
14
+ end
15
+
16
+ describe '.from_config' do
17
+ subject(:linemodifier) do
18
+ described_class.from_config(
19
+ def_type: 'requirement'
20
+ )
21
+ end
22
+
23
+ it { is_expected.not_to(be(nil)) }
24
+ it { is_expected.to(have_attributes(def_type: 'requirement')) }
25
+ it { is_expected.to(have_attributes(document: nil)) }
26
+ end
27
+
28
+ describe '#replace' do
29
+ subject(:linemodifier) do
30
+ described_class.from_config(
31
+ def_type: 'requirement'
32
+ )
33
+ end
34
+
35
+ let(:definition) { instance_double(DefMastership::Definition, 'definition') }
36
+ let(:document) { instance_double(DefMastership::Document, 'document') }
37
+ let(:definitions) { { 'REFERENCE' => definition } }
38
+
39
+ before do
40
+ linemodifier.document = document
41
+ allow(File).to(receive(:rename))
42
+ end
43
+
44
+ context 'when definition has not the good type' do
45
+ it do
46
+ expect(linemodifier.replace('[define,req,REFERENCE]'))
47
+ .to(eq('[define,req,REFERENCE]'))
48
+ end
49
+ end
50
+
51
+ context 'when definition has the good type' do
52
+ before do
53
+ allow(document).to(receive(:ref_to_def).with('REFERENCE').and_return(definition))
54
+ allow(definition).to(receive(:sha256).and_return('abcd1234'))
55
+ end
56
+
57
+ it do
58
+ expect(linemodifier.replace('[define,requirement,REFERENCE]'))
59
+ .to(eq('[define,requirement,REFERENCE(abcd1234)]'))
60
+ end
61
+
62
+ it do
63
+ expect(linemodifier.replace('[define,requirement,REFERENCE(bad)]'))
64
+ .to(eq('[define,requirement,REFERENCE(abcd1234)]'))
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,75 @@
1
+ # Copyright (c) 2020 Jerome Arbez-Gindre
2
+ # frozen_string_literal: true
3
+
4
+ require('defmastership')
5
+
6
+ RSpec.describe(DefMastership::UpdateDefChecksumModifier) 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::UpdateDefChecksumLineModifier, 'lineModifier') }
28
+ let(:document) { instance_double(DefMastership::Document, 'document') }
29
+
30
+ before do
31
+ allow(DefMastership::UpdateDefChecksumLineModifier).to(
32
+ receive(:from_config).with('fake config').and_return(line_modifier)
33
+ )
34
+ allow(DefMastership::Document).to(receive(:new).and_return(document))
35
+ allow(document).to(receive(:parse_file_with_preprocessor))
36
+ allow(line_modifier).to(receive(:'document=').with(document))
37
+ allow(line_modifier).to(receive(:replace).with("file1 line1\n").and_return("new file1 line1\n"))
38
+ allow(line_modifier).to(receive(:replace).with('file1 line2').and_return('new file1 line2'))
39
+ allow(line_modifier).to(receive(:replace).with("file2 line1\n").and_return("new file2 line1\n"))
40
+ allow(line_modifier).to(receive(:replace).with('file2 line2').and_return('new file2 line2'))
41
+ allow(line_modifier).to(receive(:config).and_return('new fake config'))
42
+ allow(line_modifier).to(receive(:changes).and_return([%w[from1 to1], %w[from2 to2]]))
43
+ end
44
+
45
+ context 'when detailed expectations' do
46
+ before { modifier.do_modifications(adoc_texts) }
47
+
48
+ it do
49
+ expect(DefMastership::UpdateDefChecksumLineModifier).to(
50
+ have_received(:from_config).with('fake config')
51
+ )
52
+ end
53
+
54
+ it { expect(document).to(have_received(:parse_file_with_preprocessor).with('file1.adoc')) }
55
+ it { expect(document).to(have_received(:parse_file_with_preprocessor).with('file2.adoc')) }
56
+ it { expect(line_modifier).to(have_received(:'document=').with(document)) }
57
+ it { expect(line_modifier).to(have_received(:replace).with("file1 line1\n")) }
58
+ it { expect(line_modifier).to(have_received(:replace).with('file1 line2')) }
59
+ it { expect(line_modifier).to(have_received(:replace).with("file2 line1\n")) }
60
+ it { expect(line_modifier).to(have_received(:replace).with('file2 line2')) }
61
+ it { expect(line_modifier).to(have_received(:config)) }
62
+ it { expect(line_modifier).to(have_received(:changes)) }
63
+ it { is_expected.to(have_attributes(config: 'new fake config')) }
64
+ it { is_expected.to(have_attributes(changes: [%w[from1 to1], %w[from2 to2]])) }
65
+ end
66
+
67
+ it do
68
+ expected_adoc = {
69
+ 'file1.adoc' => "new file1 line1\nnew file1 line2",
70
+ 'file2.adoc' => "new file2 line1\nnew file2 line2"
71
+ }
72
+ expect(modifier.do_modifications(adoc_texts)).to(eq(expected_adoc))
73
+ end
74
+ end
75
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defmastership
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jérôme Arbez-Gindre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-22 00:00:00.000000000 Z
11
+ date: 2021-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aruba
@@ -193,6 +193,7 @@ files:
193
193
  - defmastership.gemspec
194
194
  - defmastership.rdoc
195
195
  - features/changeref.feature
196
+ - features/checksum.feature
196
197
  - features/defmastership.feature
197
198
  - features/export.feature
198
199
  - features/modify.feature
@@ -216,6 +217,8 @@ files:
216
217
  - lib/defmastership/modifier_base.rb
217
218
  - lib/defmastership/rename_included_files_line_modifier.rb
218
219
  - lib/defmastership/rename_included_files_modifier.rb
220
+ - lib/defmastership/update_def_checksum_line_modifier.rb
221
+ - lib/defmastership/update_def_checksum_modifier.rb
219
222
  - lib/defmastership/version.rb
220
223
  - spec/spec_helper.rb
221
224
  - spec/unit/defmastership/batch_modifier_spec.rb
@@ -230,6 +233,8 @@ files:
230
233
  - spec/unit/defmastership/document_spec.rb
231
234
  - spec/unit/defmastership/rename_included_files_line_modifier_spec.rb
232
235
  - spec/unit/defmastership/rename_included_files_modifier_spec.rb
236
+ - spec/unit/defmastership/update_def_checksum_line_modifier_spec.rb
237
+ - spec/unit/defmastership/update_def_checksum_modifier_spec.rb
233
238
  - spec/unit/defmastership_spec.rb
234
239
  homepage: https://gitlab.com/jjag/defmastership/
235
240
  licenses: