Class: InfernoRequirementsTools::Tasks::CollectRequirements

Inherits:
Object
  • Object
show all
Defined in:
lib/inferno_requirements_tools/tasks/collect_requirements.rb

Overview

This class manages the collection of requirements details from requirements planning excel workbooks into a CSV representation. Currently splits out Requirements and Planned Not Tested Requirements into two separate files.

The ‘run_check` method will check whether the previously generated file is up-to-date.

Constant Summary collapse

TEST_KIT_ID =

Update these constants based on the test kit.

'carin-for-blue-button-test-kit'
INPUT_SETS =
['hl7.fhir.us.carin-bb_2.0.0'].freeze
TEST_KIT_CODE_FOLDER =

Derivative constants

TEST_KIT_ID.gsub('-', '_')
INPUT_HEADERS =
['ID*', 'URL*', 'Requirement*', 'Conformance*', 'Actor*', 'Sub-Requirement(s)', 'Conditionality',
'Verifiable?', 'Verifiability Details', 'Planning To Test?', 'Planning To Test Details'].freeze
REQUIREMENTS_OUTPUT_HEADERS =
['Req Set', 'ID', 'URL', 'Requirement', 'Conformance', 'Actor',
'Sub-Requirement(s)', 'Conditionality'].freeze
REQUIREMENTS_OUTPUT_FILE_NAME =
"#{TEST_KIT_ID}_requirements.csv"
REQUIREMENTS_OUTPUT_FILE =
File.join('lib', TEST_KIT_CODE_FOLDER, 'requirements',
REQUIREMENTS_OUTPUT_FILE_NAME).freeze
PLANNED_NOT_TESTED_OUTPUT_HEADERS =
['Req Set', 'ID', 'Reason', 'Details'].freeze
PLANNED_NOT_TESTED_OUTPUT_FILE_NAME =
"#{TEST_KIT_ID}_out_of_scope_requirements.csv"
PLANNED_NOT_TESTED_OUTPUT_FILE =
File.join('lib', TEST_KIT_CODE_FOLDER, 'requirements',
PLANNED_NOT_TESTED_OUTPUT_FILE_NAME).freeze
BOM =
"\xEF\xBB\xBF"

Instance Method Summary collapse

Instance Method Details

#check_for_req_set_files(input_directory) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 101

def check_for_req_set_files(input_directory)
  available_worksheets = Dir.glob(File.join(input_directory, '*.xlsx')).reject { |f| f.include?('~$') }

  INPUT_SETS.each do |req_set_id|
    req_set_file = available_worksheets&.find { |worksheet_file| worksheet_file.include?(req_set_id) }

    if req_set_file&.empty?
      puts "Could not find input file for set #{req_set_id} in directory #{input_directory}. Aborting requirements collection..."
      exit(1)
    end
    input_file_map[req_set_id] = req_set_file
  end
end

#extract_input_rows_for_set(req_set_id) ⇒ Object



47
48
49
50
51
52
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 47

def extract_input_rows_for_set(req_set_id)
  CSV.parse(Roo::Spreadsheet.open(input_file_map[req_set_id]).sheet('Requirements').to_csv,
            headers: true).map do |row|
    row.to_h.slice(*INPUT_HEADERS)
  end
end

#input_file_mapObject



34
35
36
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 34

def input_file_map
  @input_file_map ||= {}
end

#input_rowsObject



38
39
40
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 38

def input_rows
  @input_rows ||= {}
end

#input_rows_for_set(req_set_id) ⇒ Object



42
43
44
45
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 42

def input_rows_for_set(req_set_id)
  input_rows[req_set_id] = extract_input_rows_for_set(req_set_id) unless input_rows.has_key?(req_set_id)
  input_rows[req_set_id]
end

#new_planned_not_tested_csvObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 76

def new_planned_not_tested_csv
  @new_planned_not_tested_csv ||=
    CSV.generate do |csv|
      csv << PLANNED_NOT_TESTED_OUTPUT_HEADERS

      INPUT_SETS.each do |req_set_id|
        input_rows = input_rows_for_set(req_set_id)
        input_rows.each do |row| # NOTE: use row order from source file
          not_verifiable = row['Verifiable?']&.downcase == 'no' || row['Verifiable?']&.downcase == 'false'
          not_tested = row['Planning To Test?']&.downcase == 'no' || row['Planning To Test?']&.downcase == 'false'
          next unless not_verifiable || not_tested

          csv << [req_set_id,
                  row['ID*'],
                  not_verifiable ? 'Not Verifiable' : 'Not Tested',
                  not_verifiable ? row['Verifiability Details'] : row['Planning To Test Details']]
        end
      end
    end
end

#new_requirements_csvObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 54

def new_requirements_csv
  @new_requirements_csv ||=
    CSV.generate do |csv|
      csv << REQUIREMENTS_OUTPUT_HEADERS

      INPUT_SETS.each do |req_set_id|
        input_rows = input_rows_for_set(req_set_id)
        input_rows.each do |row| # NOTE: use row order from source file
          row['Req Set'] = req_set_id

          csv << REQUIREMENTS_OUTPUT_HEADERS.map do |header|
            row.has_key?(header) ? row[header] : row["#{header}*"]
          end
        end
      end
    end
end

#old_planned_not_tested_csvObject



97
98
99
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 97

def old_planned_not_tested_csv
  @old_planned_not_tested_csv ||= File.read(PLANNED_NOT_TESTED_OUTPUT_FILE)
end

#old_requirements_csvObject



72
73
74
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 72

def old_requirements_csv
  @old_requirements_csv ||= File.read(REQUIREMENTS_OUTPUT_FILE)
end

#run(input_directory) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 115

def run(input_directory)
  check_for_req_set_files(input_directory)

  update_requirements = if File.exist?(REQUIREMENTS_OUTPUT_FILE)
                          if old_requirements_csv == (BOM + new_requirements_csv)
                            puts "'#{REQUIREMENTS_OUTPUT_FILE_NAME}' file is up to date."
                            false
                          else
                            puts 'Requirements set has changed.'
                            true
                          end
                        else
                          puts "No existing #{REQUIREMENTS_OUTPUT_FILE_NAME}."
                          true
                        end
  if update_requirements
    puts "Writing to file #{REQUIREMENTS_OUTPUT_FILE}..."
    File.write(REQUIREMENTS_OUTPUT_FILE, BOM + new_requirements_csv, encoding: Encoding::UTF_8)
  end

  udpate_planned_not_tested = if File.exist?(PLANNED_NOT_TESTED_OUTPUT_FILE)
                                if old_planned_not_tested_csv == (BOM + new_planned_not_tested_csv)
                                  puts "'#{PLANNED_NOT_TESTED_OUTPUT_FILE_NAME}' file is up to date."
                                  false
                                else
                                  puts 'Planned Not Tested Requirements set has changed.'
                                  true
                                end
                              else
                                puts "No existing #{PLANNED_NOT_TESTED_OUTPUT_FILE_NAME}."
                                true
                              end
  if udpate_planned_not_tested
    puts "Writing to file #{PLANNED_NOT_TESTED_OUTPUT_FILE}..."
    File.write(PLANNED_NOT_TESTED_OUTPUT_FILE, BOM + new_planned_not_tested_csv, encoding: Encoding::UTF_8)
  end

  puts 'Done.'
end

#run_check(input_directory) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/inferno_requirements_tools/tasks/collect_requirements.rb', line 155

def run_check(input_directory)
  check_for_req_set_files(input_directory)

  requirements_ok = if File.exist?(REQUIREMENTS_OUTPUT_FILE)
                      if old_requirements_csv == (BOM + new_requirements_csv)
                        puts "'#{REQUIREMENTS_OUTPUT_FILE_NAME}' file is up to date."
                        true
                      else
                        puts "#{REQUIREMENTS_OUTPUT_FILE_NAME} file is out of date."
                        false
                      end
                    else
                      puts "No existing #{REQUIREMENTS_OUTPUT_FILE_NAME} file."
                      false
                    end

  planned_not_tested_requirements_ok = if File.exist?(PLANNED_NOT_TESTED_OUTPUT_FILE)
                                         if old_planned_not_tested_csv == (BOM + new_planned_not_tested_csv)
                                           puts "'#{PLANNED_NOT_TESTED_OUTPUT_FILE_NAME}' file is up to date."
                                           true
                                         else
                                           puts "#{PLANNED_NOT_TESTED_OUTPUT_FILE_NAME} file is out of date."
                                           false
                                         end
                                       else
                                         puts "No existing #{PLANNED_NOT_TESTED_OUTPUT_FILE_NAME} file."
                                         false
                                       end

  return if planned_not_tested_requirements_ok && requirements_ok

  puts <<~MESSAGE
    Check Failed. To resolve, run:

          bundle exec rake "requirements:collect_requirements[<input_directory>]"

  MESSAGE
  exit(1)
end