Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit e7bbb91

Browse files
authored
Fix Theme Check to be resilient when it's running offline without bundled resources (#734)
1 parent 3dd5d15 commit e7bbb91

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/theme_check/shopify_liquid/source_index.rb

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def deprecated_filters
5656

5757
def load_file(file_name)
5858
read_json(local_path!(file_name))
59+
rescue StandardError
60+
# If files get manually deleted, fallback with an empty list.
61+
[]
5962
end
6063

6164
def local_path!(file_name)

lib/theme_check/shopify_liquid/source_manager.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module ShopifyLiquid
99
class SourceManager
1010
REQUIRED_FILE_NAMES = [:filters, :objects, :tags, :latest].freeze
1111

12+
class DownloadResourceError < StandardError; end
13+
1214
class << self
1315
def download_or_refresh_files(destination = default_destination)
1416
if has_required_files?(destination)
@@ -24,6 +26,9 @@ def download(destination = default_destination)
2426
REQUIRED_FILE_NAMES.each do |file_name|
2527
download_file(local_path(file_name, destination), remote_path(file_name))
2628
end
29+
rescue DownloadResourceError
30+
# If a request error occurs, ignore it. This ensures that Theme Check
31+
# can rely on the sources included during the bundling phase.
2732
end
2833

2934
def refresh(destination = default_destination)
@@ -73,8 +78,8 @@ def remote_path(file_name)
7378
end
7479

7580
def download_file(local_path, remote_uri)
81+
content = open_uri(remote_uri)
7682
File.open(local_path, "wb") do |file|
77-
content = open_uri(remote_uri)
7883
file.write(content)
7984
end
8085
end
@@ -104,6 +109,8 @@ def open_uri(uri_str)
104109
end
105110

106111
res.body
112+
rescue StandardError
113+
raise DownloadResourceError
107114
end
108115
end
109116
end

test/shopify_liquid/source_manager_test.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ def test_download_creates_directory
4242

4343
download_or_refresh_files
4444

45-
assert_test_documentation_up_to_date(tmp_dir)
45+
ensure
46+
FileUtils.remove_entry(@tmp_dir)
47+
end
48+
49+
def test_download_when_a_request_error_happens
50+
tmp_dir = Pathname.new("#{@tmp_dir}/new")
51+
52+
@source_manager_class.stubs(:open_uri).raises(SourceManager::DownloadResourceError)
53+
@source_manager_class.stubs(:default_destination).returns(tmp_dir)
54+
55+
# Nothing is raised
56+
download_or_refresh_files
4657
ensure
4758
FileUtils.remove_entry(@tmp_dir)
4859
end

0 commit comments

Comments
 (0)