-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formulae that depend on `node` sometimes contain files that use a shebang like `#!/usr/bin/env node` and this can lead to issues when the `node` in a user's environment isn't brewed `node`. For example, some node modules are compiled when the formula is built but if the user's `node` is a different major version than brew's `node`, the differing `NODE_MODULE_VERSION` can produce an error when certain parts of the application are used. The formula may build and test fine and the issue may only become apparent when more of the application is exercised. This adds a `Language::Node::Shebang` module (borrowing from the existing Perl and Python examples), which allows us to use `rewrite_shebang detected_node_shebang, ...` in formulae to address this type of issue.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# typed: strict | ||
|
||
module Language | ||
module Node | ||
module Shebang | ||
include Kernel | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
require "language/node" | ||
require "utils/shebang" | ||
|
||
describe Language::Node::Shebang do | ||
let(:file) { Tempfile.new("node-shebang") } | ||
let(:node18_f) do | ||
formula "node@18" do | ||
url "https://brew.sh/node-18.0.0.tgz" | ||
end | ||
end | ||
let(:f) do | ||
formula "foo" do | ||
url "https://brew.sh/foo-1.0.tgz" | ||
|
||
depends_on "node@18" | ||
end | ||
end | ||
|
||
before do | ||
file.write <<~EOS | ||
#!/usr/bin/env node | ||
a | ||
b | ||
c | ||
EOS | ||
file.flush | ||
end | ||
|
||
after { file.unlink } | ||
|
||
describe "#detected_node_shebang" do | ||
let(:f_no_deps) do | ||
formula "foo" do | ||
url "https://brew.sh/foo-1.0.tgz" | ||
end | ||
end | ||
|
||
let(:f_multiple_deps) do | ||
formula "foo" do | ||
url "https://brew.sh/foo-1.0.tgz" | ||
|
||
depends_on "node" | ||
depends_on "node@18" | ||
end | ||
end | ||
|
||
it "can be used to replace Node shebangs" do | ||
allow(Formulary).to receive(:factory) | ||
allow(Formulary).to receive(:factory).with(node18_f.name).and_return(node18_f) | ||
Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f), file.path | ||
|
||
expect(File.read(file)).to eq <<~EOS | ||
#!#{HOMEBREW_PREFIX/"opt/node@18/bin/node"} | ||
a | ||
b | ||
c | ||
EOS | ||
end | ||
|
||
it "errors if formula doesn't depend on node" do | ||
expect { Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f_no_deps), file.path } | ||
.to raise_error(ShebangDetectionError, "Cannot detect Node shebang: formula does not depend on Node.") | ||
end | ||
|
||
it "errors if formula depends on more than one node" do | ||
expect { Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f_multiple_deps), file.path } | ||
.to raise_error(ShebangDetectionError, "Cannot detect Node shebang: formula has multiple Node dependencies.") | ||
end | ||
end | ||
end |