From a84e310ce067d41cab3a192c6b54085b42ae66b5 Mon Sep 17 00:00:00 2001 From: ZainnQureshii <43629888+ZainnQureshii@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:25:38 +0100 Subject: [PATCH] [options] Use parsed values when loading configuration `--config-location=FILE` and abbreviated forms such as `--config-loc` and `--ignore-conf` on the command line were missed because config loading tested raw argv tokens. Check the parsed option values instead. Co-Authored-By: Claude Opus 5.5 --- test/test_options.py | 80 +++++++++++++++++++++++++++++++++++++++++++ youtube_dl/options.py | 4 +-- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/test/test_options.py b/test/test_options.py index 3a25a6ba3..ba9350df9 100644 --- a/test/test_options.py +++ b/test/test_options.py @@ -8,6 +8,11 @@ import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +import io +import shutil +import tempfile + +from youtube_dl import options from youtube_dl.options import _hide_login_info @@ -22,5 +27,80 @@ class TestOptions(unittest.TestCase): ['--username=PRIVATE']) +class TestConfig(unittest.TestCase): + def setUp(self): + self.test_dir = tempfile.mkdtemp() + self.config_file = os.path.join(self.test_dir, 'youtube-dl.conf') + with io.open(self.config_file, 'w', encoding='utf-8') as config: + config.write('--user-agent custom') + + def tearDown(self): + shutil.rmtree(self.test_dir) + + def _parse_options(self, args, system_config='', user_config=''): + old_argv, old_open = sys.argv, options.open + + def fake_open(filename, **kwargs): + if filename == self.config_file: + return old_open(filename, **kwargs) + return io.StringIO( + system_config if filename == '/etc/youtube-dl.conf' else user_config) + + sys.argv = ['youtube-dl'] + args + options.open = fake_open + try: + return options.parseOpts()[1] + finally: + sys.argv, options.open = old_argv, old_open + + def test_config_defaults(self): + opts = self._parse_options([]) + self.assertEqual(opts.config_location, None) + self.assertFalse(opts.ignore_config) + self.assertEqual(self._parse_options( + [], '--user-agent system', '--user-agent user').user_agent, 'user') + + def test_config_location(self): + for args in ( + ['--config-location', self.config_file], + ['--config-location=' + self.config_file], + ['--config-loc', self.config_file], + ['--config-loc=' + self.config_file]): + self.assertEqual(self._parse_options( + args, '--user-agent system', '--user-agent user').user_agent, 'custom') + + def test_config_location_directory(self): + for args in ( + ['--config-location', self.test_dir], + ['--config-location=' + self.test_dir]): + self.assertEqual(self._parse_options(args).user_agent, 'custom') + + def test_config_location_missing(self): + missing = os.path.join(self.test_dir, 'missing.conf') + for args in ( + ['--config-location', missing], + ['--config-location=' + missing]): + self.assertRaises(SystemExit, self._parse_options, args) + + def test_ignore_config(self): + for flag in ('--ignore-config', '--ignore-conf'): + opts = self._parse_options( + [flag], '--user-agent system', '--user-agent user') + self.assertTrue(opts.ignore_config) + self.assertEqual(opts.user_agent, None) + + def test_config_location_with_ignore_config(self): + for args in ( + ['--ignore-config', '--config-location', self.config_file], + ['--config-location=' + self.config_file, '--ignore-config']): + self.assertEqual(self._parse_options( + args, '--user-agent system', '--user-agent user').user_agent, 'custom') + + def test_command_line_overrides_config(self): + self.assertEqual(self._parse_options([ + '--config-location=' + self.config_file, + '--user-agent', 'command-line']).user_agent, 'command-line') + + if __name__ == '__main__': unittest.main() diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 9b0d77a23..1566f9af2 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -916,14 +916,14 @@ def parseOpts(overrideArguments=None): system_conf = user_conf = custom_conf = [] - if '--config-location' in command_line_conf: + if opts.config_location is not None: location = compat_expanduser(opts.config_location) if os.path.isdir(location): location = os.path.join(location, 'youtube-dl.conf') if not os.path.exists(location): parser.error('config-location %s does not exist.' % location) custom_conf = _readOptions(location) - elif '--ignore-config' in command_line_conf: + elif opts.ignore_config: pass else: system_conf = _readOptions('/etc/youtube-dl.conf')