win32-taskscheduler 1.0.2 → 1.0.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 172397962621f9f6b292f086e64d849feb48a43f52e93beb70e0bbc966bb0b31
4
- data.tar.gz: bc1c199304f78f12f7b5a50b00c0b41273a4ecc990e0e1772c6bef37954429f5
3
+ metadata.gz: 6dbcf68915019eb0cfb32a2fdd4dc7b980dc5b51e9397c5c8dd3ad995a4ff7fc
4
+ data.tar.gz: e7bc4c68a739975f08a6e726591804e48df0e9d8622d164fdc37818fb615b798
5
5
  SHA512:
6
- metadata.gz: 68d08019224864d016bc01959287600923f5e452d3b44fe08722d5785d6bc95f966b5f9d6b17ddbd1e1a22bba393b108086363bd91aa37d5821dd497b3cb9ed0
7
- data.tar.gz: 716004b5d40cbbb80a0036213411947644f54f2753fbb821937c043f34f596f3311976cf57f5a6521f1e97b4a6d26053a7b4f8c840eaae6192ef2b1ead1ab584
6
+ metadata.gz: b720b93bfa6fd8628872567f64a82bc6777a707cdf9b0e92de1aca4982b2aa9d79eea3e312cdb3e41e0e8695cb7a807a5e941b20fb9023777613604875993b65
7
+ data.tar.gz: 6e66aa9252904dfc6ece10372b9c26ea433efbd7f1d20868b032e760a6b783487a14cbd915519f68c4af9b91f0a4a9a783bdf67ca1ae0a07ead4f0370e9d8564
data/CHANGELOG.md CHANGED
@@ -1,14 +1,36 @@
1
1
  # win32-taskscheduler change log
2
2
 
3
- Note: this log contains only changes from win32-taskscheduler release 0.4.1 and later
3
+ Note: this log contains only changes from win32-taskscheduler release 0.4.0 and later
4
4
  -- it does not contain the changes from prior releases. To view change history
