Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deep freeze only strings, arrays and hashes
Freezing a class prevents it from being modified at all, which can happen in unit tests with mocha or rspec-expections. Calling a function with `String` as an argument will freeze the entire class and prevent `allow_any_instance_of(String)...` type expectations from adding their hooks to the class. e.g. stdlib: https://github.com/puppetlabs/puppetlabs-stdlib/blob/00c881d/spec/functions/is_a_spec.rb#L18 https://github.com/puppetlabs/puppetlabs-stdlib/blob/00c881d/spec/functions/pw_hash_spec.rb#L57 This whitelists strings, arrays and hashes as safe classes to freeze when testing functions, others are left thawed to prevent side effects. Deep freezing is used on arrays/hashes to match Puppet's behaviour of freezing facts.
- Loading branch information
Showing
5 changed files
with
35 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
spec/fixtures/modules/test/lib/puppet/functions/frozen_array_function.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Puppet::Functions.create_function(:frozen_array_function) do | ||
def frozen_array_function(value) | ||
value.frozen? && value.all?(&:frozen?) | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
spec/fixtures/modules/test/lib/puppet/functions/frozen_function.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
Puppet::Functions.create_function(:frozen_function) do | ||
def frozen_function(value) | ||
value.reverse! | ||
value.frozen? | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
spec/fixtures/modules/test/lib/puppet/functions/frozen_hash_function.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Puppet::Functions.create_function(:frozen_hash_function) do | ||
def frozen_hash_function(value) | ||
value.frozen? && value.all? { |k,v| k.frozen? && v.frozen? } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters