Class: FeatureMap::Private::Validations::FeaturesUpToDate

Inherits:
Object
  • Object
show all
Includes:
Validator
Defined in:
lib/feature_map/private/validations/features_up_to_date.rb

Instance Method Summary collapse

Methods included from Validator

all, included

Instance Method Details

#validation_errors(files:, autocorrect: true, stage_changes: true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/feature_map/private/validations/features_up_to_date.rb', line 7

def validation_errors(files:, autocorrect: true, stage_changes: true)
  return [] if Private.configuration.skip_features_validation

  actual_content_lines = AssignmentsFile.actual_contents_lines
  expected_content_lines = AssignmentsFile.expected_contents_lines

  features_file_up_to_date = actual_content_lines == expected_content_lines
  errors = []

  if !features_file_up_to_date
    if autocorrect
      AssignmentsFile.write!
      if stage_changes
        `git add #{AssignmentsFile.path}`
      end
    # If there is no current file or its empty, display a shorter message.
    elsif actual_content_lines == ['']
      errors << <<~FEATURES_FILE_ERROR
        .feature_map/assignments.yml out of date. Run `bin/featuremap validate` to update the .feature_map/assignments.yml file
      FEATURES_FILE_ERROR
    else
      missing_lines = expected_content_lines - actual_content_lines
      extra_lines = actual_content_lines - expected_content_lines

      missing_lines_text = if missing_lines.any?
                             <<~COMMENT
                               .feature_map/assignments.yml should contain the following lines, but does not:
                               #{missing_lines.map { |line| "- \"#{line}\"" }.join("\n")}
                             COMMENT
                           end

      extra_lines_text = if extra_lines.any?
                           <<~COMMENT
                             .feature_map/assignments.yml should not contain the following lines, but it does:
                             #{extra_lines.map { |line| "- \"#{line}\"" }.join("\n")}
                           COMMENT
                         end

      diff_text = if missing_lines_text && extra_lines_text
                    "#{missing_lines_text}\n#{extra_lines_text}".chomp
                  elsif missing_lines_text
                    missing_lines_text
                  elsif extra_lines_text
                    extra_lines_text
                  else
                    <<~TEXT
                      There may be extra lines, or lines are out of order.
                      You can try to regenerate the .feature_map/assignments.yml file from scratch:
                      1) `rm .feature_map/assignments.yml`
                      2) `bin/featuremap validate`
                    TEXT
                  end

      errors << <<~FEATURES_FILE_ERROR
        .feature_map/assignments.yml out of date. Run `bin/featuremap validate` to update the .feature_map/assignments.yml file

        #{diff_text.chomp}
      FEATURES_FILE_ERROR
    end
  end

  errors
end