diff --git a/.gitignore b/.gitignore index 3407cf7..7e435f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,11 @@ +## MacOS +.DS_Store + +## Gems + *.gem Gemfile.lock -gems +vendor .bundle ## Documentation cache and generated files: diff --git a/Gemfile b/Gemfile index 2d23001..1a3a177 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/lib/fastlane/plugin/fetch_version_code/actions/fetch_version_code_action.rb b/lib/fastlane/plugin/fetch_version_code/actions/fetch_version_code_action.rb index 6ca362b..77b6a97 100644 --- a/lib/fastlane/plugin/fetch_version_code/actions/fetch_version_code_action.rb +++ b/lib/fastlane/plugin/fetch_version_code/actions/fetch_version_code_action.rb @@ -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 diff --git a/lib/fastlane/plugin/fetch_version_code/helper/fetch_version_code_helper.rb b/lib/fastlane/plugin/fetch_version_code/helper/fetch_version_code_helper.rb index 76203ab..9a98068 100644 --- a/lib/fastlane/plugin/fetch_version_code/helper/fetch_version_code_helper.rb +++ b/lib/fastlane/plugin/fetch_version_code/helper/fetch_version_code_helper.rb @@ -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 diff --git a/lib/fastlane/plugin/fetch_version_code/version.rb b/lib/fastlane/plugin/fetch_version_code/version.rb index 7ea17e9..4833454 100644 --- a/lib/fastlane/plugin/fetch_version_code/version.rb +++ b/lib/fastlane/plugin/fetch_version_code/version.rb @@ -1,5 +1,5 @@ module Fastlane module FetchVersionCode - VERSION = "0.1.1" + VERSION = "0.1.2" end end diff --git a/spec/fetch_version_code_action_spec.rb b/spec/fetch_version_code_action_spec.rb index b235365..33f7727 100644 --- a/spec/fetch_version_code_action_spec.rb +++ b/spec/fetch_version_code_action_spec.rb @@ -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 diff --git a/spec/fetch_version_code_helper_spec.rb b/spec/fetch_version_code_helper_spec.rb new file mode 100644 index 0000000..00ff413 --- /dev/null +++ b/spec/fetch_version_code_helper_spec.rb @@ -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 diff --git a/spec/fetch_version_code_version_spec.rb b/spec/fetch_version_code_version_spec.rb new file mode 100644 index 0000000..bbccb09 --- /dev/null +++ b/spec/fetch_version_code_version_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 37cb149..95d3fd1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 @@ -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)