Skip to content

Commit

Permalink
Move DATA_PATH access to method
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpgross committed Jan 24, 2025
1 parent 30a1384 commit 30cd4f0
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 28 deletions.
5 changes: 5 additions & 0 deletions dev/lib/product_taxonomy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

module ProductTaxonomy
DATA_PATH = File.expand_path("../../data", __dir__)
private_constant :DATA_PATH

class << self
def data_path = DATA_PATH
end
end

require_relative "product_taxonomy/cli"
Expand Down
6 changes: 3 additions & 3 deletions dev/lib/product_taxonomy/commands/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def execute(...)
def load_taxonomy
ProductTaxonomy::Value.load_from_source(YAML.load_file(File.expand_path(
"values.yml",
ProductTaxonomy::DATA_PATH,
ProductTaxonomy.data_path,
)))
ProductTaxonomy::Attribute.load_from_source(YAML.load_file(File.expand_path(
"attributes.yml",
ProductTaxonomy::DATA_PATH,
ProductTaxonomy.data_path,
)))

glob = Dir.glob(File.expand_path("categories/*.yml", ProductTaxonomy::DATA_PATH))
glob = Dir.glob(File.expand_path("categories/*.yml", ProductTaxonomy.data_path))
categories_source_data = glob.each_with_object([]) do |file, array|
array.concat(YAML.safe_load_file(file))
end
Expand Down
2 changes: 1 addition & 1 deletion dev/lib/product_taxonomy/commands/generate_dist_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(options)

@version = options[:version] || File.read(File.expand_path("../../../../VERSION", __dir__)).strip
@locales = if options[:locales] == ["all"]
glob = Dir.glob(File.expand_path("localizations/categories/*.yml", ProductTaxonomy::DATA_PATH))
glob = Dir.glob(File.expand_path("localizations/categories/*.yml", ProductTaxonomy.data_path))
glob.map { File.basename(_1, ".yml") }
else
options[:locales]
Expand Down
2 changes: 1 addition & 1 deletion dev/lib/product_taxonomy/models/integration_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module ProductTaxonomy
# - The full names of categories in the integration's taxonomy.
# - The mapping rules for converting between the integration's taxonomy and Shopify's taxonomy.
class IntegrationVersion
INTEGRATIONS_PATH = File.expand_path("integrations", ProductTaxonomy::DATA_PATH)
INTEGRATIONS_PATH = File.expand_path("integrations", ProductTaxonomy.data_path)

class << self
# Generate all distribution files for all integration versions.
Expand Down
2 changes: 1 addition & 1 deletion dev/lib/product_taxonomy/models/mixins/localized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def localizations
return @localizations if @localizations

humanized_name = name.split("::").last.humanize.downcase.pluralize
localization_path = File.join(DATA_PATH, "localizations", humanized_name, "*.yml")
localization_path = File.join(ProductTaxonomy.data_path, "localizations", humanized_name, "*.yml")
@localizations = Dir.glob(localization_path).each_with_object({}) do |file, localizations|
locale = File.basename(file, ".yml")
localizations[locale] = YAML.safe_load_file(file).dig(locale, humanized_name)
Expand Down
10 changes: 5 additions & 5 deletions dev/test/integration/all_data_files_import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ class AllDataFilesImportTest < ActiveSupport::TestCase
i_suck_and_my_tests_are_order_dependent!

def before_all
@raw_values_data = YAML.safe_load_file(File.expand_path("values.yml", ProductTaxonomy::DATA_PATH))
@raw_attributes_data = YAML.safe_load_file(File.expand_path("attributes.yml", ProductTaxonomy::DATA_PATH))
@raw_verticals_data = Dir.glob(File.expand_path("categories/*.yml", ProductTaxonomy::DATA_PATH))
@raw_values_data = YAML.safe_load_file(File.expand_path("values.yml", ProductTaxonomy.data_path))
@raw_attributes_data = YAML.safe_load_file(File.expand_path("attributes.yml", ProductTaxonomy.data_path))
@raw_verticals_data = Dir.glob(File.expand_path("categories/*.yml", ProductTaxonomy.data_path))
.map { |file| YAML.safe_load_file(file) }
@raw_integrations_data = YAML.safe_load_file(File.expand_path(
"integrations/integrations.yml",
ProductTaxonomy::DATA_PATH,
ProductTaxonomy.data_path,
))
mapping_rule_files = Dir.glob(File.expand_path(
"integrations/*/*/mappings/*_shopify.yml",
ProductTaxonomy::DATA_PATH,
ProductTaxonomy.data_path,
))
@raw_mapping_rules_data = mapping_rule_files.map { { content: YAML.safe_load_file(_1), file_name: _1 } }

