Skip to content

Commit 0812ca2

Browse files
Use with_tempfile and datapath in spec/compiler
1 parent 76d09a1 commit 0812ca2

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

spec/compiler/compiler_spec.cr

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
require "../spec_helper"
2-
require "tempfile"
2+
require "./spec_helper"
33

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

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

11-
File.exists?(tempfile.path).should be_true
12-
13-
`#{tempfile.path}`.should eq("Hello!")
14-
ensure
15-
File.delete(tempfile.path) if tempfile
11+
`#{path}`.should eq("Hello!")
12+
end
1613
end
1714

1815
it "runs subcommand in preference to a filename " do
19-
Dir.cd "#{__DIR__}/data/" do
20-
tempfile = Tempfile.new "compiler_spec_output"
21-
tempfile.close
22-
23-
Crystal::Command.run ["build", "#{__DIR__}/data/compiler_sample", "-o", tempfile.path]
16+
Dir.cd datapath do
17+
with_tempfile "compiler_spec_output" do |path|
18+
Crystal::Command.run ["build", "compiler_sample", "-o", path]
2419

25-
File.exists?(tempfile.path).should be_true
20+
File.exists?(path).should be_true
2621

27-
`#{tempfile.path}`.should eq("Hello!")
28-
ensure
29-
File.delete(tempfile.path) if tempfile
22+
`#{path}`.should eq("Hello!")
23+
end
3024
end
3125
end
3226
end

spec/compiler/crystal/tools/init_spec.cr

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ require "ini"
55
require "spec"
66
require "yaml"
77

8-
PROJECT_ROOT_DIR = "#{__DIR__}/../../../.."
9-
108
private def exec_init(project_name, project_dir = nil, type = "lib", force = false, skip_existing = false)
119
args = [type, project_name]
1210
args << project_dir if project_dir
@@ -21,14 +19,11 @@ end
2119
# Creates a temporary directory, cd to it and run the block inside it.
2220
# The directory and its content is deleted when the block return.
2321
private def within_temporary_directory
24-
tmp_path = "#{PROJECT_ROOT_DIR}/tmp/init_spec_tmp_dir-#{Process.pid}"
25-
Dir.mkdir_p(tmp_path)
26-
begin
22+
with_tempfile "init_spec_tmp" do |tmp_path|
23+
Dir.mkdir_p(tmp_path)
2724
Dir.cd(tmp_path) do
2825
yield
2926
end
30-
ensure
31-
FileUtils.rm_rf(tmp_path)
3227
end
3328
end
3429

spec/compiler/spec_helper.cr

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "../spec_helper"
2+
require "../support/tempfile"
3+
4+
def datapath(*components)
5+
File.join("spec", "compiler", "data", *components)
6+
end

0 commit comments

Comments
 (0)