Skip to content

Instantly share code, notes, and snippets.

@duduribeiro
Forked from alexevanczuk/LICENSE
Created April 26, 2023 16:06
Show Gist options
  • Save duduribeiro/ee5e328530b4a38671d60a7a47f10aa5 to your computer and use it in GitHub Desktop.
Save duduribeiro/ee5e328530b4a38671d60a7a47f10aa5 to your computer and use it in GitHub Desktop.
Code for conditional builds
require 'parse_packwerk'
class AffectedPackagesHelper
attr_reader :strict
def initialize(modified_packs)
@packs = modified_packs
@memo = {}
@all_packages ||= ParsePackwerk.all
@strict = true
@all_inbound_violations = generate_inbound_package_todo_hash
end
def dependent_packs
@packs.each do |pack|
inbound_dependencies_traversal(pack)
end
@memo.keys
end
def strict=(val)
if @strict != val
@memo = {}
@strict = val
end
end
# Returns an array of packnames of packs with `enforce_dependencies: false` as
# a stopgap until we compute dependencies at runtime or pulled from cache.
def packs_without_enforce_dependencies
@all_packages.reject(&:enforces_dependencies?).map(&:name)
end
private
# Creates a hash with { key: String package_name, value: Array(String) package_names
# that are deprecation violations definined in the key's package_todo.yml
def generate_package_todo_hash
@all_packages.map.each_with_object({}) { |package, h|
h[package.name] = package.violations
.select(&:dependency?)
.map(&:to_package_name)
.uniq
}
end
# Creates a hash with { key: Sring package_name, value: Array(String) package_names
# that are inbound deprecation violations definined in the value's package_todo.yml
def generate_inbound_package_todo_hash
generate_package_todo_hash.each_with_object({}) do |(k, arr), h|
arr.each do |v|
(h[v] ||= []) << k
end
end
end
# Using BFS traversal, finds list of explicitly dependent packages defined
# by each pack's `package.yml` and adds it to the memoized traversed packages
# array @memo.
#
# @param String the name of a pack in `packs/some_pack` format
def inbound_dependencies_traversal(package_name)
return if @memo.key?(package_name)
queue = [package_name]
while !queue.empty?
curr_package = queue.pop
# Package does not exist or it has already been evaluated
next if @memo.key?(curr_package) || !curr_package
inbound_dependencies = @memo[curr_package] || @all_packages.select { |package| package.dependencies.include?(curr_package) }.map(&:name)
# Also loop in package_todo as dependencies
if strict
# The root pack does not have any inbound violations
package_todo = @all_inbound_violations[curr_package] || []
inbound_dependencies = (inbound_dependencies + package_todo).uniq
end
@memo[curr_package] = inbound_dependencies
inbound_dependencies.each do |dependency|
if !@memo.key?(dependency)
queue << dependency
end
end
end
end
end
RSpec.describe AffectedPackagesHelper do
describe '#dependent_packs' do
subject do
helper = described_class.new(['packs/bar'])
helper.strict = strict
helper
end
let(:strict) { false }
let(:package_foo) do
ParsePackwerk::Package.new(
name: 'packs/foo',
enforce_privacy: true,
enforce_dependencies: true,
dependencies: ['packs/bar'],
metadata: {},
config: {}
)
end
let(:package_bar) do
ParsePackwerk::Package.new(
name: 'packs/bar',
enforce_privacy: true,
enforce_dependencies: true,
dependencies: [],
metadata: {},
config: {}
)
end
let(:package_baz) do
ParsePackwerk::Package.new(
name: 'packs/baz',
enforce_privacy: true,
enforce_dependencies: true,
dependencies: [],
metadata: {},
config: {}
)
end
let(:package_qux) do
ParsePackwerk::Package.new(
name: 'packs/qux',
enforce_privacy: true,
enforce_dependencies: false,
dependencies: [],
metadata: {},
config: {}
)
end
let(:dependency_violation) do
ParsePackwerk::Violation.new(
class_name: 'FakeClass',
files: ['packs/bar/fake_class.rb'],
to_package_name: 'packs/bar',
type: 'dependency'
)
end
let(:privacy_violation) do
ParsePackwerk::Violation.new(
class_name: 'FakeClass',
files: ['packs/bar/fake_class.rb'],
to_package_name: 'packs/bar',
type: 'privacy'
)
end
let(:all_packages) { [package_foo, package_bar, package_baz, package_qux] }
before do
allow(ParsePackwerk).to receive(:all).and_return(all_packages)
end
context 'when strict is true' do
let(:strict) { true }
context 'when there are no violations' do
it 'returns the affected packages' do
expect(subject.dependent_packs).to match_array(['packs/bar', 'packs/foo'])
end
end
context 'when there are violations' do
before do
allow(package_baz).to receive(:violations).and_return([dependency_violation, privacy_violation])
end
it 'returns packs that are deprecated dependency violations' do
expect(subject.dependent_packs).to match_array(['packs/bar', 'packs/foo', 'packs/baz'])
end
it 'returns unique set of packs' do
allow(package_baz).to receive(:violations).and_return([dependency_violation, dependency_violation])
expect(subject.dependent_packs).to match_array(['packs/bar', 'packs/foo', 'packs/baz'])
end
end
end
context 'when strict is false' do
let(:strict) { false }
context 'when there are no violations' do
it 'returns the affected packages' do
expect(subject.dependent_packs).to match_array(['packs/bar', 'packs/foo'])
end
end
context 'when there are violations' do
it 'returns packs without the deprecated dependency violations' do
allow(package_baz).to receive(:violations).and_return([dependency_violation])
expect(subject.dependent_packs).to match_array(['packs/bar', 'packs/foo'])
end
end
end
context 'packs_without_enforce_dependencies' do
it 'returns the names of the packs without `enforce_dependencies: true`' do
expect(subject.packs_without_enforce_dependencies).to match_array(['packs/qux'])
end
end
end
end
MIT License
Copyright (c) 2022 Gusto
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment