Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex: Handle additional dynamic version reading approaches #4442

Merged
merged 4 commits into from
Nov 30, 2021
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
30 changes: 27 additions & 3 deletions hex/lib/dependabot/hex/file_updater/mixfile_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,40 @@ def initialize(mixfile_content:)
@mixfile_content = mixfile_content
end

FILE_READ = /File.read\(.*?\)/.freeze
FILE_READ_BANG = /File.read!\(.*?\)/.freeze
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (non-blocking): it looks like we're trying to match File.read. Do we also need to match IO.read and/or Pathname#read?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't. These are matching Elixir functions what share a name with Ruby methods. They do have a very familiar feel as the language's creator came out of the Ruby community 😄

PIPE = Regexp.escape("|>").freeze
VERSION_FILE = /"VERSION"/i.freeze

NESTED_VERSION_FILE_READ = /String\.trim\(#{FILE_READ}\)/.freeze
NESTED_VERSION_FILE_READ_BANG = /String\.trim\(#{FILE_READ_BANG}\)/.freeze
PIPED_VERSION_FILE_READ =
/#{VERSION_FILE}[[:space:]]+#{PIPE}[[:space:]]+#{FILE_READ}/.freeze
PIPED_VERSION_FILE_READ_BANG =
/#{VERSION_FILE}[[:space:]]+#{PIPE}[[:space:]]+#{FILE_READ_BANG}/.freeze

def sanitized_content
mixfile_content.
gsub(/File\.read!\(.*?\)/, '"0.0.1"').
gsub(/File\.read\(.*?\)/, '{:ok, "0.0.1"}').
gsub(/^\s*config_path:.*(?:,|$)/, "")
yield_self(&method(:prevent_version_file_loading)).
yield_self(&method(:prevent_config_path_loading))
end

private

attr_reader :mixfile_content

def prevent_version_file_loading(configuration)
configuration.
gsub(NESTED_VERSION_FILE_READ_BANG, 'String.trim("0.0.1")').
gsub(NESTED_VERSION_FILE_READ, 'String.trim({:ok, "0.0.1"})').
gsub(PIPED_VERSION_FILE_READ, '{:ok, "0.0.1"}').
gsub(PIPED_VERSION_FILE_READ_BANG, '"0.0.1"')
end

def prevent_config_path_loading(configuration)
configuration.
gsub(/^\s*config_path:.*(?:,|$)/, "")
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions hex/spec/dependabot/hex/update_checker/file_preparer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@
to include('@version String.trim({:ok, "0.0.1"})')
end
end

context "when file loading is done with pipes" do
let(:mixfile_fixture_name) { "loads_file_with_pipes" }

it "removes the call to load the file" do
expect(prepared_mixfile.content).
to include('@version {:ok, "0.0.1"} |> String.trim()')
end
end

context "when file loading is done with pipes and a !" do
let(:mixfile_fixture_name) { "loads_file_with_pipes_and_bang" }

it "removes the call to load the file" do
expect(prepared_mixfile.content).
to include('@version "0.0.1" |> String.trim()')
end
end
end

context "with unlock_requirement set to false" do
Expand Down
26 changes: 26 additions & 0 deletions hex/spec/fixtures/mixfiles/loads_file_with_pipes
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule DependabotTest.Mixfile do
use Mix.Project

@version "VERSION" |> File.read() |> String.trim()

def project do
[
app: :dependabot_test,
version: @version,
elixir: "~> 1.5",
start_permanent: Mix.env == :prod,
deps: deps()
]
end

def application do
[extra_applications: [:logger]]
end

defp deps do
[
{:plug, "1.3.0"},
{:phoenix, "== 1.2.1"}
]
end
end
26 changes: 26 additions & 0 deletions hex/spec/fixtures/mixfiles/loads_file_with_pipes_and_bang
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule DependabotTest.Mixfile do
use Mix.Project

@version "VERSION" |> File.read!() |> String.trim()

def project do
[
app: :dependabot_test,
version: @version,
elixir: "~> 1.5",
start_permanent: Mix.env == :prod,
deps: deps()
]
end

def application do
[extra_applications: [:logger]]
end

defp deps do
[
{:plug, "1.3.0"},
{:phoenix, "== 1.2.1"}
]
end
end