From 75ce6c8736ad6e9c7995623d5767af6c09b25f16 Mon Sep 17 00:00:00 2001 From: Sam Davies Date: Wed, 11 Oct 2023 12:49:19 +0100 Subject: [PATCH] WEB-6543: Updating linter file existence checker to work with subfiles Paths in markdown metadata should always be relative to the markdown file itself. This updates the file existence checker so that that is the case. --- app/lib/linting/metadata/captions_file.rb | 5 ++++- .../file_attribute_existence_checker.rb | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/app/lib/linting/metadata/captions_file.rb b/app/lib/linting/metadata/captions_file.rb index 8a84911..a527aa2 100644 --- a/app/lib/linting/metadata/captions_file.rb +++ b/app/lib/linting/metadata/captions_file.rb @@ -40,13 +40,16 @@ def video_course_captions def module_captions caption_files = ModuleFile.new(file:, attributes:).file_path_list caption_files.map do |file| - if yaml?(file) + captions_file = if yaml?(file) load_yaml(File.read(file)).deep_symbolize_keys[:captions_file] else @markdown_metadata = nil @path = file markdown_metadata[:captions_file] end + next unless captions_file.present? + + [captions_file, file] end.compact end diff --git a/app/lib/linting/metadata/file_attribute_existence_checker.rb b/app/lib/linting/metadata/file_attribute_existence_checker.rb index 8e469b6..ae66738 100644 --- a/app/lib/linting/metadata/file_attribute_existence_checker.rb +++ b/app/lib/linting/metadata/file_attribute_existence_checker.rb @@ -8,15 +8,21 @@ module FileAttributeExistenceChecker def file_attribute_annotations file_path_list.map do |path| - next if relative_file_exists?(path) + if path.is_a?(Array) + relative_to = path.second + path = path.first + else + relative_to = file + end + next if relative_file_exists?(path, relative_to:) line = missing_file_line(path) Annotation.new( start_line: line, end_line: line, - absolute_path: file, + absolute_path: relative_to, annotation_level: 'failure', - message: "`release.yaml` includes references to unknown #{file_description} file: `#{path}", + message: "`#{relative_to}` includes references to unknown #{file_description} file: `#{path}", title: "Missing #{file_description} file" ) end.compact @@ -33,9 +39,9 @@ def file_description private # Check whether script file exists - def relative_file_exists?(path) + def relative_file_exists?(path, relative_to: nil) # Find path relative to the release.yaml file - file_path = Pathname.new(file).dirname.join(path) + file_path = Pathname.new(relative_to || file).dirname.join(path) # Check whether this exists file_exists?(file_path) end @@ -43,7 +49,7 @@ def relative_file_exists?(path) # Search release.yaml line by line to find the file references def missing_file_line(path) File.readlines(file).each_with_index do |line, index| - return index + 1 if line.include?(path) + return index + 1 if line.include?(path.to_s) end end end