me 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +2 -0
  5. data/README.md +4 -2
  6. data/bin/me +6 -0
  7. data/lib/me/activation.rb +39 -0
  8. data/lib/me/activation_view_model.rb +5 -0
  9. data/lib/me/cli/activate_command.rb +30 -0
  10. data/lib/me/cli/activation_view.rb +23 -0
  11. data/lib/me/cli/active_identity_view.rb +11 -0
  12. data/lib/me/cli/git_config_command.rb +23 -0
  13. data/lib/me/cli/git_config_view.rb +11 -0
  14. data/lib/me/cli/git_not_configured_view.rb +9 -0
  15. data/lib/me/cli/new_active_identity_view.rb +11 -0
  16. data/lib/me/cli/ssh_config_command.rb +23 -0
  17. data/lib/me/cli/ssh_config_view.rb +17 -0
  18. data/lib/me/cli/ssh_not_configured_view.rb +9 -0
  19. data/lib/me/cli/switch_command.rb +45 -0
  20. data/lib/me/cli/whoami_command.rb +18 -0
  21. data/lib/me/cli.rb +86 -0
  22. data/lib/me/error_presenter.rb +36 -0
  23. data/lib/me/errors.rb +32 -0
  24. data/lib/me/executor.rb +15 -0
  25. data/lib/me/git_activation.rb +30 -0
  26. data/lib/me/git_config.rb +71 -0
  27. data/lib/me/git_config_view_model.rb +5 -0
  28. data/lib/me/identity.rb +64 -0
  29. data/lib/me/identity_view_model.rb +5 -0
  30. data/lib/me/mappers/git_config_store2.rb +65 -0
  31. data/lib/me/mappers/identity_store2.rb +40 -0
  32. data/lib/me/mappers/ssh_config_store2.rb +68 -0
  33. data/lib/me/registry.rb +54 -0
  34. data/lib/me/ssh_activation.rb +28 -0
  35. data/lib/me/ssh_config.rb +65 -0
  36. data/lib/me/ssh_config_view_model.rb +5 -0
  37. data/lib/me/store2.rb +147 -0
  38. data/lib/me/thread_scope.rb +29 -0
  39. data/lib/me/version.rb +1 -1
  40. data/lib/me/view.rb +15 -0
  41. data/me.gemspec +2 -0
  42. data/spec/me/activation_spec.rb +22 -0
  43. data/spec/me/cli/activate_command_spec.rb +56 -0
  44. data/spec/me/cli/git_config_command_spec.rb +53 -0
  45. data/spec/me/cli/ssh_config_command_spec.rb +54 -0
  46. data/spec/me/cli/switch_command_spec.rb +64 -0
  47. data/spec/me/cli/whoami_command_spec.rb +26 -0
  48. data/spec/me/executor_spec.rb +23 -0
  49. data/spec/me/git_config_spec.rb +150 -0
  50. data/spec/me/identity_spec.rb +101 -0
  51. data/spec/me/mappers/git_config_store2_spec.rb +71 -0
  52. data/spec/me/mappers/identity_store2_spec.rb +37 -0
  53. data/spec/me/mappers/ssh_config_store2_spec.rb +65 -0
  54. data/spec/me/ssh_config_spec.rb +125 -0
  55. data/spec/me/store2_spec.rb +151 -0
  56. data/spec/spec_helper.rb +38 -0
  57. data/test.sh +38 -0
  58. metadata +85 -4
