command_kit 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +15 -0
  3. data/.rubocop.yml +138 -0
  4. data/ChangeLog.md +29 -0
  5. data/Gemfile +3 -0
  6. data/README.md +141 -121
  7. data/Rakefile +3 -2
  8. data/command_kit.gemspec +4 -4
  9. data/examples/command.rb +1 -1
  10. data/gemspec.yml +7 -0
  11. data/lib/command_kit/arguments.rb +1 -1
  12. data/lib/command_kit/colors.rb +221 -45
  13. data/lib/command_kit/command.rb +1 -1
  14. data/lib/command_kit/commands.rb +4 -4
  15. data/lib/command_kit/help/man.rb +4 -25
  16. data/lib/command_kit/inflector.rb +47 -17
  17. data/lib/command_kit/main.rb +7 -9
  18. data/lib/command_kit/man.rb +44 -0
  19. data/lib/command_kit/open_app.rb +69 -0
  20. data/lib/command_kit/options/option.rb +1 -6
  21. data/lib/command_kit/options/parser.rb +15 -17
  22. data/lib/command_kit/options.rb +2 -2
  23. data/lib/command_kit/os/linux.rb +157 -0
  24. data/lib/command_kit/os.rb +159 -11
  25. data/lib/command_kit/package_manager.rb +200 -0
  26. data/lib/command_kit/pager.rb +46 -4
  27. data/lib/command_kit/printing/indent.rb +2 -2
  28. data/lib/command_kit/printing.rb +1 -1
  29. data/lib/command_kit/sudo.rb +40 -0
  30. data/lib/command_kit/terminal.rb +5 -0
  31. data/lib/command_kit/version.rb +1 -1
  32. data/spec/arguments/argument_spec.rb +1 -1
  33. data/spec/colors_spec.rb +256 -0
  34. data/spec/commands_spec.rb +1 -1
  35. data/spec/exception_handler_spec.rb +1 -1
  36. data/spec/help/man_spec.rb +0 -32
  37. data/spec/inflector_spec.rb +70 -8
  38. data/spec/man_spec.rb +46 -0
  39. data/spec/open_app_spec.rb +85 -0
  40. data/spec/options/option_spec.rb +2 -2
  41. data/spec/os/linux_spec.rb +154 -0
  42. data/spec/os_spec.rb +200 -13
  43. data/spec/package_manager_spec.rb +806 -0
  44. data/spec/pager_spec.rb +71 -6
  45. data/spec/sudo_spec.rb +51 -0
  46. data/spec/terminal_spec.rb +30 -0
  47. data/spec/usage_spec.rb +1 -1
  48. metadata +19 -4
