defmastership 1.0.6 → 1.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/bin/defmastership +12 -7
  3. data/features/changeref.feature +33 -0
  4. data/features/definition_checksum.feature +298 -0
  5. data/features/definition_version.feature +204 -0
  6. data/features/export.feature +21 -74
  7. data/features/modify.feature +23 -1
  8. data/lib/defmastership.rb +6 -0
  9. data/lib/defmastership/batch_modifier.rb +2 -0
  10. data/lib/defmastership/change_ref_line_modifier.rb +2 -1
  11. data/lib/defmastership/change_ref_modifier.rb +2 -2
  12. data/lib/defmastership/constants.rb +4 -2
  13. data/lib/defmastership/csv_formatter.rb +12 -6
  14. data/lib/defmastership/csv_formatter_body.rb +8 -5
  15. data/lib/defmastership/csv_formatter_header.rb +5 -1
  16. data/lib/defmastership/definition.rb +4 -4
  17. data/lib/defmastership/document.rb +33 -27
  18. data/lib/defmastership/modifier_base.rb +1 -1
  19. data/lib/defmastership/rename_included_files_modifier.rb +2 -17
  20. data/lib/defmastership/update_def_checksum_line_modifier.rb +38 -0
  21. data/lib/defmastership/update_def_checksum_modifier.rb +21 -0
  22. data/lib/defmastership/update_def_version_line_modifier.rb +58 -0
  23. data/lib/defmastership/update_def_version_modifier.rb +25 -0
  24. data/lib/defmastership/version.rb +1 -1
  25. data/spec/unit/defmastership/batch_modifier_spec.rb +8 -0
  26. data/spec/unit/defmastership/change_ref_line_modifier_spec.rb +15 -0
  27. data/spec/unit/defmastership/csv_formatter_body_spec.rb +42 -60
  28. data/spec/unit/defmastership/csv_formatter_header_spec.rb +23 -1
  29. data/spec/unit/defmastership/csv_formatter_spec.rb +204 -67
  30. data/spec/unit/defmastership/definition_spec.rb +19 -4
  31. data/spec/unit/defmastership/document_spec.rb +129 -5
  32. data/spec/unit/defmastership/update_def_checksum_line_modifier_spec.rb +78 -0
  33. data/spec/unit/defmastership/update_def_checksum_modifier_spec.rb +75 -0
  34. data/spec/unit/defmastership/update_def_version_line_modifier_spec.rb +127 -0
  35. data/spec/unit/defmastership/update_def_version_modifier_spec.rb +80 -0
  36. metadata +12 -2
@@ -15,62 +15,9 @@ Feature: The extract command
15
15
  When I successfully run `defmastership export toto.adoc`
16
16
  Then the file "toto.csv" should contain:
17
17
  """
18
- Type,Reference,Value,sha256
18
+ Type,Reference,Value,Checksum
19
19
  requirement,TOTO-0001,"Exemple of multiline requirement.
20
- Second line.",b86dcbde
21
- """
22
- And the stdout should not contain anything
23
- And the stderr should not contain anything
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
20
+ Second line.",~b86dcbde
74
21
  """
75
22
  And the stdout should not contain anything
76
23
  And the stderr should not contain anything
@@ -87,9 +34,9 @@ Feature: The extract command
87
34
  When I successfully run `defmastership export --separator=';' toto.adoc`
88
35
  Then the file "toto.csv" should contain:
89
36
  """
90
- Type;Reference;Value;sha256
37
+ Type;Reference;Value;Checksum
91
38
  requirement;TOTO-0001;"Exemple of multiline requirement.
92
- Second line.";b86dcbde
39
+ Second line.";~b86dcbde
93
40
  """
94
41
 
95
42
  Scenario: Extract one definition with variable to CSV
@@ -107,10 +54,10 @@ Feature: The extract command
107
54
  When I successfully run `defmastership export toto.adoc`
108
55
  Then the file "toto.csv" should contain:
109
56
  """
110
- Type,Reference,Value,sha256
57
+ Type,Reference,Value,Checksum
111
58
  requirement,TOTO-0001,"Exemple of multiline requirement with variable: Variable content.
112
59
  Variable content : Variable content Toto.
