Skip to content

Commit

Permalink
Fix parsing of requirements without spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmj committed Mar 3, 2016
1 parent 5a69a46 commit c566e59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/hex/version.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ defmodule Hex.Version do

defp custom_requirement(requirement) do
try do
req = String.split(requirement, " ", trim: true) |> custom_parse
req = String.split(requirement, " ", trim: true)
|> split_ops
|> custom_parse
{:ok, %Requirement{source: requirement, req: req}}
catch
:error ->
Expand All @@ -130,6 +132,17 @@ defmodule Hex.Version do
defp custom_parse(_),
do: throw :error

def split_ops([op|rest]) when op in @version_ops,
do: [op|split_ops(rest)]
def split_ops([<<op::binary-2, version::binary>>|rest]) when op in @version_ops,
do: [op, version|split_ops(rest)]
def split_ops([<<op::binary-1, version::binary>>|rest]) when op in @version_ops,
do: [op, version|split_ops(rest)]
def split_ops([version|rest]),
do: [version|split_ops(rest)]
def split_ops([]),
do: []

defp allow_pre? do
Code.ensure_loaded?(Version) and function_exported?(Version, :match?, 3)
end
Expand Down
10 changes: 10 additions & 0 deletions test/hex/version_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ defmodule Hex.VersionTest do
refute V.match?("1.1.0-beta", ">= 1.0.0")
assert V.match?("1.1.0-beta", ">= 1.0.0-beta")
end

test "parse requirement" do
assert {:ok, _} = V.parse_requirement("1.0.0")
assert {:ok, _} = V.parse_requirement("== 1.0.0")
assert {:ok, _} = V.parse_requirement("==1.0.0")
assert {:ok, _} = V.parse_requirement("== 1.0.0 and == 1.0.0")
assert {:ok, _} = V.parse_requirement("==1.0.0 and ==1.0.0")

assert :error = V.parse_requirement("foo")
end
end

0 comments on commit c566e59

Please sign in to comment.