data/spec/man_spec.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'command_kit/man'
3
+
4
+ describe CommandKit::Man do
5
+ module TestMan
6
+ class TestCommand
7
+ include CommandKit::Man
8
+ end
9
+ end
10
+
11
+ let(:command_class) { TestMan::TestCommand }
12
+
13
+ subject { command_class.new }
14
+
15
+ describe "#man" do
16
+ let(:man_page) { 'foo' }
17
+
18
+ it "must call system() with the given man page" do
19
+ expect(subject).to receive(:system).with('man',man_page)
20
+
21
+ subject.man(man_page)
22
+ end
23
+
24
+ context "when given a non-String man-page argument" do
25
+ let(:man_page_arg) { double(:non_string_arg) }
26
+
27
+ it "must call #to_s on the man-page argument" do
28
+ expect(man_page_arg).to receive(:to_s).and_return(man_page)
29
+
30
+ expect(subject).to receive(:system).with('man',man_page)
31
+
32
+ subject.man(man_page_arg)
33
+ end
34
+ end
35
+
36
+ context "when given the section: keyword argument" do
37
+ let(:section) { 7 }
38
+
39
+ it "must call system() with the given section number and man page" do
40
+ expect(subject).to receive(:system).with('man',section.to_s,man_page)
41
+
42
+ subject.man(man_page, section: section)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+ require 'command_kit/open_app'
3
+
4
+ describe CommandKit::OpenApp do
5
+ module TestOpenApp
6
+ class TestCommand
7
+ include CommandKit::OpenApp
8
+ end
9
+ end
10
+
11
+ let(:command_class) { TestOpenApp::TestCommand }
12
+
13
+ subject { command_class.new }
14
+
15
+ describe "#initialize" do
16
+ context "when the OS is macOS" do
17
+ subject { command_class.new(os: :macos) }
18
+
19
+ it "must set @open_command to \"open\"" do
20
+ expect(subject.instance_variable_get("@open_command")).to eq("open")
21
+ end
22
+ end
23
+
24
+ context "when the OS is Linux" do
25
+ subject { command_class.new(os: :linux) }
26
+
27
+ it "must set @open_command to \"xdg-open\"" do
28
+ expect(subject.instance_variable_get("@open_command")).to eq("xdg-open")
29
+ end
30
+ end
31
+
32
+ context "when the OS is FreeBSD" do
33
+ subject { command_class.new(os: :freebsd) }
34
+
35
+ it "must set @open_command to \"xdg-open\"" do
36
+ expect(subject.instance_variable_get("@open_command")).to eq("xdg-open")
37
+ end
38
+ end
39
+
40
+ context "when the OS is OpenBSD" do
41
+ subject { command_class.new(os: :openbsd) }
42
+
43
+ it "must set @open_command to \"xdg-open\"" do
44
+ expect(subject.instance_variable_get("@open_command")).to eq("xdg-open")
45
+ end
46
+ end
47
+
48
+ context "when the OS is NetBSD" do
49
+ subject { command_class.new(os: :openbsd) }
50
+
51
+ it "must set @open_command to \"xdg-open\"" do
52
+ expect(subject.instance_variable_get("@open_command")).to eq("xdg-open")
53
+ end
54
+ end
55
+
56
+ context "when the OS is Windows" do
57
+ subject { command_class.new(os: :windows) }
58
+
59
+ it "must set @open_command to \"start\"" do
60
+ expect(subject.instance_variable_get("@open_command")).to eq("start")
61
+ end
62
+ end
63
+ end
64
+
65
+ describe "#open_app_for" do
66
+ context "when @open_command is set" do
67
+ let(:file_or_uri) { "foo" }
68
+ let(:status) { true }
69
+
70
+ it "must execute the @open_command with the given URI or file" do
71
+ expect(subject).to receive(:system).with(subject.instance_variable_get("@open_command"),file_or_uri).and_return(status)
72
+
73
+ expect(subject.open_app_for(file_or_uri)).to be(status)
74
+ end
75
+ end
76
+
77
+ context "when @open_command is not set" do
78
+ before do
79
+ subject.instance_variable_set("@open_command",nil)
80
+ end
81
+
82
+ it { expect(subject.open_app_for("foo")).to be(nil) }
83
+ end
84
+ end
85
+ end
@@ -18,12 +18,12 @@ describe CommandKit::Options::Option do
18
18
  end
19
19
 
20
20
  subject do
21
- described_class.new name, short: short,
21
+ described_class.new(name, short: short,
22
22
  long: long,
23
23
  equals: equals,
24
24
  desc: desc,
25
25
  value: value,
26
- &block
26
+ &block)
27
27
  end
28
28
 
29
29
  describe "#initialize" do
