gas 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gas.rb CHANGED
@@ -35,7 +35,6 @@ module Gas
35
35
  user = @config[nickname]
36
36
 
37
37
  @gitconfig.change_user user.name, user.email
38
- @gitconfig.save!
39
38
 
40
39
  self.show
41
40
  end
@@ -1,69 +1,26 @@
1
1
  module Gas
2
2
 
3
- # Class that class that interacts with the .gitconfig
3
+ # Class that class that interacts with the git config
4
4
  class Gitconfig
5
5
 
6
- def initialize(gitconfig = nil)
7
- @gitconfig_file = File.expand_path('~/.gitconfig')
8
- @gitconfig = ''
9
-
10
- if gitconfig.nil? && File.exists?(@gitconfig_file)
11
- @gitconfig = File.read(@gitconfig_file)
12
- elsif !gitconfig.nil?
13
- @gitconfig = gitconfig
14
- end
15
- end
16
-
17
6
  # Parse out the current user from the gitconfig
18
- # TODO: Error handling
19
7
  # @param [String] gitconfig The git configuration
20
8
  # @return [User] The current user or nil if not present
21
9
  def current_user
22
- regex = /\[user\]\s+name = (.+)\s+email = (.+)/
23
- matches = regex.match @gitconfig
10
+ name = `git config --global --get user.name`
11
+ email = `git config --global --get user.email`
24
12
 
25
- return nil if matches.nil?
13
+ return nil if name.nil? && email.nil?
26
14
 
27
- User.new matches[1], matches[2]
15
+ User.new name.delete("\n"), email.delete("\n") # git cli returns the name and email with \n at the end
28
16
  end
29
17
 
30
18
  # Changes the user
31
19
  # @param [String] name The new name
32
20
  # @param [String] email The new email
33
21
  def change_user(name, email)
34
- if current_user_present?
35
- @gitconfig.gsub! /^\s*name\s?=\s?.+/, " name = #{name}"
36
- @gitconfig.gsub! /^\s*email\s?=\s?.+/, " email = #{email}"
37
- else
38
- create_user(name, email)
39
- end
40
- end
41
-
42
- # Create a user section
43
- # @param [String] name The new name
44
- # @param [String] email The new email
45
- def create_user(name, email)
46
- return if current_user_present?
47
-
48
- @gitconfig << <<-EOS
49
-
50
- # Those lines are added by gas, feel free to change them.
51
- [user]
52
- name = #{name}
53
- email = #{email}
54
- EOS
55
- end
56
-
57
- # Check if user section is present in gitconfig
58
- def current_user_present?
59
- !current_user.nil?
60
- end
61
-
62
- # Saves the gitconfig
63
- def save!
64
- File.open @gitconfig_file, 'w' do |file|
65
- file.write @gitconfig
66
- end
22
+ `git config --global user.name "#{name}"`
23
+ `git config --global user.email "#{email}"`
67
24
  end
68
25
 
69
26
  end
@@ -1,6 +1,6 @@
1
1
  module Gas
2
2
 
3
- VERSION = '0.1.5'
3
+ VERSION = '0.1.6'
4
4
 
5
5
  end
6
6
 
@@ -7,50 +7,38 @@ describe Gas::Gitconfig do
7
7
  before :each do
8
8
  @name = 'Fredrik Wallgren'
9
9
  @email = 'fredrik.wallgren@gmail.com'
10
- gitconfig = "[other stuff]\n foo = bar\n\n[user]\n name = #{@name}\n email = #{@email}\n\n[foo]\n bar = foo"
11
- @gitconfig = Gas::Gitconfig.new gitconfig
12
- @empty_gitconfig = Gas::Gitconfig.new ''
10
+ @gitconfig = Gas::Gitconfig.new
13
11
  end
14
12
 
15
13
  it 'should be able to get current user from gitconfig' do
14
+ mock_cli_call(@gitconfig, 'git config --global --get user.name') { @name + "\n" }
15
+ mock_cli_call(@gitconfig, 'git config --global --get user.email') { @email + "\n" }
16
+
16
17
  user = @gitconfig.current_user
17
18
  user.name.should == @name
18
19
  user.email.should == @email
19
20
  end
20
21
 
21
22
  it 'should return nil if no current user is present in gitconfig' do
22
- @empty_gitconfig.current_user.should == nil
23
+ mock_cli_call(@gitconfig, 'git config --global --get user.name') { nil }
24
+ mock_cli_call(@gitconfig, 'git config --global --get user.email') { nil }
25
+
26
+ @gitconfig.current_user.should == nil
23
27
  end
24
28
 
25
29
  it 'should be able to change the current user' do
26
30
  name = 'Test Testsson'
27
31
  email = 'test@testsson.com'
28
- @gitconfig.change_user name, email
29
32
 
30
- user = @gitconfig.current_user
31
- user.name.should == name
32
- user.email.should == email
33
- end
33
+ mock_cli_call(@gitconfig, "git config --global user.name \"#{name}\"") { nil }
34
+ mock_cli_call(@gitconfig, "git config --global user.email \"#{email}\"") { nil }
35
+ mock_cli_call(@gitconfig, 'git config --global --get user.name') { name + "\n" }
36
+ mock_cli_call(@gitconfig, 'git config --global --get user.email') { email + "\n" }
34
37
 
35
- it 'should add a new user if no user section exists in gitconfig' do
36
- name = 'Test Testsson'
37
- email = 'test@testsson.com'
38
- @empty_gitconfig.change_user name, email
38
+ @gitconfig.change_user name, email
39
39
 
40
- user = @empty_gitconfig.current_user
40
+ user = @gitconfig.current_user
41
41
  user.name.should == name
42
42
  user.email.should == email
43
43
  end
44
-
45
- it 'should not blow up if no gitconfig exists (use empty string instead)' do
46
- # Stub out File#exists?
47
- class File
48
- def self.exists?(path)
49
- false
50
- end
51
- end
52
-
53
- gitconfig_file_absent = Gas::Gitconfig.new
54
- gitconfig_file_absent.instance_variable_get(:@gitconfig).should == ''
55
- end
56
44
  end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :rr
5
+ end
6
+
7
+ # Mocks a cli call using ` with rr.
8
+ # Takes a block to use as rr return block
9
+ # @param [Object] mock_object The object to mock
10
+ # @param [String] command The command to mock
11
+ def mock_cli_call(mock_object, command)
12
+ # To be able to mock ` (http://blog.astrails.com/2010/7/5/how-to-mock-backticks-operator-in-your-test-specs-using-rr)
13
+ mock(mock_object).__double_definition_create__.call(:`, command) { yield }
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-23 00:00:00.000000000 +02:00
12
+ date: 2011-07-30 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: thor
17
- requirement: &12746540 !ruby/object:Gem::Requirement
17
+ requirement: &21428080 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 0.14.6
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *12746540
25
+ version_requirements: *21428080
26
26
  - !ruby/object:Gem::Dependency
27
- name: rspec
28
- requirement: &12746120 !ruby/object:Gem::Requirement
27
+ name: rake
28
+ requirement: &21427660 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,32 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *12746120
36
+ version_requirements: *21427660
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &12745660 !ruby/object:Gem::Requirement
39
+ requirement: &21427200 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *21427200
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &21426780 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *21426780
59
+ - !ruby/object:Gem::Dependency
60
+ name: rr
61
+ requirement: &21426360 !ruby/object:Gem::Requirement
40
62
  none: false
41
63
  requirements:
42
64
  - - ! '>='
@@ -44,7 +66,7 @@ dependencies:
44
66
  version: '0'
45
67
  type: :development
46
68
  prerelease: false
47
- version_requirements: *12745660
69
+ version_requirements: *21426360
48
70
  description: Gas is a utility to keep track of your git authors. Add them to gas and
49
71
  switch at any time. Great if you use one author at work and one at home for instance.
50
72
  email: fredrik.wallgren@gmail.com