Class: PackageProtections::Private::OutgoingDependencyProtection

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
PackageProtections::ProtectionInterface
Defined in:
lib/package_protections/private/outgoing_dependency_protection.rb

Constant Summary collapse

IDENTIFIER =
'prevent_this_package_from_violating_its_stated_dependencies'

Instance Method Summary collapse

Methods included from PackageProtections::ProtectionInterface

#default_behavior, #get_offenses, #supports_violation_behavior?

Instance Method Details

#get_offenses_for_existing_violations(protected_packages) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/package_protections/private/outgoing_dependency_protection.rb', line 78

def get_offenses_for_existing_violations(protected_packages)
  protected_packages.flat_map do |protected_package|
    violation_behavior = protected_package.violation_behavior_for(identifier)

    case violation_behavior
    when ViolationBehavior::FailNever, ViolationBehavior::FailOnNew
      []
    when ViolationBehavior::FailOnAny
      listed_violations = protected_package.violations.select(&:dependency?).flat_map do |violation|
        PerFileViolation.from(violation, protected_package.original_package)
      end

      listed_violations.flat_map do |per_file_violation|
        Offense.new(
          file: per_file_violation.filepath,
          message: message_for_fail_on_any(per_file_violation),
          violation_type: identifier,
          package: protected_package.original_package
        )
      end
    else
      T.absurd(violation_behavior)
    end
  end
end

#get_offenses_for_new_violations(new_violations) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/package_protections/private/outgoing_dependency_protection.rb', line 48

def get_offenses_for_new_violations(new_violations)
  new_violations.select(&:dependency?).flat_map do |per_file_violation|
    reference_source_package = Private.get_package_with_name(per_file_violation.reference_source_package.name)
    violation_behavior = reference_source_package.violation_behavior_for(identifier)

    case violation_behavior
    when ViolationBehavior::FailNever
      next []
    when ViolationBehavior::FailOnNew
      message = message_for_fail_on_new(per_file_violation)
    when ViolationBehavior::FailOnAny
      message = message_for_fail_on_any(per_file_violation)
    else
      T.absurd(violation_behavior)
    end

    Offense.new(
      file: per_file_violation.filepath,
      message: message,
      violation_type: identifier,
      package: reference_source_package.original_package
    )
  end
end

#humanized_protection_descriptionObject



35
36
37
38
39
40
41
# File 'lib/package_protections/private/outgoing_dependency_protection.rb', line 35

def humanized_protection_description
  <<~MESSAGE
    To resolve these violations, should you add a dependency in the client's `package.yml`?
    Is the code referencing the constant, and the referenced constant, in the right packages?
    See https://go/packwerk_cheatsheet_dependency for more info.
  MESSAGE
end

#humanized_protection_nameObject



30
31
32
# File 'lib/package_protections/private/outgoing_dependency_protection.rb', line 30

def humanized_protection_name
  'Dependency Violations'
end

#identifierObject



14
15
16
# File 'lib/package_protections/private/outgoing_dependency_protection.rb', line 14

def identifier
  IDENTIFIER
end

#unmet_preconditions_for_behavior(behavior, package) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/package_protections/private/outgoing_dependency_protection.rb', line 19

def unmet_preconditions_for_behavior(behavior, package)
  if behavior.enabled? && !package.enforces_dependencies?
    "Package #{package.name} must have `enforce_dependencies: true` to use this protection"
  elsif !behavior.enabled? && package.enforces_dependencies?
    "Package #{package.name} must have `enforce_dependencies: false` to turn this protection off"
  else
    nil
  end
end