Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort arrays before output #63

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/cyclonedx/cocoapods/bom_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ class BOMBuilder
attr_reader :component, :pods, :dependencies

def initialize(pods:, component: nil, dependencies: nil)
@pods = pods
@pods = pods.sort_by(&:purl)
@component = component
@dependencies = dependencies
@dependencies = dependencies&.sort
end

def bom(version: 1)
Expand All @@ -152,7 +152,7 @@ def bom(version: 1)
def bom_dependencies(xml, dependencies)
dependencies&.each do |key, array|
xml.dependency(ref: key) do
array.each do |value|
array.sort.each do |value|
xml.dependency(ref: value)
end
end
Expand Down
90 changes: 65 additions & 25 deletions spec/cyclonedx/cocoapods/bom_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
context 'which includes text' do
let(:license) {
license_with_text = described_class.new(identifier: described_class::SPDX_LICENSES.sample)
license_with_text.text = "Copyright 2012\nPermission is granted to..."
license_with_text.text = 'Copyright 2012\nPermission is granted to...'
license_with_text
}

Expand All @@ -198,7 +198,7 @@
context 'which includes url' do
let(:license) {
license_with_url = described_class.new(identifier: described_class::SPDX_LICENSES.sample)
license_with_url.url = "https://opensource.org/licenses/MIT"
license_with_url.url = 'https://opensource.org/licenses/MIT'
license_with_url
}

Expand All @@ -214,7 +214,7 @@

RSpec.describe CycloneDX::CocoaPods::Component do
context 'when generating a component in a BOM' do
shared_examples "component" do
shared_examples 'component' do
it 'should generate a root component element' do
expect(xml.at('/component')).not_to be_nil
expect(xml.at('/component')['type']).to eq(component.type)
Expand All @@ -232,7 +232,7 @@
let(:component) { described_class.new(name: 'Application', version: '1.3.5', type: 'application') }
let(:xml) { Nokogiri::XML(Nokogiri::XML::Builder.new(encoding: 'UTF-8') { |xml| component.add_to_bom(xml) }.to_xml) }

it_behaves_like "component"
it_behaves_like 'component'

it 'should not generate any group element' do
expect(xml.at('/component/group')).to be_nil
Expand All @@ -243,7 +243,7 @@
let(:component) { described_class.new(group: 'application-group', name: 'Application', version: '1.3.5', type: 'application') }
let(:xml) { Nokogiri::XML(Nokogiri::XML::Builder.new(encoding: 'UTF-8') { |xml| component.add_to_bom(xml) }.to_xml) }

it_behaves_like "component"
it_behaves_like 'component'

it 'should generate a proper group element' do
expect(xml.at('/component/group')).not_to be_nil
Expand All @@ -255,6 +255,7 @@

RSpec.describe CycloneDX::CocoaPods::BOMBuilder do
context 'when generating a BOM' do
# Important: these pods are NOT in alphabetical order; they will be sorted in output
let(:pods) do
{
'Alamofire' => '5.6.2',
Expand All @@ -265,6 +266,7 @@
'MSAL/app-lib' => '1.2.1'
}.map { |name, version| CycloneDX::CocoaPods::Pod.new(name: name, version: version) }
end
# Important: these dependencies are NOT in alphabetical order; they will be sorted in output
let(:dependencies) do
{
'pkg:cocoapods/[email protected]' => [],
Expand All @@ -275,7 +277,7 @@
}
end

shared_examples "bom_generator" do
shared_examples 'bom_generator' do
context 'with an incorrect version' do
it 'should raise for non integer versions' do
expect { bom_builder.bom(version: 'foo') }.to raise_error(ArgumentError)
Expand Down Expand Up @@ -362,25 +364,62 @@
it 'should properly delegate component node generation to pods' do
components_generated_from_bom_builder = xml.at('bom/components')

components = Nokogiri::XML(Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
xml.components(xmlns: described_class::NAMESPACE) do
bom_builder.pods.each { |pod| pod.add_to_bom(xml) }
end
end.to_xml).at('components')