@@ -0,0 +1,154 @@
1
+ require 'spec_helper'
2
+ require 'command_kit/os/linux'
3
+
4
+ describe CommandKit::OS::Linux do
5
+ module TestOSLinux
6
+ class TestCommand
7
+ include CommandKit::OS::Linux
8
+ end
9
+ end
10
+
11
+ let(:command_class) { TestOSLinux::TestCommand }
12
+
13
+ describe ".linux_distro" do
14
+ subject { command_class }
15
+
16
+ context "and the /etc/fedora-release file exists" do
17
+ before do
18
+ expect(File).to receive(:file?).with("/etc/fedora-release").and_return(true)
19
+ end
20
+
21
+ it { expect(subject.linux_distro).to be(:fedora) }
22
+ end
23
+
24
+ context "and the /etc/redhat-release file exists" do
25
+ before do
26
+ allow(File).to receive(:file?).with("/etc/fedora-release").and_return(false)
27
+ expect(File).to receive(:file?).with("/etc/redhat-release").and_return(true)
28
+ end
29
+
30
+ it { expect(subject.linux_distro).to be(:redhat) }
31
+ end
32
+
33
+ context "and the /etc/debian_version file exists" do
34
+ before do
35
+ allow(File).to receive(:file?).with("/etc/fedora-release").and_return(false)
36
+ allow(File).to receive(:file?).with("/etc/redhat-release").and_return(false)
37
+ expect(File).to receive(:file?).with("/etc/debian_version").and_return(true)
38
+ end
39
+
40
+ it { expect(subject.linux_distro).to be(:debian) }
41
+ end
42
+
43
+ context "and the /etc/SuSE-release file exists" do
44
+ before do
45
+ allow(File).to receive(:file?).with("/etc/fedora-release").and_return(false)
46
+ allow(File).to receive(:file?).with("/etc/redhat-release").and_return(false)
47
+ allow(File).to receive(:file?).with("/etc/debian_version").and_return(false)
48
+ expect(File).to receive(:file?).with("/etc/SuSE-release").and_return(true)
49
+ end
50
+
51
+ it { expect(subject.linux_distro).to be(:suse) }
52
+ end
53
+
54
+ context "and the /etc/arch-release file exists" do
55
+ before do
56
+ allow(File).to receive(:file?).with("/etc/fedora-release").and_return(false)
57
+ allow(File).to receive(:file?).with("/etc/redhat-release").and_return(false)
58
+ allow(File).to receive(:file?).with("/etc/debian_version").and_return(false)
59
+ allow(File).to receive(:file?).with("/etc/SuSE-release").and_return(false)
60
+ expect(File).to receive(:file?).with("/etc/arch-release").and_return(true)
61
+ end
62
+
63
+ it { expect(subject.linux_distro).to be(:arch) }
64
+ end
65
+ end
66
+
67
+ subject { command_class.new }
68
+
69
+ describe "#initialize" do
70
+ it "must default #linux_distro to self.class.linux_distro" do
71
+ expect(subject.linux_distro).to eq(command_class.linux_distro)
72
+ end
73
+
74
+ context "when the linux_distro: keyword is given" do
75
+ let(:linux_distro) { :suse }
76
+
77
+ subject { command_class.new(linux_distro: linux_distro) }
78
+
79
+ it "must set #linux_distro" do
80
+ expect(subject.linux_distro).to eq(linux_distro)
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "#redhat_linux?" do
86
+ context "when #linux_distro is :redhat" do
87
+ subject { command_class.new(linux_distro: :redhat) }
88
+
89
+ it { expect(subject.redhat_linux?).to be(true) }
90
+ end
91
+
92
+ context "when #linux_distro is not :redhat" do
93
+ subject { command_class.new(linux_distro: :debian) }
94
+
95
+ it { expect(subject.redhat_linux?).to be(false) }
96
+ end
97
+ end
98
+
99
+ describe "#fedora_linux?" do
100
+ context "when #linux_distro is :fedora" do
101
+ subject { command_class.new(linux_distro: :fedora) }
102
+
103
+ it { expect(subject.fedora_linux?).to be(true) }
104
+ end
105
+
106
+ context "when #linux_distro is not :fedora" do
107
+ subject { command_class.new(linux_distro: :debian) }
108
+
109
+ it { expect(subject.fedora_linux?).to be(false) }
110
+ end
111
+ end
112
+
113
+ describe "#debian_linux?" do
114
+ context "when #linux_distro is :debian" do
115
+ subject { command_class.new(linux_distro: :debian) }
116
+
117
+ it { expect(subject.debian_linux?).to be(true) }
118
+ end
119
+
120
+ context "when #linux_distro is not :fedora" do
121
+ subject { command_class.new(linux_distro: :redhat) }
122
+
123
+ it { expect(subject.debian_linux?).to be(false) }
124
+ end
125
+ end
126
+
127
+ describe "#suse_linux?" do
128
+ context "when #linux_distro is :suse" do
129
+ subject { command_class.new(linux_distro: :suse) }
130
+
131
+ it { expect(subject.suse_linux?).to be(true) }
132
+ end
133
+
134
+ context "when #linux_distro is not :suse" do
135
+ subject { command_class.new(linux_distro: :debian) }
136
+
137
+ it { expect(subject.suse_linux?).to be(false) }
138
+ end
139
+ end
140
+
141
+ describe "#arch_linux?" do
142
+ context "when #linux_distro is :arch" do
143
+ subject { command_class.new(linux_distro: :arch) }
144
+
145
+ it { expect(subject.arch_linux?).to be(true) }
146
+ end
147
+
148
+ context "when #linux_distro is not :arch" do
149
+ subject { command_class.new(linux_distro: :debian) }
150
+
151
+ it { expect(subject.arch_linux?).to be(false) }
152
+ end
153
+ end
154
+ end
data/spec/os_spec.rb CHANGED
@@ -2,45 +2,232 @@ require 'spec_helper'
2
2
  require 'command_kit/os'
