-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add tempfile and datapath helpers to specs #5951
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
Merged
sdogruyol
merged 10 commits into
crystal-lang:master
from
straight-shoota:jm/feature/specs-tempdir
Jun 19, 2018
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
11b7ef3
Add spec helper `with_tempfile`
straight-shoota 9c1c545
Add spec_helper with datapath in spec/std
straight-shoota 346a2d2
Use with_tempfile and datapath in spec/std
straight-shoota d7f0aa2
Use with_tempfile and datapath in spec/compiler
straight-shoota f321e0c
Use with_tempfile in spec/spec_helper.cr
straight-shoota bfbf43d
fixup! Add spec helper `with_tempfile`
straight-shoota 67e6780
fixup! Add spec helper `with_tempfile`
straight-shoota 5ae1347
fixup! Use with_tempfile and datapath in spec/std
straight-shoota bc64183
fixup! Use with_tempfile and datapath in spec/std
straight-shoota 1667e49
fixup! Use with_tempfile in spec/spec_helper.cr
straight-shoota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| require "../spec_helper" | ||
| require "../support/tempfile" | ||
|
|
||
| def datapath(*components) | ||
| File.join("spec", "compiler", "data", *components) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ ENV["CRYSTAL_PATH"] = "#{__DIR__}/../src" | |
| require "spec" | ||
| require "../src/compiler/crystal/**" | ||
| require "./support/syntax" | ||
| require "./support/tempfile" | ||
|
|
||
| class Crystal::Program | ||
| def union_of(type1, type2, type3) | ||
|
|
@@ -153,7 +154,7 @@ class Crystal::SpecRunOutput | |
| end | ||
| end | ||
|
|
||
| def run(code, filename = nil, inject_primitives = true, debug = Crystal::Debug::None) | ||
| def run(code, filename = nil, inject_primitives = true, debug = Crystal::Debug::None, *, file = __FILE__) | ||
| code = inject_primitives(code) if inject_primitives | ||
|
|
||
| # Code that requires the prelude doesn't run in LLVM's MCJIT | ||
|
|
@@ -170,60 +171,47 @@ def run(code, filename = nil, inject_primitives = true, debug = Crystal::Debug:: | |
| ast.expressions[-1] = exps | ||
| code = ast.to_s | ||
|
|
||
| output_filename = Crystal.tempfile("crystal-spec-output") | ||
| with_tempfile("crystal-spec-output", file: file) do |output_filename| | ||
| compiler = Compiler.new | ||
| compiler.debug = debug | ||
| compiler.compile Compiler::Source.new("spec", code), output_filename | ||
|
|
||
| compiler = Compiler.new | ||
| compiler.debug = debug | ||
| compiler.compile Compiler::Source.new("spec", code), output_filename | ||
| output = `#{output_filename}` | ||
|
|
||
| output = `#{output_filename}` | ||
| File.delete(output_filename) | ||
|
|
||
| SpecRunOutput.new(output) | ||
| SpecRunOutput.new(output) | ||
| end | ||
| else | ||
| Program.new.run(code, filename: filename, debug: debug) | ||
| end | ||
| end | ||
|
|
||
| def build_and_run(code) | ||
| code_file = Tempfile.new("build_and_run_code") | ||
| code_file.close | ||
|
|
||
| # write code to the temp file | ||
| File.write(code_file.path, code) | ||
| def build_and_run(code, *, file = __FILE__) | ||
| with_tempfile("build_and_run_code", "build_and_run_bin", file: file) do |code_path, binary_path| | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| # write code to the temp file | ||
| File.write(code_path, code) | ||
|
|
||
| binary_file = Tempfile.new("build_and_run_bin") | ||
| binary_file.close | ||
| `bin/crystal build #{code_path.inspect} -o #{binary_path.inspect}` | ||
| File.exists?(binary_path).should be_true | ||
|
|
||
| `bin/crystal build #{code_file.path.inspect} -o #{binary_file.path.inspect}` | ||
| File.exists?(binary_file.path).should be_true | ||
| out_io, err_io = IO::Memory.new, IO::Memory.new | ||
| status = Process.run(binary_path, output: out_io, error: err_io) | ||
|
|
||
| out_io, err_io = IO::Memory.new, IO::Memory.new | ||
| status = Process.run(binary_file.path, output: out_io, error: err_io) | ||
|
|
||
| {status, out_io.to_s, err_io.to_s} | ||
| ensure | ||
| File.delete(code_file.path) if code_file | ||
| File.delete(binary_file.path) if binary_file | ||
| {status, out_io.to_s, err_io.to_s} | ||
| end | ||
| end | ||
|
|
||
| def test_c(c_code, crystal_code) | ||
| c_filename = "#{__DIR__}/temp_abi.c" | ||
| o_filename = "#{__DIR__}/temp_abi.o" | ||
| begin | ||
| File.write(c_filename, c_code) | ||
| def test_c(c_code, crystal_code, *, file = __FILE__) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| with_tempfile("abi.c", "abi.o", file: file) do |c_path, o_path| | ||
| File.write(c_path, c_code) | ||
|
|
||
| `#{Crystal::Compiler::CC} #{c_filename} -c -o #{o_filename}`.should be_truthy | ||
| `#{Crystal::Compiler::CC} #{c_path} -c -o #{o_path}`.should be_truthy | ||
|
|
||
| yield run(%( | ||
| require "prelude" | ||
|
|
||
| @[Link(ldflags: "#{o_filename}")] | ||
| @[Link(ldflags: "#{o_path}")] | ||
| #{crystal_code} | ||
| )) | ||
| ensure | ||
| File.delete(c_filename) | ||
| File.delete(o_filename) | ||
| end | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The specs doesn't care about the location of this files.
They are not even generated sometimes (JIT is used unless prelude is required).
I would avoid adding this named arg and just using a fixed tempfile prefix/sufix in this helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand why you'd want to remove this. Maybe it's not strictly necessary, but using
with_tempfilehere certainly doesn't hurt and it's easier than managing a tempfile location manually.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said, if specs are failing I would probably run just a few or one. Or even add a debug message for the spec/tempfile association while running specs in some verbose mode.
Having
filenameandfilearguments in the same method seems misleading.So, between not been able see an addition here, plus the misleading name, plus having more data to cary on runtime on the high populated spec suite of the compiler, I would vote for removing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll revert it.