Commit ae9838d0 authored by Yorick Peterse's avatar Yorick Peterse

Backport try_megabytes_to_bytes from EE

EE adds this method to Gitlab::Utils, which is also required by our
SimpleCov helper. This prevents us from injecting EE modules into
Gitlab::Utils, because the necessary bits for this are not yet in place.

To work around this we just backport try_megabytes_to_bytes, as there's
no particular reason to keep this in EE only.
parent 018fc6c6
...@@ -104,6 +104,12 @@ module Gitlab ...@@ -104,6 +104,12 @@ module Gitlab
nil nil
end end
def try_megabytes_to_bytes(size)
Integer(size).megabytes
rescue ArgumentError
size
end
def bytes_to_megabytes(bytes) def bytes_to_megabytes(bytes)
bytes.to_f / Numeric::MEGABYTE bytes.to_f / Numeric::MEGABYTE
end end
......
...@@ -213,4 +213,22 @@ describe Gitlab::Utils do ...@@ -213,4 +213,22 @@ describe Gitlab::Utils do
expect(subject[:variables].first[:key]).to eq('VAR1') expect(subject[:variables].first[:key]).to eq('VAR1')
end end
end end
describe '.try_megabytes_to_bytes' do
context 'when the size can be converted to megabytes' do
it 'returns the size in megabytes' do
size = described_class.try_megabytes_to_bytes(1)
expect(size).to eq(1.megabytes)
end
end
context 'when the size can not be converted to megabytes' do
it 'returns the input size' do
size = described_class.try_megabytes_to_bytes('foo')
expect(size).to eq('foo')
end
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment