Skip to content
Closed
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
5 changes: 2 additions & 3 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,7 +14,7 @@ describe "Codegen: private" do
]
compiler.prelude = "empty"

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

Expand All @@ -42,7 +41,7 @@ describe "Codegen: private" do
]
compiler.prelude = "empty"

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

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ 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 = File.tempfile("build_and_run_code")
code_file.close

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

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

`bin/crystal build #{code_file.path.inspect} -o #{binary_file.path.inspect}`
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 @@ -437,4 +437,22 @@ describe "Dir" do
Dir.rmdir("foo\0bar")
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 if 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 if old_tmpdir
end
end
end
89 changes: 89 additions & 0 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,93 @@ describe "File" do
end
{% end %}

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

File.exists?(path).should be_false
end

it "has the given extension" do
path = File.tempname ".sock"

File.extname(path).should eq(".sock")
end
end

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

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

it "has given extension if passed to constructor" do
tempfile = File.tempfile "foo", ".pdf"
File.extname(tempfile.path).should eq(".pdf")
end

it "creates and deletes" do
tempfile = File.tempfile "foo"
tempfile.close
tempfile.delete

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

it "doesn't delete on open with block" do
tempfile = File.tempfile("foo") do |f|
f.print "Hello!"
end
File.exists?(tempfile.path).should be_true
ensure
File.delete(tempfile.path) if tempfile
end

it "has given extension if passed to open" do
tempfile = File.tempfile("foo", ".pdf") { |f| }
File.extname(tempfile.path).should eq(".pdf")
ensure
File.delete(tempfile.path) if tempfile
end

it "creates and writes with TMPDIR environment variable" do
old_tmpdir = ENV["TMPDIR"]?
ENV["TMPDIR"] = "/tmp"

tempfile = File.tempfile "foo"
tempfile.print "Hello!"
tempfile.close

File.exists?(tempfile.path).should be_true
File.read(tempfile.path).should eq("Hello!")
ensure
ENV["TMPDIR"] = old_tmpdir if old_tmpdir
File.delete(tempfile.path) if tempfile
end

it "is seekable" do
tempfile = File.tempfile "foo"
tempfile.puts "Hello!"
tempfile.seek(0, IO::Seek::Set)
tempfile.tell.should eq(0)
tempfile.pos.should eq(0)
tempfile.gets(chomp: false).should eq("Hello!\n")
tempfile.pos = 0
tempfile.gets(chomp: false).should eq("Hello!\n")
tempfile.close
ensure
File.delete(tempfile.path) if tempfile
end
end

describe "closed stream" do
it "raises if writing on a closed stream" do
io = File.open(datapath("test_file.txt"), "r")
Expand Down Expand Up @@ -1275,6 +1362,8 @@ describe "File" do
File.match?("ab{{c,d}ef,}", "abcef").should be_true
File.match?("ab{{c,d}ef,}", "abdef").should be_true
end


end

describe File::Permissions 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"

private class RaiseErrno < IO
def initialize(@value : Int32)
Expand Down Expand Up @@ -326,8 +325,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.

7 changes: 3 additions & 4 deletions spec/support/tempfile.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require "tempfile"
require "file_utils"

{% if flag?(:win32) %}
SPEC_TEMPFILE_PATH = File.join(Tempfile.dirname, "cr-spec-#{Random.new.hex(4)}").gsub("C:\\", '/').gsub('\\', '/')
SPEC_TEMPFILE_PATH = File.join(Dir.tempdir, "cr-spec-#{Random.new.hex(4)}").gsub("C:\\", '/').gsub('\\', '/')
{% else %}
SPEC_TEMPFILE_PATH = File.join(Tempfile.dirname, "cr-spec-#{Random.new.hex(4)}")
SPEC_TEMPFILE_PATH = File.join(Dir.tempdir, "cr-spec-#{Random.new.hex(4)}")
{% end %}

SPEC_TEMPFILE_CLEANUP = ENV["SPEC_TEMPFILE_CLEANUP"]? != "0"
Expand All @@ -17,7 +16,7 @@ SPEC_TEMPFILE_CLEANUP = ENV["SPEC_TEMPFILE_CLEANUP"]? != "0"
# The constructed path is yielded to the block and cleaned up afterwards.
#
# Paths should still be uniquely chosen inside a spec file. This helper
# ensures they're placed in the temporary location (`Tempfile.dirname`),
# ensures they're placed in the temporary location (`Dir.tempdir`),
# avoids name clashes between parallel spec runs and cleans up afterwards.
#
# The unique directory for the spec run is removed `at_exit`.
Expand Down
1 change: 0 additions & 1 deletion src/compiler/crystal/tools/playground/server.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "http/server"
require "tempfile"
require "logger"
require "ecr/macros"
require "markdown"
Expand Down
26 changes: 26 additions & 0 deletions src/dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ class Dir
end
end

# Returns the tmp dir for system.
#
# ```
# Dir.tempdir # => "/tmp"
# ```
def self.tempdir : String
Crystal::System::File.tempdir
end

# Returns a fully-qualified path to a temporary directory without actually
# creating the directory.
#
# ```
# Dir.tempname # => "/tmp/20171206-1234-449386"
# ```
def self.tempname
time = Time.now.to_s("%Y%m%d")
rand = Random.rand(0x100000000).to_s(36)
{% if flag?(:win32) %}
# TODO: Remove this once Process is implemented
File.join(dirname, "#{time}-#{rand}")
{% else %}
File.join(dirname, "#{time}-#{Process.pid}-#{rand}")
{% end %}
end

# Calls the block once for each entry in this directory,
# passing the filename of each entry as a parameter to the block.
#
Expand Down
Loading