Skip to content

Commit e29b1cf

Browse files
authored
Prefer with_env helper over mutating ENV in specs (#16608)
The `with_env` helper still mutates `ENV` internally, but we can later fix it to mock `ENV` instead of mutating it.
1 parent d1af97b commit e29b1cf

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

spec/compiler/ffi/ffi_spec.cr

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require "../spec_helper"
55
require "compiler/crystal/ffi"
66
require "compiler/crystal/loader"
77
require "../loader/spec_helper"
8+
require "../../support/env"
89

910
# all the integral return types must be at least as large as the register size
1011
# to avoid integer promotion by FFI!
@@ -36,18 +37,16 @@ end
3637
{% end %}
3738

3839
describe Crystal::FFI::CallInterface do
39-
before_all do
40+
around_all do |example|
4041
FileUtils.mkdir_p(SPEC_CRYSTAL_LOADER_LIB_PATH)
4142
build_c_dynlib(compiler_datapath("ffi", "sum.c"))
4243

4344
{% if flag?(:win32) && flag?(:gnu) %}
44-
ENV["PATH"] = "#{SPEC_CRYSTAL_LOADER_LIB_PATH}#{Process::PATH_DELIMITER}#{ENV["PATH"]}"
45-
{% end %}
46-
end
47-
48-
after_all do
49-
{% if flag?(:win32) && flag?(:gnu) %}
50-
ENV["PATH"] = ENV["PATH"].delete_at(0, ENV["PATH"].index!(Process::PATH_DELIMITER) + 1)
45+
with_env({"PATH" => "#{SPEC_CRYSTAL_LOADER_LIB_PATH}#{Process::PATH_DELIMITER}#{ENV["PATH"]}"}) do
46+
example.run
47+
end
48+
{% else %}
49+
example.run
5150
{% end %}
5251

5352
FileUtils.rm_rf(SPEC_CRYSTAL_LOADER_LIB_PATH)

spec/compiler/interpreter/lib_spec.cr

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{% skip_file if flag?(:without_interpreter) %}
22
require "./spec_helper"
33
require "../loader/spec_helper"
4+
require "../../support/env"
45

56
private def ldflags
67
{% if flag?(:msvc) %}
@@ -19,18 +20,16 @@ private def ldflags_with_backtick
1920
end
2021

2122
describe Crystal::Repl::Interpreter do
22-
before_all do
23+
around_all do |example|
2324
FileUtils.mkdir_p(SPEC_CRYSTAL_LOADER_LIB_PATH)
2425
build_c_dynlib(compiler_datapath("interpreter", "sum.c"))
2526

2627
{% if flag?(:win32) %}
27-
ENV["PATH"] = "#{SPEC_CRYSTAL_LOADER_LIB_PATH}#{Process::PATH_DELIMITER}#{ENV["PATH"]}"
28-
{% end %}
29-
end
30-
31-
after_all do
32-
{% if flag?(:win32) %}
33-
ENV["PATH"] = ENV["PATH"].delete_at(0, ENV["PATH"].index!(Process::PATH_DELIMITER) + 1)
28+
with_env({"PATH" => "#{SPEC_CRYSTAL_LOADER_LIB_PATH}#{Process::PATH_DELIMITER}#{ENV["PATH"]}"}) do
29+
example.run
30+
end
31+
{% else %}
32+
example.run
3433
{% end %}
3534

3635
FileUtils.rm_rf(SPEC_CRYSTAL_LOADER_LIB_PATH)

spec/std/dir_spec.cr

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
require "./spec_helper"
22
require "../support/env"
33

4-
private def unset_tempdir(&)
4+
private def reset_tempdir(tmp_path = nil, &)
55
{% if flag?(:windows) %}
6-
with_env("TMP": nil, "TEMP": nil, "USERPROFILE": nil) { yield }
6+
with_env("TMP": tmp_path, "TEMP": nil, "USERPROFILE": nil) { yield }
77
{% else %}
8-
with_env("TMPDIR": nil) { yield }
8+
with_env("TMPDIR": tmp_path) { yield }
99
{% end %}
1010
end
1111

@@ -665,7 +665,7 @@ describe "Dir" do
665665

666666
describe ".tempdir" do
667667
it "returns default directory for tempfiles" do
668-
unset_tempdir do
668+
reset_tempdir do
669669
{% if flag?(:windows) %}
670670
# GetTempPathW defaults to the Windows directory when %TMP%, %TEMP%
671671
# and %USERPROFILE% are not set.
@@ -681,9 +681,8 @@ describe "Dir" do
681681
end
682682

683683
it "returns configure directory for tempfiles" do
684-
unset_tempdir do
685-
tmp_path = Path["my_temporary_path"].expand.to_s
686-
ENV[{{ flag?(:windows) ? "TMP" : "TMPDIR" }}] = tmp_path
684+
tmp_path = Path["my_temporary_path"].expand.to_s
685+
reset_tempdir(tmp_path) do
687686
Dir.tempdir.should eq tmp_path
688687
end
689688
end

0 commit comments

Comments
 (0)