|
| 1 | +# typed: true |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "test_helper" |
| 5 | + |
| 6 | +# make sure PrivateThing.constantize succeeds to pass the privacy validity check |
| 7 | +require "fixtures/skeleton/components/timeline/app/models/private_thing.rb" |
| 8 | + |
| 9 | +module Packwerk |
| 10 | + class CheckPackageManifestsForPrivacyTest < Minitest::Test |
| 11 | + extend T::Sig |
| 12 | + include RailsApplicationFixtureHelper |
| 13 | + |
| 14 | + setup do |
| 15 | + setup_application_fixture |
| 16 | + end |
| 17 | + |
| 18 | + teardown do |
| 19 | + teardown_application_fixture |
| 20 | + end |
| 21 | + |
| 22 | + test "check_package_manifests_for_privacy returns an error for unresolvable privatized constants" do |
| 23 | + use_template(:skeleton) |
| 24 | + ConstantResolver.expects(:new).returns(stub("resolver", resolve: nil)) |
| 25 | + |
| 26 | + result = Packwerk::ApplicationValidator::CheckPackageManifestsForPrivacy.call( |
| 27 | + validator.package_set, |
| 28 | + config |
| 29 | + ) |
| 30 | + |
| 31 | + refute result.ok?, result.error_value |
| 32 | + assert_match( |
| 33 | + /'::PrivateThing', listed in #{to_app_path('components\/timeline\/package.yml')}, could not be resolved/, |
| 34 | + result.error_value |
| 35 | + ) |
| 36 | + assert_match( |
| 37 | + /Add a private_thing.rb file/, |
| 38 | + result.error_value |
| 39 | + ) |
| 40 | + end |
| 41 | + |
| 42 | + test "check_package_manifests_for_privacy returns an error for privatized constants in other packages" do |
| 43 | + use_template(:skeleton) |
| 44 | + context = ConstantResolver::ConstantContext.new("::PrivateThing", "private_thing.rb") |
| 45 | + |
| 46 | + ConstantResolver.expects(:new).returns(stub("resolver", resolve: context)) |
| 47 | + |
| 48 | + result = Packwerk::ApplicationValidator::CheckPackageManifestsForPrivacy.call( |
| 49 | + validator.package_set, |
| 50 | + config |
| 51 | + ) |
| 52 | + |
| 53 | + refute result.ok?, result.error_value |
| 54 | + assert_match( |
| 55 | + %r{'::PrivateThing' is declared as private in the 'components/timeline' package}, |
| 56 | + result.error_value |
| 57 | + ) |
| 58 | + assert_match( |
| 59 | + /but appears to be defined\sin the '.' package/, |
| 60 | + result.error_value |
| 61 | + ) |
| 62 | + end |
| 63 | + |
| 64 | + test "check_package_manifests_for_privacy returns an error for constants without `::` prefix" do |
| 65 | + use_template(:minimal) |
| 66 | + merge_into_app_yaml_file("package.yml", { "enforce_privacy" => ["::PrivateThing", "OtherThing"] }) |
| 67 | + |
| 68 | + result = Packwerk::ApplicationValidator::CheckPackageManifestsForPrivacy.call( |
| 69 | + validator.package_set, |
| 70 | + config |
| 71 | + ) |
| 72 | + |
| 73 | + refute result.ok?, result.error_value |
| 74 | + assert_match( |
| 75 | + /'OtherThing', listed in the 'enforce_privacy' option in .*package.yml, is invalid./, |
| 76 | + result.error_value |
| 77 | + ) |
| 78 | + assert_match( |
| 79 | + /Private constants need to be prefixed with the top-level namespace operator `::`/, |
| 80 | + result.error_value |
| 81 | + ) |
| 82 | + end |
| 83 | + |
| 84 | + sig { returns(Packwerk::ApplicationValidator) } |
| 85 | + def validator |
| 86 | + @application_validator ||= Packwerk::ApplicationValidator.new( |
| 87 | + config_file_path: config.config_path, |
| 88 | + configuration: config, |
| 89 | + environment: "test" |
| 90 | + ) |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments