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

Commit

Permalink
Merge pull request #54 from randallreedjr/feature/version-flag
Browse files Browse the repository at this point in the history
Add version flag option (-v or --version)
  • Loading branch information
randallreedjr committed Jan 22, 2019
2 parents a88c293 + d37e97a commit 91ab5d2
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 15 deletions.
30 changes: 23 additions & 7 deletions lib/git_switch/options.rb
Expand Up @@ -8,14 +8,18 @@ def initialize(args)
def flags
@flags ||= args.select do |arg|
arg.match(/\A\-[cglv]{1}\z/) ||
arg.match(/\A\-\-(config|global|list|verbose){1}\z/)
arg.match(/\A\-\-(config|global|list|verbose|version){1}\z/)
end
end

def valid_args?
if config_flag? && args.count == 1
if config?
return true
elsif list_flag? && args.count == 1
elsif list?
return true
elsif version?
return true
elsif verbose?
return true
elsif no_flags?
return true
Expand All @@ -41,22 +45,34 @@ def list?
list_flag? && args.count == 1
end

def version?
version_flag? && args.count == 1
end

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

def verbose?
(flags.include? '-v') || (flags.include? '--verbose')
verbose_flag? && args.count > 1
end

private

def config_flag?
(flags.include? '-c') || (flags.include? '--config')
end

def list_flag?
(flags.include? '-l') || (flags.include? '--list')
end

def config_flag?
(flags.include? '-c') || (flags.include? '--config')
def version_flag?
((flags.include? '-v') && args.count == 1) || (flags.include? '--version')
end

def verbose_flag?
((flags.include? '-v') && args.count > 1) || (flags.include? '--verbose')
end

def no_flags?
Expand All @@ -68,7 +84,7 @@ def one_flag?
end

def flag_only?
list_flag? || config_flag?
list_flag? || config_flag? || version_flag?
end

def flag_count(args)
Expand Down
13 changes: 11 additions & 2 deletions lib/git_switch/switcher.rb
Expand Up @@ -4,7 +4,7 @@
module GitSwitch
class Switcher
attr_reader :config, :options
delegate :usage?, :config?, :list?, :global?, to: :options
delegate :usage?, :config?, :list?, :version?, :global?, to: :options
delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :valid_profile?, to: :config

def initialize(args)
Expand All @@ -21,6 +21,8 @@ def run
configure!
elsif list?
print_list
elsif version?
print_version
else
set!
end
Expand All @@ -47,6 +49,10 @@ def print_usage
puts usage
end

def print_version
puts GitSwitch::VERSION
end

def print_settings
if options.verbose?
puts "\nGit Config:"
Expand Down Expand Up @@ -77,7 +83,7 @@ def set_ssh

def usage
<<~USAGE
usage: git switch [-l | --list] [-c | --config]
usage: git switch [-c | --config] [-l | --list] [-v | --version]
<profile> [-v | --verbose] [-g | --global]
configure profiles
Expand All @@ -94,6 +100,9 @@ def usage
see available profiles
git switch -l
view installed gem version
git switch -v
USAGE
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/git_switch/version.rb
@@ -1,3 +1,3 @@
module GitSwitch
VERSION = "0.4.1"
VERSION = "0.4.2"
end
150 changes: 146 additions & 4 deletions spec/lib/git_switch/options_spec.rb
Expand Up @@ -19,6 +19,20 @@
end
end

context 'when run in version mode' do
let(:args) { ['-v'] }
it 'returns an array of the flags' do
expect(options.flags).to eq ['-v']
end
end

context 'when run in verbose mode' do
let(:args) { ['-v', 'personal'] }
it 'returns an array of the flags' do
expect(options.flags).to eq ['-v']
end
end

context 'when run in global mode' do
let(:args) { ['personal', '-g'] }
it 'returns an array of the flags' do
Expand Down Expand Up @@ -56,6 +70,20 @@
end
end

context 'when run in version mode' do
let(:args) { ['--version'] }
it 'returns an array of the flags' do
expect(options.flags).to eq ['--version']
end
end

context 'when run in verbose mode' do
let(:args) { ['--verbose', 'personal'] }
it 'returns an array of the flags' do
expect(options.flags).to eq ['--verbose']
end
end

context 'when run in global mode' do
let(:args) { ['personal', '--global'] }
it 'returns an array of the flags' do
Expand Down Expand Up @@ -117,6 +145,14 @@
end
end

context 'when run with config flag' do
let(:args) { ['-c'] }

it 'returns true' do
expect(options.valid_args?).to be true
end
end

context 'when run with list flag' do
let(:args) { ['-l'] }

Expand All @@ -125,14 +161,46 @@
end
end

context 'when run with config flag' do
let(:args) { ['-c'] }
context 'when run with version flag' do
let(:args) { ['-v'] }

it 'returns true' do
expect(options.valid_args?).to be true
end
end

context 'when run with version flag with profile' do
let(:args) { ['--version', 'personal'] }

it 'returns false' do
expect(options.valid_args?).to be false
end

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

context 'when run with verbose flag with profile' do
let(:args) { ['-v', 'personal'] }

it 'returns true' do
expect(options.valid_args?).to be true
end
end

context 'when run with verbose flag without profile' do
let(:args) { ['--verbose'] }

it 'returns false' do
expect(options.valid_args?).to be false
end

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

context 'when run with multiple flags' do
let(:args) { ['personal','-g','-l'] }
let(:options) { GitSwitch::Options.new(args) }
Expand Down Expand Up @@ -250,6 +318,80 @@
end
end

describe 'version?' do
context 'when args includes -v' do
let(:args) { ['-v'] }
it 'returns true' do
expect(options.version?).to be true
end
end

context 'when args includes --version' do
let(:args) { ['--version'] }
it 'returns true' do
expect(options.version?).to be true
end
end

context 'when args include -v and a profile' do
let(:args) { ['-v', 'personal'] }
it 'returns false' do
expect(options.version?).to be false
end
end

context 'when args include --version and a profile' do
let(:args) { ['--version', 'personal'] }
it 'returns false' do
expect(options.version?).to be false
end
end

context 'when args do not include -v or --version' do
let(:args) { [] }
it 'returns false' do
expect(options.version?).to be false
end
end
end

describe 'verbose?' do
context 'when args includes -v' do
let(:args) { ['-v', 'personal'] }
it 'returns true' do
expect(options.verbose?).to be true
end
end

context 'when args includes --verbose' do
let(:args) { ['--verbose', 'personal'] }
it 'returns true' do
expect(options.verbose?).to be true
end
end

context 'when args include -v and no profile' do
let(:args) { ['-v'] }
it 'returns false' do
expect(options.verbose?).to be false
end
end

context 'when args include --verbose and no profile' do
let(:args) { ['--verbose'] }
it 'returns false' do
expect(options.verbose?).to be false
end
end

context 'when args do not include -v or --version' do
let(:args) { [] }
it 'returns false' do
expect(options.version?).to be false
end
end
end

describe 'global?' do
context 'when args includes -g' do
let(:args) { ['-g'] }
Expand All @@ -275,14 +417,14 @@

describe 'verbose?' do
context 'when args includes -v' do
let(:args) { ['-v'] }
let(:args) { ['-v', 'personal'] }
it 'returns true' do
expect(options.verbose?).to be true
end
end

context 'when args includes --verbose' do
let(:args) { ['--verbose'] }
let(:args) { ['--verbose', 'personal'] }
it 'returns true' do
expect(options.verbose?).to be true
end
Expand Down
22 changes: 21 additions & 1 deletion spec/lib/git_switch/switcher_spec.rb
Expand Up @@ -168,6 +168,14 @@
end
end

context 'in version mode' do
let(:switcher) { GitSwitch::Switcher.new(['-v']) }
it 'calls print_version' do
expect(switcher).to receive(:print_version)
switcher.run
end
end

context 'in set mode' do
let(:switcher) { GitSwitch::Switcher.new(['personal']) }
let(:options) { switcher.options }
Expand Down Expand Up @@ -258,7 +266,7 @@
let(:switcher) { GitSwitch::Switcher.new([]) }
let(:expected_output) do
<<~USAGE
usage: git switch [-l | --list] [-c | --config]
usage: git switch [-c | --config] [-l | --list] [-v | --version]
<profile> [-v | --verbose] [-g | --global]
configure profiles
Expand All @@ -275,6 +283,9 @@
see available profiles
git switch -l
view installed gem version
git switch -v
USAGE
end

Expand All @@ -283,6 +294,15 @@
end
end

describe '#print_version' do
let(:switcher) { GitSwitch::Switcher.new(['-v']) }
let(:expected_output) { "#{GitSwitch::VERSION}\n" }

it 'outputs usage details' do
expect{switcher.print_version}.to output(expected_output).to_stdout
end
end

describe 'git_repo?' do
let(:switcher) { GitSwitch::Switcher.new(['personal']) }
context 'when GitHelper returns true' do
Expand Down

0 comments on commit 91ab5d2

Please sign in to comment.