Commit ec0d1f5f authored by lauraMon's avatar lauraMon

Refactored a little and added more specs

parent 214a372a
......@@ -13,14 +13,13 @@ module API
post '/lint' do
result = Gitlab::Ci::YamlProcessor.new(params[:content], user: current_user).execute
error = result.errors.first
warning = result.warnings.first
status 200
response = if error.blank?
{ status: 'valid', errors: [], warnings: [warning] }
{ status: 'valid', errors: [], warnings: result.warnings }
else
{ status: 'invalid', errors: [error], warnings: [] }
{ status: 'invalid', errors: [error], warnings: result.warnings }
end
response.tap do |response|
......
......@@ -9,12 +9,13 @@ RSpec.describe API::Lint do
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
end
it 'passes validation' do
it 'passes validation without warnings' do
post api('/ci/lint'), params: { content: yaml_content }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_an Hash
expect(json_response['status']).to eq('valid')
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq([])
end
......@@ -26,8 +27,8 @@ RSpec.describe API::Lint do
end
end
context 'with valid .gitlab-ci.yaml but with warnings' do
let(:yaml_content) { { job: { script: 'ls', rules: [{ when: 'always' }] } }.to_yaml }
context 'with valid .gitlab-ci.yaml with warnings' do
let(:yaml_content) { { job: { script: 'ls', rules: [{ when: 'always' }] } }.to_yaml }
it 'passes validation but raises a warning' do
post api('/ci/lint'), params: { content: yaml_content }
......@@ -48,6 +49,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('invalid')
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq(['Invalid configuration format'])
end
......@@ -67,6 +69,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('invalid')
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
end
......@@ -106,7 +109,7 @@ RSpec.describe API::Lint do
end
end
RSpec.shared_examples 'valid config' do
RSpec.shared_examples 'valid config without warnings' do
it 'passes validation' do
ci_lint
......@@ -118,6 +121,7 @@ RSpec.describe API::Lint do
expect(json_response).to be_an Hash
expect(json_response['merged_yaml']).to eq(expected_yaml)
expect(json_response['valid']).to eq(true)
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq([])
end
end
......@@ -129,6 +133,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['merged_yaml']).to eq(yaml_content)
expect(json_response['valid']).to eq(false)
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
end
end
......@@ -181,6 +186,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['merged_yaml']).to eq(nil)
expect(json_response['valid']).to eq(false)
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq(['Insufficient permissions to create a new pipeline'])
end
end
......@@ -210,7 +216,7 @@ RSpec.describe API::Lint do
)
end
it_behaves_like 'valid config'
it_behaves_like 'valid config without warnings'
end
end
end
......@@ -266,13 +272,13 @@ RSpec.describe API::Lint do
context 'when running as dry run' do
let(:dry_run) { true }
it_behaves_like 'valid config'
it_behaves_like 'valid config without warnings'
end
context 'when running static validation' do
let(:dry_run) { false }
it_behaves_like 'valid config'
it_behaves_like 'valid config without warnings'
end
context 'With warnings' do
......
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