Skip to content

Commit

Permalink
Updating code to hopefully fix weird memory issue (#18)
Browse files Browse the repository at this point in the history
* updated Ameba with corrections to hopefully catch something

* Making a few more adjustments, and trying the patch. Fixes #17
  • Loading branch information
jwoertink authored Aug 17, 2023
1 parent 8abc098 commit 734d2df
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ license: MIT
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: ~> 1.0.0
version: ~> 1.5.0
3 changes: 2 additions & 1 deletion spec/lucky_task/runner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ include HaveDefaultHelperMessageMatcher

describe LuckyTask::Runner do
it "adds tasks to the runner when task classes are created" do
expected_task_names = ["another_task", "my.cool_task", "my.custom_name"]
expected_task_names = ["another_task", "my.cool_task", "my.custom_name", "task_with_args", "task_with_required_format_args", "task_with_switch_flags", "task_with_int32_flags", "task_with_float64_flags", "task_with_positional_args", "task_with_fancy_output"]

task_names = LuckyTask::Runner.tasks.map(&.name)
task_names.size.should eq(expected_task_names.size)

expected_task_names.each do |expected_task_name|
task_names.should contain(expected_task_name)
Expand Down
26 changes: 13 additions & 13 deletions spec/lucky_task/task_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ describe LuckyTask::Task do

describe "with command line args" do
it "creates methods for the args and returns their values" do
task = TaskWithArgs.new.print_help_or_call(args: ["--model-name=User", "--model-type=Polymorphic"]).not_nil!
task = TaskWithArgs.new.print_help_or_call(args: ["--model-name=User", "--model-type=Polymorphic"]).as(TaskWithArgs)
task.model_name.should eq "User"
task.model_type.should eq "Polymorphic"
end

it "allows the args to be optional" do
task = TaskWithArgs.new.print_help_or_call(args: ["--model-name=User"]).not_nil!
task = TaskWithArgs.new.print_help_or_call(args: ["--model-name=User"]).as(TaskWithArgs)
task.model_name.should eq "User"
task.model_type.should eq nil
end

it "allows using an arg shortcut" do
task = TaskWithArgs.new.print_help_or_call(args: ["-m User"]).not_nil!
task = TaskWithArgs.new.print_help_or_call(args: ["-m User"]).as(TaskWithArgs)
task.model_name.should eq "User"
end

it "raises an error when an arg is required and not passed" do
task = TaskWithRequiredFormatArgs.new.print_help_or_call(args: [""]).not_nil!
task = TaskWithRequiredFormatArgs.new.print_help_or_call(args: [""]).as(TaskWithRequiredFormatArgs)
expect_raises(Exception, /--theme=SOME_VALUE/) do
task.theme
end
Expand All @@ -80,23 +80,23 @@ describe LuckyTask::Task do
end

it "sets switch flags that default to false" do
task = TaskWithSwitchFlags.new.print_help_or_call(args: [] of String).not_nil!
task = TaskWithSwitchFlags.new.print_help_or_call(args: [] of String).as(TaskWithSwitchFlags)
task.admin?.should eq false
end

it "sets switch flags from args" do
task = TaskWithSwitchFlags.new.print_help_or_call(args: ["-a"]).not_nil!
task = TaskWithSwitchFlags.new.print_help_or_call(args: ["-a"]).as(TaskWithSwitchFlags)
task.admin?.should eq true
end

it "sets int32 flags that default to 0" do
task = TaskWithInt32Flags.new.print_help_or_call(args: [] of String).not_nil!
task = TaskWithInt32Flags.new.print_help_or_call(args: [] of String).as(TaskWithInt32Flags)
task.zero.should eq 0
task.uno.should eq 1
end

it "sets int32 flags from args" do
task = TaskWithInt32Flags.new.print_help_or_call(args: ["-u 10000", "--zero=1_000"]).not_nil!
task = TaskWithInt32Flags.new.print_help_or_call(args: ["-u 10000", "--zero=1_000"]).as(TaskWithInt32Flags)
task.zero.should eq 1_000
task.uno.should eq 10_000
expect_raises(Exception, /"nada" is an invalid value for uno/) do
Expand All @@ -105,20 +105,20 @@ describe LuckyTask::Task do
end

it "sets explicit negative/positive int32 flags from args" do
task = TaskWithInt32Flags.new.print_help_or_call(args: ["-u -10_000", "--zero=+1_000"]).not_nil!
task = TaskWithInt32Flags.new.print_help_or_call(args: ["-u -10_000", "--zero=+1_000"]).as(TaskWithInt32Flags)
task.zero.should eq 1_000
task.uno.should eq -10_000
end

it "sets float64 flags that default to 0" do
task = TaskWithFloat64Flags.new.print_help_or_call(args: [] of String).not_nil!
task = TaskWithFloat64Flags.new.print_help_or_call(args: [] of String).as(TaskWithFloat64Flags)
task.zero.should eq 0.0
task.uno.should eq 1.0
task.pi.should eq 3.14
end

it "sets float64 flags from args" do
task = TaskWithFloat64Flags.new.print_help_or_call(args: ["-u 123_456.789", "--zero=1_000"]).not_nil!
task = TaskWithFloat64Flags.new.print_help_or_call(args: ["-u 123_456.789", "--zero=1_000"]).as(TaskWithFloat64Flags)
task.zero.should eq 1_000.0
task.uno.should eq 123_456.789
task.pi.should eq 3.14
Expand All @@ -128,13 +128,13 @@ describe LuckyTask::Task do
end

it "sets explicit negative/positive float64 flags from args" do
task = TaskWithFloat64Flags.new.print_help_or_call(args: ["-u -100", "--zero=+505.1"]).not_nil!
task = TaskWithFloat64Flags.new.print_help_or_call(args: ["-u -100", "--zero=+505.1"]).as(TaskWithFloat64Flags)
task.zero.should eq 505.1
task.uno.should eq -100.0
end

it "allows positional args that do not require a flag name" do
task = TaskWithPositionalArgs.new.print_help_or_call(args: ["User", "name:String", "email:String"]).not_nil!
task = TaskWithPositionalArgs.new.print_help_or_call(args: ["User", "name:String", "email:String"]).as(TaskWithPositionalArgs)
task.model.should eq "User"
task.columns.should eq ["name:String", "email:String"]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lucky_task_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe LuckyTask do
file.gets_to_end
end

version = /version\:(.*?)\n/.match(content).not_nil![1].strip
version = /version\:(.*?)\n/.match(content).as(Regex::MatchData)[1].strip
version.should eq LuckyTask::VERSION
end
end
2 changes: 1 addition & 1 deletion src/lucky_task/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ class LuckyTask::Runner
end

def self.longest_task_name
tasks.map(&.name.size).max
tasks.max_of(&.name.size)
end
end
12 changes: 7 additions & 5 deletions src/lucky_task/task.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ abstract class LuckyTask::Task
property option_parser : OptionParser = OptionParser.new
property output : IO = STDOUT

macro finished
{% if !@type.abstract? %}
LuckyTask::Runner.tasks << self.new
{% end %}
end

def name
def name : String
"{{@type.name.gsub(/::/, ".").underscore}}"
end

def help_message
def help_message : String
<<-TEXT
#{summary}
Expand All @@ -33,7 +35,7 @@ abstract class LuckyTask::Task
end
end

private def wants_help_message?(args)
private def wants_help_message?(args) : Bool
args.any? { |arg| {"--help", "-h", "help"}.includes?(arg) }
end
end
Expand Down Expand Up @@ -105,7 +107,7 @@ abstract class LuckyTask::Task
if @{{ arg_name.id }}.nil?
raise "{{ arg_name.id }} is required, but no value was passed."
end
@{{ arg_name.id }}.not_nil!
@{{ arg_name.id }}.as({% if to_end %}Array(String){% else %}String{% end %})
end
end

Expand Down Expand Up @@ -155,7 +157,7 @@ abstract class LuckyTask::Task
--{{ arg_name.id.stringify.underscore.gsub(/_/, "-").id }}=SOME_VALUE
ERROR
end
@{{ arg_name.id }}.not_nil!
@{{ arg_name.id }}.as(String)
{% else %}
@{{ arg_name.id }}
{% end %}
Expand Down

0 comments on commit 734d2df

Please sign in to comment.