Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-opening a test fix PR with updates #91

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions .expeditor/run_windows_tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Stop script execution when a non-terminating error occurs
$ErrorActionPreference = "Stop"

# This will run ruby test on windows platform

Write-Output "--- Bundle install"

bundle config --local path vendor/bundle
If ($lastexitcode -ne 0) { Exit $lastexitcode }

bundle install --jobs=7 --retry=3
If ($lastexitcode -ne 0) { Exit $lastexitcode }

Write-Output "--- Bundle Execute"

bundle exec rake
If ($lastexitcode -ne 0) { Exit $lastexitcode }
34 changes: 34 additions & 0 deletions .expeditor/verify.pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
expeditor:
cached_folders:
- vendor
defaults:
buildkite:
retry:
automatic:
limit: 1
timeout_in_minutes: 30

steps:

- label: run-specs-ruby-3.0-windows
commands:
- .expeditor/run_windows_tests.ps1

expeditor:
executor:
docker:
host_os: windows
shell: ["powershell"]
image: rubydistros/windows-2019:3.0

- label: run-specs-ruby-3.1-windows
commands:
- .expeditor/run_windows_tests.ps1

expeditor:
executor:
docker:
host_os: windows
shell: ["powershell"]
image: rubydistros/windows-2019:3.1
11 changes: 0 additions & 11 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,3 @@ jobs:
- run: |
gem install chefstyle
chefstyle -c .rubocop.yml

spellcheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: carlosperate/download-file-action@v2.0.0
id: download-custom-dictionary
with:
file-url: 'https://raw.githubusercontent.com/chef/chef_dictionary/main/chef.txt'
file-name: 'chef_dictionary.txt'
- uses: streetsidesoftware/cspell-action@v2.12.0
2 changes: 1 addition & 1 deletion examples/taskscheduler_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

ts.activate("foo")
ts.priority = TaskScheduler::IDLE
ts.working_directory = 'C:\\'
ts.working_directory = "C:\\"

puts "App name: " + ts.application_name
puts "Creator: " + ts.creator
Expand Down
44 changes: 28 additions & 16 deletions lib/win32/taskscheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require_relative "taskscheduler/time_calc_helper"
require_relative "taskscheduler/constants"
require_relative "taskscheduler/version"
require "win32ole"
require "socket"
require "time"
require "win32ole" unless defined?(WIN32OLE)
require "time" unless defined?(Time.zone_offset)
require "socket" unless defined?(Socket)
require "structured_warnings"

# The Win32 module serves as a namespace only
Expand Down Expand Up @@ -122,7 +122,7 @@ class Error < StandardError; end
attr_accessor :password
attr_reader :host

def root_path(path = '\\')
def root_path(path = "\\")
path
end

Expand All @@ -142,7 +142,7 @@ def initialize(task = nil, trigger = nil, folder = root_path, force = false)
@task = nil
@password = nil

raise ArgumentError, "invalid folder" unless folder.include?('\\')
raise ArgumentError, "invalid folder" unless folder.include?("\\")

unless [TrueClass, FalseClass].include?(force.class)
raise TypeError, "invalid force value"
Expand Down Expand Up @@ -197,13 +197,13 @@ def exists?(full_task_path)
path = nil
task_name = nil

if full_task_path.include?('\\')
*path, task_name = full_task_path.split('\\')
if full_task_path.include?("\\")
*path, task_name = full_task_path.split("\\")
else
task_name = full_task_path
end

folder = path.nil? ? root_path : path.join('\\')
folder = path.nil? ? root_path : path.join("\\")

begin
root = @service.GetFolder(folder)
Expand All @@ -212,13 +212,13 @@ def exists?(full_task_path)
end

