Skip to content
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
28 changes: 24 additions & 4 deletions spec/amber/cli/commands/exec_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,30 @@ module Amber::CLI
File.read(logs.last?.to_s).should eq "[:a, :b]\n"
end

it "opens editor and executes .cr file on close" do
MainCommand.run(["exec", "-e", "echo 'puts 1000' > "])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq "1000\n"
it "does not execute shell metacharacters in the editor option" do
marker = "amber_exec_editor_pwned"
File.delete(marker) if File.exists?(marker)

expect_raises(Exception) do
MainCommand.run(["exec", "-e", "sh -c 'touch #{marker}' #"])
end

File.exists?(marker).should be_false
end

it "does not execute shell metacharacters in copied filenames" do
filename = "amber_exec_spec_test; touch amber_exec_copy_pwned #.cr"
marker = "amber_exec_copy_pwned"
File.delete(marker) if File.exists?(marker)
File.write filename, "puts([:safe])"

begin
MainCommand.run(["exec", filename, "-e", "tail"])
ensure
File.delete(filename) if File.exists?(filename)
end

File.exists?(marker).should be_false
end

it "copies previous run into new file for editing and runs it returning results" do
Expand Down
4 changes: 2 additions & 2 deletions src/amber/cli/commands/exec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Amber::CLI
Dir.glob("./tmp/*_console.cr").sort.reverse![options.back.to_i(strict: false) - 1]?
end

system("cp #{_filename} #{@filename}") if _filename
Process.run("cp", [_filename, @filename]) if _filename
end

private def show
Expand Down Expand Up @@ -72,7 +72,7 @@ module Amber::CLI

if args.code.blank? || File.exists?(args.code)
prepare_file
system("#{options.editor} #{@filename}")
Process.run(options.editor, [@filename], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
else
File.write(@filename, wrap(args.code))
end
Expand Down