blob: 5b70da14c9ccb1ac1c71ca894db61cfa6fc21508 [file] [edit]
#!/usr/bin/env python3
#
# Copyright (C) 2025 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Tests for utils."""
import os
import tempfile
import textwrap
import unittest
from unittest.mock import patch, MagicMock
from utils import utils
class UtilsTest(unittest.TestCase):
"""Unit tests for the utils."""
@patch("subprocess.run")
def test_run_subprocess_cmd_success(self, mock_run):
"""Tests that a successful command returns the correct result."""
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = "Success"
mock_process.stderr = ""
mock_run.return_value = mock_process
result = utils.run_subprocess_cmd(["echo", "hello"])
self.assertEqual(result.returncode, 0)
self.assertEqual(result.stdout, "Success")
mock_run.assert_called_once_with(
["echo", "hello"],
capture_output=True,
text=True,
check=False,
encoding="utf-8",
timeout=30,
)
@patch("subprocess.run")
def test_run_subprocess_cmd_failure(self, mock_run):
"""Tests that a failed command raises a RuntimeError."""
mock_process = MagicMock()
mock_process.returncode = 1
mock_process.stderr = "Error"
mock_run.return_value = mock_process
with self.assertRaises(RuntimeError):
utils.run_subprocess_cmd(["false"])
@patch("subprocess.run")
def test_run_subprocess_cmd_failure_ignored(self, mock_run):
"""Test failed commands with ignore_error_and_return=True set."""
mock_process = MagicMock()
mock_process.returncode = 1
mock_process.stderr = "Error"
mock_run.return_value = mock_process
result = utils.run_subprocess_cmd(
["false"], ignore_error_and_return=True)
self.assertEqual(result.returncode, 1)
self.assertEqual(result.stderr, "Error")
def test_make_and_write_file(self):
"""Tests that a file is created with the correct data."""
with tempfile.TemporaryDirectory() as temp_dir:
directory = os.path.join(temp_dir, "test_dir")
file_name = "test_file.txt"
data = "Hello, World!"
utils.make_and_write_file(directory, file_name, data)
file_path = os.path.join(directory, file_name)
self.assertTrue(os.path.exists(file_path))
with open(file_path, "r", encoding="UTF-8") as f:
content = f.read()
self.assertEqual(content, data)
def test_list_to_markdown_table_empty(self):
"""Tests an empty list returns an empty string."""
self.assertEqual(utils.list_to_markdown_table([]), "")
def test_list_to_markdown_table_no_headers(self):
"""Tests a list without headers is formatted correctly."""
data = [("a", 1), ("b", 2)]
expected = textwrap.dedent("""\
| a | 1 |
| b | 2 |""")
self.assertEqual(utils.list_to_markdown_table(data), expected.strip())
def test_list_to_markdown_table_with_headers(self):
"""Tests a list with headers is formatted correctly."""
data = [("a", 1), ("b", 2)]
headers = ["Col1", "Col2"]
expected = textwrap.dedent("""\
| Col1 | Col2 |
| :----| ----:|
| a | 1 |
| b | 2 |""")
self.assertEqual(
utils.list_to_markdown_table(data, headers), expected.strip())
def test_list_to_markdown_table_mixed_types(self):
"""Tests a list with mixed data types is formatted correctly."""
data = [
("string", 123, 45.67, 10000),
("another", 8, 9.1, 54321),
]
headers = ["Label", "Count", "Percentage", "Value"]
expected = textwrap.dedent("""\
| Label | Count | Percentage | Value |
| :-------| -----:| ----------:| ------:|
| string | 123 | 46 | 10,000 |
| another | 8 | 9 | 54,321 |""")
self.assertEqual(
utils.list_to_markdown_table(data, headers), expected.strip())
if __name__ == "__main__":
unittest.main()