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

Trying again to get the tests to pass predictably on linux. #383

Merged
merged 5 commits into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions spec/amber/cli/commands/exec_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# {% if flag?(:run_build_tests) %}
require "../../../spec_helper"
require "../../../support/helpers/cli_helper"

include CLIHelper

module Amber::CLI
describe "amber exec" do
context "within project" do
cleanup
scaffold_app(TESTING_APP)
`shards`

it "executes one-liners from the first command-line argument" do
expected_result = "[:a, :b, :c, :d]\n"
MainCommand.run(["exec", "[:a, :b, :c] + [:d]"])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq expected_result
end

it "executes a .cr file from the first command-line argument" do
File.write "amber_exec_spec_test.cr", "puts([:a] + [:b])"
MainCommand.run(["exec", "amber_exec_spec_test.cr", "-e", "tail"])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq "[:a, :b]\n"
File.delete("amber_exec_spec_test.cr")
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"
end

it "copies previous run into new file for editing and runs it returning results" do
MainCommand.run(["exec", "1337"])
MainCommand.run(["exec", "-e", "tail", "-b", "1"])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq "1337\n"
end

cleanup
end

context "outside of project" do
it "complains if not in the root of a project" do
expected_result = "Error: 'amber exec' can only be used from the root of a valid amber project"
MainCommand.run(["exec", ":hello"])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq expected_result
end

cleanup
end
end
end
# {% end %}
4 changes: 0 additions & 4 deletions spec/amber/dsl/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ require "../../../spec_helper"
module Amber
module DSL
describe Server do

describe "pipeline" do
context "generating a single ':custom' pipeline" do
server = Amber::Server.instance
Expand All @@ -26,7 +25,6 @@ module Amber
plugs.should_not be nil
(plugs.map(&.class).should eq expected) if plugs
end

end

context "generating a single pipeline with multiple calls" do
Expand Down Expand Up @@ -121,9 +119,7 @@ module Amber
(plugs.map(&.class).should eq expected) if plugs
end
end

end
end

end
end
53 changes: 53 additions & 0 deletions src/amber/cli/commands/exec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "cli"

module Amber::CLI
class MainCommand < ::Cli::Supercommand
command "x", aliased: "exec"

class Exec < ::Cli::Command
command_name "exec"
@filename = "./tmp/#{Time.now.epoch_ms}_console.cr"

class Options
arg "code", desc: "Crystal code or .cr file to execute within the application scope", default: ""
string ["-e", "--editor"], desc: "Prefered editor: [vim, nano, pico, etc], only used when no code or .cr file is specified", default: "vim"
string ["-b", "--back"], desc: "Runs prevous command files: 'amber exec -b [times_ago]'", default: "0"
end

class Help
caption "# It runs Crystal code within the application scope"
end

def prepare_file
_filename = if File.exists?(args.code)
args.code
elsif options.back.to_i(strict: false) > 0
Dir.glob("./tmp/*_console.cr").sort.reverse[options.back.to_i(strict: false) - 1]?
end

system("cp #{_filename} #{@filename}") if _filename
end

def run
Dir.mkdir("tmp") unless Dir.exists?("tmp")

unless args.code.blank? || File.exists?(args.code)
File.write(@filename, "puts (#{args.code}).inspect")
else
prepare_file
system("#{options.editor} #{@filename}")
end

result = ""
result = `crystal eval 'require "./config/*"; require "#{@filename}"'` if File.exists?(@filename)

if result.includes?("while requiring \"./config/*\": can't find file './config/*' relative to '.'")
result = "Error: 'amber exec' can only be used from the root of a valid amber project"
end

File.write(@filename.sub("console.cr", "console_result.log"), result) unless result.blank?
puts result
end
end
end
end
1 change: 1 addition & 0 deletions src/amber/cli/templates/app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.crystal/
/.shards/
/.vscode/
/tmp/
.env
.amber_secret_key
production.yml
Expand Down
1 change: 0 additions & 1 deletion src/amber/scripts/environment_loader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ str = String.build do |s|
s.puts %(@port_reuse = #{settings["port_reuse"]? == nil ? true : settings["port_reuse"]})
s.puts %(@process_count = #{settings["process_count"]? || 1})
s.puts %(@log = #{settings["log"]? || "::Logger.new(STDOUT)"}.tap{|l| l.level = #{settings["log_level"]? || "::Logger::INFO"}})
#s.puts %(@log.level = #{settings["log_level"]? || "::Logger::INFO"})
s.puts %(@color = #{settings["color"]? == nil ? true : settings["color"]})
s.puts %(@redis_url = "#{settings["redis_url"]? || "redis://localhost:6379"}")
s.puts %(@port = #{settings["port"]? || 3000})
Expand Down
2 changes: 1 addition & 1 deletion src/amber/server/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Amber
def self.instance
@@instance ||= new
end

# Configure should probably be deprecated in favor of settings.
def self.configure
with settings yield settings
Expand Down