def test_method_open(self): """The method=open.""" class Filter(self.MockTool): method = 'open' assert getattr(Filter, 'output') is None assert getattr(Filter, 'input') is None Filter().open(StringIO(), 'filename') assert Filter.result == ([], None)
def test_method_output(self): """The method=output.""" class Filter(self.MockTool): method = 'output' assert getattr(Filter, 'input') is None assert getattr(Filter, 'open') is None Filter().output(StringIO('bla'), StringIO()) assert Filter.result == ([], 'bla')
def test_argv_variables(self): """In argv, a number of placeholders can be used. Ensure they work.""" class Filter(self.MockTool): argv = [ # The filter instance '{self.__class__.__name__}', # Keyword and positional args to filter method '{kwarg}', '{0.closed}', # Special placeholders that are passed through '{input}', '{output}'] Filter().output(StringIO('content'), StringIO(), kwarg='value') assert Filter.result == ( ["Filter", 'value', 'False', '{input}', '{output}'], 'content')
def test_getconfig_os_env_types(self): """Test type conversion for values read from the environment. """ m = Environment(None, None) f = Filter() f.set_environment(m) get_config = f.get_config with os_environ_sandbox(): os.environ['foo'] = 'one,two\,three' assert get_config(env='foo', type=list) == ['one', 'two,three'] # Make sure the split is not applied to env config values m.config['foo'] = 'one,two\,three' assert get_config(setting='foo', type=list) == 'one,two\,three'
def test_get_config(self): """Test the ``get_config`` helper. """ m = Environment(None, None) f = Filter() f.set_environment(m) get_config = f.get_config # For the purposes of the following tests, we use two test # names which we expect to be undefined in os.env. NAME = 'FOO%s' % id(object()) NAME2 = 'FOO%s' % id(NAME) assert NAME != NAME2 assert not NAME in os.environ and not NAME2 in os.environ try: # Test raising of error, and test not raising it. assert_raises(EnvironmentError, get_config, NAME) assert get_config(NAME, require=False) == None # Start with only the environment variable set. os.environ[NAME] = 'bar' assert get_config(NAME) == 'bar' assert get_config(env=NAME, setting=False) == 'bar' assert_raises(EnvironmentError, get_config, setting=NAME, env=False) # Set the value in the environment as well. m.config[NAME] = 'foo' # Ensure that settings take precedence. assert_equals(get_config(NAME), 'foo') # Two different names can be supplied. assert get_config(setting=NAME2, env=NAME) == 'bar' # Unset the env variable, now with only the setting. del os.environ[NAME] assert get_config(NAME) == 'foo' assert get_config(setting=NAME, env=False) == 'foo' assert_raises(EnvironmentError, get_config, env=NAME) finally: if NAME in os.environ: del os.environ[NAME]