diff --git a/lib/git_switch/options.rb b/lib/git_switch/options.rb index c56e338..4270694 100644 --- a/lib/git_switch/options.rb +++ b/lib/git_switch/options.rb @@ -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 @@ -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? @@ -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) diff --git a/lib/git_switch/switcher.rb b/lib/git_switch/switcher.rb index 2b37cd3..5b20229 100644 --- a/lib/git_switch/switcher.rb +++ b/lib/git_switch/switcher.rb @@ -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) @@ -21,6 +21,8 @@ def run configure! elsif list? print_list + elsif version? + print_version else set! end @@ -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:" @@ -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] [-v | --verbose] [-g | --global] configure profiles @@ -94,6 +100,9 @@ def usage see available profiles git switch -l + + view installed gem version + git switch -v USAGE end end diff --git a/lib/git_switch/version.rb b/lib/git_switch/version.rb index 61b4eb7..ecc4a46 100644 --- a/lib/git_switch/version.rb +++ b/lib/git_switch/version.rb @@ -1,3 +1,3 @@ module GitSwitch - VERSION = "0.4.1" + VERSION = "0.4.2" end diff --git a/spec/lib/git_switch/options_spec.rb b/spec/lib/git_switch/options_spec.rb index a41bc17..6ba117f 100644 --- a/spec/lib/git_switch/options_spec.rb +++ b/spec/lib/git_switch/options_spec.rb @@ -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 @@ -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 @@ -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'] } @@ -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) } @@ -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'] } @@ -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 diff --git a/spec/lib/git_switch/switcher_spec.rb b/spec/lib/git_switch/switcher_spec.rb index 4d01178..a5e6de1 100644 --- a/spec/lib/git_switch/switcher_spec.rb +++ b/spec/lib/git_switch/switcher_spec.rb @@ -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 } @@ -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] [-v | --verbose] [-g | --global] configure profiles @@ -275,6 +283,9 @@ see available profiles git switch -l + + view installed gem version + git switch -v USAGE end @@ -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