diff --git a/lib/code_teams/testing.rb b/lib/code_teams/testing.rb index f28886d..f3ecc57 100644 --- a/lib/code_teams/testing.rb +++ b/lib/code_teams/testing.rb @@ -51,6 +51,11 @@ def self.create_code_team(attributes) ) code_teams << code_team + # Plugin's registry is keyed by team name, and the hook above only busts it after an + # example that built a fake team. An example reading only real teams therefore leaves + # real-team plugin instances behind, and a fake team sharing a real team's name would + # inherit that cached data. Drop the registry so the fake registers first. + Plugin.bust_caches! code_team end diff --git a/spec/lib/code_teams/testing_spec.rb b/spec/lib/code_teams/testing_spec.rb index 53b6e0e..4e8611d 100644 --- a/spec/lib/code_teams/testing_spec.rb +++ b/spec/lib/code_teams/testing_spec.rb @@ -11,5 +11,23 @@ expect(CodeTeams.find('Temp Team')).to eq(team) expect(team.raw_hash.dig('extra_data', 'foo', 'bar')).to eq(1) end + + it 'does not let a fake team inherit a real same-named team\'s cached plugin data' do + test_plugin_class = Class.new(CodeTeams::Plugin) do + def test_plugin + Data.define(:source).new(@team.raw_hash['extra_data']['source']) + end + end + stub_const('TestPlugin', test_plugin_class) + CodeTeams.bust_caches! + write_team_yml(extra_data: { 'source' => 'real' }) + + # Reading the real team caches its plugin instance under the name 'My Team'. + expect(CodeTeams.find('My Team').test_plugin.source).to eq('real') + + fake = described_class.create_code_team({ name: 'My Team', extra_data: { 'source' => 'fake' } }) + + expect(fake.test_plugin.source).to eq('fake') + end end end