expect(components_generated_from_bom_builder).to be_equivalent_to(components)
# Important: these expected components are sorted alphabetically
expected_components_string =
'<components>
<component type="library">
<name>Alamofire</name>
<version>5.6.2</version>
<purl>pkg:cocoapods/[email protected]</purl>
<bomRef>pkg:cocoapods/[email protected]</bomRef>
</component>
<component type="library">
<name>FirebaseAnalytics</name>
<version>7.10.0</version>
<purl>pkg:cocoapods/[email protected]</purl>
<bomRef>pkg:cocoapods/[email protected]</bomRef>
</component>
<component type="library">
<name>MSAL</name>
<version>1.2.1</version>
<purl>pkg:cocoapods/[email protected]</purl>
<bomRef>pkg:cocoapods/[email protected]</bomRef>
</component>
<component type="library">
<name>MSAL/app-lib</name>
<version>1.2.1</version>
<purl>pkg:cocoapods/[email protected]#app-lib</purl>
<bomRef>pkg:cocoapods/[email protected]#app-lib</bomRef>
</component>
<component type="library">
<name>Realm</name>
<version>5.5.1</version>
<purl>pkg:cocoapods/[email protected]</purl>
<bomRef>pkg:cocoapods/[email protected]</bomRef>
</component>
<component type="library">
<name>RxSwift</name>
<version>5.1.2</version>
<purl>pkg:cocoapods/[email protected]</purl>
<bomRef>pkg:cocoapods/[email protected]</bomRef>
</component>
</components>'

components = Nokogiri::XML expected_components_string

expect(components_generated_from_bom_builder.to_xml).to be_equivalent_to(components.root.to_xml).respecting_element_order
end

it 'should generate a child dependencies node' do
expect(xml.at('bom/dependencies')).not_to be_nil
end

it 'shoudl properly set dependencies node' do
it 'should properly set dependencies node' do
dependencies_generated_from_bom_builder = xml.at('bom/dependencies')

dependencies = Nokogiri::XML dependencies_result

expect(dependencies_generated_from_bom_builder.to_xml).to be_equivalent_to(dependencies.root.to_xml)
expect(dependencies_generated_from_bom_builder.to_xml).to be_equivalent_to(dependencies.root.to_xml).respecting_element_order
end
end
end
Expand All @@ -389,25 +428,26 @@
let(:bom_builder) { described_class.new(pods: pods) }
let(:dependencies_result) { '<dependencies/>' }

it_behaves_like "bom_generator"
it_behaves_like 'bom_generator'
end

context 'with a component' do
let(:component) { CycloneDX::CocoaPods::Component.new(name: 'Application', version: '1.3.5', type: 'application') }
let(:bom_builder) { described_class.new(component: component, pods: pods, dependencies: dependencies) }
# Important: these expected dependencies are sorted alphabetically
let(:dependencies_result) do
'<dependencies>
<dependency ref="pkg:cocoapods/[email protected]"/>
<dependency ref="pkg:cocoapods/[email protected]">
<dependency ref="pkg:cocoapods/[email protected]#app-lib"/>
</dependency>
<dependency ref="pkg:cocoapods/[email protected]"/>
<dependency ref="pkg:cocoapods/RxSwift@5.1.2"/>
<dependency ref="pkg:cocoapods/Realm@5.5.1"/>
</dependencies>'
<dependency ref="pkg:cocoapods/[email protected]"/>
<dependency ref="pkg:cocoapods/[email protected]"/>
<dependency ref="pkg:cocoapods/[email protected]">
<dependency ref="pkg:cocoapods/[email protected]#app-lib"/>
</dependency>
<dependency ref="pkg:cocoapods/Realm@5.5.1"/>
<dependency ref="pkg:cocoapods/RxSwift@5.1.2"/>
</dependencies>'
end

it_behaves_like "bom_generator"
it_behaves_like 'bom_generator'
end
end
end