Skip to content

Commit

Permalink
Rollup merge of #132771 - ismailarilik:test/configure/cover-parse-arg…
Browse files Browse the repository at this point in the history
…s-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.
  • Loading branch information
matthiaskrgr authored Nov 12, 2024
2 parents 1dd975b + 1ba72db commit 3a0cc81
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/bootstrap/bootstrap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import absolute_import, division, print_function
import os
import unittest
from unittest.mock import patch
import tempfile
import hashlib
import sys
Expand Down Expand Up @@ -99,6 +100,52 @@ def test_same_dates(self):
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):
"""Test that we can serialize and deserialize a config.toml file"""
def test_no_args(self):
Expand Down

0 comments on commit 3a0cc81

Please sign in to comment.