sys-proctable 1.2.5 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'win32ole'
2
4
  require 'socket'
3
5
  require 'date'
@@ -5,10 +7,8 @@ require 'sys/proctable/version'
5
7
 
6
8
  # The Sys module serves as a namespace only
7
9
  module Sys
8
-
9
10
  # The ProcTable class encapsulates process table information
10
11
  class ProcTable
11
-
12
12
  # There is no constructor
13
13
  private_class_method :new
14
14
 
@@ -77,8 +77,8 @@ module Sys
77
77
  # may be useful if you want to know in advance what fields are available
78
78
  # without having to perform at least one read of the /proc table.
79
79
  #
80
- def self.fields
81
- @fields
80
+ class << self
81
+ attr_reader :fields
82
82
  end
83
83
 
84
84
  # call-seq:
@@ -96,19 +96,21 @@ module Sys
96
96
  pid = kwargs[:pid]
97
97
  host = kwargs[:host] || Socket.gethostname
98
98
 
99
- raise TypeError unless pid.kind_of?(Numeric) if pid
99
+ if pid && !pid.is_a?(Numeric)
100
+ raise TypeError
101
+ end
100
102
 
101
103
  array = block_given? ? nil : []
102
104
  struct = nil
103
105
 
104
106
  begin
105
107
  wmi = WIN32OLE.connect("winmgmts://#{host}/root/cimv2")
106
- rescue WIN32OLERuntimeError => e
107
- raise Error, e # Re-raise as ProcTable::Error
108
+ rescue WIN32OLERuntimeError => err
109
+ raise Error, err # Re-raise as ProcTable::Error
108
110
  else
109
- wmi.InstancesOf("Win32_Process").each{ |wproc|
110
- if pid
111
- next unless wproc.ProcessId == pid
111
+ wmi.InstancesOf("Win32_Process").each do |wproc|
112
+ if pid && wproc.ProcessId != pid
113
+ next
112
114
  end
113
115
 
114
116
  # Some fields are added later, and so are nil initially
@@ -117,7 +119,7 @@ module Sys
117
119
  nil, # Added later, based on OS version
118
120
  wproc.Name,
119
121
  wproc.CreationClassName,
120
- self.parse_ms_date(wproc.CreationDate),
122
+ parse_ms_date(wproc.CreationDate),
121
123
  wproc.CSCreationClassName,
122
124
  wproc.CSName,
123
125
  wproc.Description,
@@ -125,40 +127,40 @@ module Sys
125
127
  wproc.ExecutionState,
126
128
  wproc.Handle,
127
129
  wproc.HandleCount,
128
- self.parse_ms_date(wproc.InstallDate),
129
- self.convert(wproc.KernelModeTime),
130
+ parse_ms_date(wproc.InstallDate),
131
+ convert(wproc.KernelModeTime),
130
132
  wproc.MaximumWorkingSetSize,
131
133
  wproc.MinimumWorkingSetSize,
132
134
  wproc.Name,
133
135
  wproc.OSCreationClassName,
134
136
  wproc.OSName,
135
- self.convert(wproc.OtherOperationCount),
136
- self.convert(wproc.OtherTransferCount),
137
+ convert(wproc.OtherOperationCount),
138
+ convert(wproc.OtherTransferCount),
137
139
  wproc.PageFaults,
138
140
  wproc.PageFileUsage,
139
141
  wproc.ParentProcessId,
140
- self.convert(wproc.PeakPageFileUsage),
141
- self.convert(wproc.PeakVirtualSize),
142
- self.convert(wproc.PeakWorkingSetSize),
142
+ convert(wproc.PeakPageFileUsage),
143
+ convert(wproc.PeakVirtualSize),
144
+ convert(wproc.PeakWorkingSetSize),
143
145
  wproc.Priority,
144
- self.convert(wproc.PrivatePageCount),
146
+ convert(wproc.PrivatePageCount),
145
147
  wproc.ProcessId,
146
148
  wproc.QuotaNonPagedPoolUsage,
147
149
  wproc.QuotaPagedPoolUsage,
148
150
  wproc.QuotaPeakNonPagedPoolUsage,
149
151
  wproc.QuotaPeakPagedPoolUsage,
150
- self.convert(wproc.ReadOperationCount),
151
- self.convert(wproc.ReadTransferCount),
152
+ convert(wproc.ReadOperationCount),
153
+ convert(wproc.ReadTransferCount),
152
154
  wproc.SessionId,
153
155
  wproc.Status,
154
- self.parse_ms_date(wproc.TerminationDate),
156
+ parse_ms_date(wproc.TerminationDate),
155
157
  wproc.ThreadCount,
156
- self.convert(wproc.UserModeTime),
157
- self.convert(wproc.VirtualSize),
158
+ convert(wproc.UserModeTime),
159
+ convert(wproc.VirtualSize),
158
160
  wproc.WindowsVersion,
159
- self.convert(wproc.WorkingSetSize),
160
- self.convert(wproc.WriteOperationCount),
161
- self.convert(wproc.WriteTransferCount)
161
+ convert(wproc.WorkingSetSize),
162
+ convert(wproc.WriteOperationCount),
163
+ convert(wproc.WriteTransferCount)
162
164
  )
