Commit c1fb673a authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'debian_extract_metadata_service' into 'master'

Add Packages::Debian::ExtractMetadataService

See merge request gitlab-org/gitlab!50434
parents 8c5b7e92 bcecb0f7
# frozen_string_literal: true
module Packages
module Debian
class ExtractMetadataService
include Gitlab::Utils::StrongMemoize
ExtractionError = Class.new(StandardError)
def initialize(package_file)
@package_file = package_file
end
def execute
raise ExtractionError.new('invalid package file') unless valid_package_file?
extract_metadata
end
private
attr_reader :package_file
def valid_package_file?
package_file &&
package_file.package&.debian? &&
package_file.file.size > 0 # rubocop:disable Style/ZeroLengthPredicate
end
def file_type_basic
%i[dsc deb udeb buildinfo changes].each do |format|
return format if package_file.file_name.end_with?(".#{format}")
end
nil
end
def file_type_source
# https://manpages.debian.org/buster/dpkg-dev/dpkg-source.1.en.html
%i[gzip bzip2 lzma xz].each do |format|
return :source if package_file.file_name.end_with?(".tar.#{format}")
end
nil
end
def file_type
strong_memoize(:file_type) do
file_type_basic || file_type_source || :unknown
end
end
def file_type_debian?
file_type == :deb || file_type == :udeb
end
def file_type_meta?
file_type == :dsc || file_type == :buildinfo || file_type == :changes
end
def extracted_fields
if file_type_debian?
package_file.file.use_file do |file_path|
::Packages::Debian::ExtractDebMetadataService.new(file_path).execute
end
elsif file_type_meta?
package_file.file.use_file do |file_path|
::Packages::Debian::ParseDebian822Service.new(File.read(file_path)).execute.each_value.first
end
end
end
def extract_metadata
fields = extracted_fields
architecture = fields.delete(:Architecture) if file_type_debian?
{
file_type: file_type,
architecture: architecture,
fields: fields
}
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Debian::ExtractMetadataService do
let(:service) { described_class.new(package_file) }
subject { service.execute }
RSpec.shared_context 'Debian ExtractMetadata Service' do |trait|
let(:package_file) { create(:debian_package_file, trait) }
end
RSpec.shared_examples 'Test Debian ExtractMetadata Service' do |expected_file_type, expected_architecture, expected_fields|
it "returns file_type #{expected_file_type.inspect}" do
expect(subject[:file_type]).to eq(expected_file_type)
end
it "returns architecture #{expected_architecture.inspect}" do
expect(subject[:architecture]).to eq(expected_architecture)
end
it "returns fields #{expected_fields.nil? ? '' : 'including '}#{expected_fields.inspect}" do
if expected_fields.nil?
expect(subject[:fields]).to be_nil
else
expect(subject[:fields]).to include(**expected_fields)
end
end
end
using RSpec::Parameterized::TableSyntax
where(:case_name, :trait, :expected_file_type, :expected_architecture, :expected_fields) do
'with invalid' | :invalid | :unknown | nil | nil
'with source' | :source | :source | nil | nil
'with dsc' | :dsc | :dsc | nil | { 'Binary': 'sample-dev, libsample0, sample-udeb' }
'with deb' | :deb | :deb | 'amd64' | { 'Multi-Arch': 'same' }
'with udeb' | :udeb | :udeb | 'amd64' | { 'Package': 'sample-udeb' }
'with buildinfo' | :buildinfo | :buildinfo | nil | { 'Architecture': 'amd64 source', 'Build-Architecture': 'amd64' }
'with changes' | :changes | :changes | nil | { 'Architecture': 'source amd64', 'Binary': 'libsample0 sample-dev sample-udeb' }
end
with_them do
include_context 'Debian ExtractMetadata Service', params[:trait] do
it_behaves_like 'Test Debian ExtractMetadata Service',
params[:expected_file_type],
params[:expected_architecture],
params[:expected_fields]
end
end
context 'with invalid package file' do
let(:package_file) { create(:conan_package_file) }
it 'raise error' do
expect { subject }.to raise_error(described_class::ExtractionError, 'invalid package file')
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