113
- Third line with {not_defined_variable}.",3bf370ed
60
+ Third line with {not_defined_variable}.",~3bf370ed
114
61
  """
115
62
  And the stdout should not contain anything
116
63
  And the stderr should not contain anything
@@ -135,11 +82,11 @@ Feature: The extract command
135
82
  When I successfully run `defmastership export toto.adoc`
136
83
  Then the file "toto.csv" should contain:
137
84
  """
138
- Type,Reference,Value,sha256
85
+ Type,Reference,Value,Checksum
139
86
  requirement,TOTO-0001,"Exemple of multiline requirement.
140
- Second line.",b86dcbde
87
+ Second line.",~b86dcbde
141
88
  something_else,TOTO-0003,"only one paragraphe.
142
- with two sentences.",4761e172
89
+ with two sentences.",~4761e172
143
90
  """
144
91
  And the file "toto.csv" should not contain:
145
92
  """
@@ -225,9 +172,9 @@ Feature: The extract command
225
172
  When I successfully run `defmastership export toto.adoc`
226
173
  Then the file "toto.csv" should contain:
227
174
  """
228
- Type,Reference,Value,sha256,Labels
175
+ Type,Reference,Value,Checksum,Labels
229
176
  requirement,TOTO-0001,"Exemple of multiline requirement.
230
- Second line.",b86dcbde,"label1
177
+ Second line.",~b86dcbde,"label1
231
178
  label2"
232
179
  """
233
180
  And the stdout should not contain anything
@@ -248,9 +195,9 @@ Feature: The extract command
248
195
  When I successfully run `defmastership export toto.adoc`
249
196
  Then the file "toto.csv" should contain:
250
197
  """
251
- Type,Reference,Value,sha256,Participate to:
198
+ Type,Reference,Value,Checksum,Participate to:
252
199
  requirement,TOTO-0001,"Exemple of multiline requirement.
253
- Second line.",b86dcbde,"SYSTEM-0012
200
+ Second line.",~b86dcbde,"SYSTEM-0012
254
201
  SYSTEM-0014"
255
202
  """
256
203
  And the stdout should not contain anything
@@ -271,9 +218,9 @@ Feature: The extract command
271
218
  When I successfully run `defmastership export toto.adoc`
272
219
  Then the file "toto.csv" should contain:
273
220
  """
274
- Type,Reference,Value,sha256,Participate to:
221
+ Type,Reference,Value,Checksum,Participate to:
275
222
  requirement,TOTO-0001,"Exemple of multiline requirement.
276
- Second line.",b86dcbde,"SYSTEM-0012
223
+ Second line.",~b86dcbde,"SYSTEM-0012
277
224
  SYSTEM-0014"
278
225
  """
279
226
  And the stdout should not contain anything
@@ -295,9 +242,9 @@ Feature: The extract command
295
242
  And the stderr should not contain anything
296
243
  And the file "toto.csv" should contain:
297
244
  """
298
- Type,Reference,Value,sha256,Participate to:
245
+ Type,Reference,Value,Checksum,Participate to:
299
246
  requirement,TOTO-0001,"Exemple of multiline requirement.
300
- Second line.",b86dcbde,"SYSTEM-0012
247
+ Second line.",~b86dcbde,"SYSTEM-0012
301
248
  SYSTEM-0014"
302
249
  """
303
250
 
@@ -314,9 +261,9 @@ Feature: The extract command
314
261
  When I successfully run `defmastership export toto.adoc`
315
262
  Then the file "toto.csv" should contain:
316
263
  """
317
- Type,Reference,Value,sha256,Internal links
264
+ Type,Reference,Value,Checksum,Internal links
318
265
  requirement,TOTO-0001,"Exemple of multiline requirement.
319
- Second line: defs:iref[TOTO-0001], defs:iref[TOTO-0123].",059b7188,"TOTO-0001
266
+ Second line: defs:iref[TOTO-0001], defs:iref[TOTO-0123].",~059b7188,"TOTO-0001
320
267
  TOTO-0123
321
268
  TOTO-0002"
322
269
  """
@@ -339,8 +286,8 @@ Feature: The extract command
339
286
  And the stdout should not contain anything
340
287
  Then the file "toto.csv" should contain:
