Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
consolidate beta_checks on SpecificationHandler::Default
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecy Correa committed May 28, 2021
1 parent 3bb3a62 commit ce0e404
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 48 deletions.
21 changes: 21 additions & 0 deletions lib/project_types/extension/features/argo_runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ArgoRuntime

property! :renderer, accepts: Models::NpmPackage
property! :cli, accepts: Models::NpmPackage
property :beta_access, accepts: Array, default: -> { [] }

def accepts_port?
case cli
Expand Down Expand Up @@ -49,6 +50,26 @@ def accepts_argo_version?
end
end

def accepts_shop?
return false unless beta_access.include?(:argo_admin_beta)
case cli
when admin?
cli >= ARGO_ADMIN_CLI_0_11_0
else
false
end
end

def accepts_api_key?
return false unless beta_access.include?(:argo_admin_beta)
case cli
when admin?
cli >= ARGO_ADMIN_CLI_0_11_0
else
false
end
end

private

def admin?
Expand Down
7 changes: 2 additions & 5 deletions lib/project_types/extension/features/argo_serve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ArgoServe
property! :context, accepts: ShopifyCli::Context
property! :port, accepts: Integer, default: 39351
property :tunnel_url, accepts: String, default: ""
property :beta_access, accepts: Array, default: -> { [] }

def call
validate_env!
Expand Down Expand Up @@ -55,16 +56,12 @@ def npm_serve_command
serve_options.npm_serve_command
end

def argo_admin_beta?
ShopifyCli::Shopifolk.check && ShopifyCli::Feature.enabled?(:argo_admin_beta)
end

def validate_env!
ExtensionProject.reload

return if required_fields.none?

return unless argo_admin_beta?
return unless beta_access.include?(:argo_admin_beta)

ShopifyCli::Tasks::EnsureEnv.call(context, required: required_fields)
ShopifyCli::Tasks::EnsureDevStore.call(context) if required_fields.include?(:shop)
Expand Down
9 changes: 3 additions & 6 deletions lib/project_types/extension/features/argo_serve_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ def npm_serve_command
NPM_SERVE_COMMAND + ["--"] + options
end

def argo_admin_beta?
ShopifyCli::Shopifolk.check && ShopifyCli::Feature.enabled?(:argo_admin_beta)
end

private

def options
project = ExtensionProject.current
api_key = project.env.api_key

@serve_options ||= [].tap do |options|
options << "--port=#{port}" if argo_runtime.accepts_port?
options << "--shop=#{project.env.shop}" if required_fields.include?(:shop) && argo_admin_beta?
options << "--apiKey=#{project.env.api_key}" if required_fields.include?(:api_key) && argo_admin_beta?
options << "--shop=#{project.env.shop}" if required_fields.include?(:shop) && argo_runtime.accepts_shop?
options << "--apiKey=#{api_key}" if required_fields.include?(:api_key) && argo_runtime.accepts_api_key?
options << "--argoVersion=#{renderer_package.version}" if argo_runtime.accepts_argo_version?
options << "--uuid=#{project.registration_uuid}" if argo_runtime.accepts_uuid?
options << "--publicUrl=#{public_url}" if argo_runtime.accepts_tunnel_url?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ def establish_tunnel?(context)
end

def serve(context:, port:, tunnel_url:)
Features::ArgoServe.new(specification_handler: self, argo_runtime: argo_runtime(context),
context: context, port: port, tunnel_url: tunnel_url).call
Features::ArgoServe.new(
specification_handler: self,
argo_runtime: argo_runtime(context),
context: context,
port: port,
tunnel_url: tunnel_url,
beta_access: beta_access
).call
end

def renderer_package(context)
Expand All @@ -62,10 +68,19 @@ def renderer_package(context)
def argo_runtime(context)
@argo_runtime ||= Features::ArgoRuntime.new(
renderer: renderer_package(context),
cli: cli_package(context)
cli: cli_package(context),
beta_access: beta_access
)
end

def beta_access
argo_admin_beta? ? [:argo_admin_beta] : []
end

def argo_admin_beta?
ShopifyCli::Shopifolk.check && ShopifyCli::Feature.enabled?(:argo_admin_beta)
end

def cli_package(context)
cli_package_name = specification.features.argo&.cli_package_name
return unless cli_package_name
Expand Down
38 changes: 38 additions & 0 deletions test/project_types/extension/features/argo_runtime_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ def test_accepts_argo_version
end
end

def test_accepts_api_key
runtimes = {
checkout_runtime_0_3_8 => does_not_support_feature,
checkout_runtime_0_4_0 => does_not_support_feature,
admin_runtime_0_11_0 => does_not_support_feature,
admin_runtime_0_11_0_with_beta_access => supports_feature,
admin_runtime_0_9_3 => does_not_support_feature,
admin_runtime_0_9_2 => does_not_support_feature,
}

runtimes.each do |runtime, accepts_argo_version|
assert_equal accepts_argo_version, runtime.accepts_api_key?
end
end

def test_accepts_shop
runtimes = {
checkout_runtime_0_3_8 => does_not_support_feature,
checkout_runtime_0_4_0 => does_not_support_feature,
admin_runtime_0_11_0 => does_not_support_feature,
admin_runtime_0_11_0_with_beta_access => supports_feature,
admin_runtime_0_9_3 => does_not_support_feature,
admin_runtime_0_9_2 => does_not_support_feature,
}

runtimes.each do |runtime, accepts_argo_version|
assert_equal accepts_argo_version, runtime.accepts_shop?
end
end

private

def checkout_runtime_0_3_8
Expand Down Expand Up @@ -93,6 +123,14 @@ def admin_runtime_0_11_0
)
end

def admin_runtime_0_11_0_with_beta_access
ArgoRuntime.new(
cli: Models::NpmPackage.new(name: "@shopify/argo-admin-cli", version: "0.11.0"),
renderer: Models::NpmPackage.new(name: "@shopify/argo-admin", version: "0.9.3"),
beta_access: [:argo_admin_beta]
)
end

def admin_runtime_0_9_2
ArgoRuntime.new(
cli: Models::NpmPackage.new(name: "@shopify/argo-admin-cli", version: "0.9.2"),
Expand Down
36 changes: 12 additions & 24 deletions test/project_types/extension/features/argo_serve_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def test_argo_serve_defers_to_js_system_when_shopifolk_check_is_false
argo_runtime = Features::ArgoRuntime.new(cli: cli, renderer: renderer)
specification_handler = ExtensionTestHelpers.test_specifications["TEST_EXTENSION"]

ShopifyCli::Shopifolk.stubs(:check).returns(false)

argo_serve = Features::ArgoServe.new(context: @context, argo_runtime: argo_runtime,
specification_handler: specification_handler)
argo_serve = Features::ArgoServe.new(
context: @context,
argo_runtime: argo_runtime,
specification_handler: specification_handler
)

Tasks::FindNpmPackages.expects(:exactly_one_of).returns(ShopifyCli::Result.success(renderer))
argo_serve.expects(:validate_env!).once
Expand All @@ -33,30 +34,17 @@ def test_argo_serve_defers_to_js_system_for_argo_admin_beta
cli = Models::NpmPackage.new(name: "@shopify/argo-admin-cli", version: "0.11.0")
renderer = Models::NpmPackage.new(name: "@shopify/argo-admin", version: "0.0.1")
argo_runtime = Features::ArgoRuntime.new(cli: cli, renderer: renderer)
specification_handler = ExtensionTestHelpers.test_specifications["TEST_EXTENSION"]

specification = Extension::Models::Specification.new(
{
identifier: "test",
features: {
argo: {
surface: "admin",
git_template: "https://github.com/Shopify/argo-admin.git",
renderer_package_name: "@shopify/argo-admin",
required_fields: [:shop, :api_key],
required_shop_beta_flags: [:argo_admin_beta],
},
},
}
)
specification_handler = Extension::Models::SpecificationHandlers::Default.new(specification)

ShopifyCli::Shopifolk.stubs(:check).returns(true)
ShopifyCli::Feature.stubs(:enabled?).with(:argo_admin_beta).returns(true)
ShopifyCli::Tasks::EnsureEnv.stubs(:call)
ShopifyCli::Tasks::EnsureDevStore.stubs(:call)

argo_serve = Features::ArgoServe.new(context: @context, argo_runtime: argo_runtime,
specification_handler: specification_handler)
argo_serve = Features::ArgoServe.new(
context: @context,
argo_runtime: argo_runtime,
specification_handler: specification_handler,
beta_access: [:argo_admin_beta]
)

Tasks::FindNpmPackages.expects(:exactly_one_of).returns(ShopifyCli::Result.success(renderer))

Expand Down
22 changes: 12 additions & 10 deletions test/project_types/extension/tasks/argo_serve_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ def test_serve_options_include_port_if_port_supported

def test_serve_options_include_api_key_when_required
required_fields = [:api_key]
argo_runtime = setup_argo_runtime(renderer_package: argo_admin, version: "0.1.2-doesnt-matter")

ShopifyCli::Shopifolk.expects(:check).returns(true)
ShopifyCli::Feature.expects(:enabled?).with(:argo_admin_beta).returns(true)
argo_runtime = setup_argo_runtime(
renderer_package: argo_admin,
version: "0.11.0",
beta_access: [:argo_admin_beta]
)

options = Features::ArgoServeOptions.new(argo_runtime: argo_runtime, context: @context,
renderer_package: argo_admin, required_fields: required_fields)
Expand All @@ -35,10 +36,11 @@ def test_serve_options_include_api_key_when_required

def test_serve_options_include_shop_when_required
required_fields = [:shop]
argo_runtime = setup_argo_runtime(renderer_package: argo_admin, version: "0.1.2-doesnt-matter")

ShopifyCli::Shopifolk.expects(:check).returns(true)
ShopifyCli::Feature.expects(:enabled?).with(:argo_admin_beta).returns(true)
argo_runtime = setup_argo_runtime(
renderer_package: argo_admin,
version: "0.11.0",
beta_access: [:argo_admin_beta]
)

options = Features::ArgoServeOptions.new(argo_runtime: argo_runtime, context: @context,
renderer_package: argo_admin, required_fields: required_fields)
Expand Down Expand Up @@ -92,10 +94,10 @@ def argo_renderer_package(package_name:, version: "0.1.2")
)
end

def setup_argo_runtime(renderer_package:, version:, cli_package_name: "@shopify/argo-admin-cli")
def setup_argo_runtime(renderer_package:, version:, cli_package_name: "@shopify/argo-admin-cli", beta_access: [])
cli = Models::NpmPackage.new(name: cli_package_name, version: version)

Features::ArgoRuntime.new(renderer: renderer_package, cli: cli)
Features::ArgoRuntime.new(renderer: renderer_package, cli: cli, beta_access: beta_access)
end
end
end
Expand Down

0 comments on commit ce0e404

Please sign in to comment.