163
165
 
164
166
  ###############################################################
@@ -179,30 +181,32 @@ module Sys
179
181
  else
180
182
  array << struct
181
183
  end
182
- }
184
+ end
183
185
  end
184
186
 
185
187
  pid ? struct : array
186
188
  end
187
189
 
188
- private
189
-
190
190
  #######################################################################
191
191
  # Converts a string in the format '20040703074625.015625-360' into a
192
192
  # Ruby Time object.
193
193
  #######################################################################
194
194
  def self.parse_ms_date(str)
195
195
  return if str.nil?
196
- return DateTime.parse(str)
196
+ DateTime.parse(str)
197
197
  end
198
198
 
199
+ private_class_method :parse_ms_date
200
+
199
201
  #####################################################################
200
202
  # There is a bug in win32ole where uint64 types are returned as a
201
203
  # String instead of a Fixnum. This method deals with that for now.
202
204
  #####################################################################
203
205
  def self.convert(str)
204
206
  return nil if str.nil? # Return nil, not 0
205
- return str.to_i
207
+ str.to_i
206
208
  end
209
+
210
+ private_class_method :convert
207
211
  end
208
212
  end
data/spec/spec_helper.rb CHANGED
@@ -1,22 +1,13 @@
1
- RSpec.configure do |config|
2
- config.filter_run_excluding :skip_jruby if RUBY_PLATFORM == 'java'
3
- end
1
+ require 'rspec'
2
+ require 'sys-proctable'
3
+ require 'sys-top'
4
4
 
5
- if RUBY_PLATFORM == 'java'
6
- require 'ffi'
7
- module Exec
8
- extend FFI::Library
9
- ffi_lib FFI::Library::LIBC
10
- attach_function :fork, [], :int
11
- end
12
-
13
- def fork
14
- pid = Exec.fork
15
- if pid == 0
16
- yield if block_given?
17
- return nil
18
- else
19
- return pid
20
- end
21
- end
5
+ RSpec.configure do |config|
6
+ config.filter_run_excluding(:aix) unless RbConfig::CONFIG['host_os'] =~ /aix/i
7
+ config.filter_run_excluding(:darwin) unless RbConfig::CONFIG['host_os'] =~ /mac|darwin/i
8
+ config.filter_run_excluding(:linux) unless RbConfig::CONFIG['host_os'] =~ /linux/i
9
+ config.filter_run_excluding(:freebsd) unless RbConfig::CONFIG['host_os'] =~ /freebsd/i
10
+ config.filter_run_excluding(:sunos) unless RbConfig::CONFIG['host_os'] =~ /sunos|solaris/i
11
+ config.filter_run_excluding(:windows) unless Gem.win_platform?
12
+ config.filter_run_excluding(:jruby) if RUBY_PLATFORM == 'java'
22
13
  end
@@ -2,13 +2,11 @@
2
2
  # sys_proctable_aix_spec.rb
3
3
  #
4
4
  # Test suite for the AIX version of the sys-proctable library. You
5
- # should run these tests via the 'rake test' task.
5
+ # should run these tests via the 'rake spec' task.
6
6
  #######################################################################
7
- require 'rspec'
8
- require 'sys/proctable'
9
- require_relative 'sys_proctable_all_spec'
7
+ require 'spec_helper'
10
8
 
11
- describe Sys::ProcTable do
9
+ RSpec.describe Sys::ProcTable, :aix do
12
10
  let(:fields){
13
11
  %w[
14
12
  addr argc argv bindpro cid clname cmd_args cmdline cwd egid environ
@@ -30,8 +28,8 @@ describe Sys::ProcTable do
30
28
  end
31
29
 
32
30
  @myenv = ENV.to_hash
33
- @p1args = %w/aix-child.rb testing how well this works 1/
34
- @p2args = %w/aix-child.rb testing how well this works 2/
31
+ @p1args = %w[aix-child.rb testing how well this works 1]
32
+ @p2args = %w[aix-child.rb testing how well this works 2]
35
33
 
36
34
  @pid1 = fork do
37
35
  exec('ruby', *@p1args)
@@ -61,212 +59,212 @@ describe Sys::ProcTable do
61
59
  File.unlink('aix-child.rb') rescue nil
62
60
  end
63
61
 
64
- context "fields singleton method" do
65
- it "responds to a fields method" do
62
+ context 'fields singleton method' do
63
+ it 'responds to a fields method' do
66
64
  expect(described_class).to respond_to(:fields)
67
65
  end
68
66
 
69
- it "returns the expected results for the fields method" do
67
+ it 'returns the expected results for the fields method' do
70
68
  expect(described_class.fields).to be_kind_of(Array)
71
69
  expect(described_class.fields).to eql(fields)
72
70
  end
73
71
  end
74
72
 
75
- context "ProcTable::Struct members" do
76
- it "contains a flag member and returns the expected value" do
73
+ context 'ProcTable::Struct members' do
74
+ it 'contains a flag member and returns the expected value' do
77
75
  expect(@p1info).to respond_to(:flag)
78
- expect(@p1info.flag).to be_kind_of(Fixnum)
76
+ expect(@p1info.flag).to be_kind_of(Integer)
79
77
  end
80
78
 
81
- it "contains a flag2 member and returns the expected value" do
79
+ it 'contains a flag2 member and returns the expected value' do
82
80
  expect(@p1info).to respond_to(:flag2)
83
- expect(@p1info.flag2).to be_kind_of(Fixnum)
81
+ expect(@p1info.flag2).to be_kind_of(Integer)
84
82
  end
85
83
 
86
- it "contains a nlwp member and returns the expected value" do
84
+ it 'contains a nlwp member and returns the expected value' do
87
85
  expect(@p1info).to respond_to(:nlwp)
88
- expect(@p1info.nlwp).to be_kind_of(Fixnum)
86
+ expect(@p1info.nlwp).to be_kind_of(Integer)
89
87
  end
90
88
 
91
- it "contains a uid member and returns the expected value" do
89
+ it 'contains a uid member and returns the expected value' do
92
90
  expect(@p1info).to respond_to(:uid)
93
91
  expect(@p1info.uid).to be_kind_of(Integer)
94
- expect(@p1info.uid).to eql(@p1info.uid)
92
+ expect(@p1info.uid).to eql(Process.uid)
95
93
  end
96
94
 
97
- it "contains a euid member and returns the expected value" do
95
+ it 'contains a euid member and returns the expected value' do
98
96
  expect(@p1info).to respond_to(:euid)
99
97
  expect(@p1info.euid).to be_kind_of(Integer)
100
- expect(@p1info.euid).to eql(@p1info.euid)
98
+ expect(@p1info.euid).to eql(Process.euid)
101
99
  end
102
100
 
103
- it "contains a gid member and returns the expected value" do
101
+ it 'contains a gid member and returns the expected value' do
104
102
  expect(@p1info).to respond_to(:gid)
105
103
  expect(@p1info.gid).to be_kind_of(Integer)
106
- expect(@p1info.gid).to eql(@p1info.gid)
104
+ expect(@p1info.gid).to eql(Process.gid)
107
105
  end
108
106
 
109
- it "contains a egid member and returns the expected value" do
107
+ it 'contains a egid member and returns the expected value' do
110
108
  expect(@p1info).to respond_to(:egid)
111
109
  expect(@p1info.egid).to be_kind_of(Integer)
112
- expect(@p1info.egid).to eql(@p1info.egid)
110
+ expect(@p1info.egid).to eql(Process.egid)
113
111
  end
114
112
 
115
- it "contains a pid member and returns the expected value" do
113
+ it 'contains a pid member and returns the expected value' do
116
114
  expect(@p1info).to respond_to(:pid)
117
115
  expect(@p1info.pid).to be_kind_of(Integer)
118
- expect(@p1info.pid).to eql(@p1info.pid)
116
+ expect(@p1info.pid).to eql(@pid1)
119
117
  end
120
118
 
121
- it "contains a ppid member and returns the expected value" do
119
+ it 'contains a ppid member and returns the expected value' do
122
120
  expect(@p1info).to respond_to(:ppid)
123
121
  expect(@p1info.ppid).to be_kind_of(Integer)
124
- expect(@p1info.ppid).to eql(@p1info.ppid)
122
+ expect(@p1info.ppid).to eql(Process.pid)
125
123
  end
126
124
 
127
- it "contains a pgid member and returns the expected value" do
125
+ it 'contains a pgid member and returns the expected value' do
128
126
  expect(@p1info).to respond_to(:pgid)
129
127
  expect(@p1info.pgid).to be_kind_of(Integer)
130
- expect(@p1info.pgid).to eql(@p1info.pgid)
128
+ expect(@p1info.pgid).to eql(Process.getpgrp)
131
129
  end
132
130
 
133
- it "contains a sid member and returns the expected value" do
131
+ it 'contains a sid member and returns the expected value' do
134
132
  expect(@p1info).to respond_to(:sid)
135
133
  expect(@p1info.sid).to be_kind_of(Integer)
136
134
  end
137
135
 
138
- it "contains a ttydev member and returns the expected value" do
136
+ it 'contains a ttydev member and returns the expected value' do
139
137
  expect(@p1info).to respond_to(:ttydev)
140
138
  expect(@p1info.ttydev).to be_kind_of(Integer)
141
139
  end
142
140
 
143
- it "contains a addr member and returns the expected value" do
141
+ it 'contains a addr member and returns the expected value' do
144
142
  expect(@p1info).to respond_to(:addr)
145
143
  expect(@p1info.addr).to be_kind_of(Integer)
146
144
  end
147
145
 
148
- it "contains a size member and returns the expected value" do
146
+ it 'contains a size member and returns the expected value' do
149
147
  expect(@p1info).to respond_to(:size)
150
148
  expect(@p1info.size).to be_kind_of(Integer)
151
149
  end
152
150
 
153
- it "contains a rssize member and returns the expected value" do
151
+ it 'contains a rssize member and returns the expected value' do
154
152
  expect(@p1info).to respond_to(:rssize)
155
153
  expect(@p1info.rssize).to be_kind_of(Integer)
156
154
  end
157
155
 
158
- it "contains a start member and returns the expected value" do
156
+ it 'contains a start member and returns the expected value' do
159
157
  expect(@p1info).to respond_to(:start)
160
158
  expect(@p1info.start).to be_kind_of(Time)
161
159
  end
162
160
 
163
- it "contains a time member and returns the expected value" do
161
+ it 'contains a time member and returns the expected value' do
164
162
  expect(@p1info).to respond_to(:time)
165
163
  expect(@p1info.time).to be_kind_of(Time)
166
164
  end
167
165
 
168
- it "contains a cid member and returns the expected value" do
166
+ it 'contains a cid member and returns the expected value' do
169
167
  expect(@p1info).to respond_to(:cid)
170
- expect(@p1info.cid).to be_kind_of(Fixnum)
168
+ expect(@p1info.cid).to be_kind_of(Integer)
171
169
  end
172
170
 
173
- it "contains an argc member and returns the expected value" do
171
+ it 'contains an argc member and returns the expected value' do
174
172
  expect(@p1info).to respond_to(:argc)
175
- expect(@p1info.argc).to be_kind_of(Fixnum)
173
+ expect(@p1info.argc).to be_kind_of(Integer)
176
174
  expect(@p1info.argc).to eql(@p1args.size + 1)
177
175
  end
178
176
 
179
- it "contains an argv member and returns the expected value" do
177
+ it 'contains an argv member and returns the expected value' do
180
178
  expect(@p1info).to respond_to(:argv)
181
179
  expect(@p1info.argv).to be_kind_of(Integer)
182
180
  end
183
181
 
184
- it "contains an envp member and returns the expected value" do
182
+ it 'contains an envp member and returns the expected value' do
185
183
  expect(@p1info).to respond_to(:envp)
186
184
  expect(@p1info.envp).to be_kind_of(Integer)
187
185
  end
188
186
 
189
- it "contains an fname member and returns the expected value" do
187
+ it 'contains an fname member and returns the expected value' do
190
188
  expect(@p1info).to respond_to(:fname)
191
189
  expect(@p1info.fname).to be_kind_of(String)
192
190
  end
193
191
 
194
- it "contains an fname member and returns the expected value" do
192
+ it 'contains an psargs member and returns the expected value' do
195
193
  expect(@p1info).to respond_to(:psargs)
196
194
  expect(@p1info.psargs).to be_kind_of(String)
197
195
  end
198
196
 
199
- it "contains an lwpid member and returns the expected value" do
197
+ it 'contains an lwpid member and returns the expected value' do
200
198
  expect(@p1info).to respond_to(:lwpid)
201
199
  expect(@p1info.lwpid).to be_kind_of(Integer)
202
200
  end
203
201
 
204
- it "contains a wchan member and returns the expected value" do
202
+ it 'contains a wchan member and returns the expected value' do
205
203
  expect(@p1info).to respond_to(:wchan)
206
204
  expect(@p1info.wchan).to be_kind_of(Integer)
207
205
  end
208
206
 
209
- it "contains a wtype member and returns the expected value" do
207
+ it 'contains a wtype member and returns the expected value' do
210
208
  expect(@p1info).to respond_to(:wtype)
211
- expect(@p1info.wchan).to be_kind_of(Fixnum)
209
+ expect(@p1info.wchan).to be_kind_of(Integer)
212
210
  end
213
211
 
214
- it "contains a state member and returns the expected value" do
212
+ it 'contains a state member and returns the expected value' do
215
213
  expect(@p1info).to respond_to(:state)
216
- expect(@p1info.state).to be_kind_of(Fixnum)
214
+ expect(@p1info.state).to be_kind_of(Integer)
217
215
  end
218
216
 
219
- it "contains an sname member and returns the expected value" do
217
+ it 'contains an sname member and returns the expected value' do
220
218
  expect(@p1info).to respond_to(:sname)
221
219
  expect(@p1info.sname).to be_kind_of(String)
222
220
  end
223
221
 
224
- it "contains a nice member and returns the expected value" do
222
+ it 'contains a nice member and returns the expected value' do
225
223
  expect(@p1info).to respond_to(:nice)
226
- expect(@p1info.nice).to be_kind_of(Fixnum)
224
+ expect(@p1info.nice).to be_kind_of(Integer)
227
225
  end
228
226
 
229
- it "contains a pri member and returns the expected value" do
227
+ it 'contains a pri member and returns the expected value' do
230
228
  expect(@p1info).to respond_to(:pri)
231
- expect(@p1info.pri).to be_kind_of(Fixnum)
229
+ expect(@p1info.pri).to be_kind_of(Integer)
232
230
  end
233
231
 
234
- it "contains a policy member and returns the expected value" do
232
+ it 'contains a policy member and returns the expected value' do
235
233
  expect(@p1info).to respond_to(:policy)
236
- expect(@p1info.policy).to be_kind_of(Fixnum)
234
+ expect(@p1info.policy).to be_kind_of(Integer)
237
235
  end
238
236
 
239
- it "contains a clname member and returns the expected value" do
237
+ it 'contains a clname member and returns the expected value' do
240
238
  expect(@p1info).to respond_to(:clname)
241
239
  expect(@p1info.clname).to be_kind_of(String)
242
240
  end
243
241
 
244
- it "contains an onpro member and returns the expected value" do
242
+ it 'contains an onpro member and returns the expected value' do
245
243
  expect(@p1info).to respond_to(:onpro)
246
- expect(@p1info.onpro).to be_kind_of(Fixnum)
244
+ expect(@p1info.onpro).to be_kind_of(Integer)
247
245
  end
248
246
 
249
- it "contains a bindpro member and returns the expected value" do
247
+ it 'contains a bindpro member and returns the expected value' do
250
248
  expect(@p1info).to respond_to(:bindpro)
251
- expect(@p1info.bindpro).to be_kind_of(Fixnum)
249
+ expect(@p1info.bindpro).to be_kind_of(Integer)
252
250
  end
253
251
 
254
- it "contains a ptid member and returns the expected value" do
252
+ it 'contains a ptid member and returns the expected value' do
255
253
  expect(@p1info).to respond_to(:ptid)
256
- expect(@p1info.ptid).to be_kind_of(Fixnum)
254
+ expect(@p1info.ptid).to be_kind_of(Integer)
257
255
  end
258
256
 
259
- it "contains a comm member and returns the expected value" do
257
+ it 'contains a comm member and returns the expected value' do
260
258
  expect(@p1info).to respond_to(:comm)
261
259
  expect(@p1info.comm).to be_kind_of(String)
262
260
  end
263
261
 
264
- it "contains a fd member and returns the expected value" do
262
+ it 'contains a fd member and returns the expected value' do
265
263
  expect(@p1info).to respond_to(:fd)
266
264
  expect(@p1info.fd).to be_kind_of(Array)
267
265
  end
268
266
 
269
- it "contains a cmd_args member and returns the expected value" do
267
+ it 'contains a cmd_args member and returns the expected value' do
270
268
  expect(@p1info).to respond_to(:cmd_args)
271
269
  expect(@p1info.cmd_args).to be_kind_of(Array)
272
270
  expect(@p1info.cmd_args).to_eql(['ruby', @p1args].flatten)
@@ -276,7 +274,7 @@ describe Sys::ProcTable do
276
274
  expect(@p2info.cmd_args).to_eql(['ruby', @p2args].flatten)
277
275
  end
278
276
 
279
- it "contains an environ member and returns the expected value" do
277
+ it 'contains an environ member and returns the expected value' do
280
278
  expect(@p1info).to respond_to(:environ)
281
279
  expect(@p1info.environ).to be_kind_of(Hash)
282
280
  expect(@p1info.environ).to eql(@myenv)
@@ -286,29 +284,29 @@ describe Sys::ProcTable do
286
284
  expect(@p2info.environ).to eql(@myenv)
287
285
  end
288
286
 
289
- it "contains a cmdline member and returns the expected value" do
287
+ it 'contains a cmdline member and returns the expected value' do
290
288
  expect(@p1info).to respond_to(:cmdline)
291
289
  expect(@p1info.cmdline).to be_kind_of(String)
292
290
  end
293
291
 
294
- it "contains a cwd member and returns the expected value" do
292
+ it 'contains a cwd member and returns the expected value' do
295
293
  expect(@p1info).to respond_to(:cwd)
296
294
  expect(@p1info.cwd).to be_kind_of(String) if @p1info.cwd
297
295
  end
298
296
 
299
- it "contains a map member and returns the expected value" do
297
+ it 'contains a map member and returns the expected value' do
300
298
  expect(@p1info).to respond_to(:map)
301
299
  expect(@p1info.map).to be_kind_of(Array) if @p1info.map
302
300
  end
303
301
  end
304
302
 
305
- context "Struct::ProcTableMapStruct" do
306
- it "contains the expected members" do
303
+ context 'Struct::ProcTableMapStruct' do
304
+ it 'contains the expected members' do
307
305
  expect(@p1info.map).to be_kind_of(Array)
308
306
  expect(map_fields.sort).to eql(@p1info.map[0].members.sort)
309
307
  end
310
308
 
311
- it "has members of the expected type" do
309
+ it 'has members of the expected type' do
312
310
  expect(@p1info.map).to be_kind_of(Array)
313
311
  p1info_map = @p1info.map
314
312