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

fix test failure for ruby 3 and 3.1 #85

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
39 changes: 27 additions & 12 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 @@ -121,7 +121,7 @@ class Error < StandardError; end
attr_accessor :password
attr_reader :host

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

Expand All @@ -141,7 +141,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 @@ -196,13 +196,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 @@ -211,13 +211,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 @@ -371,6 +371,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 @@ -406,6 +407,7 @@ def parameters
#
def parameters=(param)
raise TypeError unless param.is_a?(String)

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -439,6 +441,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 @@ -494,6 +497,7 @@ def priority
#
def priority=(priority)
raise TypeError unless priority.is_a?(Numeric)

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -527,6 +531,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 @@ -621,6 +626,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 @@ -639,6 +645,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 @@ -654,6 +661,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 @@ -957,6 +965,7 @@ def comment
#
def comment=(comment)
raise TypeError unless comment.is_a?(String)

check_for_active_task

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

check_for_active_task

definition = @task.Definition
Expand Down Expand Up @@ -1057,6 +1067,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 @@ -1116,7 +1127,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 @@ -1134,6 +1145,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 @@ -1171,6 +1183,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 @@ -1206,6 +1219,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 @@ -1235,6 +1249,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
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
4 changes: 4 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 @@ -128,6 +129,9 @@ def extra_days(days_count, month_count, year_count, init_month, init_year)
def time_details(time_str)
tm_detail = {}
if time_str.to_s != ""
# raising exception if time is not a string
raise TypeError.new("TypeError: Inccorrect Type please pass input value as a String.") unless time_str.is_a? String
Comment on lines +132 to +133
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, but lets do it before the if

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tagging @antima-gupta ~ please review the comments above.


# 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
8 changes: 4 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# Creating folder 'Test'; This will be treated as root
def create_test_folder
@service ||= service
@root_path = '\\'
@test_path = '\\Test'
@root_path = "\\"
@test_path = "\\Test"
@root_folder = @service.GetFolder(@root_path)
@test_folder = @root_folder.CreateFolder(@test_path)
@ts.instance_variable_set(:@root, @test_folder) if @ts
Expand Down Expand Up @@ -66,6 +66,7 @@ def no_of_tasks(folder = @test_folder)
# the related functionalities over a task in RSpecs
def create_task
return nil unless @service

@task_definition = @service.NewTask(0)
task_registration
task_prinicipals
Expand Down Expand Up @@ -138,8 +139,7 @@ def tasksch_err
def all_triggers
all_triggers = {}

%w{ONCE DAILY WEEKLY MONTHLYDATE MONTHLYDOW
ON_IDLE AT_SYSTEMSTART AT_LOGON}.each do |trig_type|
%w{ONCE DAILY WEEKLY MONTHLYDATE MONTHLYDOW ON_IDLE AT_SYSTEMSTART AT_LOGON}.each do |trig_type|
trigger = {}
trigger[:trigger_type] = Win32::TaskScheduler.class_eval(trig_type)
start_end_params(trigger)
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/win32/taskscheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@
end

it "Does not require root to be appended" do
task = @test_path + @folder + '\\' + @task
task = @test_path + @folder + "\\" + @task
expect(@ts.exists?(task)).to be_falsey
end
end

context "At Nested folder" do
it "Returns false for non existing folder" do
task = folder + '\\' + @task
task = folder + "\\" + @task
expect(@ts.exists?(task)).to be_falsey
end

it "Returns true for existing 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 Down
2 changes: 1 addition & 1 deletion test/test_taskscheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def setup_task

test "working_directory= works as expected" do
setup_task
assert_nothing_raised { @ts.working_directory = 'C:\\' }
assert_nothing_raised { @ts.working_directory = "C:\\" }
end

test "working_directory= requires a string argument" do
Expand Down