341
288
  """
342
- Type,Reference,Value,sha256,Verified by:,Criticity:
343
- requirement,TOTO-0001,One single line.,554b0ff5,Beautiful Test,Very cool
289
+ Type,Reference,Value,Checksum,Verified by:,Criticity:
290
+ requirement,TOTO-0001,One single line.,~554b0ff5,Beautiful Test,Very cool
344
291
  """
345
292
  And the stdout should not contain anything
346
293
  And the stderr should not contain anything
@@ -3,7 +3,7 @@ Feature: The modify command
3
3
  In order to manage to applu predefined modifications
4
4
  I want defmastership to apply automatic modifications
5
5
 
6
- Scenario: Change one modifier
6
+ Scenario: Changes with one modifier
7
7
  Given a file named "modifications.yml" with:
8
8
  """
9
9
  ---
@@ -36,6 +36,28 @@ Feature: The modify command
36
36
  [define, requirement, TOTO-0123]
37
37
  """
38
38
 
39
+
40
+ Scenario: Change with wrong modifier
41
+ Given a file named "modifications.yml" with:
42
+ """
43
+ ---
44
+ :temp-references:
45
+ :type: change_ref
46
+ :config:
47
+ :from_regexp: TOTO-TEMP-[X\d]{4}
48
+ :to_template: TOTO-%<next_ref>04d
49
+ :next_ref: 123
50
+ """
51
+ And a file named "thedoc.adoc" with:
52
+ """
53
+ [define, requirement, TOTO-TEMP-XXX1]
54
+ """
55
+ When I run `defmastership modify --modifications wrong-modification thedoc.adoc`
56
+ Then it should fail with:
57
+ """
58
+ error: wrong-modification is not a known modification
59
+ """
60
+
39
61
  Scenario: Make modifications with yaml file parameter
40
62
  Given a file named "pouet.yml" with:
