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 #58 from randallreedjr/feature/edit-flag
Browse files Browse the repository at this point in the history
[Feature] Add edit flag (-e) to open configuration file in text editor
  • Loading branch information
randallreedjr committed Feb 7, 2019
2 parents eccd243 + 8236d05 commit bfcac0f
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/git_switch/config.rb
Expand Up @@ -51,6 +51,10 @@ def configure!
write_profiles_to_config_file if @profiles.any?
end

def edit!
system("#{ENV['EDITOR']} '#{File.expand_path('~/.gitswitch')}'")
end

def build_profiles
puts "How many profiles would you like to create?"
profile_count = STDIN.gets.chomp.to_i
Expand Down
14 changes: 12 additions & 2 deletions lib/git_switch/options.rb
Expand Up @@ -7,14 +7,16 @@ def initialize(args)

def flags
@flags ||= args.select do |arg|
arg.match(/\A\-[cglv]{1}\z/) ||
arg.match(/\A\-\-(config|global|list|verbose|version){1}\z/)
arg.match(/\A\-[ceglv]{1}\z/) ||
arg.match(/\A\-\-(config|edit|global|list|verbose|version){1}\z/)
end
end

def valid_args?
if config?
return true
elsif edit?
return true
elsif list?
return true
elsif version?
Expand All @@ -41,6 +43,10 @@ def config?
config_flag? && args.count == 1
end

def edit?
edit_flag? && args.count == 1
end

def list?
list_flag? && args.count == 1
end
Expand All @@ -63,6 +69,10 @@ def config_flag?
(flags.include? '-c') || (flags.include? '--config')
end

def edit_flag?
(flags.include? '-e') || (flags.include? '--edit')
end

def list_flag?
(flags.include? '-l') || (flags.include? '--list')
end
Expand Down
11 changes: 8 additions & 3 deletions lib/git_switch/switcher.rb
Expand Up @@ -4,8 +4,8 @@
module GitSwitch
class Switcher
attr_reader :config, :options
delegate :usage?, :config?, :list?, :version?, :global?, to: :options
delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :valid_profile?, to: :config
delegate :usage?, :config?, :edit?, :list?, :version?, :global?, to: :options
delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :edit!, :valid_profile?, to: :config

def initialize(args)
raise ArgumentError unless args.is_a? Array
Expand All @@ -19,6 +19,8 @@ def run
print_usage
elsif config?
configure!
elsif edit?
edit!
elsif list?
print_list
elsif version?
Expand Down Expand Up @@ -87,12 +89,15 @@ def set_ssh

def usage
<<~USAGE
usage: git switch [-c | --config] [-l | --list] [-v | --version]
usage: git switch [-c | --config] [-e | --edit] [-l | --list] [-v | --version]
<profile> [-v | --verbose] [-g | --global]
configure profiles
git switch -c
open configuration file in editor
git switch -e
switch to a profile for local development only
git switch <profile>
Expand Down
2 changes: 1 addition & 1 deletion lib/git_switch/version.rb
@@ -1,3 +1,3 @@
module GitSwitch
VERSION = "0.4.3"
VERSION = "0.5.0"
end
13 changes: 13 additions & 0 deletions spec/lib/git_switch/config_spec.rb
Expand Up @@ -129,6 +129,19 @@
end
end

describe 'edit!' do
let(:config) { GitSwitch::Config.new(['-e']) }
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('EDITOR').and_return('code -w')
end

it 'opens the config file in the editor' do
expect(config).to receive(:system).with("code -w '#{File.expand_path('spec/fixtures/.gitswitch')}'")
config.edit!
end
end

describe 'ordinal' do
let(:config) { GitSwitch::Config.new(['-c']) }

Expand Down
52 changes: 52 additions & 0 deletions spec/lib/git_switch/options_spec.rb
Expand Up @@ -12,6 +12,13 @@
end
end

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

context 'when run in list mode' do
let(:args) { ['-l'] }
it 'returns an array of the flags' do
Expand Down Expand Up @@ -63,6 +70,13 @@
end
end

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

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

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

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

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

Expand Down Expand Up @@ -288,6 +310,36 @@
end
end

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

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

context 'when there are multiple args' do
let(:args) { ['-e', 'foo'] }
it 'returns false' do
expect(options.edit?).to be false
end
end

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

describe 'list?' do
context 'when args includes -l' do
let(:args) { ['-l'] }
Expand Down
60 changes: 58 additions & 2 deletions spec/lib/git_switch/switcher_spec.rb
Expand Up @@ -97,6 +97,50 @@
end
end

describe '#edit?' do
context 'when -e is passed as only argument' do
it 'returns true' do
expect(GitSwitch::Switcher.new(['-e']).edit?).to be true
end
end

context 'when -e is passed as first argument' do
it 'returns false' do
expect(GitSwitch::Switcher.new(['-e','foo']).edit?).to be false
end
end

context 'when -e is passed as second argument' do
it 'returns false' do
expect(GitSwitch::Switcher.new(['foo','-e']).edit?).to be false
end
end

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

context 'when --edit is passsed as first argument' do
it 'returns false' do
expect(GitSwitch::Switcher.new(['--edit', 'foo']).edit?).to be false
end
end

context 'when --edit is passsed as second argument' do
it 'returns false' do
expect(GitSwitch::Switcher.new(['foo','--edit']).edit?).to be false
end
end

context 'when no flag is passed' do
it 'returns false' do
expect(GitSwitch::Switcher.new(['foo']).edit?).to be false
end
end
end

describe '#list?' do
context 'when -l is passed as only argument' do
it 'returns true' do
Expand Down Expand Up @@ -152,13 +196,22 @@

context 'in config mode' do
let(:switcher) { GitSwitch::Switcher.new(['-c']) }
it 'calls configure' do
it 'calls configure!' do
expect(switcher).to receive(:configure!).and_call_original
expect(switcher.config).to receive(:configure!)
switcher.run
end
end

context 'in edit mode' do
let(:switcher) { GitSwitch::Switcher.new(['-e']) }
it 'calls edit!' do
expect(switcher).to receive(:edit!).and_call_original
expect(switcher.config).to receive(:edit!)
switcher.run
end
end

context 'in list mode' do
let(:switcher) { GitSwitch::Switcher.new(['-l']) }
it 'calls print_list' do
Expand Down Expand Up @@ -266,12 +319,15 @@
let(:switcher) { GitSwitch::Switcher.new([]) }
let(:expected_output) do
<<~USAGE
usage: git switch [-c | --config] [-l | --list] [-v | --version]
usage: git switch [-c | --config] [-e | --edit] [-l | --list] [-v | --version]
<profile> [-v | --verbose] [-g | --global]
configure profiles
git switch -c
open configuration file in editor
git switch -e
switch to a profile for local development only
git switch <profile>
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -5,12 +5,20 @@
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"

# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true

# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!

config.expect_with :rspec do |c|
c.syntax = :expect
end

config.before(:each) do
allow(File).to receive(:expand_path).and_call_original
allow(File).to receive(:expand_path).with('~/.gitswitch').and_return(File.expand_path('spec/fixtures/.gitswitch'))
Expand Down

0 comments on commit bfcac0f

Please sign in to comment.