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

Commit

Permalink
Fix tests and coverage 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentCATILLON committed Mar 16, 2019
1 parent 6623ae8 commit 3a229b5
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 35 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## MacOS
.DS_Store

## Gems

*.gem
Gemfile.lock
gems
vendor
.bundle

## Documentation cache and generated files:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
eval_gemfile(plugins_path) if File.exist?(plugins_path)

gem 'http'
gem 'webmock'
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
require 'fastlane/action'
require_relative '../helper/fetch_version_code_helper'
require 'http'

module Fastlane
UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")

module Actions
class FetchVersionCodeAction < Action
def self.run(params)
UI.important "About to fetch a version code for: #{params[:platform]}"
url = "https://#{params[:version_api_host]}/build/api/v1/versions/#{params[:platform]}/increment"
res = HTTP.headers(:api_secret => params[:secret]).post(url)
if res.status != 200
UI.error "Some error occureds [status:#{res.status}]: #{res.body}"
UI.crash!(res.body)
end
version_code = res.body.to_s

UI.success "Got version Code #{version_code}"
UI.message("About to fetch a version code for: #{params[:platform]}")

version_code = Helper::FetchVersionCodeHelper.fetch_version_code(params)
UI.success("Got version code: #{version_code}")

return version_code
end

def self.description
"Fetch (and increment) version code for given platform"
'Fetch (and increment) version code for given platform'
end

def self.authors
["AdrieanKhisbe"]
end

def self.output
[]
['AdrieanKhisbe', 'VincentCATILLON']
end

def self.return_value
"Version code of given platform"
'Version code of given platform'
end

def self.available_options
[
FastlaneCore::ConfigItem.new(
key: :version_api_host,
env_name: "VERSION_API_HOST",
description: "API Token for FetchVersionCodeAction",
env_name: 'VERSION_API_HOST',
description: 'API Token for FetchVersionCodeAction',
is_string: true,
default_value: "api.coorpacademy.com"
default_value: 'api.coorpacademy.com'
),
FastlaneCore::ConfigItem.new(key: :platform, is_string: false, description: "The platform to fetch version for"),
FastlaneCore::ConfigItem.new(key: :secret, env_name: "VERSION_API_SECRET"),
FastlaneCore::ConfigItem.new(key: :platform, is_string: false, description: 'The platform to fetch version for'),
FastlaneCore::ConfigItem.new(key: :secret, env_name: 'VERSION_API_SECRET')
]
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
require 'fastlane_core/ui/ui'
require 'http'

module Fastlane
UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")

module Helper
class FetchVersionCodeHelper
# class methods that you define here become available in your action
# as `Helper::FetchVersionCodeHelper.your_method`
#
def self.show_message
UI.message("Hello from the fetch_version_code plugin helper!")
def self.fetch_version_code(params)
url = self.get_api_url(params)
UI.message("Calling API: #{url}")

res = HTTP.headers(api_secret: params[:secret]).post(url)

if res.status != 200
UI.error("Some error occureds [status:#{res.status}]: #{res.body}")
UI.crash!(res.body)
end

return res.body.to_s
end

def self.get_api_url(params)
return "https://#{params[:version_api_host]}/build/api/v1/versions/#{params[:platform]}/increment"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fastlane/plugin/fetch_version_code/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Fastlane
module FetchVersionCode
VERSION = "0.1.1"
VERSION = "0.1.2"
end
end
60 changes: 57 additions & 3 deletions spec/fetch_version_code_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
describe Fastlane::Actions::FetchVersionCodeAction do
describe '#run' do
it 'prints a message' do
expect(Fastlane::UI).to receive(:message).with("The fetch_version_code plugin is working!")
it 'should run action' do
allow(Fastlane::Helper::FetchVersionCodeHelper).to receive(:fetch_version_code).and_return('42')

Fastlane::Actions::FetchVersionCodeAction.run(nil)
expect(Fastlane::UI).to receive(:message).with('About to fetch a version code for: ios')
expect(Fastlane::UI).to receive(:success).with('Got version code: 42')
result = Fastlane::Actions::FetchVersionCodeAction.run(version_api_host: 'domain.tld', platform: 'ios', secret: 'foobarbaz')
expect(result).to eq('42')
end
end

