Skip to content

Commit

Permalink
Add tempfile and datapath helpers to specs (#5951)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and Serdar Dogruyol committed Jun 19, 2018
1 parent 5c22fb3 commit 750258d
Show file tree
Hide file tree
Showing 27 changed files with 785 additions and 832 deletions.
30 changes: 12 additions & 18 deletions spec/compiler/compiler_spec.cr
@@ -1,32 +1,26 @@
require "../spec_helper"
require "tempfile"
require "./spec_helper"

describe "Compiler" do
it "compiles a file" do
tempfile = Tempfile.new "compiler_spec_output"
tempfile.close
with_tempfile "compiler_spec_output" do |path|
Crystal::Command.run ["build", datapath("compiler_sample"), "-o", path]

Crystal::Command.run ["build", "#{__DIR__}/data/compiler_sample", "-o", tempfile.path]
File.exists?(path).should be_true

File.exists?(tempfile.path).should be_true

`#{tempfile.path}`.should eq("Hello!")
ensure
File.delete(tempfile.path) if tempfile
`#{path}`.should eq("Hello!")
end
end

it "runs subcommand in preference to a filename " do
Dir.cd "#{__DIR__}/data/" do
tempfile = Tempfile.new "compiler_spec_output"
tempfile.close

Crystal::Command.run ["build", "#{__DIR__}/data/compiler_sample", "-o", tempfile.path]
Dir.cd datapath do
with_tempfile "compiler_spec_output" do |path|
Crystal::Command.run ["build", "compiler_sample", "-o", path]

File.exists?(tempfile.path).should be_true
File.exists?(path).should be_true

`#{tempfile.path}`.should eq("Hello!")
ensure
File.delete(tempfile.path) if tempfile
`#{path}`.should eq("Hello!")
end
end
end
end
9 changes: 2 additions & 7 deletions spec/compiler/crystal/tools/init_spec.cr
Expand Up @@ -5,8 +5,6 @@ require "ini"
require "spec"
require "yaml"

PROJECT_ROOT_DIR = "#{__DIR__}/../../../.."

private def exec_init(project_name, project_dir = nil, type = "lib", force = false, skip_existing = false)
args = [type, project_name]
args << project_dir if project_dir
Expand All @@ -21,14 +19,11 @@ end
# Creates a temporary directory, cd to it and run the block inside it.
# The directory and its content is deleted when the block return.
private def within_temporary_directory
tmp_path = "#{PROJECT_ROOT_DIR}/tmp/init_spec_tmp_dir-#{Process.pid}"
Dir.mkdir_p(tmp_path)
begin
with_tempfile "init_spec_tmp" do |tmp_path|
Dir.mkdir_p(tmp_path)
Dir.cd(tmp_path) do
yield
end
ensure
FileUtils.rm_rf(tmp_path)
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/compiler/spec_helper.cr
@@ -0,0 +1,6 @@
require "../spec_helper"
require "../support/tempfile"

def datapath(*components)
File.join("spec", "compiler", "data", *components)
end
95 changes: 44 additions & 51 deletions spec/std/callstack_spec.cr
@@ -1,75 +1,68 @@
require "spec"
require "tempfile"
require "./spec_helper"

describe "Backtrace" do
it "prints file line:colunm" do
tempfile = Tempfile.new("compiler_spec_output")
tempfile.close
sample = "#{__DIR__}/data/backtrace_sample"
with_tempfile("compiler_spec_output") do |path|
sample = datapath("backtrace_sample")

# CallStack tries to make files relative to the current dir,
# so we do the same for tests
current_dir = Dir.current
current_dir += File::SEPARATOR unless current_dir.ends_with?(File::SEPARATOR)
sample = sample.lchop(current_dir)
# CallStack tries to make files relative to the current dir,
# so we do the same for tests
current_dir = Dir.current
current_dir += File::SEPARATOR unless current_dir.ends_with?(File::SEPARATOR)
sample = sample.lchop(current_dir)

`bin/crystal build --debug #{sample.inspect} -o #{tempfile.path.inspect}`
File.exists?(tempfile.path).should be_true
`bin/crystal build --debug #{sample.inspect} -o #{path.inspect}`
File.exists?(path).should be_true

{% if flag?(:darwin) %}
`dsymutil --flat #{tempfile.path}`
{% end %}
{% if flag?(:darwin) %}
`dsymutil --flat #{path}`
{% end %}

output = `#{tempfile.path}`
output = `#{path}`

# resolved file line:column
output.should match(/#{sample}:3:10 in 'callee1'/)
# resolved file line:column
output.should match(/#{sample}:3:10 in 'callee1'/)

unless output =~ /#{sample}:13:5 in 'callee3'/
fail "didn't find callee3 in the backtrace"
end
unless output =~ /#{sample}:13:5 in 'callee3'/
fail "didn't find callee3 in the backtrace"
end

# skipped internal details
output.should_not match(/src\/callstack\.cr/)
output.should_not match(/src\/exception\.cr/)
output.should_not match(/src\/raise\.cr/)
ensure
File.delete(tempfile.path) if tempfile
# skipped internal details
output.should_not match(/src\/callstack\.cr/)
output.should_not match(/src\/exception\.cr/)
output.should_not match(/src\/raise\.cr/)
end
end

it "prints exception backtrace to stderr" do
tempfile = Tempfile.new("compiler_spec_output")
tempfile.close
sample = "#{__DIR__}/data/exception_backtrace_sample"
with_tempfile("compiler_spec_output") do |path|
sample = datapath("exception_backtrace_sample")

`bin/crystal build --debug #{sample.inspect} -o #{tempfile.path.inspect}`
File.exists?(tempfile.path).should be_true
`bin/crystal build --debug #{sample.inspect} -o #{path.inspect}`
File.exists?(path).should be_true

output, error = {IO::Memory.new, IO::Memory.new}.tap do |outio, errio|
Process.run tempfile.path, output: outio, error: errio
end
output, error = {IO::Memory.new, IO::Memory.new}.tap do |outio, errio|
Process.run path, output: outio, error: errio
end

output.to_s.empty?.should be_true
error.to_s.should contain("IndexError")
ensure
File.delete(tempfile.path) if tempfile
output.to_s.empty?.should be_true
error.to_s.should contain("IndexError")
end
end

it "prints crash backtrace to stderr" do
tempfile = Tempfile.new("compiler_spec_output")
tempfile.close
sample = "#{__DIR__}/data/crash_backtrace_sample"
with_tempfile("compiler_spec_output") do |path|
sample = datapath("crash_backtrace_sample")

`bin/crystal build --debug #{sample.inspect} -o #{tempfile.path.inspect}`
File.exists?(tempfile.path).should be_true
`bin/crystal build --debug #{sample.inspect} -o #{path.inspect}`
File.exists?(path).should be_true

output, error = {IO::Memory.new, IO::Memory.new}.tap do |outio, errio|
Process.run tempfile.path, output: outio, error: errio
end
output, error = {IO::Memory.new, IO::Memory.new}.tap do |outio, errio|
Process.run path, output: outio, error: errio
end

output.to_s.empty?.should be_true
error.to_s.should contain("Invalid memory access")
ensure
File.delete(tempfile.path) if tempfile
output.to_s.empty?.should be_true
error.to_s.should contain("Invalid memory access")
end
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 750258d

Please sign in to comment.