Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions spec/compiler/codegen/private_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "../../spec_helper"
require "tempfile"

describe "Codegen: private" do
it "codegens private def in same file" do
Expand All @@ -15,13 +14,11 @@ describe "Codegen: private" do
]
compiler.prelude = "empty"

tempfile = Tempfile.new("crystal-spec-output")
output_filename = tempfile.path
tempfile.close
output_filename = File.tempname("crystal-spec-output")

compiler.compile sources, output_filename
ensure
File.delete(tempfile.path) if tempfile
File.delete(output_filename) if output_filename
end

it "codegens overloaded private def in same file" do
Expand All @@ -42,13 +39,11 @@ describe "Codegen: private" do
]
compiler.prelude = "empty"

tempfile = Tempfile.new("crystal-spec-output")
output_filename = tempfile.path
tempfile.close
output_filename = File.tempname("crystal-spec-output")

compiler.compile sources, output_filename
ensure
File.delete(tempfile.path) if tempfile
File.delete(output_filename) if output_filename
end

it "doesn't include filename for private types" do
Expand Down
16 changes: 7 additions & 9 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,23 @@ def run(code, filename = nil, inject_primitives = true, debug = Crystal::Debug::
end

def build_and_run(code)
code_file = Tempfile.new("build_and_run_code")
code_file.close
code_file = File.tempname("build_and_run_code")

# write code to the temp file
File.write(code_file.path, code)
File.write(code_file, code)

binary_file = Tempfile.new("build_and_run_bin")
binary_file.close
binary_file = File.tempname("build_and_run_bin")

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

out_io, err_io = IO::Memory.new, IO::Memory.new
status = Process.run(binary_file.path, output: out_io, error: err_io)
status = Process.run(binary_file, 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
File.delete(code_file) if code_file
File.delete(binary_file) if binary_file
end

def test_c(c_code, crystal_code)
Expand Down
18 changes: 18 additions & 0 deletions spec/std/dir_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,24 @@ describe "Dir" do
end
end

describe ".tempdir" do
it "returns default directory for tempfiles" do
old_tmpdir = ENV["TMPDIR"]?
ENV.delete("TMPDIR")
Dir.tempdir.should eq("/tmp")
ensure
ENV["TMPDIR"] = old_tmpdir
end

it "returns configure directory for tempfiles" do
old_tmpdir = ENV["TMPDIR"]?
ENV["TMPDIR"] = "/my/tmp"
Dir.tempdir.should eq("/my/tmp")
ensure
ENV["TMPDIR"] = old_tmpdir
end
end

it "opens with new" do
filenames = [] of String

Expand Down
144 changes: 144 additions & 0 deletions spec/std/file/tempfile_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
require "../spec_helper"

describe File do
describe ".tempname" do
it "creates a path without creating the file" do
path = File.tempname

File.exists?(path).should be_false
File.dirname(path).should eq Dir.tempdir
end

it "accepts single suffix argument" do
path = File.tempname ".bar"

File.exists?(path).should be_false
File.dirname(path).should eq Dir.tempdir
File.extname(path).should eq(".bar")
end

it "accepts prefix and suffix arguments" do
path = File.tempname "foo", ".bar"

File.exists?(path).should be_false
File.dirname(path).should eq Dir.tempdir
File.extname(path).should eq(".bar")
File.basename(path).starts_with?("foo").should be_true
end

it "accepts prefix with separator" do
path = File.tempname "foo/", nil
File.dirname(path).should eq File.join(Dir.tempdir, "foo")
File.basename(path).starts_with?("foo").should be_false
end

it "accepts dir argument" do
path = File.tempname(dir: "foo")
File.dirname(path).should eq("foo")
end
end

describe ".tempfile" do
it "creates and writes" do
tempfile = File.tempfile
tempfile.print "Hello!"
tempfile.close

File.exists?(tempfile.path).should be_true
File.read(tempfile.path).should eq("Hello!")
ensure
tempfile.try &.delete
end

it "accepts single suffix argument" do
tempfile = File.tempfile ".bar"
tempfile.print "Hello!"
tempfile.close

File.extname(tempfile.path).should eq(".bar")

File.exists?(tempfile.path).should be_true
File.read(tempfile.path).should eq("Hello!")
ensure
tempfile.try &.delete
end

it "accepts prefix and suffix arguments" do
tempfile = File.tempfile "foo", ".bar"
tempfile.print "Hello!"
tempfile.close

File.extname(tempfile.path).should eq(".bar")
File.basename(tempfile.path).starts_with?("foo").should be_true

File.exists?(tempfile.path).should be_true
File.read(tempfile.path).should eq("Hello!")
ensure
tempfile.try &.delete
end

it "accepts dir argument" do
file = File.tempfile(dir: datapath)
File.dirname(file.path).should eq(datapath)
end

it "fails in unwritable folder" do
expect_raises(Errno, /mkstemp: ".*\/non-existing-folder\/.*": No such file or directory/) do
File.tempfile dir: datapath("non-existing-folder")
end
end

describe "with block" do
it "closes file" do
filepath = nil
tempfile = File.tempfile do |tempfile|
filepath = tempfile.path
end
tempfile.path.should eq filepath
tempfile.closed?.should be_true

filepath = filepath.not_nil!
File.exists?(filepath).should be_true
ensure
File.delete(filepath) if filepath
end

it "accepts single suffix argument" do
tempfile = File.tempfile(".bar") do |tempfile|
File.exists?(tempfile.path).should be_true
tempfile.closed?.should be_false
end
tempfile.closed?.should be_true

File.extname(tempfile.path).should eq(".bar")

File.exists?(tempfile.path).should be_true
ensure
File.delete(tempfile.path) if tempfile
end

it "accepts prefix and suffix arguments" do
tempfile = File.tempfile("foo", ".bar") do |tempfile|
File.exists?(tempfile.path).should be_true
tempfile.closed?.should be_false
end
tempfile.closed?.should be_true

File.extname(tempfile.path).should eq(".bar")
File.basename(tempfile.path).starts_with?("foo").should be_true

File.exists?(tempfile.path).should be_true
ensure
File.delete(tempfile.path) if tempfile
end

it "accepts dir argument" do
tempfile = File.tempfile(dir: datapath) do |tempfile|
end
File.dirname(tempfile.path).should eq(datapath)
ensure
File.delete(tempfile.path) if tempfile
end
end
end
end
16 changes: 16 additions & 0 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,22 @@ describe "File" do
end
end

describe "#delete" do
it "deletes" do
path = datapath("file-to-be-deleted")
File.touch(path)

file = File.new path
file.close

File.exists?(path).should be_true
file.delete
File.exists?(path).should be_false
ensure
File.delete(path) if path && File.exists?(path)
end
end

# TODO: these specs don't compile on win32 because iconv isn't implemented
{% unless flag?(:win32) %}
describe "encoding" do
Expand Down
5 changes: 2 additions & 3 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "spec"
require "http/server"
require "http/client/response"
require "tempfile"
require "../../../support/ssl"

private class RaiseErrno < IO
Expand Down Expand Up @@ -354,8 +353,8 @@ module HTTP
{% if flag?(:unix) %}
describe "#bind_unix" do
it "binds to different unix sockets" do
path1 = Tempfile.tempname
path2 = Tempfile.tempname
path1 = File.tempname
path2 = File.tempname

begin
server = Server.new do |context|
Expand Down
105 changes: 0 additions & 105 deletions spec/std/tempfile_spec.cr

This file was deleted.

Loading