2017-05-25 08:53:03 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::Utils do
|
2018-03-06 16:16:55 -05:00
|
|
|
delegate :to_boolean, :boolean_to_yes_no, :slugify, :random_string, :which, :ensure_array_from_string, to: :described_class
|
2017-08-07 14:00:11 -04:00
|
|
|
|
|
|
|
describe '.slugify' do
|
|
|
|
{
|
|
|
|
'TEST' => 'test',
|
|
|
|
'project_with_underscores' => 'project-with-underscores',
|
|
|
|
'namespace/project' => 'namespace-project',
|
|
|
|
'a' * 70 => 'a' * 63,
|
|
|
|
'test_trailing_' => 'test-trailing'
|
|
|
|
}.each do |original, expected|
|
|
|
|
it "slugifies #{original} to #{expected}" do
|
|
|
|
expect(slugify(original)).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-28 16:56:13 -04:00
|
|
|
|
2018-01-05 16:36:18 -05:00
|
|
|
describe '.remove_line_breaks' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:original, :expected) do
|
|
|
|
"foo\nbar\nbaz" | "foobarbaz"
|
|
|
|
"foo\r\nbar\r\nbaz" | "foobarbaz"
|
|
|
|
"foobar" | "foobar"
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it "replace line breaks with an empty string" do
|
|
|
|
expect(described_class.remove_line_breaks(original)).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-28 16:56:13 -04:00
|
|
|
describe '.to_boolean' do
|
|
|
|
it 'accepts booleans' do
|
|
|
|
expect(to_boolean(true)).to be(true)
|
|
|
|
expect(to_boolean(false)).to be(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts a valid string to a boolean' do
|
|
|
|
expect(to_boolean(true)).to be(true)
|
|
|
|
expect(to_boolean('true')).to be(true)
|
|
|
|
expect(to_boolean('YeS')).to be(true)
|
|
|
|
expect(to_boolean('t')).to be(true)
|
|
|
|
expect(to_boolean('1')).to be(true)
|
|
|
|
expect(to_boolean('ON')).to be(true)
|
|
|
|
|
|
|
|
expect(to_boolean('FaLse')).to be(false)
|
|
|
|
expect(to_boolean('F')).to be(false)
|
|
|
|
expect(to_boolean('NO')).to be(false)
|
|
|
|
expect(to_boolean('n')).to be(false)
|
|
|
|
expect(to_boolean('0')).to be(false)
|
|
|
|
expect(to_boolean('oFF')).to be(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts an invalid string to nil' do
|
|
|
|
expect(to_boolean('fals')).to be_nil
|
|
|
|
expect(to_boolean('yeah')).to be_nil
|
|
|
|
expect(to_boolean('')).to be_nil
|
|
|
|
expect(to_boolean(nil)).to be_nil
|
|
|
|
end
|
2017-05-25 08:53:03 -04:00
|
|
|
end
|
2017-05-25 07:31:21 -04:00
|
|
|
|
2017-05-25 08:53:03 -04:00
|
|
|
describe '.boolean_to_yes_no' do
|
2017-05-25 07:31:21 -04:00
|
|
|
it 'converts booleans to Yes or No' do
|
|
|
|
expect(boolean_to_yes_no(true)).to eq('Yes')
|
|
|
|
expect(boolean_to_yes_no(false)).to eq('No')
|
|
|
|
end
|
2016-10-28 16:56:13 -04:00
|
|
|
end
|
2017-07-20 11:32:17 -04:00
|
|
|
|
|
|
|
describe '.random_string' do
|
|
|
|
it 'generates a string' do
|
|
|
|
expect(random_string).to be_kind_of(String)
|
|
|
|
end
|
|
|
|
end
|
2017-11-08 15:53:10 -05:00
|
|
|
|
|
|
|
describe '.which' do
|
|
|
|
it 'finds the full path to an executable binary' do
|
|
|
|
expect(File).to receive(:executable?).with('/bin/sh').and_return(true)
|
|
|
|
|
|
|
|
expect(which('sh', 'PATH' => '/bin')).to eq('/bin/sh')
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 16:16:55 -05:00
|
|
|
|
|
|
|
describe '.ensure_array_from_string' do
|
|
|
|
it 'returns the same array if given one' do
|
|
|
|
arr = ['a', 4, true, { test: 1 }]
|
|
|
|
|
|
|
|
expect(ensure_array_from_string(arr)).to eq(arr)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'turns comma-separated strings into arrays' do
|
|
|
|
str = 'seven, eight, 9, 10'
|
|
|
|
|
|
|
|
expect(ensure_array_from_string(str)).to eq(%w[seven eight 9 10])
|
|
|
|
end
|
|
|
|
end
|
2016-10-28 16:56:13 -04:00
|
|
|
end
|