@@ -0,0 +1,125 @@
1
+ require "me/ssh_config"
2
+ require "me/activation"
3
+ require "me/executor"
4
+ require "me/registry"
5
+
6
+ module Me
7
+ RSpec.describe SshConfig do
8
+ subject(:ssh_config) { SshConfig.new(keys, identity_name).with_mapper(mapper) }
9
+
10
+ let(:identity_name) { "sarah_work" }
11
+ let(:keys) { ["id_rsa", "github.rsa"] }
12
+
13
+ let(:found_ssh_config) { instance_double(SshConfig) }
14
+
15
+ let(:mapper_factory) { class_double(SshConfig::Mapper) }
16
+ let(:mapper) { instance_double(SshConfig::Mapper) }
17
+
18
+ let(:executor_factory) { class_double(Executor, new: executor) }
19
+ let(:executor) { instance_double(Executor, call: nil) }
20
+
21
+ before do
22
+ Registry.register_ssh_config_mapper_factory(mapper_factory)
23
+ allow(mapper_factory)
24
+ .to receive(:find_by_identity)
25
+ .with(identity_name)
26
+ .and_return(found_ssh_config)
27
+
28
+ Registry.register_executor_factory(executor_factory)
29
+ end
30
+
31
+ describe "#initialize" do
32
+ it "can be created with keys and identity_name" do
33
+ expect(SshConfig.new(["key_a", "key_b"], identity_name))
34
+ .to eq(SshConfig.new(["key_a", "key_b"], identity_name))
35
+ end
36
+ end
37
+
38
+ describe "#==" do
39
+ it "is equal to other config with the same keys" do
40
+ expect(SshConfig.new(["key_c", "key_b"], identity_name))
41
+ .to eq(SshConfig.new(["key_c", "key_b"], identity_name))
42
+ end
43
+
44
+ it "is not equal to other config with different keys" do
45
+ expect(SshConfig.new(["key_c", "key_b"], identity_name))
46
+ .not_to eq(SshConfig.new(["key_c", "github"], identity_name))
47
+ end
48
+
49
+ it "handles edge cases properly" do
50
+ expect(SshConfig.new(["key_c", "key_b"], identity_name)).not_to eq(nil)
51
+ expect(SshConfig.new(["key_c", "key_b"], identity_name)).not_to eq(Object.new)
52
+ end
53
+
54
+ it "doesn't care about identity_name" do
55
+ expect(SshConfig.new(["key_c", "key_b"], identity_name))
56
+ .to eq(SshConfig.new(["key_c", "key_b"], "different name"))
57
+ end
58
+ end
59
+
60
+ describe "#configure" do
61
+ context "when keys are not empty" do
62
+ let(:keys) { instance_double(Array, empty?: false) }
63
+
64
+ it "delegates to store to configure ssh" do
65
+ expect(mapper)
66
+ .to receive(:update)
67
+ .with(keys: keys)
68
+ .once
69
+
70
+ ssh_config.configure
71
+ end
72
+ end
73
+
74
+ context "when keys are empty" do
75
+ let(:keys) { instance_double(Array, empty?: true) }
76
+
77
+ it "does nothing" do
78
+ expect(mapper).not_to receive(:update)
79
+ ssh_config.configure
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "#build_view" do
85
+ let(:view_factory) { double("ViewFactory") }
86
+ let(:view) { double("View") }
87
+
88
+ it "gives keys to view factory" do
89
+ allow(view_factory)
90
+ .to receive(:new)
91
+ .with(keys: keys)
92
+ .and_return(view)
93
+ expect(ssh_config.build_view(view_factory)).to eq(view)
94
+ end
95
+ end
96
+
97
+ describe ".for_identity" do
98
+ subject(:ssh_config) { SshConfig.for_identity(identity_name) }
99
+
100
+ it "finds ssh config for specified identity" do
101
+ expect(ssh_config).to eq(found_ssh_config)
102
+ end
103
+ end
104
+
105
+ describe "#activate" do
106
+ let(:expected_commands) { [
107
+ ["ssh-add", "-D"],
108
+ ["ssh-add", "id_rsa"],
109
+ ["ssh-add", "github.rsa"],
110
+ ] }
111
+
112
+ it "sets up proper ssh keys" do
113
+ expected_commands.each do |command|
114
+ expect(executor).to receive(:call).with(command).ordered.once
115
+ end
116
+
117
+ ssh_config.activate
118
+ end
119
+
120
+ it "returns proper activation object" do
121
+ expect(ssh_config.activate).to eq(Activation.new._with_commands(expected_commands))
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,151 @@
1
+ require "me/store2"
2
+
3
+ module Me
4
+ RSpec.describe Store2 do
5
+ subject(:store) { Store2.new }
6
+
7
+ describe ".build" do
8
+ it "creates scoped instance with empty scope" do
9
+ expect(Store2.build).to eq(Store2::Scoped.new(Store2.new, []))
10
+ end
11
+ end
12
+
13
+ describe "#scoped" do
14
+ it "creates scoped instance with specified scope" do
15
+ expect(store.scoped("hello", "world"))
16
+ .to eq(Store2::Scoped.new(Store2.new, ["hello", "world"]))
17
+ end
18
+ end
19
+
20
+ describe "#get and #set" do
21
+ it "gets the value given list of keys" do
22
+ store.set(["hello"], {})
23
+ store.set(["hello", "world"], {})
24
+ store.set(["hello", "world", "test"], "12345")
25
+
26
+ expect(store.get(["hello", "world", "test"]))
27
+ .to eq("12345")
28
+ end
29
+ end
30
+
31
+ describe "#has?" do
32
+ it "is true if key is present" do
33
+ store.set(["hello"], {})
34
+ store.set(["hello", "world"], {})
35
+ store.set(["hello", "world", "test"], "12345")
36
+
37
+ expect(store.has?(["hello", "world", "test"])).to eq(true)
38
+ end
39
+
40
+ it "is false if key is not present" do
41
+ store.set(["hello"], {})
42
+ store.set(["hello", "world"], {})
43
+
44
+ expect(store.has?(["hello", "world", "test"])).to eq(false)
45
+ end
46
+ end
47
+
48
+ describe "#get_or_set" do
49
+ it "is current value if it is present" do
50
+ store.set(["hello"], {})
51
+ store.set(["hello", "world"], {})
52
+ store.set(["hello", "world", "test"], "09876")
53
+
54
+ expect(store.get_or_set(["hello", "world", "test"], "12345")).to eq("09876")
55
+ end
56
+
57
+ it "is default value if it is not present" do
58
+ store.set(["hello"], {})
59
+ store.set(["hello", "world"], {})
60
+
61
+ expect(store.get_or_set(["hello", "world", "test"], "12345")).to eq("12345")
62
+ end
63
+
64
+ it "makes the value present if it wasn't" do
65
+ store.set(["hello"], {})
66
+ store.set(["hello", "world"], {})
67
+ store.get_or_set(["hello", "world", "test"], "12345")
68
+
69
+ expect(store.get(["hello", "world", "test"])).to eq("12345")
70
+ end
71
+ end
72
+
73
+ describe "#fetch" do
74
+ it "behaves like get if value is present" do
75
+ store.set(["hello"], {})
76
+ store.set(["hello", "world"], {})
77
+ store.set(["hello", "world", "test"], "09876")
78
+
79
+ expect(store.fetch(["hello", "world", "test"]) { fail }).to eq("09876")
80
+ end
81
+
82
+ it "calls block if value is not present" do
83
+ store.set(["hello"], {})
84
+ store.set(["hello", "world"], {})
85
+
86
+ expect { store.fetch(["hello", "world", "test"]) { fail } }
87
+ .to raise_error(RuntimeError)
88
+ end
89
+ end
90
+ end
91
+
92
+ RSpec.describe Store2::Scoped do
93
+ subject(:scoped) { described_class.new(store, scope) }
94
+
95
+ let(:store) { instance_double(Store2) }
96
+ let(:scope) { ["hello", "world", "test"] }
97
+
98
+ let(:a_value) { double("A value") }
99
+ let(:another_value) { double("A value") }
100
+ let(:a_proc) { Proc.new { a_value } }
101
+
102
+ describe "#scoped" do
103
+ it "creates instance of Scoped with additional scope" do
104
+ expect(scoped.scoped("additional", "scope"))
105
+ .to eq(described_class.new(store, scope + ["additional", "scope"]))
106
+ end
107
+ end
108
+
109
+ describe "#get" do
110
+ it "delegates to store#get" do
111
+ allow(store).to receive(:get).with(scope + ["some", "keys"]).and_return(a_value)
112
+ expect(scoped.get("some", "keys")).to eq(a_value)
113
+ end
114
+ end
115
+
116
+ describe "#set" do
117
+ it "delegates to store#set" do
118
+ expect(store).to receive(:set).with(scope + ["some", "keys"], a_value)
119
+ scoped.set("some", "keys", a_value)
120
+ end
121
+ end
122
+
123
+ describe "#has?" do
124
+ it "delegates to store#has?" do
125
+ allow(store).to receive(:has?).with(scope + ["some", "keys"]).and_return(a_value)
126
+ expect(scoped.has?("some", "keys")).to eq(a_value)
127
+ end
128
+ end
129
+
130
+ describe "#get_or_set" do
131
+ it "delegates to store#get_or_set" do
132
+ allow(store).to receive(:get_or_set).with(scope + ["some", "keys"], a_value).and_return(another_value)
133
+ expect(scoped.get_or_set("some", "keys", a_value)).to eq(another_value)
134
+ end
135
+ end
136
+
137
+ describe "#fetch" do
138
+ it "delegates to store#fetch" do
139
+ allow(store).to receive(:fetch).with(scope + ["some", "keys"]).and_yield
140
+ expect(scoped.fetch("some", "keys", &a_proc)).to eq(a_value)
141
+ end
142
+ end
143
+
144
+ describe "#save" do
145
+ it "delegates to store#save" do
146
+ expect(store).to receive(:save)
147
+ scoped.save
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,38 @@
1
+ ENV["ME_ENV"] ||= "test"
2
+
3
+ require "me/thread_scope"
4
+
5
+ RSpec.configure do |config|
6
+ config.expect_with :rspec do |expectations|
7
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
8
+ end
9
+
10
+ config.mock_with :rspec do |mocks|
11
+ mocks.verify_partial_doubles = true
12
+ end
13
+
14
+ config.filter_run :focus
15
+ config.run_all_when_everything_filtered = true
16
+
17
+ config.disable_monkey_patching!
18
+
19
+ config.warnings = true
20
+
21
+ if config.files_to_run.one?
22
+ config.default_formatter = 'doc'
23
+ end
24
+
25
+ config.profile_examples = 10
26
+
27
+ config.order = :random
28
+
29
+ Kernel.srand config.seed
30
+
31
+ config.before do
32
+ Me::Store2.new._reset
33
+ end
34
+
35
+ config.after do
36
+ Me::ThreadScope._reset
37
+ end
38
+ end
data/test.sh ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env bash
2
+
3
+ ## This script tested on ubuntu, probably will not work properly on
4
+ ## mac os x..
5
+
6
+ echo -n "free" > tests.lock
7
+
8
+ function termtitle {
9
+ echo -ne "\033]0;${1}\007"
10
+ }
11
+
12
+ function success {
13
+ notify-send "SUCCESS" -i face-angel -t 300
14
+ termtitle "SUCCESS"
15
+ }
16
+
17
+ function failure {
18
+ notify-send "FAILURE" -i face-angry -t 300
19
+ termtitle "FAILURE"
20
+ }
21
+
22
+ function run_rspec {
23
+ rspec && success || failure
24
+ echo -n "free" > tests.lock
25
+ }
26
+
27
+ function run_tests {
28
+ lock=$(cat tests.lock)
29
+ if [[ "$lock" = "free" ]]; then
30
+ echo -n "taken" > tests.lock
31
+ run_rspec &
32
+ fi
33
+ }
34
+
35
+ inotifywait -r -m -e modify --format '%w%f' lib spec 2>/dev/null | while read line; do
36
+ echo $line
37
+ run_tests
38
+ done
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksii Fedorov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-06 00:00:00.000000000 Z
11
+ date: 2015-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -41,18 +55,70 @@ dependencies:
41
55
  description: Small tool for switching common identities (ssh keys, git config, etc)
42
56
  email:
43
57
  - waterlink000@gmail.com
44
- executables: []
58
+ executables:
59
+ - me
45
60
  extensions: []
46
61
  extra_rdoc_files: []
47
62
  files:
48
63
  - ".gitignore"
64
+ - ".rspec"
49
65
  - Gemfile
50
66
  - LICENSE.txt
51
67
  - README.md
52
68
  - Rakefile
69
+ - bin/me
53
70
  - lib/me.rb
71
+ - lib/me/activation.rb
72
+ - lib/me/activation_view_model.rb
73
+ - lib/me/cli.rb
74
+ - lib/me/cli/activate_command.rb
75
+ - lib/me/cli/activation_view.rb
76
+ - lib/me/cli/active_identity_view.rb
77
+ - lib/me/cli/git_config_command.rb
78
+ - lib/me/cli/git_config_view.rb
79
+ - lib/me/cli/git_not_configured_view.rb
80
+ - lib/me/cli/new_active_identity_view.rb
81
+ - lib/me/cli/ssh_config_command.rb
82
+ - lib/me/cli/ssh_config_view.rb
83
+ - lib/me/cli/ssh_not_configured_view.rb
84
+ - lib/me/cli/switch_command.rb
85
+ - lib/me/cli/whoami_command.rb
86
+ - lib/me/error_presenter.rb
87
+ - lib/me/errors.rb
88
+ - lib/me/executor.rb
89
+ - lib/me/git_activation.rb
90
+ - lib/me/git_config.rb
91
+ - lib/me/git_config_view_model.rb
92
+ - lib/me/identity.rb
93
+ - lib/me/identity_view_model.rb
94
+ - lib/me/mappers/git_config_store2.rb
95
+ - lib/me/mappers/identity_store2.rb
96
+ - lib/me/mappers/ssh_config_store2.rb
97
+ - lib/me/registry.rb
98
+ - lib/me/ssh_activation.rb
99
+ - lib/me/ssh_config.rb
100
+ - lib/me/ssh_config_view_model.rb
101
+ - lib/me/store2.rb
102
+ - lib/me/thread_scope.rb
54
103
  - lib/me/version.rb
104
+ - lib/me/view.rb
55
105
  - me.gemspec
106
+ - spec/me/activation_spec.rb
107
+ - spec/me/cli/activate_command_spec.rb
108
+ - spec/me/cli/git_config_command_spec.rb
109
+ - spec/me/cli/ssh_config_command_spec.rb
110
+ - spec/me/cli/switch_command_spec.rb
111
+ - spec/me/cli/whoami_command_spec.rb
112
+ - spec/me/executor_spec.rb
113
+ - spec/me/git_config_spec.rb
114
+ - spec/me/identity_spec.rb
115
+ - spec/me/mappers/git_config_store2_spec.rb
116
+ - spec/me/mappers/identity_store2_spec.rb
117
+ - spec/me/mappers/ssh_config_store2_spec.rb
118
+ - spec/me/ssh_config_spec.rb
119
+ - spec/me/store2_spec.rb
120
+ - spec/spec_helper.rb
121
+ - test.sh
56
122
  homepage: https://github.com/waterlink/me
57
123
  licenses:
58
124
  - MIT
@@ -77,4 +143,19 @@ rubygems_version: 2.2.2
77
143
  signing_key:
78
144
  specification_version: 4
79
145
  summary: Small tool for switching common identities (ssh keys, git config, etc)
80
- test_files: []
146
+ test_files:
147
+ - spec/me/activation_spec.rb
148
+ - spec/me/cli/activate_command_spec.rb
149
+ - spec/me/cli/git_config_command_spec.rb
150
+ - spec/me/cli/ssh_config_command_spec.rb
151
+ - spec/me/cli/switch_command_spec.rb
152
+ - spec/me/cli/whoami_command_spec.rb
153
+ - spec/me/executor_spec.rb
154
+ - spec/me/git_config_spec.rb
155
+ - spec/me/identity_spec.rb
156
+ - spec/me/mappers/git_config_store2_spec.rb
157
+ - spec/me/mappers/identity_store2_spec.rb
158
+ - spec/me/mappers/ssh_config_store2_spec.rb
159
+ - spec/me/ssh_config_spec.rb
160
+ - spec/me/store2_spec.rb
161
+ - spec/spec_helper.rb