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.
This commit is contained in:
Yorick Peterse 2019-03-12 14:04:05 +01:00
parent 018fc6c696
commit ae9838d035
No known key found for this signature in database
GPG key ID: EDD30D2BEB691AC9
2 changed files with 24 additions and 0 deletions

View file

@ -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

View file

@ -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