Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Commit

Permalink
Prints error when selected profile is not configured (#13)
Browse files Browse the repository at this point in the history
Also:
*Print basic error message if running with invalid args
*Also display error message if running git `switch profile -l`
  • Loading branch information
randallreedjr committed Aug 29, 2018
1 parent 333da5f commit 37a5959
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 25 deletions.
37 changes: 29 additions & 8 deletions lib/git_switch/switcher.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# require 'version'
require 'yaml'
require_relative './version'

module GitSwitch
class Switcher
attr_reader :config, :profile, :valid, :global, :list
attr_reader :args, :config, :profile, :global, :list

def initialize(args)
raise ArgumentError unless args.is_a? Array
@args = args
@config = load_config
@global = check_global(args)
@profile = get_profile(args)
@valid = valid_args?(args)
@list = check_list(args)
end

Expand All @@ -37,20 +36,42 @@ def get_profile(args)
args.detect {|a| !a.start_with? '-'}
end

def valid_args?(args)
no_flags?(args) || one_flag?(args)
def valid_args?
if check_list(args) && args.count > 1
puts "Invalid args"
return false
elsif no_flags?(args) || one_flag?(args)
return true
else
puts "Invalid args"
return false
end
end

def valid_profile?
if config.has_key?(profile)
return true
else
puts "Profile '#{profile}' not found!"
return false
end
end

def no_flags?(args)
args.length == 1 && args.count {|a| a.start_with? '-'} == 0
args.length == 1 && flag_count(args) == 0
end

def one_flag?(args)
args.length == 2 && args.count {|a| a.start_with?('-')} == 1
args.length == 2 && flag_count(args) == 1
end

def flag_count(args)
args.count {|a| a.start_with? '-'}
end

def set!
return unless valid
return unless valid_args? && valid_profile?

flag = global ? '--global' : ''

puts "\nGit Config:"
Expand Down
111 changes: 94 additions & 17 deletions spec/lib/git_switch/switcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

RSpec.describe GitSwitch::Switcher do
describe '#global' do
before do
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.gitswitch'))
end
context 'when -g is passed as first argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['-g','foo']).global).to be true
Expand Down Expand Up @@ -37,9 +34,6 @@
end

describe '#profile' do
before do
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.gitswitch'))
end
context 'when profile is only argument' do
it 'sets correctly' do
expect(GitSwitch::Switcher.new(['foo']).profile).to eq 'foo'
Expand All @@ -51,6 +45,7 @@
expect(GitSwitch::Switcher.new(['foo1','-g']).profile).to eq 'foo1'
end
end

context 'when profile is second argument' do
it 'sets correctly' do
expect(GitSwitch::Switcher.new(['--global','foo2']).profile).to eq 'foo2'
Expand All @@ -59,9 +54,6 @@
end

describe '#list' do
before do
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.gitswitch'))
end
context 'when -l is passed as only argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['-l']).list).to be true
Expand Down Expand Up @@ -106,9 +98,6 @@
end

describe '#run' do
before do
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.gitswitch'))
end
context 'in list mode' do
let(:switcher) { GitSwitch::Switcher.new(['-l']) }
it 'calls print_list' do
Expand All @@ -119,19 +108,24 @@

context 'in set mode' do
let(:switcher) { GitSwitch::Switcher.new(['foo']) }
it 'calls print_list' do
it 'calls set!' do
expect(switcher).to receive(:set!)
switcher.run
end
end

context 'when profile is missing' do
let(:switcher) { GitSwitch::Switcher.new(['foo']) }
let(:expected_output) { "Profile 'foo' not found!\n" }
it 'prints error message' do
expect{switcher.run}.to output(expected_output).to_stdout
end
end
end

describe '#print_list' do
let(:switcher) { GitSwitch::Switcher.new(['-l']) }
context 'when profiles have been configured' do
before do
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.gitswitch'))
end
let(:expected_output) { "personal\nwork\n" }
it 'outputs available profiles' do
expect{switcher.print_list}.to output(expected_output).to_stdout
Expand All @@ -141,13 +135,96 @@
context 'when no profiles have been configured' do
let(:expected_output) { '' }
before do
# unstub
allow(File).to receive(:expand_path).and_call_original
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.empty'))
end

it 'outputs an empty string' do
# allow(switcher).to receive(:config).and_return({})
expect{switcher.print_list}.to output(expected_output).to_stdout
end
end
end

describe 'valid_args?' do
let(:expected_error) { "Invalid args\n" }
context 'when run with a single profile' do
let(:switcher) { GitSwitch::Switcher.new(['personal']) }
it 'returns true' do
expect(switcher.valid_args?).to be true
end
end

context 'when run with a profile and a flag' do
let(:switcher) { GitSwitch::Switcher.new(['personal','-g']) }
it 'returns true' do
expect(switcher.valid_args?).to be true
end
end

context 'when run with a profile and non-profile flag' do
let(:switcher) { GitSwitch::Switcher.new(['personal','-l']) }
it 'returns false' do
expect(switcher.valid_args?).to be false
end

it 'prints error message' do
expect{switcher.valid_args?}.to output(expected_error).to_stdout
end
end

context 'when run with multiple flags' do
let(:switcher) { GitSwitch::Switcher.new(['personal','-g','-l']) }
it 'returns false' do
expect(switcher.valid_args?).to be false
end

it 'prints error message' do
expect{switcher.valid_args?}.to output(expected_error).to_stdout
end
end

context 'when run with multiple profiles' do
let(:switcher) { GitSwitch::Switcher.new(['personal','work']) }
it 'returns false' do
expect(switcher.valid_args?).to be false
end

it 'prints error message' do
expect{switcher.valid_args?}.to output(expected_error).to_stdout
end
end

context 'when run with no args' do
let(:switcher) { GitSwitch::Switcher.new([]) }
it 'returns false' do
expect(switcher.valid_args?).to be false
end

it 'prints error message' do
expect{switcher.valid_args?}.to output(expected_error).to_stdout
end
end
end

describe 'valid_profile?' do
context 'when profile is configured' do
let(:switcher) { GitSwitch::Switcher.new(['personal']) }
it 'returns true' do
expect(switcher.valid_profile?).to be true
end
end

context 'when profile is not configured' do
let(:switcher) { GitSwitch::Switcher.new(['foo']) }
let(:expected_output) { "Profile 'foo' not found!\n" }
it 'returns false' do
expect(switcher.valid_profile?).to be false
end

it 'prints error message' do
expect{switcher.valid_profile?}.to output(expected_output).to_stdout
end
end
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.before(:each) do
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.gitswitch'))
end
end

0 comments on commit 37a5959

Please sign in to comment.