As the bug and fix If71620e808744736cb4fe3abda76d81a6335311b showed
it is dangerous to forget instantiating the Mock class before it is
used in the test as changes on the class directly leaks out from the
test. In almost all the cases using Mock class directly is a bug and the
author original intention is to use an instance instead, just forgot
about the parents. So this patch adds two new hacking rules:
N367: catches the case when Mock class is aliased in the test: self.mock_mystuff = mock.Mock
N368: catches when mock.patch instructed to use the Mock class as
replacement value during patching: mock.patch('Bar.foo', new=mock.Mock)
For N367 the previous patch removed the last hit. For N368 this patch
removes the two hits exists.
Reviewed: https:/ /review. opendev. org/c/openstack /nova/+ /805668 /opendev. org/openstack/ nova/commit/ 9f8cc2f03827c10 f89ee652bc73790 b3c58111d3
Committed: https:/
Submitter: "Zuul (22348)"
Branch: master
commit 9f8cc2f03827c10 f89ee652bc73790 b3c58111d3
Author: Balazs Gibizer <email address hidden>
Date: Mon Aug 23 19:23:31 2021 +0200
Add two new hacking rules
As the bug and fix If71620e8087447 36cb4fe3abda76d 81a6335311b showed
it is dangerous to forget instantiating the Mock class before it is
used in the test as changes on the class directly leaks out from the
test. In almost all the cases using Mock class directly is a bug and the
author original intention is to use an instance instead, just forgot
about the parents. So this patch adds two new hacking rules:
N367: catches the case when Mock class is aliased in the test:
self.mock_ mystuff = mock.Mock
N368: catches when mock.patch instructed to use the Mock class as
mock.patch( 'Bar.foo' , new=mock.Mock)
replacement value during patching:
For N367 the previous patch removed the last hit. For N368 this patch
removes the two hits exists.
Change-Id: Id42ca571b15698 86ef47aa350369e 9d2068e77bc
Related-Bug: #1936849