Skip to content

Commit

Permalink
Add test to check for orphan yaml variables in the content (#4173)
Browse files Browse the repository at this point in the history
* Adds a test that checks for orphan variables in the config/values/*.yml files that are not called in the front end content of the site

* Added check for variables in front matter components

* Improved regex

* Rubocop has some opinions

* Remove ruby files from list of files to be scanned

* Refactored spec to keep rubocop happy and format error messages to make them easier to read

* Fix regex using non-capturing group, added an ignore list to reject files with variables we want to keep

* Removed unused variables from front end

* Remove images from components list in markdown template handler as this seems to be handled differently and there is a class with the naming

* Remove calls_to_action from template handlers

* Added another let for merging yaml and component hashes
  • Loading branch information
spencerldixon authored Sep 13, 2024
1 parent 599b7de commit 2b06e85
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@ When I spot the exact barrier stopping a child from learning, I always make sure
When you train to teach physics, you can get an income. During my teacher training my family and I could afford for me to train for a year.

We’re here to answer your questions and provide advice about teaching, whether you’re just thinking about it or you’re ready to apply. [Find out how you can get help and support with getting into teaching](/help-and-support).

$physics-initial-teacher-training$
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,3 @@ You need [specific qualifications to teach a class of pupils with hearing impair
A special educational needs coordinator (SENCO) assesses, plans and monitors the progress of disabled pupils and pupils with special educational needs.

Once you’re a qualified teacher you'll need to complete the [National Award in Special Educational Needs Coordination (NASENCo)](https://nasen.org.uk/page/nasenco) when you take up your SENCO post.

$get-an-adviser$
95 changes: 95 additions & 0 deletions spec/views/identify_orphan_variables_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require "rails_helper"

YAML_FILES = Dir.glob("config/values/*.yml")

CONTENT_FILES = [
PageLister.all_md_files,
PageLister.all_erb_files,
PageLister.all_locale_files,
].flatten

VARIABLE_REGEX = /<%=\s*(?:v|value)\s*:(?<content>[a-zA-Z_0-9-]+?)\s*%> |
\$(?<content>[a-zA-Z_0-9-]+?)\$ |
%\{(?<content>[a-zA-Z_0-9-]+?)\} |
\#\{\s*v\s*:(?<content>[a-zA-Z_0-9-]+>?)\}/x

# Matches <%= v :thing %> or <%= value :thing %>
# Matches $thing$
# Matches %{thing}
# Matches #{v :thing}

COMPONENT_TYPES = TemplateHandlers::Markdown::COMPONENT_TYPES + %w[calls_to_action images]

IGNORE_VARIABLES = {
"config/locales/loaf.yml" => %w[invalid valid],
}.freeze

RSpec.describe "orphan variables checker" do
include I18n::Backend::Flatten

let!(:yaml_files_and_values) do
YAML_FILES.to_h do |file|
yaml = YAML.load(File.read(file))
flattened_yaml = flatten_translations(nil, yaml, nil, false).transform_keys { |key| key.to_s.gsub(".", "_") }

[file, flattened_yaml.keys]
end
end

let!(:content_files_and_values) do
CONTENT_FILES.index_with do |file|
File.read(file).scan(VARIABLE_REGEX).map(&:compact).flatten
end
end

let!(:component_files_and_keys) do
PageLister.all_md_files.index_with do |file|
content = File.open(file).read
front_matter = FrontMatterParser::Parser.new(:md).call(content).front_matter

COMPONENT_TYPES.map { |type| front_matter[type] }.compact.map(&:keys).flatten
end
end

let!(:frontend_files_and_values) { (yaml_files_and_values.values + component_files_and_keys.values).flatten }

let!(:orphan_frontend_variables) do
orphan_frontend_variables = content_files_and_values.transform_values { |values|
values.reject { |value| frontend_files_and_values.include?(value) }
}.compact_blank

remove_ignored_variables(orphan_frontend_variables)
end

let!(:orphan_yaml_variables) do
orphan_yaml_variables = yaml_files_and_values.transform_values { |values|
values.reject { |value| content_files_and_values.values.flatten.include?(value) }
}.compact_blank

remove_ignored_variables(orphan_yaml_variables)
end

it "does not find any variables in the config/values/*.yml files that are not references in any markdown, erb, or locale file" do
expect(orphan_yaml_variables).to be_empty, error_message(orphan_yaml_variables)
end

it "does not find any variables in the frontend that are not referenced in any front matter component or /config/values/*.yml file" do
expect(orphan_frontend_variables).to be_empty, error_message(orphan_frontend_variables)
end

def error_message(file_variables_hash)
file_variables_hash.map.with_index { |(file, orphan), index|
"#{index.next}: #{file} contains orphan key(s) '#{orphan.join(', ')}'"
}.join("\n")
end

def remove_ignored_variables(hash, ignore_hash = IGNORE_VARIABLES)
ignore_hash.map do |file, orphans|
next if hash[file].nil?

hash[file] = hash[file] - orphans
end

hash.compact_blank
end
end

0 comments on commit 2b06e85

Please sign in to comment.