describe '#description' do
it 'should return description' do
result = Fastlane::Actions::FetchVersionCodeAction.description
expect(result).to eq('Fetch (and increment) version code for given platform')
end
end

describe '#authors' do
it 'should return authors' do
result = Fastlane::Actions::FetchVersionCodeAction.authors
expect(result).to eq(['AdrieanKhisbe', 'VincentCATILLON'])
end
end

describe '#return_value' do
it 'should return return_value' do
result = Fastlane::Actions::FetchVersionCodeAction.return_value
expect(result).to eq('Version code of given platform')
end
end

describe '#available_options' do
it 'should return available_options' do
result = Fastlane::Actions::FetchVersionCodeAction.available_options
expected = [
FastlaneCore::ConfigItem.new(
key: :version_api_host,
env_name: 'VERSION_API_HOST',
description: 'API Token for FetchVersionCodeAction',
is_string: true,
default_value: 'api.coorpacademy.com'
),
FastlaneCore::ConfigItem.new(key: :platform, is_string: false, description: 'The platform to fetch version for'),
FastlaneCore::ConfigItem.new(key: :secret, env_name: 'VERSION_API_SECRET')
]
expect(result.to_json).to eq(expected.to_json)
end
end

describe '#is_supported' do
it 'should return false' do
result = Fastlane::Actions::FetchVersionCodeAction.is_supported?(:windows)
expect(result).to be false
end

it 'should return true' do
result = Fastlane::Actions::FetchVersionCodeAction.is_supported?(:ios)
expect(result).to be true
end
end
end
32 changes: 32 additions & 0 deletions spec/fetch_version_code_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
describe Fastlane::Helper::FetchVersionCodeHelper do
describe '#get_api_url' do
it 'should create URL' do
result = Fastlane::Helper::FetchVersionCodeHelper.get_api_url(version_api_host: 'domain.tld', platform: 'ios')
expect(result).to eq('https://domain.tld/build/api/v1/versions/ios/increment')
end
end

describe '#fetch_version_code' do
it 'should raise error' do
allow(Fastlane::Helper::FetchVersionCodeHelper).to receive(:get_api_url).and_return('https://domain.tld')
stub_request(:post, 'https://domain.tld')
.with(headers: { api_secret: 'foobarbaz' })
.to_return(status: 404, body: '42')

expect(Fastlane::UI).to receive(:message).with('Calling API: https://domain.tld')
expect(Fastlane::UI).to receive(:error).with('Some error occureds [status:404 Not Found]: 42')
expect { Fastlane::Helper::FetchVersionCodeHelper.fetch_version_code(version_api_host: 'domain.tld', platform: 'ios', secret: 'foobarbaz') }.to raise_error('42')
end

it 'should fetch version' do
allow(Fastlane::Helper::FetchVersionCodeHelper).to receive(:get_api_url).and_return('https://domain.tld')
stub_request(:post, 'https://domain.tld')
.with(headers: { api_secret: 'foobarbaz' })
.to_return(status: 200, body: '42')

expect(Fastlane::UI).to receive(:message).with('Calling API: https://domain.tld')
result = Fastlane::Helper::FetchVersionCodeHelper.fetch_version_code(version_api_host: 'domain.tld', platform: 'ios', secret: 'foobarbaz')
expect(result).to eq('42')
end
end
end
7 changes: 7 additions & 0 deletions spec/fetch_version_code_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe Fastlane::FetchVersionCode do
describe '#VERSION' do
it 'should returns version' do
expect(Fastlane::FetchVersionCode::VERSION).to eq('0.1.2')
end
end
end
10 changes: 8 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

require 'simplecov'

# SimpleCov.minimum_coverage 95
SimpleCov.start
SimpleCov.minimum_coverage(100)
SimpleCov.start do
add_filter('vendor')
end
SimpleCov.coverage_dir('coverage')

# This module is only used to check the environment is currently a testing env
module SpecHelper
Expand All @@ -13,3 +16,6 @@ module SpecHelper
require 'fastlane/plugin/fetch_version_code' # import the actual plugin

Fastlane.load_actions # load other actions (in case your plugin calls other actions or shared values)

require 'webmock/rspec'
include(WebMock)

0 comments on commit 3a229b5

Please sign in to comment.