2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-25 10:09:26 +01:00
|
|
|
require "extend/ENV"
|
|
|
|
|
2024-02-18 15:11:11 -08:00
|
|
|
RSpec.describe "ENV" do
|
2021-01-31 14:50:29 -05:00
|
|
|
shared_examples EnvActivation do
|
|
|
|
subject(:env) { env_activation.extend(described_class) }
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
let(:env_activation) { {}.extend(EnvActivation) }
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
it "supports switching compilers" do
|
|
|
|
subject.clang
|
2022-03-01 00:01:13 +00:00
|
|
|
expect(subject["LD"]).to be_nil
|
2021-01-31 14:50:29 -05:00
|
|
|
expect(subject["CC"]).to eq(subject["OBJC"])
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#with_build_environment" do
|
|
|
|
it "restores the environment" do
|
|
|
|
before = subject.dup
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2020-07-28 02:04:50 +02:00
|
|
|
subject.with_build_environment do
|
2017-02-25 10:09:26 +01:00
|
|
|
subject["foo"] = "bar"
|
|
|
|
end
|
|
|
|
|
2022-03-01 00:01:13 +00:00
|
|
|
expect(subject["foo"]).to be_nil
|
2021-01-31 14:50:29 -05:00
|
|
|
expect(subject).to eq(before)
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
it "ensures the environment is restored" do
|
|
|
|
before = subject.dup
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2021-01-31 14:50:29 -05:00
|
|
|
subject.with_build_environment do
|
|
|
|
subject["foo"] = "bar"
|
|
|
|
raise StandardError
|
|
|
|
end
|
2023-03-08 23:14:46 +00:00
|
|
|
end.to raise_error(StandardError)
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2022-03-01 00:01:13 +00:00
|
|
|
expect(subject["foo"]).to be_nil
|
2021-01-31 14:50:29 -05:00
|
|
|
expect(subject).to eq(before)
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
it "returns the value of the block" do
|
|
|
|
expect(subject.with_build_environment { 1 }).to eq(1)
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
it "does not mutate the interface" do
|
|
|
|
expected = subject.methods
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
subject.with_build_environment do
|
|
|
|
expect(subject.methods).to eq(expected)
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
expect(subject.methods).to eq(expected)
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#append" do
|
|
|
|
it "appends to an existing key" do
|
|
|
|
subject["foo"] = "bar"
|
|
|
|
subject.append "foo", "1"
|
|
|
|
expect(subject["foo"]).to eq("bar 1")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
it "appends to an existing empty key" do
|
|
|
|
subject["foo"] = ""
|
|
|
|
subject.append "foo", "1"
|
|
|
|
expect(subject["foo"]).to eq("1")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
it "appends to a non-existent key" do
|
|
|
|
subject.append "foo", "1"
|
|
|
|
expect(subject["foo"]).to eq("1")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2024-04-26 20:55:51 +02:00
|
|
|
# NOTE: This may be a wrong behavior; we should probably reject objects that
|
|
|
|
# do not respond to `#to_str`. For now this documents existing behavior.
|
2021-01-31 14:50:29 -05:00
|
|
|
it "coerces a value to a string" do
|
|
|
|
subject.append "foo", 42
|
|
|
|
expect(subject["foo"]).to eq("42")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#prepend" do
|
|
|
|
it "prepends to an existing key" do
|
|
|
|
subject["foo"] = "bar"
|
|
|
|
subject.prepend "foo", "1"
|
|
|
|
expect(subject["foo"]).to eq("1 bar")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
it "prepends to an existing empty key" do
|
|
|
|
subject["foo"] = ""
|
|
|
|
subject.prepend "foo", "1"
|
|
|
|
expect(subject["foo"]).to eq("1")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "prepends to a non-existent key" do
|
|
|
|
subject.prepend "foo", "1"
|
|
|
|
expect(subject["foo"]).to eq("1")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
# NOTE: this may be a wrong behavior; we should probably reject objects that
|
|
|
|
# do not respond to #to_str. For now this documents existing behavior.
|
|
|
|
it "coerces a value to a string" do
|
|
|
|
subject.prepend "foo", 42
|
|
|
|
expect(subject["foo"]).to eq("42")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#append_path" do
|
|
|
|
it "appends to a path" do
|
|
|
|
subject.append_path "FOO", "/usr/bin"
|
|
|
|
expect(subject["FOO"]).to eq("/usr/bin")
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
subject.append_path "FOO", "/bin"
|
|
|
|
expect(subject["FOO"]).to eq("/usr/bin#{File::PATH_SEPARATOR}/bin")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#prepend_path" do
|
|
|
|
it "prepends to a path" do
|
|
|
|
subject.prepend_path "FOO", "/usr/local"
|
|
|
|
expect(subject["FOO"]).to eq("/usr/local")
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
subject.prepend_path "FOO", "/usr"
|
|
|
|
expect(subject["FOO"]).to eq("/usr#{File::PATH_SEPARATOR}/usr/local")
|
|
|
|
end
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#compiler" do
|
|
|
|
it "allows switching compilers" do
|
2024-08-22 17:42:46 -04:00
|
|
|
subject.public_send(:"gcc-9")
|
|
|
|
expect(subject.compiler).to eq("gcc-9")
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
example "deparallelize_block_form_restores_makeflags" do
|
|
|
|
subject["MAKEFLAGS"] = "-j4"
|
|
|
|
|
|
|
|
subject.deparallelize do
|
2022-03-01 00:01:13 +00:00
|
|
|
expect(subject["MAKEFLAGS"]).to be_nil
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
2018-03-01 17:48:08 +00:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
expect(subject["MAKEFLAGS"]).to eq("-j4")
|
2019-07-13 22:48:22 +08:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#sensitive_environment" do
|
|
|
|
it "list sensitive environment" do
|
|
|
|
subject["SECRET_TOKEN"] = "password"
|
|
|
|
expect(subject.sensitive_environment).to include("SECRET_TOKEN")
|
|
|
|
end
|
2018-03-01 17:48:08 +00:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#clear_sensitive_environment!" do
|
|
|
|
it "removes sensitive environment variables" do
|
|
|
|
subject["SECRET_TOKEN"] = "password"
|
|
|
|
subject.clear_sensitive_environment!
|
|
|
|
expect(subject).not_to include("SECRET_TOKEN")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "leaves non-sensitive environment variables alone" do
|
|
|
|
subject["FOO"] = "bar"
|
|
|
|
subject.clear_sensitive_environment!
|
|
|
|
expect(subject["FOO"]).to eq "bar"
|
|
|
|
end
|
2018-03-01 17:48:08 +00:00
|
|
|
end
|
2018-07-05 20:15:57 +01:00
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe Stdenv do
|
|
|
|
include_examples EnvActivation
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe Superenv do
|
|
|
|
include_examples EnvActivation
|
2017-02-25 10:09:26 +01:00
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
it "initializes deps" do
|
|
|
|
expect(env.deps).to eq([])
|
|
|
|
expect(env.keg_only_deps).to eq([])
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 14:50:29 -05:00
|
|
|
describe "#cxx11" do
|
2024-08-22 17:42:46 -04:00
|
|
|
it "supports gcc-11" do
|
|
|
|
env["HOMEBREW_CC"] = "gcc-11"
|
2021-01-31 14:50:29 -05:00
|
|
|
env.cxx11
|
|
|
|
expect(env["HOMEBREW_CCCFG"]).to include("x")
|
2024-08-22 17:42:46 -04:00
|
|
|
expect(env["HOMEBREW_CCCFG"]).not_to include("g")
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports clang" do
|
|
|
|
env["HOMEBREW_CC"] = "clang"
|
|
|
|
env.cxx11
|
|
|
|
expect(env["HOMEBREW_CCCFG"]).to include("x")
|
|
|
|
expect(env["HOMEBREW_CCCFG"]).to include("g")
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
2022-08-04 18:11:35 -07:00
|
|
|
|
|
|
|
describe "#set_debug_symbols" do
|
|
|
|
it "sets the debug symbols flag" do
|
|
|
|
env.set_debug_symbols
|
|
|
|
expect(env["HOMEBREW_CCCFG"]).to include("D")
|
|
|
|
end
|
|
|
|
end
|
2017-02-25 10:09:26 +01:00
|
|
|
end
|
|
|
|
end
|