5
- prior to release 0.4.1, please visit the [source repository](https://github.com/chef/win32-taskscheduler/commits).
5
+ prior to release 0.4.0, please visit the [source repository](https://github.com/chef/win32-taskscheduler/commits).
6
6
 
7
- <!-- latest_release -->
7
+ <!-- latest_release 1.0.9 -->
8
+ ## [win32-taskscheduler-1.0.9](https://github.com/chef/win32-taskscheduler/tree/win32-taskscheduler-1.0.9) (2018-07-23)
9
+
10
+ #### Merged Pull Requests
11
+ - Fix priority should return unique value. [#60](https://github.com/chef/win32-taskscheduler/pull/60) ([Vasu1105](https://github.com/Vasu1105))
8
12
  <!-- latest_release -->
9
13
 
14
+ <!-- release_rollup since=1.0.2 -->
15
+ ### Changes since 1.0.2 release
16
+
17
+ #### Merged Pull Requests
18
+ - Fix priority should return unique value. [#60](https://github.com/chef/win32-taskscheduler/pull/60) ([Vasu1105](https://github.com/Vasu1105)) <!-- 1.0.9 -->
19
+ - Add DisallowStartIfOnBatteries and StopIfGoingOnBatteries task configs [#61](https://github.com/chef/win32-taskscheduler/pull/61) ([dheerajd-msys](https://github.com/dheerajd-msys)) <!-- 1.0.8 -->
20
+ - [MSYS-827] Add functional test cases [#58](https://github.com/chef/win32-taskscheduler/pull/58) ([Nimesh-Msys](https://github.com/Nimesh-Msys)) <!-- 1.0.7 -->
21
+ - Remove the Manifest file and add a gitignore file [#57](https://github.com/chef/win32-taskscheduler/pull/57) ([tas50](https://github.com/tas50)) <!-- 1.0.6 -->
22
+ - Add github templates and codeowners file [#52](https://github.com/chef/win32-taskscheduler/pull/52) ([tas50](https://github.com/tas50)) <!-- 1.0.5 -->
23
+ - Remove the cert [#53](https://github.com/chef/win32-taskscheduler/pull/53) ([tas50](https://github.com/tas50)) <!-- 1.0.4 -->
24
+ - MSYS-835 Setup appveyor [#55](https://github.com/chef/win32-taskscheduler/pull/55) ([Vasu1105](https://github.com/Vasu1105)) <!-- 1.0.3 -->
10
25
  <!-- release_rollup -->
11
- <!-- release_rollup -->
26
+
27
+ <!-- latest_stable_release -->
28
+ ## [win32-taskscheduler-1.0.2](https://github.com/chef/win32-taskscheduler/tree/win32-taskscheduler-1.0.2) (2018-06-13)
29
+ - Fix for exists? method returning false for task without full path. [#43](https://github.com/chef/win32-taskscheduler/pull/43)([#Vasu1105](https://github.com/Vasu1105))
30
+ - Fix to set user information at the time of creation of task. [#42](https://github.com/chef/win32-taskscheduler/pull/42)([#Vasu1105](https://github.com/Vasu1105))
31
+ - Fix exists? method to find task in given path and if path or folder not present return false. [#40](https://github.com/chef/win32-taskscheduler/pull/40)([#Vasu1105](https://github.com/Vasu1105))
32
+ - Fix for undefined method nil:Nilclass error when force flag is passed to create folder. [#32](https://github.com/chef/win32-taskscheduler/pull/32)([#Vasu1105](https://github.com/Vasu1105))
33
+
12
34
 
13
35
  <!-- latest_stable_release -->
14
36
 
@@ -16,8 +38,6 @@ prior to release 0.4.1, please visit the [source repository](https://github.com/
16
38
  - Fix the issue of "no mapping" while creating Windows task for SYSTEM USERS. [#30](https://github.com/chef/win32-taskscheduler/pull/30) ([#NAshwini](https://github.com/NAshwini))
17
39
  - Fix for not to set start time if not set if its 0000-00-00T00:00:00 [#29][(https://github.com/chef/win32-taskscheduler/pull/29) ([#Vasu1105](https://github.com/Vasu1105))
18
40
 
19
- <!-- latest_stable_release -->
20
-
21
41
 
22
42
  ## [v0.4.0](https://github.com/chef/win32-taskscheduler/tree/win32-taskscheduler-0.4.0) (5-Apr-2018)
23
43
  - Updated code to create task without trigger. [#25](https://github.com/chef/win32-taskscheduler/pull/25) ([#Vasu1105](https://github.com/Vasu1105))
@@ -140,4 +160,4 @@ prior to release 0.4.1, please visit the [source repository](https://github.com/
140
160
  accidentally omitted previously.
141
161
 
142
162
  ## [v0.0.1](24-Apr-2004)
143
- - Initial release
163
+ - Initial release
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rake'
2
2
  require 'rake/clean'
3
3
  require 'rake/testtask'
4
+ require 'rspec/core/rake_task'
4
5
 
5
6
  CLEAN.include("**/*.gem", "**/*.rbc")
6
7
 
@@ -24,6 +25,10 @@ task :example do
24
25
  ruby '-Iib examples/taskscheduler_example.rb'
25
26
  end
26
27
 
28
+ RSpec::Core::RakeTask.new(:spec) do |spec|
29
+ spec.pattern = FileList["spec/**/*_spec.rb", "spec/**/**/*_spec.rb"].to_a
30
+ end
31
+
27
32
  desc 'Run the test suite for the win32-taskscheduler library'
28
33
  Rake::TestTask.new do |t|
29
34
  t.verbose = true
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.9
data/appveyor.yml ADDED
@@ -0,0 +1,40 @@
1
+ version: "ole-{build}"
2
+
3
+ os: Windows server 2012
4
+ platform:
5
+ - x64
6
+
7
+ cache:
8
+ - vendor/bundle
9
+
10
+ clone_folder: c:\projects\win32-taskscheduler
11
+ clone_depth: 1
12
+ branches:
13
+ only:
14
+ - ole
15
+
16
+ environment:
17
+ matrix:
18
+ - ruby_version: "25-x64"
19
+ - ruby_version: "25"
20
+ - ruby_version: "24-x64"
21
+ - ruby_version: "24"
22
+
23
+ install:
24
+ - systeminfo
25
+ - winrm quickconfig -q
26
+ - SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
27
+ - echo %PATH%
28
+ - bundle config --local path vendor/bundle # use the cache we define above
29
+
30
+ build_script:
31
+ - bundle install || bundle install || bundle install
32
+
33
+ before_test:
34
+ - ruby --version
35
+ - gem --version
36
+ - bundler --version
37
+ - bundle env
38
+
39
+ test_script:
40
+ - bundle exec rake spec
@@ -121,6 +121,11 @@ module Win32
121
121
  attr_accessor :password
122
122
  attr_reader :host
123
123
 
124
+
125
+ def root_path(path = '\\')
126
+ path
127
+ end
128
+
124
129
  # Returns a new TaskScheduler object, attached to +folder+. If that
125
130
  # folder does not exist, but the +force+ option is set to true, then
126
131
  # it will be created. Otherwise an error will be raised. The default
@@ -129,7 +134,7 @@ module Win32
129
134
  # If +task+ and +trigger+ are present, then a new task is generated
130
135
  # as well. This is effectively the same as .new + #new_work_item.
131
136
  #
132
- def initialize(task = nil, trigger = nil, folder = "\\", force = false)
137
+ def initialize(task = nil, trigger = nil, folder = root_path, force = false)
133
138
  @folder = folder
134
139
  @force = force
135
140
 
@@ -151,14 +156,13 @@ module Win32
151
156
 
152
157
  @service.Connect
153
158
 
154
- if folder != "\\"
159
+ if folder != root_path
155
160
  begin
156
161
  @root = @service.GetFolder(folder)
157
162
  rescue WIN32OLERuntimeError => err
158
163
  if force
159
- @root = @service.GetFolder("\\")
160
- @root.CreateFolder(folder)
161
- @root = @service.GetFolder(folder)
164
+ @root = @service.GetFolder(root_path)
165
+ @root = @root.CreateFolder(folder)
162
166
  else
163
167
  raise ArgumentError, "folder '#{folder}' not found"
164
168
  end
@@ -197,12 +201,13 @@ module Win32
197
201
 
198
202
  # Used the splat operator to put all folder elements into path and leave only the task name`
199
203
  if full_task_path.include?("\\")
200
- *path, task_name = full_task_path.split("\\")
204
+ full_task_path = root_path + full_task_path
205
+ *path, task_name = full_task_path.split('\\')
201
206
  else
202
207
  task_name = full_task_path
203
208
  end
204
209
 
205
- folder = path.nil? ? "\\" : path.join("\\")
210
+ folder = path.nil? ? root_path : path.join("\\")
206
211
 
207
212
  begin
208
213
  root = @service.GetFolder(folder)
@@ -461,9 +466,9 @@ module Win32
461
466
  dir
462
467
  end
463
468
 
464
- # Returns the task's priority level. Possible values are 'idle',
465
- # 'normal', 'high', 'realtime', 'below_normal', 'above_normal',
466
- # and 'unknown'.
469
+ # Returns the task's priority level. Possible values are 'idle', 'lowest'.
470
+ # 'below_normal_8', 'below_normal_7', 'normal_6', 'normal_5', 'normal_4',
471
+ # 'above_normal_3', 'above_normal_2', 'highest', 'critical' and 'unknown'.
467
472
  #
468
473
  def priority
469
474
  check_for_active_task
@@ -474,19 +479,19 @@ module Win32
474
479
  when 1
475
480
  priority = 'highest'
476
481
  when 2
477
- priority = 'above_normal'
482
+ priority = 'above_normal_2'
478
483
  when 3
479
- priority = 'above_normal'
484
+ priority = 'above_normal_3'
480
485
  when 4
481
- priority = 'normal'
486
+ priority = 'normal_4'
482
487
  when 5
483
- priority = 'normal'
488
+ priority = 'normal_5'
484
489
  when 6
485
- priority = 'normal'
490
+ priority = 'normal_6'
486
491
  when 7
487
- priority = 'below_normal'
492
+ priority = 'below_normal_7'
488
493
  when 8
489
- priority = 'below_normal'
494
+ priority = 'below_normal_8'
490
495
  when 9
491
496
  priority = 'lowest'
492
497
  when 10
@@ -1178,7 +1183,7 @@ module Win32
1178
1183
  definition.Settings.AllowHardTerminate = allow_hard_terminate if allow_hard_terminate
1179
1184
  definition.Settings.Compatibility = compatibility if compatibility
1180
1185
  definition.Settings.DeleteExpiredTaskAfter = delete_expired_task_after if delete_expired_task_after
1181
- definition.Settings.DisallowStartIfOnBatteries = disallow_start_if_on_batteries if disallow_start_if_on_batteries
1186
+ definition.Settings.DisallowStartIfOnBatteries = disallow_start_if_on_batteries if !disallow_start_if_on_batteries.nil?
1182
1187
  definition.Settings.Enabled = enabled if enabled
1183
1188
  definition.Settings.ExecutionTimeLimit = execution_time_limit if execution_time_limit
1184
1189
  definition.Settings.Hidden = hidden if hidden
@@ -1193,7 +1198,7 @@ module Win32
1193
1198
  definition.Settings.RunOnlyIfIdle = run_only_if_idle if run_only_if_idle
1194
1199
  definition.Settings.RunOnlyIfNetworkAvailable = run_only_if_network_available if run_only_if_network_available
1195
1200
  definition.Settings.StartWhenAvailable = start_when_available if start_when_available
1196
- definition.Settings.StopIfGoingOnBatteries = stop_if_going_on_batteries if stop_if_going_on_batteries
1201
+ definition.Settings.StopIfGoingOnBatteries = stop_if_going_on_batteries if !stop_if_going_on_batteries.nil?
1197
1202
  definition.Settings.WakeToRun = wake_to_run if wake_to_run
1198
1203
  definition.Settings.XmlText = xml_text if xml_text
1199
1204
 
@@ -1311,6 +1316,10 @@ module Win32
1311
1316
  symbolize_keys(settings_hash)
1312
1317
  end
1313
1318
 
1319
+ def task_user_id(definition)
1320
+ definition.Principal.UserId
1321
+ end
1322
+
1314
1323
  private
1315
1324
 
1316
1325
  # Returns a camle-case string to its underscore format
@@ -1342,8 +1351,7 @@ module Win32
1342
1351
  end
1343
1352
 
1344
1353
  def update_task_definition(definition)
1345
- user = definition.Principal.UserId
1346
-
1354
+ user = task_user_id(definition) || 'SYSTEM'
1347
1355
  @task = @root.RegisterTaskDefinition(
1348
1356
  @task.Path,
1349
1357
  definition,
@@ -1,6 +1,6 @@
1
1
  module Win32
2
2
  class TaskScheduler
3
3
  # The version of the win32-taskscheduler library
4
- VERSION = '1.0.2'.freeze
4
+ VERSION = '1.0.9'.freeze
5
5
  end
6
6
  end
@@ -0,0 +1,829 @@
1
+ require 'spec_helper'
2
+ require 'win32ole'
3
+ require 'win32/taskscheduler'
4
+
5
+ RSpec.describe Win32::TaskScheduler, :windows_only do
6
+ before { create_test_folder }
7
+ after { clear_them }
8
+ before { load_task_variables }
9
+
10
+ describe '#constructor' do
11
+ let(:ts) { Win32::TaskScheduler }
12
+ context 'Task' do
13
+ it 'Does not creates task when default(nil)' do
14
+ task = nil
15
+ expect(ts.new(task)).to be_a(ts)
16
+ expect(no_of_tasks).to eq(0)
17
+ end
18
+
19
+ it 'Does not creates task without trigger' do
20
+ expect(ts.new(@task)).to be_a(ts)
21
+ expect(no_of_tasks).to eq(0)
22
+ end
23
+
24
+ it 'Creates a task with trigger' do
25
+ expect(ts.new(@task, @trigger)).to be_a(ts)
26
+ expect(no_of_tasks).to eq(1)
27
+ end
28
+ end
29
+
30
+ context 'Trigger' do
31
+ it 'Does not creates task when default(nil)' do
32
+ trigger = nil
33
+ expect(ts.new(@task, trigger)).to be_a(ts)
34
+ expect(no_of_tasks).to eq(0)
35
+ end
36
+
37
+ it 'Raises error without trigger type' do
38
+ @trigger[:trigger_type] = nil
39
+ expect { ts.new(@task, @trigger) }.to raise_error(ArgumentError)
40
+ expect(no_of_tasks).to eq(0)
41
+ end
42
+
43
+ it 'Creates a task with trigger type' do
44
+ expect(ts.new(@task, @trigger)).to be_a(ts)
45
+ expect(no_of_tasks).to eq(1)
46
+ end
47
+ end
48
+
49
+ context 'Folder' do
50
+ let(:folder) { '\\Foo' }
51
+ it 'Raises error when nil' do
52
+ expect { ts.new(@task, @trigger, nil) }.to raise_error(NoMethodError)
53
+ expect(no_of_tasks).to eq(0)
54
+ end
55
+
56
+ it 'Raises error when path separators(\\\) are absent' do
57
+ invalid_path = 'Foo'
58
+ expect { ts.new(@task, @trigger, invalid_path) }.to raise_error(ArgumentError)
59
+ expect(no_of_tasks).to eq(0)
60
+ end
61
+
62
+ it 'Creates a task when default(root)' do
63
+ expect(ts.new(@task, @trigger, @folder)).to be_a(ts)
64
+ expect(no_of_tasks).to eq(1)
65
+ end
66
+
67
+ context 'When force is true' do
68
+ let(:force) { true }
69
+ it 'Creates a task at specified folder' do
70
+ expect(ts.new(@task, @trigger, folder, force)).to be_a(ts)
71
+ expect(no_of_tasks(folder)).to eq(1)
72
+ end
73
+ end
74
+
75
+ context 'When force is false' do
76
+ let(:force) { false }
77
+ it 'Raises an error when folder does not exists' do
78
+ expect { ts.new(@task, @trigger, folder, force) }.to raise_error(ArgumentError)
79
+ end
80
+
81
+ it 'Creates a task at specified folder if it exists' do
82
+ new_folder = @test_folder.CreateFolder(folder)
83
+ expect(ts.new(@task, @trigger, new_folder.Path, force)).to be_a(ts)
84
+ expect(no_of_tasks(folder)).to eq(1)
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ describe '#tasks' do
91
+ before { create_task }
92
+ it 'Returns Task Names' do
93
+ expect(@ts.tasks).to include(@task)
94
+ end
95
+
96
+ it 'is an alias with enum' do
97
+ expect(@ts.enum).to include(@task)
98
+ end
99
+ end
100
+
101
+ describe '#exists?' do
102
+ let(:folder) { '\\Foo' }
103
+ let(:force) { true }
104
+ before { create_task }
105
+
106
+ context 'valid task path returns true' do
107
+ it 'at root' do
108
+ expect(@ts.exists?(@task)).to be_truthy
109
+ end
110
+
111
+ it 'at nested folder' do
112
+ @ts = Win32::TaskScheduler.new(@task, @trigger, folder, force)
113
+ task = folder + '\\' + @task
114
+ expect(@ts.exists?(task)).to be_truthy
115
+ end
116
+ end
117
+
118
+ context 'invalid task path returns false' do
119
+ it 'at root' do
120
+ expect(@ts.exists?('invalid')).to be_falsy
121
+ end
122
+
123
+ it 'at nested folder' do
124
+ @ts = Win32::TaskScheduler.new(@task, @trigger, folder, force)
125
+ task = folder + '\\' + 'invalid'
126
+ expect(@ts.exists?(task)).to be_falsy
127
+ end
128
+ end
129
+ end
130
+
131
+ describe '#get_task' do
132
+ let(:folder) { '\\Foo' }
133
+ let(:force) { true }
134
+ before { create_task }
135
+
136
+ context 'from root' do
137
+ it 'Returns the Task if exists' do
138
+ expect(@ts.get_task(@task)).to be_a(@current_task.class)
139
+ end
140
+
141
+ it 'Raises an error if task does not exists' do
142
+ expect { @ts.get_task('invalid') }.to raise_error(tasksch_err)
143
+ end
144
+ end
145
+
146
+ context 'from nested folder' do
147
+ it 'Returns the Task if exists' do
148
+ skip 'Implementation Pending'
149
+ end
150
+
151
+ it 'Raises an error if task does not exists' do
152
+ skip 'Implementation Pending'
153
+ end
154
+ end
155
+ end
156
+
157
+ describe '#activate' do
158
+ let(:folder) { '\\Foo' }
159
+ let(:force) { true }
160
+ before { create_task }
161
+
162
+ context 'from root' do
163
+ it 'Activates the Task if exists' do
164
+ expect(@ts.activate(@task)).to be_a(@current_task.class)
165
+ expect(@current_task.Enabled).to be_truthy
166
+ end
167
+
168
+ it 'Raises an error if task does not exists' do
169
+ expect { @ts.activate('invalid') }.to raise_error(tasksch_err)
170
+ end
171
+ end
172
+
173
+ context 'from nested folder' do
174
+ it 'Activates the Task if exists' do
175
+ skip 'Implementation Pending'
176
+ end
177
+
178
+ it 'Raises an error if task does not exists' do
179
+ skip 'Implementation Pending'
180
+ end
181
+ end
182
+ end
183
+
184
+ describe '#delete' do
185
+ let(:folder) { '\\Foo' }
186
+ let(:force) { true }
187
+ before { create_task }
188
+
189
+ context 'from root' do
190
+ it 'Deletes the Task if exists' do
191
+ expect(no_of_tasks).to eq(1)
192
+ @ts.delete(@task)
193
+ expect(no_of_tasks).to eq(0)
194
+ end
195
+
196
+ it 'Raises an error if task does not exists' do
197
+ expect { @ts.delete('invalid') }.to raise_error(tasksch_err)
198
+ end
199
+ end
200
+
201
+ context 'from nested folder' do
202
+ it 'Activates the Task if exists' do
203
+ skip 'Implementation Pending'
204
+ end
205
+
206
+ it 'Raises an error if task does not exists' do
207
+ skip 'Implementation Pending'
208
+ end
209
+ end
210
+ end
211
+
212
+ describe '#run' do
213
+ before { create_task }
214
+ after { stop_the_app }
215
+
216
+ it 'Execute(Start) the Task' do
217
+ @ts.run
218
+ expect(@ts.status).to eq('running').
219
+ or eq('queued').
220
+ or eq('ready') # It takes time to load sometimes
221
+ end
222
+
223
+ it 'Raises an error if task does not exists' do
224
+ @ts.instance_variable_set(:@task, nil)
225
+ expect { @ts.run }.to raise_error(tasksch_err)
226
+ end
227
+ end
228
+
229
+ describe '#terminate' do
230
+ before { create_task }
231
+ after { stop_the_app }
232
+
233
+ it 'terminates the Task if exists' do
234
+ @ts.terminate
235
+ expect(@ts.status).to eq('ready')
236
+ end
237
+
238
+ it 'has an alias stop' do
239
+ @ts.stop
240
+ expect(@ts.status).to eq('ready')
241
+ end
242
+
243
+ it 'Raises an error if task does not exists' do
244
+ @ts.instance_variable_set(:@task, nil)
245
+ expect { @ts.terminate }.to raise_error(tasksch_err)
246
+ end
247
+ end
248
+
249
+ describe '#application_name' do
250
+ before { create_task }
251
+ it 'Returns the application name of task' do
252
+ check = @app
253
+ expect(@ts.application_name).to eq(check)
254
+ end
255
+
256
+ it 'Sets the application name of task' do
257
+ stub_user
258
+ check = 'cmd.exe'
259
+ expect(@ts.application_name = check).to eq(check)
260
+ expect(@ts.application_name).to eq(check)
261
+ end
262
+
263
+ it 'Raises an error if task does not exists' do
264
+ @ts.instance_variable_set(:@task, nil)
265
+ expect { @ts.application_name }.to raise_error(tasksch_err)
266
+ end
267
+ end
268
+
269
+ describe '#parameters' do
270
+ before { create_task }
271
+ it 'Returns the parameters of task' do
272
+ check = ''
273
+ expect(@ts.parameters).to eq(check)
274
+ end
275
+
276
+ it 'Sets the parameters of task' do
277
+ stub_user
278
+ check = 'cmd.exe'
279
+ expect(@ts.parameters = check).to eq(check)
280
+ expect(@ts.parameters).to eq(check)
281
+ end
282
+
283
+ it 'Raises an error if task does not exists' do
284
+ @ts.instance_variable_set(:@task, nil)
285
+ expect { @ts.parameters }.to raise_error(tasksch_err)
286
+ end
287
+ end
288
+
289
+ describe '#working_directory' do
290
+ before { create_task }
291
+ it 'Returns the working directory of task' do
292
+ check = ''
293
+ expect(@ts.working_directory).to eq(check)
294
+ end
295
+
296
+ it 'Sets the working directory of task' do
297
+ stub_user
298
+ check = Dir.pwd
299
+ expect(@ts.working_directory = check).to eq(check)
300
+ expect(@ts.working_directory).to eq(check)
301
+ end
302
+
303
+ it 'Raises an error if task does not exists' do
304
+ @ts.instance_variable_set(:@task, nil)
305
+ expect { @ts.working_directory }.to raise_error(tasksch_err)
306
+ end
307
+ end
308
+
309
+ describe '#priority' do
310
+ before { create_task }
311
+ it 'Returns default priority of task' do
312
+ check = 'below_normal_7'
313
+ expect(@ts.priority).to eq(check)
314
+ end
315
+
316
+ it '0 Sets the priority of task to critical' do
317
+ stub_user
318
+ check = 0
319
+ expect(@ts.priority = check).to eq(check)
320
+ expect(@ts.priority).to eq('critical')
321
+ end
322
+
323
+ it '1 Sets the priority of task to highest' do
324
+ stub_user
325
+ check = 1
326
+ expect(@ts.priority = check).to eq(check)
327
+ expect(@ts.priority).to eq('highest')
328
+ end
329
+
330
+ it '2 Sets the priority of task to above_normal_2' do
331
+ stub_user
332
+ check = 2
333
+ expect(@ts.priority = check).to eq(check)
334
+ expect(@ts.priority).to eq('above_normal_2')
335
+ end
336
+
337
+ it '3 Sets the priority of task to above_normal_3' do
338
+ stub_user
339
+ check = 3
340
+ expect(@ts.priority = check).to eq(check)
341
+ expect(@ts.priority).to eq('above_normal_3')
342
+ end
343
+
344
+ it '4 Sets the priority of task to normal_4' do
345
+ stub_user
346
+ check = 4
347
+ expect(@ts.priority = check).to eq(check)
348
+ expect(@ts.priority).to eq('normal_4')
349
+ end
350
+
351
+ it '5 Sets the priority of task to normal_5' do
352
+ stub_user
353
+ check = 5
354
+ expect(@ts.priority = check).to eq(check)
355
+ expect(@ts.priority).to eq('normal_5')
356
+ end
357
+
358
+ it '6 Sets the priority of task to normal_6' do
359
+ stub_user
360
+ check = 6
361
+ expect(@ts.priority = check).to eq(check)
362
+ expect(@ts.priority).to eq('normal_6')
363
+ end
364
+
365
+ it '7 Sets the priority of task to below_normal_7' do
366
+ stub_user
367
+ check = 7
368
+ expect(@ts.priority = check).to eq(check)
369
+ expect(@ts.priority).to eq('below_normal_7')
370
+ end
371
+
372
+ it '8 Sets the priority of task to below_normal_8' do
373
+ stub_user
374
+ check = 8
375
+ expect(@ts.priority = check).to eq(check)
376
+ expect(@ts.priority).to eq('below_normal_8')
377
+ end
378
+
379
+ it '9 Sets the priority of task to lowest' do
380
+ stub_user
381
+ check = 9
382
+ expect(@ts.priority = check).to eq(check)
383
+ expect(@ts.priority).to eq('lowest')
384
+ end
385
+
386
+ it '10 Sets the priority of task to idle' do
387
+ stub_user
388
+ check = 10
389
+ expect(@ts.priority = check).to eq(check)
390
+ expect(@ts.priority).to eq('idle')
391
+ end
392
+
393
+ it 'Raises an error if task does not exists' do
394
+ @ts.instance_variable_set(:@task, nil)
395
+ expect { @ts.priority }.to raise_error(tasksch_err)
396
+ end
397
+ end
398
+
399
+ describe '#comment' do
400
+ before { create_task }
401
+ it 'Returns the comment of task' do
402
+ check = 'Sample task for testing purpose'
403
+ expect(@ts.comment).to eq(check)
404
+ end
405
+
406
+ it 'Sets the Comment(Description) of task' do
407
+ stub_user
408
+ check = 'Description To Test'
409
+ expect(@ts.comment = check).to eq(check)
410
+ expect(@ts.comment).to eq(check)
411
+ end
412
+
413
+ it 'alias with Description' do
414
+ stub_user
415
+ check = 'Description To Test'
416
+ expect(@ts.description = check).to eq(check)
417
+ expect(@ts.comment).to eq(check)
418
+ end
419
+
420
+ it 'Raises an error if task does not exists' do
421
+ @ts.instance_variable_set(:@task, nil)
422
+ expect { @ts.comment }.to raise_error(tasksch_err)
423
+ end
424
+ end
425
+
426
+ describe '#author' do
427
+ before { create_task }
428
+ it 'Returns the author of task' do
429
+ check = 'Rspec'
430
+ expect(@ts.author).to eq(check)
431
+ end
432
+
433
+ it 'Sets the Author of task' do
434
+ stub_user
435
+ check = 'Author'
436
+ expect(@ts.author = check).to eq(check)
437
+ expect(@ts.author).to eq(check)
438
+ end
439
+
440
+ it 'alias with Creator' do
441
+ stub_user
442
+ check = 'Description To Test'
443
+ expect(@ts.creator = check).to eq(check)
444
+ expect(@ts.author).to eq(check)
445
+ end
446
+
447
+ it 'Raises an error if task does not exists' do
448
+ @ts.instance_variable_set(:@task, nil)
449
+ expect { @ts.author }.to raise_error(tasksch_err)
450
+ end
451
+ end
452
+
453
+ describe '#max_run_time' do
454
+ before { create_task }
455
+ it 'Returns the Execution Time Limit of task' do
456
+ skip 'Due to logical error in implementation'
457
+ check = (72 * 60 * 60) # Task: PT72H
458
+ expect(@ts.max_run_time).to eq(check)
459
+ end
460
+
461
+ it 'Sets the max_run_time of task' do
462
+ skip 'Due to logical error in implementation'
463
+ check = 1_244_145_000_000
464
+ expect(@ts.max_run_time = check).to eq(check)
465
+ expect(@ts.max_run_time).to eq(check)
466
+ end
467
+
468
+ it 'Raises an error if task does not exists' do
469
+ @ts.instance_variable_set(:@task, nil)
470
+ expect { @ts.max_run_time }.to raise_error(tasksch_err)
471
+ end
472
+ end
473
+
474
+ describe '#enabled?' do
475
+ before { create_task }
476
+ it 'Returns true when Task is enabled' do
477
+ expect(@ts.enabled?).to be_truthy
478
+ end
479
+
480
+ it 'Raises an error if task does not exists' do
481
+ @ts.instance_variable_set(:@task, nil)
482
+ expect { @ts.enabled? }.to raise_error(tasksch_err)
483
+ end
484
+ end
485
+
486
+ describe '#next_run_time' do
487
+ before { create_task }
488
+ it 'Returns a Time object that indicates the next time the task will run' do
489
+ expect(@ts.next_run_time).to be_a(Time)
490
+ end
491
+
492
+ it 'Raises an error if task does not exists' do
493
+ @ts.instance_variable_set(:@task, nil)
494
+ expect { @ts.next_run_time }.to raise_error(tasksch_err)
495
+ end
496
+ end
497
+
498
+ describe '#most_recent_run_time' do
499
+ before { create_task }
500
+
501
+ it 'Returns nil if the task has never run' do
502
+ skip 'Error in implementation; Time.parse requires String'
503
+ expect(@ts.most_recent_run_time).to be_nil
504
+ end
505
+
506
+ it 'Returns Time object indicating the most recent time the task ran' do
507
+ skip 'Error in implementation; Time.parse requires String'
508
+ @ts.run
509
+ expect(@ts.most_recent_run_time).to be_a(Time)
510
+ @ts.stop
511
+ end
512
+
513
+ it 'Raises an error if task does not exists' do
514
+ @ts.instance_variable_set(:@task, nil)
515
+ expect { @ts.most_recent_run_time }.to raise_error(tasksch_err)
516
+ end
517
+ end
518
+
519
+ describe '#account_information' do
520
+ before { create_task }
521
+ it 'System users may not require any password' do
522
+ user = 'SYSTEM'
523
+ password = nil
524
+ expect(@ts.set_account_information(user, password)). to be_truthy
525
+ expect(@ts.account_information).to eq(user)
526
+ end
527
+
528
+ it 'User will require a password' do
529
+ user = ENV['user']
530
+ password = nil
531
+ expect { @ts.set_account_information(user, password) }. to raise_error(TypeError)
532
+ expect(@ts.account_information).to include(user)
533
+ end
534
+
535
+ it 'Raises an error if task does not exists' do
536
+ @ts.instance_variable_set(:@task, nil)
537
+ user = 'User'
538
+ password = 'XXXX'
539
+ expect { @ts.set_account_information(user, password) }. to raise_error(tasksch_err)
540
+ expect(@ts.account_information).to be_nil
541
+ end
542
+ end
543
+
544
+ describe '#status' do
545
+ before { create_task }
546
+ it 'Returns tasks status' do
547
+ expect(@ts.status).to be_a(String)
548
+ end
549
+
550
+ it 'Raises an error if task does not exists' do
551
+ @ts.instance_variable_set(:@task, nil)
552
+ expect { @ts.status }.to raise_error(tasksch_err)
553
+ end
554
+ end
555
+
556
+ describe '#exit_code' do
557
+ before { create_task }
558
+ it 'Returns the exit code from the last scheduled run' do
559
+ expect(@ts.exit_code).to be_a(Integer)
560
+ end
561
+
562
+ it 'Raises an error if task does not exists' do
563
+ @ts.instance_variable_set(:@task, nil)
564
+ expect { @ts.exit_code }.to raise_error(tasksch_err)
565
+ end
566
+ end
567
+
568
+ describe '#trigger_count' do
569
+ before { create_task }
570
+ it 'Returns No of triggers associated with the task' do
571
+ expect(@ts.trigger_count).to eq(1)
572
+ end
573
+
574
+ it 'Raises an error if task does not exists' do
575
+ @ts.instance_variable_set(:@task, nil)
576
+ expect { @ts.trigger_count }.to raise_error(tasksch_err)
577
+ end
578
+ end
579
+
580
+ describe '#trigger_string' do
581
+ before { create_task }
582
+ it 'Returns a string that describes the current trigger at '\
583
+ 'the specified index for the active task' do
584
+ expect(@ts.trigger_string(0)).to be_a(String)
585
+ end
586
+
587
+ it 'Raises an error if trigger is not found at the given index' do
588
+ expect { @ts.trigger_string(1) }.to raise_error(tasksch_err)
589
+ end
590
+
591
+ it 'Raises an error if task does not exists' do
592
+ @ts.instance_variable_set(:@task, nil)
593
+ expect { @ts.trigger_string(0) }.to raise_error(tasksch_err)
594
+ end
595
+ end
596
+
597
+ describe '#idle_settings' do
598
+ before { create_task }
599
+ it 'Returns a hash containing idle settings of the current task' do
600
+ settings = @ts.idle_settings
601
+ expect(settings).to be_a(Hash)
602
+ expect(settings).to include(stop_on_idle_end: true)
603
+ end
604
+
605
+ it 'Raises an error if task does not exists' do
606
+ @ts.instance_variable_set(:@task, nil)
607
+ expect { @ts.idle_settings }. to raise_error(tasksch_err)
608
+ end
609
+ end
610
+
611
+ describe '#network_settings' do
612
+ before { create_task }
613
+ it 'Returns a hash containing network settings of the current task' do
614
+ settings = @ts.network_settings
615
+ expect(settings).to be_a(Hash)
616
+ expect(settings).to include(name: '')
617
+ end
618
+
619
+ it 'Raises an error if task does not exists' do
620
+ @ts.instance_variable_set(:@task, nil)
621
+ expect { @ts.network_settings }. to raise_error(tasksch_err)
622
+ end
623
+ end
624
+
625
+ describe '#settings' do
626
+ before { create_task }
627
+ it 'Returns a hash containing all the settings of the current task' do
628
+ settings = @ts.settings
629
+ expect(settings).to be_a(Hash)
630
+ expect(settings).to include(enabled: true)
631
+ end
632
+
633
+ it 'Raises an error if task does not exists' do
634
+ @ts.instance_variable_set(:@task, nil)
635
+ expect { @ts.settings }. to raise_error(tasksch_err)
636
+ end
637
+ end
638
+
639
+ describe '#configure_settings' do
640
+ before { create_task }
641
+ before { stub_user }
642
+
643
+ it 'Sets idle settings of the task' do
644
+ skip 'Functionality is not working'
645
+ # Seems like it accepts time in minutes; but returns the result in ISO8601 format
646
+ settings = { idle_duration: 'PT11M',
647
+ wait_timeout: 'PT2H',
648
+ stop_on_idle_end: true,
649
+ restart_on_idle: false }
650
+ @ts.configure_settings(settings)
651
+ expect(@ts.idle_settings).to eq(settings)
652
+ end
653
+
654
+ it 'Sets network settings of the task' do
655
+ skip 'Functionality is not working'
656
+ settings = { network_settings: { name: 'NET1' } }
657
+ @ts.configure_settings(settings)
658
+ expect(@ts.network_settings).to eq(settings)
659
+ end
660
+
661
+ it 'Configures the settings of the task' do
662
+ skip 'Functionality is not working properly'
663
+ settings = @ts.settings
664
+ settings[:allow_demand_start] = false
665
+ settings[:execution_time_limit] = 'PT70H'
666
+ @ts.configure_settings(settings)
667
+ expect(@ts.settings).to eq(settings)
668
+ end
669
+
670
+ it 'Raises an error if task does not exists' do
671
+ @ts.instance_variable_set(:@task, nil)
672
+ expect { @ts.settings }. to raise_error(tasksch_err)
673
+ end
674
+ end
675
+
676
+ describe '#principals' do
677
+ before { create_task }
678
+ it 'Returns a hash containing all the principal information of the current task' do
679
+ principals = @ts.principals
680
+ expect(principals).to be_a(Hash)
681
+ expect(principals).to include(id: 'Author')
682
+ end
683
+
684
+ it 'Raises an error if task does not exists' do
685
+ @ts.instance_variable_set(:@task, nil)
686
+ expect { @ts.principals }. to raise_error(tasksch_err)
687
+ end
688
+ end
689
+
690
+ describe '#principals' do
691
+ before { create_task }
692
+ it 'Returns a hash containing all the principal information of the current task' do
693
+ principals = @ts.principals
694
+ expect(principals).to be_a(Hash)
695
+ expect(principals).to include(id: 'Author')
696
+ end
697
+
698
+ it 'Raises an error if task does not exists' do
699
+ @ts.instance_variable_set(:@task, nil)
700
+ expect { @ts.principals }. to raise_error(tasksch_err)
701
+ end
702
+ end
703
+
704
+ describe '#configure_principals' do
705
+ before { create_task }
706
+ before { stub_user }
707
+
708
+ it 'Sets the principals for current active task' do
709
+ principals = @ts.principals
710
+ principals[:user_id] = 'SYSTEM'
711
+ principals[:display_name] = 'Rspec'
712
+ principals[:run_level] = Win32::TaskScheduler::TASK_RUNLEVEL_HIGHEST
713
+ principals[:logon_type] = Win32::TaskScheduler::TASK_LOGON_SERVICE_ACCOUNT
714
+
715
+ @ts.configure_principals(principals)
716
+ expect(@ts.principals).to eq(principals)
717
+ end
718
+
719
+ it 'Raises an error if task does not exists' do
720
+ @ts.instance_variable_set(:@task, nil)
721
+ expect { @ts.principals }. to raise_error(tasksch_err)
722
+ end
723
+ end
724
+
725
+ describe '#trigger' do
726
+ before { create_task }
727
+
728
+ it 'Returns a hash that describes the trigger '\
729
+ 'at the given index for the current task' do
730
+ trigger = @ts.trigger(0)
731
+ expect(trigger).to be_a(Hash)
732
+ expect(trigger).not_to be_empty
733
+ end
734
+
735
+ it 'Raises an error if trigger is not found at the given index' do
736
+ expect { @ts.trigger(1) }.to raise_error(tasksch_err)
737
+ end
738
+
739
+ it 'Raises an error if task does not exists' do
740
+ @ts.instance_variable_set(:@task, nil)
741
+ expect { @ts.trigger(0) }.to raise_error(tasksch_err)
742
+ end
743
+ end
744
+
745
+ describe '#trigger=' do
746
+ before { create_task }
747
+ before { stub_user }
748
+ all_triggers.each do |type, trigger_hash|
749
+ it "Updates the first trigger for type: #{type}" do
750
+ @ts.trigger = trigger_hash
751
+ returned_trigger = @ts.trigger(0)
752
+ expect(returned_trigger).to eq(trigger_hash)
753
+ expect(@ts.trigger_count).to eq(1)
754
+ end
755
+
756
+ it "Updates the trigger at given index for type: #{type}" do
757
+ skip 'Implementation Pending'
758
+ @ts.trigger = trigger_hash
759
+ returned_trigger = @ts.trigger(0)
760
+ expect(returned_trigger).to eq(trigger_hash)
761
+ expect(@ts.trigger_count).to eq(1)
762
+ end
763
+
764
+ it 'Raises an error if task does not exists' do
765
+ @ts.instance_variable_set(:@task, nil)
766
+ expect { @ts.trigger = trigger_hash }.to raise_error(tasksch_err)
767
+ end
768
+ end
769
+ end
770
+
771
+ describe '#new_work_item' do
772
+ before { create_task }
773
+ context 'Creates a new task' do
774
+ all_triggers.each do |type, trigger_hash|
775
+ it "for the triger: #{type}" do
776
+ task_count = 1 # One task already exists
777
+ expect(@ts.new_work_item("Task_#{type}", trigger_hash)).to be_a(@current_task.class)
778
+ task_count += 1
779
+ expect(no_of_tasks).to eq(task_count)
780
+ end
781
+ end
782
+ end
783
+
784
+ context 'Updates the existing task' do
785
+ all_triggers.each do |type, trigger_hash|
786
+ it "for the triger: #{type}" do
787
+ expect(@ts.new_work_item(@task, trigger_hash)).to be_a(@current_task.class)
788
+ expect(no_of_tasks).to eq(1)
789
+ end
790
+
791
+ it 'Raises an error if task does not exists' do
792
+ @ts.instance_variable_set(:@task, nil)
793
+ expect { @ts.trigger = trigger_hash }.to raise_error(tasksch_err)
794
+ end
795
+ end
796
+ end
797
+ end
798
+
799
+ private
800
+
801
+ def load_task_variables
802
+ time = Time.now
803
+ # Ensuring root path will be test path
804
+ allow_any_instance_of(Win32::TaskScheduler).to receive(:root_path).and_return(@test_path)
805
+ @app = 'iexplore.exe'
806
+ @task = 'test_task'
807
+ @folder = @test_path
808
+ @force = false
809
+ @trigger = { start_year: time.year, start_month: time.month,
810
+ start_day: time.day, start_hour: time.hour,
811
+ start_minute: time.min,
812
+ # Will update this in test cases when required
813
+ trigger_type: Win32::TaskScheduler::ONCE }
814
+ @ts = Win32::TaskScheduler.new
815
+ end
816
+
817
+ # Sets the user Id as nil, hence SYSTEM will be considered
818
+ # Alternatively, we may to provide the login users password
819
+ def stub_user
820
+ # @ts.instance_variable_set(:@password, 'user_login_password')
821
+ allow(@ts).to receive(:task_user_id).and_return(nil)
822
+ end
823
+
824
+ # Will stop the appliaction only if it is running
825
+ def stop_the_app
826
+ `tasklist /FI "IMAGENAME eq #{@app}" 2>NUL | find /I /N "#{@app}">NUL
827
+ if "%ERRORLEVEL%"=="0" taskkill /f /im #{@app}`
828
+ end
829
+ end