Skip to content

Commit

Permalink
Fix Theme Check to be resilient when it's running offline without bun…
Browse files Browse the repository at this point in the history
…dled resources (Shopify#734)
  • Loading branch information
karreiro authored Jun 7, 2023
1 parent 3dd5d15 commit e7bbb91
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/theme_check/shopify_liquid/source_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def deprecated_filters

def load_file(file_name)
read_json(local_path!(file_name))
rescue StandardError
# If files get manually deleted, fallback with an empty list.
[]
end

def local_path!(file_name)
Expand Down
9 changes: 8 additions & 1 deletion lib/theme_check/shopify_liquid/source_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module ShopifyLiquid
class SourceManager
REQUIRED_FILE_NAMES = [:filters, :objects, :tags, :latest].freeze

class DownloadResourceError < StandardError; end

class << self
def download_or_refresh_files(destination = default_destination)
if has_required_files?(destination)
Expand All @@ -24,6 +26,9 @@ def download(destination = default_destination)
REQUIRED_FILE_NAMES.each do |file_name|
download_file(local_path(file_name, destination), remote_path(file_name))
end
rescue DownloadResourceError
# If a request error occurs, ignore it. This ensures that Theme Check
# can rely on the sources included during the bundling phase.
end

def refresh(destination = default_destination)
Expand Down Expand Up @@ -73,8 +78,8 @@ def remote_path(file_name)
end

def download_file(local_path, remote_uri)
content = open_uri(remote_uri)
File.open(local_path, "wb") do |file|
content = open_uri(remote_uri)
file.write(content)
end
end
Expand Down Expand Up @@ -104,6 +109,8 @@ def open_uri(uri_str)
end

res.body
rescue StandardError
raise DownloadResourceError
end
end
end
Expand Down
13 changes: 12 additions & 1 deletion test/shopify_liquid/source_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,18 @@ def test_download_creates_directory

download_or_refresh_files

assert_test_documentation_up_to_date(tmp_dir)
ensure
FileUtils.remove_entry(@tmp_dir)
end

def test_download_when_a_request_error_happens
tmp_dir = Pathname.new("#{@tmp_dir}/new")

@source_manager_class.stubs(:open_uri).raises(SourceManager::DownloadResourceError)
@source_manager_class.stubs(:default_destination).returns(tmp_dir)

# Nothing is raised
download_or_refresh_files
ensure
FileUtils.remove_entry(@tmp_dir)
end
Expand Down

0 comments on commit e7bbb91

Please sign in to comment.