public inbox for linux-kselftest@vger.kernel.org
 help / color / mirror / Atom feed
From: Daniel Latypov <dlatypov@google.com>
To: davidgow@google.com
Cc: brendanhiggins@google.com, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org,
	Daniel Latypov <dlatypov@google.com>
Subject: [PATCH 2/5] kunit: tool: fix unit test so it can run from non-root dir
Date: Mon, 30 Nov 2020 15:32:39 -0800	[thread overview]
Message-ID: <20201130233242.78413-2-dlatypov@google.com> (raw)
In-Reply-To: <20201130233242.78413-1-dlatypov@google.com>

get_absolute_path() makes an attempt to allow for this.
But that doesn't work as soon as os.chdir() gets called.

So make it so that os.chdir() does nothing to avoid this.

Note: mock.patch.object() doesn't seem to work in setUpModule(), hence
the introduction of a new base class instead.

Fixes: 5578d008d9e0 ("kunit: tool: fix running kunit_tool from outside kernel tree")
Signed-off-by: Daniel Latypov <dlatypov@google.com>
---
 tools/testing/kunit/kunit_tool_test.py | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py
index 3fbe1acd531a..9f1f1e1b772a 100755
--- a/tools/testing/kunit/kunit_tool_test.py
+++ b/tools/testing/kunit/kunit_tool_test.py
@@ -32,7 +32,13 @@ def tearDownModule():
 def get_absolute_path(path):
 	return os.path.join(os.path.dirname(__file__), path)
 
-class KconfigTest(unittest.TestCase):
+class KUnitTest(unittest.TestCase):
+	"""Contains common setup, like stopping main() from calling chdir."""
+	def setUp(self):
+		mock.patch.object(os, 'chdir').start()
+		self.addCleanup(mock.patch.stopall)
+
+class KconfigTest(KUnitTest):
 
 	def test_is_subset_of(self):
 		kconfig0 = kunit_config.Kconfig()
@@ -88,7 +94,7 @@ class KconfigTest(unittest.TestCase):
 		self.assertEqual(actual_kconfig.entries(),
 				 expected_kconfig.entries())
 
-class KUnitParserTest(unittest.TestCase):
+class KUnitParserTest(KUnitTest):
 
 	def assertContains(self, needle, haystack):
 		for line in haystack:
@@ -250,7 +256,7 @@ class KUnitParserTest(unittest.TestCase):
 				result.status)
 			self.assertEqual('kunit-resource-test', result.suites[0].name)
 
-class KUnitJsonTest(unittest.TestCase):
+class KUnitJsonTest(KUnitTest):
 
 	def _json_for(self, log_file):
 		with(open(get_absolute_path(log_file))) as file:
@@ -285,8 +291,9 @@ class StrContains(str):
 	def __eq__(self, other):
 		return self in other
 
-class KUnitMainTest(unittest.TestCase):
+class KUnitMainTest(KUnitTest):
 	def setUp(self):
+		super().setUp()
 		path = get_absolute_path('test_data/test_is_test_passed-all_passed.log')
 		with open(path) as file:
 			all_passed_log = file.readlines()
-- 
2.29.2.454.gaff20da3a2-goog


  reply	other threads:[~2020-11-30 23:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-30 23:32 [PATCH 1/5] kunit: tool: fix unit test cleanup handling Daniel Latypov
2020-11-30 23:32 ` Daniel Latypov [this message]
2020-12-01  7:32   ` [PATCH 2/5] kunit: tool: fix unit test so it can run from non-root dir David Gow
2020-12-01 18:59     ` Daniel Latypov
2020-12-02  4:41       ` David Gow
2020-12-02 19:11         ` Daniel Latypov
2020-11-30 23:32 ` [PATCH 3/5] kunit: tool: stop using bare asserts in unit test Daniel Latypov
2020-12-01  7:33   ` David Gow
2020-11-30 23:32 ` [PATCH 4/5] kunit: tool: use `with open()` " Daniel Latypov
2020-12-01  7:33   ` David Gow
2020-12-01 18:41     ` Daniel Latypov
2020-11-30 23:32 ` [PATCH 5/5] minor: kunit: tool: s/get_absolute_path/test_data_path " Daniel Latypov
2020-12-01  7:33   ` David Gow
2020-12-01  7:32 ` [PATCH 1/5] kunit: tool: fix unit test cleanup handling David Gow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201130233242.78413-2-dlatypov@google.com \
    --to=dlatypov@google.com \
    --cc=brendanhiggins@google.com \
    --cc=davidgow@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox