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

Add GitHub CI #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI

on:
pull_request:
push:
branches:
- master

jobs:
test:
runs-on: ubuntu-20.04
env:
MIX_ENV: test
strategy:
fail-fast: false
matrix:
include:
- pair:
elixir: 1.11.x
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I still retain the support of Elixir 1.11.x, not sure if we want to bump to 1.13, which is the minimal version that still have security support.,

otp: 21.x
- pair:
elixir: 1.17.x
otp: 27.x
lint: lint
steps:
- uses: actions/checkout@v4

- uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.pair.otp}}
elixir-version: ${{matrix.pair.elixir}}

- uses: actions/cache@v4
with:
path: |
deps
_build
key: ${{ runner.os }}-mix-${{matrix.pair.elixir}}-${{matrix.pair.otp}}-${{ hashFiles('**/mix.lock') }}

- run: mix deps.get

- run: mix format --check-formatted
if: ${{ matrix.lint }}

- run: mix deps.unlock --check-unused
if: ${{ matrix.lint }}

- run: mix deps.compile

- run: mix compile --warnings-as-errors
if: ${{ matrix.lint }}

- run: mix test
83 changes: 50 additions & 33 deletions lib/sizeable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ defmodule Sizeable do
filesize(parsed, options)
end

def filesize(0.0, options) do
def filesize(+0.0, options) do
spacer = Keyword.get(options, :spacer, " ")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

warning: pattern matching on 0.0 is equivalent to matching only on +0.0 from Erlang/OTP 27+. Instead you must match on +0.0 or -0.0
└─ lib/sizeable.ex: Sizeable.filesize/2

bits = Keyword.get(options, :bits, false)
output = Keyword.get(options, :output, :string)

{:ok, unit} = case bits do
true -> Enum.fetch(@bits, 0)
false -> Enum.fetch(@bytes, 0)
end
{:ok, unit} =
case bits do
true -> Enum.fetch(@bits, 0)
false -> Enum.fetch(@bytes, 0)
end

filesize_output(output, 0, unit, spacer)
end
Expand Down Expand Up @@ -73,45 +74,61 @@ defmodule Sizeable do
"8 Kb"

"""
def filesize(value, options) when (is_float(value) and is_list(options)) do
def filesize(value, options) when is_float(value) and is_list(options) do
bits = Keyword.get(options, :bits, false)
base = Keyword.get(options, :base, 2)
spacer = Keyword.get(options, :spacer, " ")
round = Keyword.get(options, :round, 2)
output = Keyword.get(options, :output, :string)

ceil = if base > 2 do 1000 else 1024 end
neg = value < 0
ceil =
if base > 2 do
1000
else
1024
end

value = case neg do
true -> -value
false -> value
end

value = if bits do 8 * value else value end
neg = value < 0

{exponent, _rem} = :math.log(value)/:math.log(ceil)
|> Float.floor
|> Float.to_string
|> Integer.parse
value =
case neg do
true -> -value
false -> value
end

value =
if bits do
8 * value
else
value
end

{exponent, _rem} =
(:math.log(value) / :math.log(ceil))
|> Float.floor()
|> Float.to_string()
|> Integer.parse()

result = Float.round(value / :math.pow(ceil, exponent), base)

result = if Float.floor(result) == result do
round result
else
Float.round(result, round)
end

{:ok, unit} = case bits do
true -> Enum.fetch(@bits, exponent)
false -> Enum.fetch(@bytes, exponent)
end

result = case neg do
true -> result * -1
false -> result
end
result =
if Float.floor(result) == result do
round(result)
else
Float.round(result, round)
end

{:ok, unit} =
case bits do
true -> Enum.fetch(@bits, exponent)
false -> Enum.fetch(@bytes, exponent)
end

result =
case neg do
true -> result * -1
false -> result
end

filesize_output(output, result, unit, spacer)
end
Expand Down
5 changes: 2 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ defmodule Sizeable.Mixfile do
links: %{
"Changelog" => "#{@source_url}/blob/master/CHANGELOG.md",
"GitHub" => @source_url
},
}
]
end

defp deps do
[
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:inch_ex, only: :docs}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we're not using Inch, remove this.

{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
]
end

Expand Down
9 changes: 0 additions & 9 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
%{
"earmark": {:hex, :earmark, "1.0.1", "2c2cd903bfdc3de3f189bd9a8d4569a075b88a8981ded9a0d95672f6e2b63141", [:mix], [], "hexpm", "db7b13d74a9edc54d3681762154d164d4a661cd27673cca80760344449877664"},
"earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"},
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
"inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm", "7123ca0450686a61416a06cd38e26af18fd0f8c1cff5214770a957c6e0724338"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"},
}
10 changes: 6 additions & 4 deletions test/sizeable_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ defmodule SizeableTest do
end

test "edgecase round" do
assert Sizeable.filesize(@edgecase, [round: 1]) == "1023 B"
assert Sizeable.filesize(@edgecase, round: 1) == "1023 B"
end

# Tests for byte values
Expand Down Expand Up @@ -171,8 +171,10 @@ defmodule SizeableTest do
end

test "ouput invalid" do
assert_raise RuntimeError, "Invalid `array` output value, possible options are :string, :list, :map", fn ->
Sizeable.filesize(@kilobyte, output: :array)
end
assert_raise RuntimeError,
"Invalid `array` output value, possible options are :string, :list, :map",
fn ->
Sizeable.filesize(@kilobyte, output: :array)
end
end
end