command_kit 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +18 -3
  3. data/.rubocop.yml +141 -0
  4. data/ChangeLog.md +165 -0
  5. data/Gemfile +3 -0
  6. data/README.md +186 -118
  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/argument.rb +2 -2
  12. data/lib/command_kit/arguments.rb +36 -7
  13. data/lib/command_kit/colors.rb +702 -53
  14. data/lib/command_kit/command.rb +2 -3
  15. data/lib/command_kit/commands/auto_load.rb +8 -1
  16. data/lib/command_kit/commands/help.rb +3 -2
  17. data/lib/command_kit/commands/subcommand.rb +1 -1
  18. data/lib/command_kit/commands.rb +24 -9
  19. data/lib/command_kit/env/path.rb +1 -1
  20. data/lib/command_kit/file_utils.rb +46 -0
  21. data/lib/command_kit/help/man.rb +17 -33
  22. data/lib/command_kit/inflector.rb +47 -17
  23. data/lib/command_kit/interactive.rb +9 -0
  24. data/lib/command_kit/main.rb +7 -9
  25. data/lib/command_kit/man.rb +44 -0
  26. data/lib/command_kit/open_app.rb +69 -0
  27. data/lib/command_kit/options/option.rb +41 -27
  28. data/lib/command_kit/options/option_value.rb +3 -2
  29. data/lib/command_kit/options/parser.rb +17 -22
  30. data/lib/command_kit/options.rb +102 -14
  31. data/lib/command_kit/os/linux.rb +157 -0
  32. data/lib/command_kit/os.rb +159 -11
  33. data/lib/command_kit/package_manager.rb +200 -0
  34. data/lib/command_kit/pager.rb +46 -4
  35. data/lib/command_kit/printing/indent.rb +4 -4
  36. data/lib/command_kit/printing.rb +14 -3
  37. data/lib/command_kit/program_name.rb +9 -0
  38. data/lib/command_kit/sudo.rb +40 -0
  39. data/lib/command_kit/terminal.rb +5 -0
  40. data/lib/command_kit/version.rb +1 -1
  41. data/spec/arguments/argument_spec.rb +1 -1
  42. data/spec/arguments_spec.rb +84 -1
  43. data/spec/colors_spec.rb +357 -70
  44. data/spec/command_spec.rb +77 -6
  45. data/spec/commands/auto_load_spec.rb +33 -2
  46. data/spec/commands_spec.rb +101 -29
  47. data/spec/env/path_spec.rb +6 -0
  48. data/spec/exception_handler_spec.rb +1 -1
  49. data/spec/file_utils_spec.rb +59 -0
  50. data/spec/fixtures/template.erb +5 -0
  51. data/spec/help/man_spec.rb +54 -57
  52. data/spec/inflector_spec.rb +70 -8
  53. data/spec/man_spec.rb +46 -0
  54. data/spec/open_app_spec.rb +85 -0
  55. data/spec/options/option_spec.rb +38 -2
  56. data/spec/options/option_value_spec.rb +55 -0
  57. data/spec/options/parser_spec.rb +0 -10
  58. data/spec/options_spec.rb +328 -0
  59. data/spec/os/linux_spec.rb +164 -0
  60. data/spec/os_spec.rb +200 -13
  61. data/spec/package_manager_spec.rb +806 -0
  62. data/spec/pager_spec.rb +71 -6
  63. data/spec/printing/indent_spec.rb +7 -5
  64. data/spec/printing_spec.rb +23 -1
  65. data/spec/program_name_spec.rb +8 -0
  66. data/spec/sudo_spec.rb +51 -0
  67. data/spec/terminal_spec.rb +30 -0
  68. data/spec/usage_spec.rb +1 -1
  69. metadata +23 -4
@@ -0,0 +1,164 @@
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
+ subject { command_class.new(linux_distro: linux_distro) }
87
+
88
+ context "when #linux_distro is :redhat" do
89
+ let(:linux_distro) { :redhat }
90
+
91
+ it { expect(subject.redhat_linux?).to be(true) }
92
+ end
93
+
94
+ context "when #linux_distro is not :redhat" do
95
+ let(:linux_distro) { :debian }
96
+
97
+ it { expect(subject.redhat_linux?).to be(false) }
98
+ end
99
+ end
100
+
101
+ describe "#fedora_linux?" do
102
+ subject { command_class.new(linux_distro: linux_distro) }
103
+
104
+ context "when #linux_distro is :fedora" do
105
+ let(:linux_distro) { :fedora }
106
+
107
+ it { expect(subject.fedora_linux?).to be(true) }
108
+ end
109
+
110
+ context "when #linux_distro is not :fedora" do
111
+ let(:linux_distro) { :debian }
112
+
113
+ it { expect(subject.fedora_linux?).to be(false) }
114
+ end
115
+ end
116
+
117
+ describe "#debian_linux?" do
118
+ subject { command_class.new(linux_distro: linux_distro) }
119
+
120
+ context "when #linux_distro is :debian" do
121
+ let(:linux_distro) { :debian }
122
+
123
+ it { expect(subject.debian_linux?).to be(true) }
124
+ end
125
+
126
+ context "when #linux_distro is not :fedora" do
127
+ let(:linux_distro) { :redhat }
128
+
129
+ it { expect(subject.debian_linux?).to be(false) }
130
+ end
131
+ end
132
+
133
+ describe "#suse_linux?" do
134
+ subject { command_class.new(linux_distro: linux_distro) }
135
+
136
+ context "when #linux_distro is :suse" do
137
+ let(:linux_distro) { :suse }
138
+
139
+ it { expect(subject.suse_linux?).to be(true) }
140
+ end
141
+
142
+ context "when #linux_distro is not :suse" do
143
+ let(:linux_distro) { :debian }
144
+
145
+ it { expect(subject.suse_linux?).to be(false) }
146
+ end
147
+ end
148
+
149
+ describe "#arch_linux?" do
150
+ subject { command_class.new(linux_distro: linux_distro) }
151
+
152
+ context "when #linux_distro is :arch" do
153
+ let(:linux_distro) { :arch }
154
+
155
+ it { expect(subject.arch_linux?).to be(true) }
156
+ end
157
+
158
+ context "when #linux_distro is not :arch" do
159
+ let(:linux_distro) { :debian }
160
+
161
+ it { expect(subject.arch_linux?).to be(false) }
162
+ end
163
+ end
164
+ 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