def test_docopt_default(tmpdir, short): """Test compatibility with "default" feature in docopt. :param tmpdir: pytest fixture. :param bool short: Use short arg names. """ config_file = tmpdir.join('config.ini') config_file.write(dedent("""\ [FlashAirMusic] threads=2 """)) # Test override. argv = ['run', '-c', str(config_file), '-t' if short else '--threads', '1'] actual = docoptcfg(DOCSTRING_FAM, argv, config_option='--config') expected = EXPECTED_FAM.copy() expected['--config'] = str(config_file) expected['--threads'] = '1' assert actual == expected # Test "default". actual = docoptcfg(DOCSTRING_FAM, ['run', '-c', str(config_file)], config_option='--config') expected = EXPECTED_FAM.copy() expected['--config'] = str(config_file) expected['--threads'] = '2' assert actual == expected
def test_override(monkeypatch, short, equals): """Test overriding env variables with command line. :param monkeypatch: pytest fixture. :param bool short: Use short arg names. :param bool equals: Use ['--key=val'] or ['-kval'] instead of ['--key', 'val']. """ monkeypatch.setenv("FAM_FFMPEG_BIN", "/tmp/ffmpeg") monkeypatch.setenv("FAM_VERBOSE", "False") if equals and short: argv = ["run", "-f/tmp/arg/ffmpeg", "-v"] elif equals: argv = ["run", "--ffmpeg-bin=/tmp/arg/ffmpeg", "--verbose"] else: argv = ["run", "--ffmpeg-bin", "/tmp/arg/ffmpeg", "--verbose"] actual = docoptcfg(DOCSTRING_FAM, argv, env_prefix="FAM_") expected = EXPECTED_FAM.copy() expected["--ffmpeg-bin"] = "/tmp/arg/ffmpeg" expected["--verbose"] = True assert actual == expected actual = docoptcfg(DOCSTRING_FAM, ["run"], env_prefix="FAM_") expected = EXPECTED_FAM.copy() expected["--ffmpeg-bin"] = "/tmp/ffmpeg" expected["--verbose"] = False assert actual == expected
def test_override(monkeypatch, short, equals): """Test overriding env variables with command line. :param monkeypatch: pytest fixture. :param bool short: Use short arg names. :param bool equals: Use ['--key=val'] or ['-kval'] instead of ['--key', 'val']. """ monkeypatch.setenv('FAM_FFMPEG_BIN', '/tmp/ffmpeg') monkeypatch.setenv('FAM_VERBOSE', 'False') if equals and short: argv = ['run', '-f/tmp/arg/ffmpeg', '-v'] elif equals: argv = ['run', '--ffmpeg-bin=/tmp/arg/ffmpeg', '--verbose'] else: argv = ['run', '--ffmpeg-bin', '/tmp/arg/ffmpeg', '--verbose'] actual = docoptcfg(DOCSTRING_FAM, argv, env_prefix='FAM_') expected = EXPECTED_FAM.copy() expected['--ffmpeg-bin'] = '/tmp/arg/ffmpeg' expected['--verbose'] = True assert actual == expected actual = docoptcfg(DOCSTRING_FAM, ['run'], env_prefix='FAM_') expected = EXPECTED_FAM.copy() expected['--ffmpeg-bin'] = '/tmp/ffmpeg' expected['--verbose'] = False assert actual == expected
def test(tmpdir, argv_short, option_short): """Test with sample config file. :param tmpdir: pytest fixture. :param bool argv_short: Use short name for argv option. :param bool option_short: Use short name for config_option value. """ config_file = tmpdir.join('config.ini') config_file.write( dedent("""\ [FlashAirMusic] config = /tmp/ignore.me ffmpeg-bin = ffmpeg help = True mac-addr = quiet = true threads = 9 version = True """)) argv = ['run', '-c' if argv_short else '--config', str(config_file)] actual = docoptcfg(DOCSTRING_FAM, argv, config_option='-c' if option_short else '--config') expected = EXPECTED_FAM.copy() expected['--config'] = str(config_file) expected['--ffmpeg-bin'] = 'ffmpeg' expected['--mac-addr'] = '' expected['--quiet'] = True expected['--threads'] = '9' assert actual == expected
def test_docopt_default(monkeypatch, short): """Test compatibility with "default" feature in docopt. :param monkeypatch: pytest fixture. :param bool short: Use short arg names. """ monkeypatch.setenv("FAM_THREADS", "2") # Test override. actual = docoptcfg(DOCSTRING_FAM, ["run", "-t" if short else "--threads", "1"], env_prefix="FAM_") expected = EXPECTED_FAM.copy() expected["--threads"] = "1" assert actual == expected # Test "default". actual = docoptcfg(DOCSTRING_FAM, ["run"], env_prefix="FAM_") expected = EXPECTED_FAM.copy() expected["--threads"] = "2" assert actual == expected
def test_override(tmpdir, short, equals): """Test overriding config values with command line. :param tmpdir: pytest fixture. :param bool short: Use short arg names. :param bool equals: Use ['--key=val'] or ['-kval'] instead of ['--key', 'val']. """ config_file = tmpdir.join('config.ini') config_file.write( dedent("""\ [FlashAirMusic] ffmpeg-bin=/tmp/ffmpeg verbose=false """)) if equals and short: argv = ['run', '-c', str(config_file), '-f/tmp/arg/ffmpeg', '-v'] elif equals: argv = [ 'run', '-c', str(config_file), '--ffmpeg-bin=/tmp/arg/ffmpeg', '--verbose' ] else: argv = [ 'run', '-c', str(config_file), '--ffmpeg-bin', '/tmp/arg/ffmpeg', '--verbose' ] actual = docoptcfg(DOCSTRING_FAM, argv, config_option='--config') expected = EXPECTED_FAM.copy() expected['--config'] = str(config_file) expected['--ffmpeg-bin'] = '/tmp/arg/ffmpeg' expected['--verbose'] = True assert actual == expected actual = docoptcfg(DOCSTRING_FAM, ['run', '-c', str(config_file)], config_option='--config') expected = EXPECTED_FAM.copy() expected['--config'] = str(config_file) expected['--ffmpeg-bin'] = '/tmp/ffmpeg' expected['--verbose'] = False assert actual == expected
def test_docopt_default(monkeypatch, short): """Test compatibility with "default" feature in docopt. :param monkeypatch: pytest fixture. :param bool short: Use short arg names. """ monkeypatch.setenv('FAM_THREADS', '2') # Test override. actual = docoptcfg(DOCSTRING_FAM, ['run', '-t' if short else '--threads', '1'], env_prefix='FAM_') expected = EXPECTED_FAM.copy() expected['--threads'] = '1' assert actual == expected # Test "default". actual = docoptcfg(DOCSTRING_FAM, ['run'], env_prefix='FAM_') expected = EXPECTED_FAM.copy() expected['--threads'] = '2' assert actual == expected
def test_fam(monkeypatch, tmpdir): """Make sure config file and environment variables aren't being handled. :param monkeypatch: pytest fixture. :param tmpdir: pytest fixture. """ monkeypatch.setenv('FFMPEG_BIN', tmpdir.ensure('ffmpeg')) tmpdir.join('config.ini').write('[FlashAirMusic]\nmac-addr = 00:11:22:33:44:55') actual = docoptcfg(DOCSTRING_FAM, ['run', '--config', str(tmpdir.join('config.ini'))]) expected = EXPECTED_FAM.copy() expected['--config'] = str(tmpdir.join('config.ini')) assert actual == expected
def test_data_types(monkeypatch, tmpdir, source, data_type): """Ensure all sources produce the exact same non-boolean data types and values. :param monkeypatch: pytest fixture. :param tmpdir: pytest fixture. :param source: Config source to test. :param data_type: Data type to test. """ argv = ['run'] expected = EXPECTED_FAM.copy() if source == 'file': config_file = tmpdir.join('config.ini') if data_type == 'str': config_file.write('[FlashAirMusic]\nmac-addr = one') elif data_type == 'int': config_file.write('[FlashAirMusic]\nmac-addr = 1') else: config_file.write('[FlashAirMusic]\nmac-addr = 2.3') monkeypatch.setenv('FAM_CONFIG', str(config_file)) expected['--config'] = str(config_file) elif source == 'env': if data_type == 'str': monkeypatch.setenv('FAM_MAC_ADDR', 'one') elif data_type == 'int': monkeypatch.setenv('FAM_MAC_ADDR', '1') else: monkeypatch.setenv('FAM_MAC_ADDR', '2.3') else: if data_type == 'str': argv.extend(['--mac-addr', 'one']) elif data_type == 'int': argv.extend(['--mac-addr', '1']) else: argv.extend(['--mac-addr', '2.3']) # Set expected. if data_type == 'str': expected['--mac-addr'] = 'one' elif data_type == 'int': expected['--mac-addr'] = '1' else: expected['--mac-addr'] = '2.3' actual = docoptcfg(DOCSTRING_FAM, argv, config_option='-c', env_prefix='FAM_') assert actual == expected
def test_data_types(monkeypatch, tmpdir, source, data_type): """Ensure all sources produce the exact same non-boolean data types and values. :param monkeypatch: pytest fixture. :param tmpdir: pytest fixture. :param source: Config source to test. :param data_type: Data type to test. """ argv = ["run"] expected = EXPECTED_FAM.copy() if source == "file": config_file = tmpdir.join("config.ini") if data_type == "str": config_file.write("[FlashAirMusic]\nmac-addr = one") elif data_type == "int": config_file.write("[FlashAirMusic]\nmac-addr = 1") else: config_file.write("[FlashAirMusic]\nmac-addr = 2.3") monkeypatch.setenv("FAM_CONFIG", str(config_file)) expected["--config"] = str(config_file) elif source == "env": if data_type == "str": monkeypatch.setenv("FAM_MAC_ADDR", "one") elif data_type == "int": monkeypatch.setenv("FAM_MAC_ADDR", "1") else: monkeypatch.setenv("FAM_MAC_ADDR", "2.3") else: if data_type == "str": argv.extend(["--mac-addr", "one"]) elif data_type == "int": argv.extend(["--mac-addr", "1"]) else: argv.extend(["--mac-addr", "2.3"]) # Set expected. if data_type == "str": expected["--mac-addr"] = "one" elif data_type == "int": expected["--mac-addr"] = "1" else: expected["--mac-addr"] = "2.3" actual = docoptcfg(DOCSTRING_FAM, argv, config_option="-c", env_prefix="FAM_") assert actual == expected
def test_config_file_in_env(monkeypatch, tmpdir): """Test specifying a config file using only env variables. :param monkeypatch: pytest fixture. :param tmpdir: pytest fixture. """ config_file = tmpdir.join('config.ini') config_file.write(dedent("""\ [FlashAirMusic] mac-addr = AA:BB:CC:DD:EE:FF """)) monkeypatch.setenv('FAM_CONFIG', str(config_file)) actual = docoptcfg(DOCSTRING_FAM, ['run'], config_option='-c', env_prefix='FAM_') expected = EXPECTED_FAM.copy() expected['--config'] = str(config_file) expected['--mac-addr'] = 'AA:BB:CC:DD:EE:FF' assert actual == expected
def test_override(monkeypatch, tmpdir, set_arg, set_env, set_file): """Test source overrides. :param monkeypatch: pytest fixture. :param tmpdir: pytest fixture. :param bool set_arg: Set value in command line arguments. :param bool set_env: Set value in environment variables. :param bool set_file: Set value in config file. """ config_file = tmpdir.join("config.ini") config_file.write( dedent( """\ [FlashAirMusic] quiet = true {0} """ ).format("ffmpeg-bin = ffmpeg_file" if set_file else "") ) monkeypatch.setenv("FAM_CONFIG", str(config_file)) monkeypatch.setenv("FAM_VERBOSE", "true") if set_env: monkeypatch.setenv("FAM_FFMPEG_BIN", "ffmpeg_env") argv = ["run", "-m", "00:11:22:33:44:55"] + (["--ffmpeg-bin", "ffmpeg_arg"] if set_arg else []) actual = docoptcfg(DOCSTRING_FAM, argv, config_option="-c", env_prefix="FAM_") expected = EXPECTED_FAM.copy() expected["--config"] = str(config_file) expected["--mac-addr"] = "00:11:22:33:44:55" expected["--quiet"] = True expected["--verbose"] = True if set_arg: expected["--ffmpeg-bin"] = "ffmpeg_arg" elif set_env: expected["--ffmpeg-bin"] = "ffmpeg_env" elif set_file: expected["--ffmpeg-bin"] = "ffmpeg_file" assert actual == expected
def test(monkeypatch, set_config, set_verbose): """Test with env variables. :param monkeypatch: pytest fixture. :param str set_config: Set FAM_CONFIG to this value if not None. :param str set_verbose: Set FAM_VERBOSE to this value if not None. """ monkeypatch.setenv("FAM_HELP", "True") monkeypatch.setenv("FAM_VERSION", "True") expected = EXPECTED_FAM.copy() if set_config is not None: expected["--config"] = str(set_config) monkeypatch.setenv("FAM_CONFIG", set_config) if set_verbose is not None: if set_verbose.strip().lower() in ("true", "yes", "on", "1"): expected["--verbose"] = True monkeypatch.setenv("FAM_VERBOSE", set_verbose) actual = docoptcfg(DOCSTRING_FAM, ["run"], env_prefix="FAM_") assert actual == expected
def test(monkeypatch, set_config, set_verbose): """Test with env variables. :param monkeypatch: pytest fixture. :param str set_config: Set FAM_CONFIG to this value if not None. :param str set_verbose: Set FAM_VERBOSE to this value if not None. """ monkeypatch.setenv('FAM_HELP', 'True') monkeypatch.setenv('FAM_VERSION', 'True') expected = EXPECTED_FAM.copy() if set_config is not None: expected['--config'] = str(set_config) monkeypatch.setenv('FAM_CONFIG', set_config) if set_verbose is not None: if set_verbose.strip().lower() in ('true', 'yes', 'on', '1'): expected['--verbose'] = True monkeypatch.setenv('FAM_VERBOSE', set_verbose) actual = docoptcfg(DOCSTRING_FAM, ['run'], env_prefix='FAM_') assert actual == expected
def test_config_file_in_env(monkeypatch, tmpdir): """Test specifying a config file using only env variables. :param monkeypatch: pytest fixture. :param tmpdir: pytest fixture. """ config_file = tmpdir.join("config.ini") config_file.write( dedent( """\ [FlashAirMusic] mac-addr = AA:BB:CC:DD:EE:FF """ ) ) monkeypatch.setenv("FAM_CONFIG", str(config_file)) actual = docoptcfg(DOCSTRING_FAM, ["run"], config_option="-c", env_prefix="FAM_") expected = EXPECTED_FAM.copy() expected["--config"] = str(config_file) expected["--mac-addr"] = "AA:BB:CC:DD:EE:FF" assert actual == expected
def test_override(monkeypatch, tmpdir, set_arg, set_env, set_file): """Test source overrides. :param monkeypatch: pytest fixture. :param tmpdir: pytest fixture. :param bool set_arg: Set value in command line arguments. :param bool set_env: Set value in environment variables. :param bool set_file: Set value in config file. """ config_file = tmpdir.join('config.ini') config_file.write(dedent("""\ [FlashAirMusic] quiet = true {0} """).format('ffmpeg-bin = ffmpeg_file' if set_file else '')) monkeypatch.setenv('FAM_CONFIG', str(config_file)) monkeypatch.setenv('FAM_VERBOSE', 'true') if set_env: monkeypatch.setenv('FAM_FFMPEG_BIN', 'ffmpeg_env') argv = ['run', '-m', '00:11:22:33:44:55'] + (['--ffmpeg-bin', 'ffmpeg_arg'] if set_arg else []) actual = docoptcfg(DOCSTRING_FAM, argv, config_option='-c', env_prefix='FAM_') expected = EXPECTED_FAM.copy() expected['--config'] = str(config_file) expected['--mac-addr'] = '00:11:22:33:44:55' expected['--quiet'] = True expected['--verbose'] = True if set_arg: expected['--ffmpeg-bin'] = 'ffmpeg_arg' elif set_env: expected['--ffmpeg-bin'] = 'ffmpeg_env' elif set_file: expected['--ffmpeg-bin'] = 'ffmpeg_file' assert actual == expected
def test_none(): """Test when user doesn't specify a config file.""" expected = EXPECTED_FAM.copy() actual = docoptcfg(DOCSTRING_FAM, ['run'], config_option='--config') assert actual == expected