Rollup merge of #132771 - ismailarilik:test/configure/cover-parse-args-in-src-bootstrap-configure-py, r=onur-ozkan

test(configure): cover `parse_args` in `src/bootstrap/configure.py`

I was reading `src/bootstrap/configure.py` and `parse_args` function there seems complex. So I added some tests to cover it and prevent regressions.
This commit is contained in:
Matthias Krüger 2024-11-12 08:07:16 +01:00 committed by GitHub
commit 3a0cc81ca3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ Run these with `x test bootstrap`, or `python -m unittest src/bootstrap/bootstra
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
import os import os
import unittest import unittest
from unittest.mock import patch
import tempfile import tempfile
import hashlib import hashlib
import sys import sys
@ -99,6 +100,52 @@ class ProgramOutOfDate(unittest.TestCase):
self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key)) self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
class ParseArgsInConfigure(unittest.TestCase):
"""Test if `parse_args` function in `configure.py` works properly"""
@patch("configure.err")
def test_unknown_args(self, err):
# It should be print an error message if the argument doesn't start with '--'
configure.parse_args(["enable-full-tools"])
err.assert_called_with("Option 'enable-full-tools' is not recognized")
err.reset_mock()
# It should be print an error message if the argument is not recognized
configure.parse_args(["--some-random-flag"])
err.assert_called_with("Option '--some-random-flag' is not recognized")
@patch("configure.err")
def test_need_value_args(self, err):
"""It should print an error message if a required argument value is missing"""
configure.parse_args(["--target"])
err.assert_called_with("Option '--target' needs a value (--target=val)")
@patch("configure.err")
def test_option_checking(self, err):
# Options should be checked even if `--enable-option-checking` is not passed
configure.parse_args(["--target"])
err.assert_called_with("Option '--target' needs a value (--target=val)")
err.reset_mock()
# Options should be checked if `--enable-option-checking` is passed
configure.parse_args(["--enable-option-checking", "--target"])
err.assert_called_with("Option '--target' needs a value (--target=val)")
err.reset_mock()
# Options should not be checked if `--disable-option-checking` is passed
configure.parse_args(["--disable-option-checking", "--target"])
err.assert_not_called()
@patch("configure.parse_example_config", lambda known_args, _: known_args)
def test_known_args(self):
# It should contain known and correct arguments
known_args = configure.parse_args(["--enable-full-tools"])
self.assertTrue(known_args["full-tools"][0][1])
known_args = configure.parse_args(["--disable-full-tools"])
self.assertFalse(known_args["full-tools"][0][1])
# It should contain known arguments and their values
known_args = configure.parse_args(["--target=x86_64-unknown-linux-gnu"])
self.assertEqual(known_args["target"][0][1], "x86_64-unknown-linux-gnu")
known_args = configure.parse_args(["--target", "x86_64-unknown-linux-gnu"])
self.assertEqual(known_args["target"][0][1], "x86_64-unknown-linux-gnu")
class GenerateAndParseConfig(unittest.TestCase): class GenerateAndParseConfig(unittest.TestCase):
"""Test that we can serialize and deserialize a config.toml file""" """Test that we can serialize and deserialize a config.toml file"""
def test_no_args(self): def test_no_args(self):