File: test_cli.py

package info (click to toggle)
python-cpuinfo 9.0.0%2Bgit20221119-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: python: 11,917; makefile: 74
file content (91 lines) | stat: -rw-r--r-- 2,648 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


import unittest
from cpuinfo import *
import helpers



class TestCLI(unittest.TestCase):
	def setUp(self):
		helpers.backup_data_source(cpuinfo)

	def tearDown(self):
		helpers.restore_data_source(cpuinfo)

	def test_json(self):
		from subprocess import Popen, PIPE
		import json

		command = [sys.executable, 'cpuinfo/cpuinfo.py', '--json']
		p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
		output = p1.communicate()[0]

		self.assertEqual(0, p1.returncode)

		output = output.decode(encoding='UTF-8')

		info = json.loads(output, object_hook = cpuinfo._utf_to_str)

		self.assertEqual(list(cpuinfo.CPUINFO_VERSION), info['cpuinfo_version'])
		self.assertEqual(cpuinfo.CPUINFO_VERSION_STRING, info['cpuinfo_version_string'])

	def test_version(self):
		from subprocess import Popen, PIPE

		command = [sys.executable, 'cpuinfo/cpuinfo.py', '--version']
		p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
		output = p1.communicate()[0]

		self.assertEqual(0, p1.returncode)

		output = output.decode(encoding='UTF-8')
		output = output.strip()

		self.assertEqual(cpuinfo.CPUINFO_VERSION_STRING, output)

	def test_trace(self):
		import os
		import re
		from subprocess import Popen, PIPE

		# Get all log files before test
		before_log_files = [f for f in os.listdir('.') if os.path.isfile(f) and re.match(r'^cpuinfo_trace_\d+-\d+-\d+_\d+-\d+-\d+-\d+.trace$', f)]
		#print('\n', before_log_files)

		# Run with trace to generate new log file
		command = [sys.executable, 'cpuinfo/cpuinfo.py', '--trace']
		p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
		output = p1.communicate()[0]
		self.assertEqual(0, p1.returncode)

		# Get all log files after test
		after_log_files = [f for f in os.listdir('.') if os.path.isfile(f) and re.match(r'^cpuinfo_trace_\d+-\d+-\d+_\d+-\d+-\d+-\d+.trace$', f)]
		#print('\n', after_log_files)

		# Read the new log file into a string
		new_log_file = list(set(after_log_files) - set(before_log_files))[0]
		with open(new_log_file, 'r') as f:
			output = f.read().strip()

		# Remove the new log file
		os.remove(new_log_file)

		self.assertTrue(len(output) > 200)
		self.assertTrue(output.startswith('!' * 80))
		self.assertTrue(output.endswith('!' * 80))

	def test_default(self):
		from subprocess import Popen, PIPE

		command = [sys.executable, 'cpuinfo/cpuinfo.py']
		p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
		output = p1.communicate()[0]

		self.assertEqual(0, p1.returncode)

		output = output.decode(encoding='UTF-8')

		version = output.split('Cpuinfo Version: ')[1].split('\n')[0].strip()

		self.assertEqual(cpuinfo.CPUINFO_VERSION_STRING, version)