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

Commit

Permalink
add scrappy tests for basic tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerball committed Dec 19, 2018
1 parent 51ba162 commit b0c861d
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 3 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.5.1
18 changes: 18 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# NOTE: These are development-only dependencies
source "https://rubygems.org"

ruby '2.5.1'

# None of these can actually be used in a development copy of dev
# They are all for CI and tests
# `dev` uses no gems

gem 'rake'
gem 'byebug'

group :test do
gem 'session'
gem 'mocha', require: false
gem 'minitest', '>= 5.0.0', require: false
gem 'minitest-reporters', require: false
end
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
GEM
remote: https://rubygems.org/
specs:
ansi (1.5.0)
builder (3.2.3)
byebug (8.2.2)
metaclass (0.0.4)
minitest (5.11.3)
minitest-reporters (1.3.5)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
mocha (1.7.0)
metaclass (~> 0.0.1)
rake (12.1.0)
ruby-progressbar (1.10.0)
session (3.2.0)

PLATFORMS
ruby

DEPENDENCIES
byebug
minitest (>= 5.0.0)
minitest-reporters
mocha
rake
session

RUBY VERSION
ruby 2.3.7p456

BUNDLED WITH
1.16.1
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require_relative 'bin/support/load_shopify'
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs += %w(test)
t.test_files = FileList['test/**/*_test.rb']
t.verbose = false
t.warning = false
end

10 changes: 7 additions & 3 deletions lib/shopify-cli/commands/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ def call(args, _name)
end

def embedded_app
CLI::UI::Frame.open('Cloning embeddedapp...') do
CLI::UI::Frame.open('Cloning embedded app...') do
git_progress('clone', '--single-branch', '[email protected]:shopify/webgen-embeddedapp.git', @name)
end
api_key = CLI::UI.ask('What is your Shopify API Key')
api_secret = CLI::UI.ask('What is your Shopify API Secret')
File.write(File.join(@name, '.env'),
"SHOPIFY_API_KEY=#{api_key}\nSHOPIFY_API_SECRET_KEY=#{api_secret}")
write_env_file(api_key, api_secret)

CLI::UI::Frame.open('Installing dependencies...') do
yarn
Expand All @@ -41,6 +40,11 @@ def self.help
"Bootstrap an app.\nUsage: {{command:#{ShopifyCli::TOOL_NAME} create <apptype> <appname>}}"
end

def write_env_file(api_key, api_secret)
File.write(File.join(@name, '.env'),
"SHOPIFY_API_KEY=#{api_key}\nSHOPIFY_API_SECRET_KEY=#{api_secret}")
end

def git_progress(*git_command)
CLI::UI::Progress.progress do |bar|
success = CLI::Kit::System.system('git', *git_command, '--progress') do |_out, err|
Expand Down
22 changes: 22 additions & 0 deletions test/minitest_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Minitest
class Test
def capture_io(&block)
cap = CLI::UI::StdoutRouter::Capture.new(with_frame_inset: true, &block)
cap.run
[cap.stdout, cap.stderr]
end

def to_s # :nodoc:
if passed? && !skipped?
return location
end
failures.flat_map do |failure|
[
"#{failure.result_label}:",
"#{location}:",
failure.message.force_encoding(Encoding::UTF_8),
]
end.join("\n")
end
end
end
45 changes: 45 additions & 0 deletions test/task/create_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

module ShopifyCli
module Tasks
class CreateTest < MiniTest::Test
def setup
@command = ShopifyCli::Commands::Create.new
end

def test_prints_help_with_no_name_argument
io = capture_io do
@command.call([], nil)
end

assert_match(CLI::UI.fmt(ShopifyCli::Commands::Create.help), io.join)
end

def test_exists_with_not_implemented_choice
CLI::UI::Prompt.expects(:ask).returns(false)
io = capture_io do
@command.call(['test-app'], nil)
end

assert_match('not yet implemented', io.join)
end

def test_embedded_app_creation
CLI::UI::Prompt.expects(:ask).returns('embedded_app')
@command.expects(:git_progress).with(
'clone', '--single-branch', '[email protected]:shopify/webgen-embeddedapp.git', 'test-app'
)
CLI::UI.expects(:ask).twice.returns('apikey', 'apisecret')
@command.expects(:write_env_file)
@command.expects(:yarn)
io = capture_io do
@command.call(['test-app'], nil)
end
output = io.join

assert_match('Cloning embedded app...', output)
assert_match('Installing dependencies...', output)
end
end
end
end
21 changes: 21 additions & 0 deletions test/task/help_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'test_helper'

module ShopifyCli
module Tasks
class HelpTest < MiniTest::Test
def setup
@command = ShopifyCli::Commands::Help.new
end

def test_default_behavior_lists_tasks
io = capture_io do
@command.call([], nil)
end
output = io.join

assert_match('Available commands', output)
assert_match(/Usage: .*shopify/, output)
end
end
end
end
19 changes: 19 additions & 0 deletions test/task/task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

module ShopifyCli
module Tasks
class TaskTest < MiniTest::Test
def setup
@command = ShopifyCli::Commands::Help.new
end

def test_non_existant
io = capture_io do
@command.call(%w(foobar), nil)
end

assert_match(/Available commands/, io.join)
end
end
end
end
19 changes: 19 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
begin
addpath = lambda do |p|
path = File.expand_path("../../#{p}", __FILE__)
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
end
addpath.call("lib")
addpath.call("vendor/lib")
end

require 'rubygems'
require 'bundler/setup'
require 'shopify-cli'
require 'byebug'

require 'minitest/autorun'
require 'minitest/reporters'
require_relative 'minitest_ext'

require 'mocha/minitest'

0 comments on commit b0c861d

Please sign in to comment.