Expand Down
19 changes: 14 additions & 5 deletions dev/test/integration/mapping_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def setup
test "category IDs in mappings are valid" do
invalid_categories = []
raw_mappings_list = []
mapping_rule_files = Dir.glob(File.expand_path("integrations/*/*/mappings/*_shopify.yml", DATA_PATH))
mapping_rule_files = Dir.glob(File.expand_path(
"integrations/*/*/mappings/*_shopify.yml",
ProductTaxonomy.data_path,
))
mapping_rule_files.each do |file|
raw_mappings_list << YAML.safe_load_file(file)
end
Expand Down Expand Up @@ -149,7 +152,10 @@ def setup
shopify_taxonomy_version_from_file = "shopify/" + current_shopify_taxonomy_version
allowed_shopify_legacy_source_taxonomies = ["shopify/2022-02", "shopify/2024-07", "shopify/2024-10"]
all_shopify_taxonomies = allowed_shopify_legacy_source_taxonomies + [shopify_taxonomy_version_from_file]
mapping_rule_files = Dir.glob(File.expand_path("integrations/*/*/mappings/*_shopify.yml", DATA_PATH))
mapping_rule_files = Dir.glob(File.expand_path(
"integrations/*/*/mappings/*_shopify.yml",
ProductTaxonomy.data_path,
))
files_include_inconsistent_shopify_taxonomy_version = []
mapping_rule_files.each do |file|
raw_mappings = YAML.safe_load_file(file)
Expand Down Expand Up @@ -213,7 +219,10 @@ def category_ids_from_taxonomy(input_or_output_taxonomy)
shopify_category_ids
else
channel_category_ids = Set.new
file_path = File.expand_path("integrations/#{input_or_output_taxonomy}/full_names.yml", DATA_PATH)
file_path = File.expand_path(
"integrations/#{input_or_output_taxonomy}/full_names.yml",
ProductTaxonomy.data_path,
)
channel_taxonomy = YAML.safe_load_file(file_path)
channel_taxonomy.each do |entry|
channel_category_ids.add(entry["id"].to_s)
Expand All @@ -239,15 +248,15 @@ def unmapped_category_ids_for_mappings(mappings_input_taxonomy, mappings_output_
"#{integration_version}/mappings/to_shopify.yml"
end

file_path = File.expand_path("integrations/#{integration_mapping_path}", DATA_PATH)
file_path = File.expand_path("integrations/#{integration_mapping_path}", ProductTaxonomy.data_path)
return unless File.exist?(file_path)

mappings = YAML.safe_load_file(file_path)
mappings["unmapped_product_category_ids"] if mappings.key?("unmapped_product_category_ids")
end

def current_shopify_taxonomy_version
@current_shopify_taxonomy_version ||= File.read(File.expand_path("../VERSION", DATA_PATH)).strip
@current_shopify_taxonomy_version ||= File.read(File.expand_path("../VERSION", ProductTaxonomy.data_path)).strip
end
end
end
4 changes: 2 additions & 2 deletions dev/test/models/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,13 @@ def stub_localizations
description: "Descripción en español (extended)"
YAML
Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "attributes", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "attributes", "*.yml"))
.returns(["fake/path/fr.yml", "fake/path/es.yml"])
YAML.stubs(:safe_load_file).with("fake/path/fr.yml").returns(YAML.safe_load(fr_yaml))
YAML.stubs(:safe_load_file).with("fake/path/es.yml").returns(YAML.safe_load(es_yaml))

Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "values", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "values", "*.yml"))
.returns([])
end

Expand Down
6 changes: 3 additions & 3 deletions dev/test/models/category_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,16 @@ def stub_localizations
name: "Grandchild en español"
YAML
Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "categories", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "categories", "*.yml"))
.returns(["fake/path/fr.yml", "fake/path/es.yml"])
YAML.stubs(:safe_load_file).with("fake/path/fr.yml").returns(YAML.safe_load(fr_yaml))
YAML.stubs(:safe_load_file).with("fake/path/es.yml").returns(YAML.safe_load(es_yaml))

Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "attributes", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "attributes", "*.yml"))
.returns([])
Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "values", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "values", "*.yml"))
.returns([])
end
end
Expand Down
2 changes: 1 addition & 1 deletion dev/test/models/extended_attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ExtendedAttributeTest < TestCase
description: "Descripción en español"
YAML
Dir.expects(:glob)
.with(File.join(DATA_PATH, "localizations", "attributes", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "attributes", "*.yml"))
.returns(["fake/path/fr.yml", "fake/path/es.yml"])
YAML.expects(:safe_load_file).with("fake/path/fr.yml").returns(YAML.safe_load(fr_yaml))
YAML.expects(:safe_load_file).with("fake/path/es.yml").returns(YAML.safe_load(es_yaml))
Expand Down
12 changes: 6 additions & 6 deletions dev/test/models/value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@ def stub_localizations
name: "Nombre en español"
YAML
Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "values", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "values", "*.yml"))
.returns(["fake/path/fr.yml", "fake/path/es.yml"])
YAML.stubs(:safe_load_file).with("fake/path/fr.yml").returns(YAML.safe_load(fr_yaml))
YAML.stubs(:safe_load_file).with("fake/path/es.yml").returns(YAML.safe_load(es_yaml))

Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "attributes", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "attributes", "*.yml"))
.returns([])
end

Expand All @@ -418,12 +418,12 @@ def stub_localizations_for_sorting
name: "Autre"
YAML
Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "values", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "values", "*.yml"))
.returns(["fake/path/fr.yml"])
YAML.stubs(:safe_load_file).with("fake/path/fr.yml").returns(YAML.safe_load(fr_yaml))

Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "attributes", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "attributes", "*.yml"))
.returns([])
end

Expand All @@ -439,7 +439,7 @@ def stub_color_values
name: "Vert"
YAML
Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "values", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "values", "*.yml"))
.returns(["fake/path/fr.yml"])
YAML.stubs(:safe_load_file).with("fake/path/fr.yml").returns(YAML.safe_load(fr_yaml))

Expand All @@ -462,7 +462,7 @@ def stub_pattern_values
name: "Autre"
YAML
Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "values", "*.yml"))
.with(File.join(ProductTaxonomy.data_path, "localizations", "values", "*.yml"))
.returns(["fake/path/fr.yml"])
YAML.stubs(:safe_load_file).with("fake/path/fr.yml").returns(YAML.safe_load(fr_yaml))

Expand Down

0 comments on commit 30cd4f0

Please sign in to comment.