Skip to content

Commit b1cae21

Browse files
authored
Unrolled build for rust-lang#132771
Rollup merge of rust-lang#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.
2 parents 9a9dadd + 1ba72db commit b1cae21

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/bootstrap/bootstrap_test.py

+47
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import absolute_import, division, print_function
66
import os
77
import unittest
8+
from unittest.mock import patch
89
import tempfile
910
import hashlib
1011
import sys
@@ -99,6 +100,52 @@ def test_same_dates(self):
99100
self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
100101

101102

103+
class ParseArgsInConfigure(unittest.TestCase):
104+
"""Test if `parse_args` function in `configure.py` works properly"""
105+
@patch("configure.err")
106+
def test_unknown_args(self, err):
107+
# It should be print an error message if the argument doesn't start with '--'
108+
configure.parse_args(["enable-full-tools"])
109+
err.assert_called_with("Option 'enable-full-tools' is not recognized")
110+
err.reset_mock()
111+
# It should be print an error message if the argument is not recognized
112+
configure.parse_args(["--some-random-flag"])
113+
err.assert_called_with("Option '--some-random-flag' is not recognized")
114+
115+
@patch("configure.err")
116+
def test_need_value_args(self, err):
117+
"""It should print an error message if a required argument value is missing"""
118+
configure.parse_args(["--target"])
119+
err.assert_called_with("Option '--target' needs a value (--target=val)")
120+
121+
@patch("configure.err")
122+
def test_option_checking(self, err):
123+
# Options should be checked even if `--enable-option-checking` is not passed
124+
configure.parse_args(["--target"])
125+
err.assert_called_with("Option '--target' needs a value (--target=val)")
126+
err.reset_mock()
127+
# Options should be checked if `--enable-option-checking` is passed
128+
configure.parse_args(["--enable-option-checking", "--target"])
129+
err.assert_called_with("Option '--target' needs a value (--target=val)")
130+
err.reset_mock()
131+
# Options should not be checked if `--disable-option-checking` is passed
132+
configure.parse_args(["--disable-option-checking", "--target"])
133+
err.assert_not_called()
134+
135+
@patch("configure.parse_example_config", lambda known_args, _: known_args)
136+
def test_known_args(self):
137+
# It should contain known and correct arguments
138+
known_args = configure.parse_args(["--enable-full-tools"])
139+
self.assertTrue(known_args["full-tools"][0][1])
140+
known_args = configure.parse_args(["--disable-full-tools"])
141+
self.assertFalse(known_args["full-tools"][0][1])
142+
# It should contain known arguments and their values
143+
known_args = configure.parse_args(["--target=x86_64-unknown-linux-gnu"])
144+
self.assertEqual(known_args["target"][0][1], "x86_64-unknown-linux-gnu")
145+
known_args = configure.parse_args(["--target", "x86_64-unknown-linux-gnu"])
146+
self.assertEqual(known_args["target"][0][1], "x86_64-unknown-linux-gnu")
147+
148+
102149
class GenerateAndParseConfig(unittest.TestCase):
103150
"""Test that we can serialize and deserialize a config.toml file"""
104151
def test_no_args(self):

0 commit comments

Comments
 (0)