@@ -1551,13 +1551,17 @@ def link_overwrite?(path)
15511551 # @see .disable!
15521552 delegate disable_reason : :"self.class"
15531553
1554- # Sandbox rules that should be skipped when installing or testing this {Formula}.
1555- # Returns `nil` if there are no sandbox rules to skip.
1556- # @!method reduced_sandbox
1557- # @return [Array<Symbol>]
1558- # @see .reduce_sandbox
1559- def reduced_sandbox
1560- self . class . reduced_sandbox || [ ]
1554+ # Whether or not the given sandbox rule should be skipped during the given phase in this {Formula}.
1555+ # @!method allowed_in_sandbox?
1556+ # @param type [Symbol] the type of sandbox rule
1557+ # @param phase [Symbol] the phase to check
1558+ # @return [Boolean]
1559+ # @see .allow_in_sandbox
1560+ def allowed_in_sandbox? ( type , phase :)
1561+ return false unless self . class . allowed_in_sandbox
1562+ return false unless self . class . allowed_in_sandbox . key? ( phase )
1563+
1564+ self . class . allowed_in_sandbox [ phase ] . include? ( type )
15611565 end
15621566
15631567 sig { returns ( T ::Boolean ) }
@@ -3287,7 +3291,7 @@ def freeze
32873291 attr_reader :keg_only_reason
32883292
32893293 # The types of sandbox restrictions that should be lifted from the formula.
3290- attr_reader :reduced_sandbox
3294+ attr_reader :allowed_in_sandbox
32913295
32923296 # A one-line description of the software. Used by users to get an overview
32933297 # of the software and Homebrew maintainers.
@@ -4309,29 +4313,59 @@ def link_overwrite(*paths)
43094313 link_overwrite_paths . merge ( paths )
43104314 end
43114315
4312- # Skip certain sandbox restrictions when installing this formula.
4316+ # Skip certain sandbox restrictions when installing and testing this formula.
43134317 # This can be useful if the upstream build system needs to write to
4314- # locations that are protected by sandbox restrictions.
4318+ # locations that are protected by sandbox restrictions. Passing a
4319+ # phase is optional, and if not provided, the rule will be applied to
4320+ # all phases. The possible phases are `:build`, `:postinstall`, and `:test`.
43154321 #
43164322 # ### Example
43174323 #
4318- # If upstream needs to write to `/private/tmp`:
4324+ # If the formula needs to write to `/private/tmp` in all phases :
43194325 #
43204326 # ```ruby
4321- # reduce_sandbox :allow_write_to_temp
4327+ # allow_in_sandbox! :write_to_temp
43224328 # ```
4323- def reduce_sandbox! ( *types )
4324- invalid_types = types . select { |type | Sandbox ::SANDBOX_REDUCTIONS . exclude? ( type ) }
4329+ #
4330+ # If the formula needs to send signals in the `:test` phase:
4331+ # ```ruby
4332+ # allow_in_sandbox! :signal, phase: :test
4333+ # ```
4334+ #
4335+ # If the formula needs to write to `/private/tmp` and send signals
4336+ # in the `:test` and `:install` phase:
4337+ # ```ruby
4338+ # allow_in_sandbox! :write_to_temp, :signal, phase: [:test, :install]
4339+ # ```
4340+ def allow_in_sandbox! ( *types , phase : nil )
4341+ invalid_types = types . select { |type | Sandbox ::SANDBOX_DSL_RULES . exclude? ( type ) }
43254342 if invalid_types . any?
43264343 noun = if invalid_types . count > 1
43274344 "types"
43284345 else
43294346 "type"
43304347 end
4331- raise ArgumentError , "Unsupported sandbox reduction #{ noun } : #{ invalid_types . join ( ", " ) } "
4348+ raise ArgumentError , "Unsupported allow in sandbox item #{ noun } : #{ invalid_types . join ( ", " ) } "
4349+ end
4350+
4351+ phase ||= Sandbox ::SANDBOX_DSL_PHASES
4352+ phases = Array ( phase )
4353+ invalid_phases = phases . select { |p | Sandbox ::SANDBOX_DSL_PHASES . exclude? ( p ) }
4354+ if invalid_phases . any?
4355+ noun = if invalid_phases . count > 1
4356+ "phases"
4357+ else
4358+ "phase"
4359+ end
4360+ raise ArgumentError , "Unsupported sandbox phase #{ noun } : #{ invalid_phases . join ( ", " ) } "
43324361 end
43334362
4334- @reduced_sandbox = types
4363+ @allowed_in_sandbox ||= { }
4364+ phases . each do |p |
4365+ @allowed_in_sandbox [ p ] ||= [ ]
4366+ @allowed_in_sandbox [ p ] . concat ( types )
4367+ @allowed_in_sandbox [ p ] = @allowed_in_sandbox [ p ] . uniq
4368+ end
43354369 end
43364370 end
43374371end
0 commit comments