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

Commit

Permalink
"Allow passing -l or --list to print profiles" (#12)
Browse files Browse the repository at this point in the history
[fixes #7]
  • Loading branch information
randallreedjr committed Aug 21, 2018
1 parent c8c0f46 commit 333da5f
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bin/git-switch
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env ruby
require_relative '../lib/git_switch'

GitSwitch::Switcher.new(ARGV).set!
GitSwitch::Switcher.new(ARGV).run
23 changes: 20 additions & 3 deletions lib/git_switch/switcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,33 @@

module GitSwitch
class Switcher
attr_reader :config, :profile, :valid, :global
# Your code goes here...
attr_reader :config, :profile, :valid, :global, :list

def initialize(args)
raise ArgumentError unless args.is_a? Array
@config = YAML.load_file(File.expand_path('~/.gitswitch'))
@config = load_config
@global = check_global(args)
@profile = get_profile(args)
@valid = valid_args?(args)
@list = check_list(args)
end

def run
list ? print_list : set!
end

def load_config
YAML.load_file(File.expand_path('~/.gitswitch')) || {}
end

def check_global(args)
(args.include? '-g') || (args.include? '--global')
end

def check_list(args)
(args.include? '-l') || (args.include? '--list')
end

def get_profile(args)
# TODO: RCR - Verify profile exists in config file
# TODO: RCR - Handle missing or empty config file
Expand Down Expand Up @@ -52,6 +65,10 @@ def set!
puts `ssh-add -l`
end

def print_list
puts config.keys
end

private

def name
Expand Down
Empty file added spec/fixtures/.empty
Empty file.
108 changes: 102 additions & 6 deletions spec/lib/git_switch/switcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
require 'spec_helper'

RSpec.describe GitSwitch::Switcher do
before do
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.gitswitch'))
end

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
end
end
context 'when -g is passed as first argument' do

context 'when -g is passed as second argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['foo','-g']).global).to be true
end
Expand All @@ -23,7 +23,7 @@
end
end

context 'when --global is passsed as first argument' do
context 'when --global is passsed as second argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['foo','--global']).global).to be true
end
Expand All @@ -37,6 +37,9 @@
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 @@ -54,4 +57,97 @@
end
end
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
end
end

context 'when -l is passed as first argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['-l','foo']).list).to be true
end
end

context 'when -l is passed as second argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['foo','-l']).list).to be true
end
end

context 'when --list is passed as only argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['-l']).list).to be true
end
end

context 'when --list is passsed as first argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['--list']).list).to be true
end
end

context 'when --list is passsed as second argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['foo','--list']).list).to be true
end
end

context 'when no flag is passed' do
it 'sets to false' do
expect(GitSwitch::Switcher.new(['foo']).list).to be false
end
end
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
expect(switcher).to receive(:print_list)
switcher.run
end
end

context 'in set mode' do
let(:switcher) { GitSwitch::Switcher.new(['foo']) }
it 'calls print_list' do
expect(switcher).to receive(:set!)
switcher.run
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
end
end

context 'when no profiles have been configured' do
let(:expected_output) { '' }
before do
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
end

0 comments on commit 333da5f

Please sign in to comment.