if root.nil?
return false
false
else
begin
task = root.GetTask(task_name)
return task && task.Name == task_name
task && task.Name == task_name
rescue WIN32OLERuntimeError => err
return false
false
end
end
end
Expand Down Expand Up @@ -372,6 +372,7 @@ def application_name
#
def application_name=(app)
raise TypeError unless app.is_a?(String)

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -407,6 +408,7 @@ def parameters
#
def parameters=(param)
raise TypeError unless param.is_a?(String)

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -440,6 +442,7 @@ def working_directory
#
def working_directory=(dir)
raise TypeError unless dir.is_a?(String)

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -495,6 +498,7 @@ def priority
#
def priority=(priority)
raise TypeError unless priority.is_a?(Numeric)

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -528,6 +532,7 @@ def new_work_item(task, trigger, userinfo = { user: nil, password: nil, interact

unless trigger.empty?
raise ArgumentError, "Unknown trigger type" unless valid_trigger_option(trigger[:trigger_type])

validate_trigger(trigger)

startTime = format("%04d-%02d-%02dT%02d:%02d:00", trigger[:start_year], trigger[:start_month], trigger[:start_day], trigger[:start_hour], trigger[:start_minute])
Expand Down Expand Up @@ -626,6 +631,7 @@ def trigger_count
#
def trigger_string(index)
raise TypeError unless index.is_a?(Numeric)

check_for_active_task
index += 1 # first item index is 1

Expand All @@ -644,6 +650,7 @@ def trigger_string(index)
#
def delete_trigger(index)
raise TypeError unless index.is_a?(Numeric)

check_for_active_task
index += 1 # first item index is 1

Expand All @@ -659,6 +666,7 @@ def delete_trigger(index)
#
def trigger(index)
raise TypeError unless index.is_a?(Numeric)

check_for_active_task
index += 1 # first item index is 1

Expand Down Expand Up @@ -712,7 +720,7 @@ def trigger(index)
when TASK_TRIGGER_SESSION_STATE_CHANGE
trigger[:user_id] = trig.UserId if trig.UserId.to_s != ""
trigger[:delay_duration] = time_in_minutes(trig.Delay)
trigger[:state_change] = trig.SessionState
# trigger[:state_change] = trig.SessionState
else
raise Error, "Unknown trigger type"
end
Expand Down Expand Up @@ -821,9 +829,6 @@ def trigger=(trigger)
end
when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
when TASK_EVENT_TRIGGER_AT_LOGON
trig.UserId = trigger[:user_id] if trigger[:user_id]
trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
when TASK_EVENT_TRIGGER_AT_LOGON
trig.UserId = trigger[:user_id] if trigger[:user_id]
trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
Expand Down Expand Up @@ -974,6 +979,7 @@ def comment
#
def comment=(comment)
raise TypeError unless comment.is_a?(String)

check_for_active_task

definition = @task.Definition
Expand All @@ -998,6 +1004,7 @@ def creator
#
def creator=(creator)
raise TypeError unless creator.is_a?(String)

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -1074,6 +1081,7 @@ def max_run_time
#
def max_run_time=(max_run_time)
raise TypeError unless max_run_time.is_a?(Numeric)

check_for_active_task

t = max_run_time
Expand Down Expand Up @@ -1133,7 +1141,7 @@ def configure_settings(settings_hash)

# Check for invalid setting
invalid_settings = settings_hash.keys - valid_settings_options
raise TypeError, "Invalid setting passed: #{invalid_settings.join(', ')}" unless invalid_settings.empty?
raise TypeError, "Invalid setting passed: #{invalid_settings.join(", ")}" unless invalid_settings.empty?

# Some modification is required in user input
hash = settings_hash.dup
Expand All @@ -1151,6 +1159,7 @@ def configure_settings(settings_hash)
idle_settings = task_settings.IdleSettings
IdleSettings.each do |setting|
next if hash[setting].nil?

idle_settings.setproperty(camelize(setting.to_s), hash[setting])
# This setting is not required to be configured now
hash.delete(setting)
Expand Down Expand Up @@ -1188,6 +1197,7 @@ def configure_settings(settings_hash)
#
def configure_registration_info(hash)
raise TypeError unless hash.is_a?(Hash)

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -1223,6 +1233,7 @@ def configure_registration_info(hash)
#
def configure_principals(principals)
raise TypeError unless principals.is_a?(Hash)

check_for_active_task
definition = @task.Definition
definition.Principal.Id = principals[:id] if principals[:id].to_s != ""
Expand Down Expand Up @@ -1252,6 +1263,7 @@ def settings
settings_hash = {}
@task.Definition.Settings.ole_get_methods.each do |setting|
next if setting.name == "XmlText" # not needed

settings_hash[setting.name] = @task.Definition.Settings._getproperty(setting.dispid, [], [])
end

Expand Down
2 changes: 1 addition & 1 deletion lib/win32/taskscheduler/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ module TaskSchedulerConstants
# When user connects to local computer like switching users
TASK_CONSOLE_CONNECT = 1
# When user disconnects to local computer like switching users
TASK_CONSOLE_DISCONNECT = 2
TASK_CONSOLE_DISCONNECT = 2
# When remote user connects to computer (e.g. RDP)
TASK_REMOTE_CONNECT = 3
# When remote user disconnects to computer (e.g. RDP)
Expand Down
3 changes: 1 addition & 2 deletions lib/win32/taskscheduler/helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "ffi"

require "ffi" unless defined?(FFI)
module Win32
class TaskScheduler
module Helper
Expand Down
2 changes: 2 additions & 0 deletions lib/win32/taskscheduler/sid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def self.from_string_sid(string_sid)
unless ConvertStringSidToSidW(utf8_to_wide(string_sid), result)
raise FFI::LastError.error
end

result_pointer = result.read_pointer
domain, name, use = account(result_pointer)
LocalFree(result_pointer)
Expand Down Expand Up @@ -108,6 +109,7 @@ def self.account(sid)
unless LookupAccountSidW(nil, sid, name, name_size, referenced_domain_name, referenced_domain_name_size, use)
raise FFI::LastError.error
end

[referenced_domain_name.read_wstring(referenced_domain_name_size.read_long), name.read_wstring(name_size.read_long), use.read_long]
end

Expand Down
6 changes: 6 additions & 0 deletions lib/win32/taskscheduler/time_calc_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def extra_days(days_count, month_count, year_count, init_month, init_year)
loop do
days -= days_in_month(mth, yr)
break if days <= 0

mth += 1
if mth > 12
mth = 1; yr += 1
Expand All @@ -127,7 +128,12 @@ def extra_days(days_count, month_count, year_count, init_month, init_year)
#
def time_details(time_str)
tm_detail = {}

raise TypeError.new("TypeError: Inccorrect Type please pass input value as a String.") unless time_str.is_a? String

if time_str.to_s != ""
# raising exception if time is not a string

# time_str will be like "PxxYxxMxxDTxxHxxMxxS"
# Ignoring 'P' and extracting date and time
dt, tm = time_str[1..-1].split("T")
Expand Down
10 changes: 4 additions & 6 deletions spec/functional/win32/taskscheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

it "at nested folder" do
@ts = Win32::TaskScheduler.new(@task, @trigger, folder, force)
task = @test_path + folder + '\\' + @task
task = @test_path + folder + "\\" + @task
expect(@ts.exists?(task)).to be_truthy
end
end
Expand All @@ -122,7 +122,7 @@

it "at nested folder" do
@ts = Win32::TaskScheduler.new(@task, @trigger, folder, force)
task = folder + '\\' + "invalid"
task = folder + "\\" + "invalid"
expect(@ts.exists?(task)).to be_falsy
end
end
Expand Down Expand Up @@ -670,8 +670,7 @@

describe "#trigger_string" do
before { create_task }
it "Returns a string that describes the current trigger at "\
"the specified index for the active task" do
it "Returns a string that describes the current trigger at the specified index for the active task" do
expect(@ts.trigger_string(0)).to be_a(String)
end

Expand Down Expand Up @@ -842,8 +841,7 @@
describe "#trigger" do
before { create_task }

it "Returns a hash that describes the trigger "\
"at the given index for the current task" do
it "Returns a hash that describes the trigger at the given index for the current task" do
trigger = @ts.trigger(0)
expect(trigger).to be_a(Hash)
expect(trigger).not_to be_empty
Expand Down
Loading