41
63
  """
data/lib/defmastership.rb CHANGED
@@ -22,3 +22,9 @@ 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')
28
+
29
+ require('defmastership/update_def_version_modifier')
30
+ require('defmastership/update_def_version_line_modifier')
@@ -26,6 +26,8 @@ module DefMastership
26
26
  private
27
27
 
28
28
  def modifier_from(modif)
29
+ raise(ArgumentError, "#{modif} is not a known modification") if @config[modif.to_sym].nil?
30
+
29
31
  class_name = "#{@config[modif.to_sym][:type].split('_').map(&:capitalize).join}Modifier"
30
32
  DefMastership.const_get(class_name).new(@config[modif.to_sym][:config])
31
33
  end
@@ -66,12 +66,13 @@ module DefMastership
66
66
  regexp_str =
67
67
  "(?<before>#{REGEXP_FROM[const][:before]})" \
68
68
  "(?<from>#{from})" \
69
+ "#{DMRegexp::DEF_VERSION_AND_CHECKSUM}" \
69
70
  "(?<after>#{REGEXP_FROM[const][:after]})"
70
71
  Regexp.new(regexp_str, Regexp::EXTENDED)
71
72
  end
72
73
 
73
74
  def text_with(match, replacement)
74
- match[:before] + replacement + match[:after]
75
+ match[:before] + replacement + (match[:version_and_checksum] || '') + match[:after]
75
76
  end
76
77
 
77
78
  def hmerge(match)
@@ -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
@@ -20,10 +20,12 @@ module DefMastership
20
20
  (,\s*\[\s*(?<labels>.*?)\s*\])?\s*\]
21
21
  AFT
22
22
 
23
- DEF_CHECKSUM = '(\((?<explicit_checksum>[[:alnum:]]+)\))?'
23
+ DEF_VERSION_AND_CHECKSUM = '(?<version_and_checksum>' \
24
+ '\((?<explicit_version>[^~]+)?(?<explicit_checksum>~[[:alnum:]]+)?\)' \
25
+ ')?'
24
26
 
25
27
  DEFINITION = Regexp.new(
26
- "#{DEF_BEFORE_REF}(?<reference>[\\w:_-]+)#{DEF_CHECKSUM}#{DEF_AFTER_REF}",
28
+ "#{DEF_BEFORE_REF}(?<reference>[\\w:_-]+)#{DEF_VERSION_AND_CHECKSUM}#{DEF_AFTER_REF}",
27
29
  Regexp::EXTENDED
28
30
  )
29
31
 
@@ -8,18 +8,13 @@ require('defmastership/csv_formatter_body')
8
8
  module DefMastership
9
9
  # to export a CSV file
10
10
  class CSVFormatter
11
- COLUMN_LIST1 = %i[fixed wrong_explicit_checksum labels eref iref attributes].freeze
12
- COLUMN_LIST2 = %i[fixed labels eref iref attributes].freeze
13
- private_constant :COLUMN_LIST1
14
- private_constant :COLUMN_LIST2
15
-
16
11
  def initialize(doc, sep = ',')
17
12
  @doc = doc
18
13
  @sep = sep
19
14
  end
20
15
 
21
16
  def export_to(output_file)
22
- column_list = @doc.wrong_explicit_checksum? ? COLUMN_LIST1 : COLUMN_LIST2
17
+ column_list = build_column_list
23
18
  CSV.open(output_file, 'w:ISO-8859-1', { col_sep: @sep }) do |csv|
24
19
  csv << header(column_list)
25
20
  @doc.definitions.each { |definition| csv << body(definition, column_list) }
@@ -39,5 +34,16 @@ module DefMastership
39
34
  body_line = column_list.map { |part| body_formatter.public_send(part, definition) }
40
35
  body_line.reduce(:+)
41
36
  end
37
+
38
+ def build_column_list
39
+ column_list = [:fixed]
40
+ column_list += [:wrong_explicit_checksum] if @doc.wrong_explicit_checksum?
41
+ column_list += [:explicit_version] if @doc.explicit_version?
42
+ column_list += [:labels] unless @doc.labels.empty?
43
+ column_list += [:eref] unless @doc.eref.empty?
44
+ column_list += [:iref] if @doc.iref
45
+ column_list += [:attributes] unless @doc.attributes.empty?
46
+ column_list
47
+ end
42
48
  end
43
49
  end
@@ -19,8 +19,13 @@ module DefMastership
19
19
  wrong_explicit_checksum ? [wrong_explicit_checksum] : ['']
20
20
  end
21
21
 
22
+ def explicit_version(definition)
23
+ explicit_version = definition.explicit_version
24
+ explicit_version ? [explicit_version] : ['']
25
+ end
26
+
22
27
  def labels(definition)
23
- @doc.labels.empty? ? [] : [definition.labels.to_a.join("\n")]
28
+ [definition.labels.to_a.join("\n")]
24
29
  end
25
30
 
26
31
  def eref(definition)
@@ -28,13 +33,11 @@ module DefMastership
28
33
  end
29
34
 
30
35
  def iref(definition)
31
- @doc.iref ? [definition.iref.join("\n")] : []
36
+ [definition.iref.join("\n")]
32
37
  end
33
38
 
34
39
  def attributes(definition)
35
- @doc.attributes.map do |key, _|
36
- definition.attributes[key]
37
- end
40
+ @doc.attributes.map { |key, _| definition.attributes[key] }
38
41
  end
39
42
  end
40
43
  end
@@ -11,13 +11,17 @@ module DefMastership
11
11
  end
12
12
 
13
13
  def fixed
14
- %w[Type Reference Value sha256]
14
+ %w[Type Reference Value Checksum]
15
15
  end
16
16
 
17
17
  def wrong_explicit_checksum
18
18
  @doc.wrong_explicit_checksum? ? ['Wrong explicit checksum'] : []
19
19
  end
20
20
 
21
+ def explicit_version
22
+ @doc.explicit_version? ? ['Version'] : []
23
+ end
24
+
21
25
  def labels
22
26
  @doc.labels.empty? ? [] : %w[Labels]
23
27
  end
@@ -6,19 +6,19 @@ require 'digest'
6
6
  module DefMastership
7
7
  # DefMastership definition: contains all data of a definition
8
8
  class Definition
9
- attr_reader :type, :reference, :lines, :labels, :eref, :iref, :attributes
9
+ attr_reader :type, :reference, :lines, :labels, :eref, :iref, :attributes, :explicit_version
10
10
 
11
11
  def initialize(match)
12
12
  @type = match[:type]
13
13
  @reference = match[:reference]
14
14
  @lines = []
15
15
  @labels = Set.new
16
- labels = match[:labels]
17
- @labels.merge(labels.split(/\s*,\s*/).to_set) if labels
16
+ @labels.merge(match[:labels].split(/\s*,\s*/).to_set) if match[:labels]
18
17
  @eref = Hash.new([])
19
18
  @iref = []
20
19
  @attributes = {}
21
20
  @explicit_checksum = match[:explicit_checksum]
21
+ @explicit_version = match[:explicit_version]
22
22
  end
23
23
 
24
24
  def <<(new_line)
@@ -31,7 +31,7 @@ module DefMastership
31
31
  end
32
32
 
33
33
  def sha256
34
- Digest::SHA2.new(256).hexdigest(value).split(//).last(8).join
34
+ "~#{Digest::SHA2.new(256).hexdigest(value).split(//).last(8).join}"
35
35
  end
36
36
 
37
37
  def wrong_explicit_checksum
@@ -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
 
@@ -16,16 +18,37 @@ module DefMastership
16
18
  @attributes = {}
17
19
  @in_literal = true
18
20
  @variables = {}
19
-
20
21
  @definition_parser = DefinitionParser.new(self)
21
22
  end
22
23
 
24
+ def parse(lines)
25
+ lines.reject(&:commented?).each do |line|
26
+ (@in_literal ? FILTERS : FILTERS_IN_LITERAL).each do |filter|
27
+ next unless line.match(filter.regexp)
28
+
29
+ line = generate_event(filter.event, Regexp.last_match, line)
30
+
31
+ break if filter.consumed_line
32
+ end
33
+ end
34
+ end
35
+
36
+ def parse_file_with_preprocessor(adoc_file)
37
+ parse(Asciidoctor.load_file(adoc_file, safe: :unsafe, parse: false).reader.read_lines)
38
+ end
39
+
23
40
  def wrong_explicit_checksum?
24
41
  @definitions.reduce(false) do |res, definition|
25
42
  res || !definition.wrong_explicit_checksum.nil?
26
43
  end
27
44
  end
28
45
 
46
+ def explicit_version?
47
+ @definitions.reduce(false) do |res, definition|
48
+ res || !definition.explicit_version.nil?
49
+ end
50
+ end
51
+
29
52
  def code_block_delimiter(_match, line)
30
53
  @in_literal ^= true
31
54
  line
@@ -38,6 +61,10 @@ module DefMastership
38
61
  line
39
62
  end
40
63
 
64
+ def ref_to_def(ref)
65
+ @definitions.find { |definition| definition.reference == ref }
66
+ end
67
+
41
68
  def add_line(_match, line)
42
69
  @definitions.last << line
43
70
  line
@@ -45,17 +72,12 @@ module DefMastership
45
72
 
46
73
  def new_eref_setup(match, line)
47
74
  @eref[match[:refname].to_sym] ||= {}
48
-
49
- @eref[match[:refname].to_sym][match[:symb].to_sym] =
50
- match[:value]
75
+ @eref[match[:refname].to_sym][match[:symb].to_sym] = match[:value]
51
76
  line
52
77
  end
53
78
 
54
79
  def new_eref_def(match, line)
55
- @definitions.last.add_eref(
56
- match[:refname].to_sym,
57
- match[:extrefs]
58
- )
80
+ @definitions.last.add_eref(match[:refname].to_sym, match[:extrefs])
59
81
  line
60
82
  end
61
83
 
@@ -73,10 +95,7 @@ module DefMastership
73
95
  end
74
96
 
75
97
  def new_attribute_value(match, line)
76
- @definitions.last.set_attribute(
77
- match[:attr].to_sym,
78
- match[:value]
79
- )
98
+ @definitions.last.set_attribute(match[:attr].to_sym, match[:value])
80
99
  line
81
100
  end
82
101
 
@@ -86,26 +105,13 @@ module DefMastership
86
105
  end
87
106
 
88
107
  def new_variable_use(_match, line)
89
- new_line = line.dup
90
108
  line.scan(DMRegexp::VARIABLE_USE) do |_|
91
109
  varname = Regexp.last_match[:varname]
92
110
  next if @variables[varname.to_sym].nil?
93
111
 
94
- new_line.gsub!("{#{varname}}", @variables[varname.to_sym])
95
- end
96
- new_line
97
- end
98
-
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
112
+ line = line.gsub("{#{varname}}", @variables[varname.to_sym])
108
113
  end
114
+ line
109
115
  end
110
116
 
111
117
  private