summaryrefstree
path: root/kernel/tools/testing/kunit/kunit_new_template.py
blob: 06dbf382d1b3a14cdb1b2429a417610b5799c8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# SPDX-License-Identifier: GPL-2.0

import os
import string

TEMPLATE_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_TEMPLATE_PATH = os.path.join(TEMPLATE_DIR, 'test_template.c')
KCONFIG_TEMPLATE_PATH = os.path.join(TEMPLATE_DIR, 'test_template.Kconfig')
MAKEFILE_TEMPLATE_PATH = os.path.join(TEMPLATE_DIR, 'test_template.Makefile')

def create_skeleton_from_template(template_path, test_prefix, test_object_file):
	with open(template_path, 'r') as f:
		return string.Template(f.read()).safe_substitute(
				test_prefix=test_prefix,
				caps_test_prefix=test_prefix.upper(),
				test_object_file=test_object_file)


class Skeletons(object):
	"""
	Represents the KUnit skeletons for a test, Kconfig entry, and Makefile
	entry.
	"""
	def __init__(self, test_skeleton, kconfig_skeleton, makefile_skeleton):
		self.test_skeleton = test_skeleton
		self.kconfig_skeleton = kconfig_skeleton
		self.makefile_skeleton = makefile_skeleton


def create_skeletons(namespace_prefix, test_object_file):
	test_prefix = namespace_prefix + '_test'
	return Skeletons(
			test_skeleton=create_skeleton_from_template(
					TEST_TEMPLATE_PATH,
					test_prefix,
					test_object_file),
			kconfig_skeleton=create_skeleton_from_template(
					KCONFIG_TEMPLATE_PATH,
					test_prefix,
					test_object_file),
			makefile_skeleton=create_skeleton_from_template(
					MAKEFILE_TEMPLATE_PATH,
					test_prefix,
					test_object_file)
	)

def namespace_prefix_from_path(path):
	file_name = os.path.basename(path)
	return os.path.splitext(file_name)

def create_skeletons_from_path(path, namespace_prefix=None, print_test_only=False):
	dir_name, file_name = os.path.split(path)
	file_prefix, _ = os.path.splitext(file_name)
	test_path = os.path.join(dir_name, file_prefix + '-test.c')
	test_object_file = file_prefix + '-test.o'
	if not namespace_prefix:
		namespace_prefix = file_prefix.replace('-', '_')
	skeletons = create_skeletons(namespace_prefix, test_object_file)
	print('### In ' + test_path)
	print(skeletons.test_skeleton)
	if print_test_only:
		return
	print('### In Kconfig')
	print(skeletons.kconfig_skeleton)
	print('### In Makefile')
	print(skeletons.makefile_skeleton)