3
3
 
4
4
  describe CommandKit::OS do
5
- class TestOS
6
- include CommandKit::OS
5
+ module TestOS
6
+ class TestCommand
7
+ include CommandKit::OS
8
+ end
7
9
  end
8
10
 
9
- subject { TestOS.new }
11
+ let(:command_class) { TestOS::TestCommand }
12
+ subject { command_class.new }
13
+
14
+ describe ".os" do
15
+ subject { command_class }
10
16
 
11
- describe "#linux?" do
12
17
  context "when RUBY_PLATFORM contains 'linux'" do
13
18
  before { stub_const('RUBY_PLATFORM','x86_64-linux') }
14
19
 
15
- it { expect(subject.linux?).to be(true) }
20
+ it { expect(subject.os).to be(:linux) }
16
21
  end
17
22
 
18
- context "when RUBY_PLATFORM does not contain 'linux'" do
23
+ context "when RUBY_PLATFORM contains 'darwin'" do
19
24
  before { stub_const('RUBY_PLATFORM','aarch64-darwin') }
20
25
 
26
+ it { expect(subject.os).to be(:macos) }
27
+ end
28
+
29
+ context "when RUBY_PLATFORM contains 'freebsd'" do
30
+ before { stub_const('RUBY_PLATFORM','x86_64-freebsd') }
31
+
32
+ it { expect(subject.os).to be(:freebsd) }
33
+ end
34
+
35
+ context "when RUBY_PLATFORM contains 'openbsd'" do
36
+ before { stub_const('RUBY_PLATFORM','x86_64-openbsd') }
37
+
38
+ it { expect(subject.os).to be(:openbsd) }
39
+ end
40
+
41
+ context "when RUBY_PLATFORM contains 'netbsd'" do
42
+ before { stub_const('RUBY_PLATFORM','x86_64-netbsd') }
43
+
44
+ it { expect(subject.os).to be(:netbsd) }
45
+ end
46
+
47
+ context "when Gem.win_platform? returns true" do
48
+ before do
49
+ stub_const('RUBY_PLATFORM','x86_64-mswin')
50
+
51
+ expect(Gem).to receive(:win_platform?).and_return(true)
52
+ end
53
+
54
+ it { expect(subject.os).to be(:windows) }
55
+ end
56
+ end
57
+
58
+ describe "#initialize" do
59
+ it "must default #os to self.class.os" do
60
+ expect(subject.os).to eq(command_class.os)
61
+ end
62
+
63
+ context "when initialized with the os: keyword" do
64
+ let(:os) { :netbsd }
65
+
66
+ subject { command_class.new(os: os) }
67
+
68
+ it "must set #os" do
69
+ expect(subject.os).to eq(os)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "#linux?" do
75
+ context "when #os is :linux" do
76
+ subject { command_class.new(os: :linux) }
77
+
78
+ it { expect(subject.linux?).to be(true) }
79
+ end
80
+
81
+ context "when #os is not :linux" do
82
+ subject { command_class.new(os: :windows) }
83
+
21
84
  it { expect(subject.linux?).to be(false) }
22
85
  end
23
86
  end
24
87
 
25
88
  describe "#macos?" do
26
- context "when RUBY_PLATFORM contains 'darwin'" do
27
- before { stub_const('RUBY_PLATFORM','aarch64-darwin') }
89
+ context "when #os is :macos" do
90
+ subject { command_class.new(os: :macos) }
28
91
 
29
92
  it { expect(subject.macos?).to be(true) }
30
93
  end
31
94
 
32
- context "when RUBY_PLATFORM does not contain 'darwin'" do
33
- before { stub_const('RUBY_PLATFORM','mswin') }
95
+ context "when #os is not :macos" do
96
+ subject { command_class.new(os: :windows) }
34
97
 
35
98
  it { expect(subject.macos?).to be(false) }
36
99
  end
37
100
  end
38
101
 
102
+ describe "#freebsd?" do
103
+ context "when #os is :freebsd" do
104
+ subject { command_class.new(os: :freebsd) }
105
+
106
+ it { expect(subject.freebsd?).to be(true) }
107
+ end
108
+
109
+ context "when #os is not :freebsd" do
110
+ subject { command_class.new(os: :windows) }
111
+
112
+ it { expect(subject.freebsd?).to be(false) }
113
+ end
114
+ end
115
+
116
+ describe "#openbsd?" do
117
+ context "when #os is :openbsd" do
118
+ subject { command_class.new(os: :openbsd) }
119
+
120
+ it { expect(subject.openbsd?).to be(true) }
121
+ end
122
+
123
+ context "when #os is not :openbsd" do
124
+ subject { command_class.new(os: :windows) }
125
+
126
+ it { expect(subject.openbsd?).to be(false) }
127
+ end
128
+ end
129
+
130
+ describe "#netbsd?" do
131
+ context "when #os is :netbsd" do
132
+ subject { command_class.new(os: :netbsd) }
133
+
134
+ it { expect(subject.netbsd?).to be(true) }
135
+ end
136
+
137
+ context "when #os is not :netbsd" do
138
+ subject { command_class.new(os: :windows) }
139
+
140
+ it { expect(subject.netbsd?).to be(false) }
141
+ end
142
+ end
143
+
144
+ describe "#bsd?" do
145
+ context "when #os is :freebsd" do
146
+ subject { command_class.new(os: :freebsd) }
147
+
148
+ it { expect(subject.bsd?).to be(true) }
149
+ end
150
+
151
+ context "when #os is :openbsd" do
152
+ subject { command_class.new(os: :openbsd) }
153
+
154
+ it { expect(subject.bsd?).to be(true) }
155
+ end
156
+
157
+ context "when #os is :netbsd" do
158
+ subject { command_class.new(os: :netbsd) }
159
+
160
+ it { expect(subject.bsd?).to be(true) }
161
+ end
162
+
163
+ context "when #os is :macos" do
164
+ subject { command_class.new(os: :macos) }
165
+
166
+ it { expect(subject.bsd?).to be(false) }
167
+ end
168
+
169
+ context "when #os is :linux" do
170
+ subject { command_class.new(os: :linux) }
171
+
172
+ it { expect(subject.bsd?).to be(false) }
173
+ end
174
+
175
+ context "when #os is :windows" do
176
+ subject { command_class.new(os: :windows) }
177
+
178
+ it { expect(subject.bsd?).to be(false) }
179
+ end
180
+ end
181
+
182
+ describe "#unix?" do
183
+ context "when #os is :freebsd" do
184
+ subject { command_class.new(os: :freebsd) }
185
+
186
+ it { expect(subject.unix?).to be(true) }
187
+ end
188
+
189
+ context "when #os is :openbsd" do
190
+ subject { command_class.new(os: :openbsd) }
191
+
192
+ it { expect(subject.unix?).to be(true) }
193
+ end
194
+
195
+ context "when #os is :netbsd" do
196
+ subject { command_class.new(os: :netbsd) }
197
+
198
+ it { expect(subject.unix?).to be(true) }
199
+ end
200
+
201
+ context "when #os is :macos" do
202
+ subject { command_class.new(os: :macos) }
203
+
204
+ it { expect(subject.unix?).to be(true) }
205
+ end
206
+
207
+ context "when #os is :linux" do
208
+ subject { command_class.new(os: :linux) }
209
+
210
+ it { expect(subject.unix?).to be(true) }
211
+ end
212
+
213
+ context "when #os is :windows" do
214
+ subject { command_class.new(os: :windows) }
215
+
216
+ it { expect(subject.unix?).to be(false) }
217
+ end
218
+ end
219
+
39
220
  describe "#windows?" do
40
- it "must call Gem.win_platform?" do
41
- expect(Gem).to receive(:win_platform?)
221
+ context "when #os is :windows" do
222
+ subject { command_class.new(os: :windows) }
223
+
224
+ it { expect(subject.windows?).to be(true) }
225
+ end
226
+
227
+ context "when #os is not :windows" do
228
+ subject { command_class.new(os: :linux) }
42
229
 
43
- subject.windows?
230
+ it { expect(subject.windows?).to be(false) }
44
231
  end
45
232
  end
46
233
  end