|
| 1 | +# typed: false |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require 'test_helper' |
| 5 | + |
| 6 | +module Packwerk |
| 7 | + module FolderVisibility |
| 8 | + class CheckerTest < Minitest::Test |
| 9 | + extend T::Sig |
| 10 | + include FactoryHelper |
| 11 | + include RailsApplicationFixtureHelper |
| 12 | + |
| 13 | + setup do |
| 14 | + setup_application_fixture |
| 15 | + end |
| 16 | + |
| 17 | + teardown do |
| 18 | + teardown_application_fixture |
| 19 | + end |
| 20 | + |
| 21 | + true_ = true |
| 22 | + |
| 23 | + [ |
| 24 | + # enforce, pack, referencing pack invalid? note |
| 25 | + [false, 'packs/a', 'packs/b/c/d/e/f', false, 'turned off'], |
| 26 | + [true_, 'packs/a', 'packs/b', false, 'siblings are ok'], |
| 27 | + [true_, 'packs/a', 'packs/c', false, 'siblings are ok'], |
| 28 | + [true_, 'packs/a/packs/1', 'packs/a/packs/2', false, 'siblings are ok'], |
| 29 | + [true_, 'packs/a/packs/1', 'packs/a/packs', false, 'access from parent is ok'], |
| 30 | + [true_, 'packs/a/packs/1', 'packs/a', false, 'access from parent of parent is ok'], |
| 31 | + [true_, 'packs/a/packs/1', 'packs', false, 'access from parent of parent is ok'], |
| 32 | + [true_, 'packs/a/packs/1', '.', false, 'access from root pack is ok'], |
| 33 | + |
| 34 | + [true_, 'packs/a', 'packs/b/c', true_, 'not siblings or child'], |
| 35 | + [true_, 'packs/a', 'packs/b/packs/c', true_, 'not siblings or child'], |
| 36 | + [true_, 'packs/a/packs/1', 'packs/b/packs/1', true_, 'not siblings or child'], |
| 37 | + [true_, 'packs/a', 'packs/a/packs/1', true_, 'access to parent not ok'], |
| 38 | + [true_, 'packs/b', 'packs/a/packs/1', true_, 'not siblings or child'] |
| 39 | + ].each do |test| |
| 40 | + test "if #{test[1]} has enforce_folder_visibility: #{test[0]} than a reference from #{test[2]} is #{test[3] ? 'A VIOLATION' : 'OK'}" do |
| 41 | + source_package = Packwerk::Package.new(name: test[2]) |
| 42 | + destination_package = Packwerk::Package.new(name: test[1], config: { 'enforce_folder_visibility' => test[0] }) |
| 43 | + reference = build_reference( |
| 44 | + source_package: source_package, |
| 45 | + destination_package: destination_package |
| 46 | + ) |
| 47 | + |
| 48 | + assert_equal test[3], folder_visibility_checker.invalid_reference?(reference) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + test 'provides a useful message' do |
| 53 | + assert_equal folder_visibility_checker.message(build_reference), <<~MSG.chomp |
| 54 | + Folder Visibility violation: '::SomeName' belongs to 'components/destination', which is not visible to 'components/source' as it is not a sibling pack or parent pack. |
| 55 | + Is there a different package to use instead, or should 'components/destination' also be visible to 'components/source'? |
| 56 | +
|
| 57 | + Inference details: this is a reference to ::SomeName which seems to be defined in some/location.rb. |
| 58 | + To receive help interpreting or resolving this error message, see: https://github.com/Shopify/packwerk/blob/main/TROUBLESHOOT.md#Troubleshooting-violations |
| 59 | + MSG |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + sig { returns(Checker) } |
| 65 | + def folder_visibility_checker |
| 66 | + FolderVisibility::Checker.new |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | +end |
0 commit comments