Example #1
0
    def test_subprocess(self):
        """Instead of the ``argv`` shortcut, subclasses can also use the
        ``subprocess`` helper manually.
        """

        class Filter(ExternalTool): pass

        # Without stdin data
        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']
        out = StringIO()
        Filter.subprocess(['test'], out)
        assert out.getvalue() == 'stdout'
        self.popen.return_value.communicate.assert_called_with(None)

        # With stdin data
        self.popen.reset_mock()
        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']
        out = StringIO()
        Filter.subprocess(['test'], out, data='data')
        assert out.getvalue() == 'stdout'
        self.popen.return_value.communicate.assert_called_with('data')

        # With error
        self.popen.return_value.returncode = 1
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']
        assert_raises(FilterError, Filter.subprocess, ['test'], StringIO())
    def test_output_moved(self):
        class Filter(ExternalTool):
            pass

        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']

        # {output} creates an output
        # this test *moves* the file into the target location, and
        # tests the fix to issue #286
        intercepted = {}

        def fake_output_file(argv, **kw):
            intercepted['filename'] = argv[0]
            with open(argv[0] + '.tmp', 'w') as f:
                f.write('bat')
            import shutil
            shutil.move(argv[0] + '.tmp', argv[0])
            return DEFAULT

        self.popen.side_effect = fake_output_file
        # We get the result we generated in the hook above
        out = StringIO()
        Filter.subprocess(['{output}'], out)
        assert out.getvalue() == 'bat'
        # File has been deleted
        assert not os.path.exists(intercepted['filename'])
Example #3
0
    def test_output_moved(self):
        class Filter(ExternalTool):
            pass

        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ["stdout", "stderr"]

        # {output} creates an output
        # this test *moves* the file into the target location, and
        # tests the fix to issue #286
        intercepted = {}

        def fake_output_file(argv, **kw):
            intercepted["filename"] = argv[0]
            with open(argv[0] + ".tmp", "w") as f:
                f.write("bat")
            import shutil

            shutil.move(argv[0] + ".tmp", argv[0])
            return DEFAULT

        self.popen.side_effect = fake_output_file
        # We get the result we generated in the hook above
        out = StringIO()
        Filter.subprocess(["{output}"], out)
        assert out.getvalue() == "bat"
        # File has been deleted
        assert not os.path.exists(intercepted["filename"])
Example #4
0
    def test_input_var(self):
        """Test {input} variable."""

        class Filter(ExternalTool):
            pass

        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = [b"stdout", b"stderr"]

        # {input} creates an input file
        intercepted = {}

        def check_input_file(argv, **kw):
            intercepted["filename"] = argv[0]
            with open(argv[0], "r") as f:
                # File has been generated with input data
                assert f.read() == "foo"
            return DEFAULT

        self.popen.side_effect = check_input_file
        Filter.subprocess(["{input}"], StringIO(), data="foo")
        # No stdin was passed
        self.popen.return_value.communicate.assert_called_with(None)
        # File has been deleted
        assert not os.path.exists(intercepted["filename"])

        # {input} requires input data
        assert_raises(ValueError, Filter.subprocess, ["{input}"], StringIO())
    def test_input_var(self):
        """Test {input} variable."""
        class Filter(ExternalTool):
            pass

        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = [
            b'stdout', b'stderr'
        ]

        # {input} creates an input file
        intercepted = {}

        def check_input_file(argv, **kw):
            intercepted['filename'] = argv[0]
            with open(argv[0], 'r') as f:
                # File has been generated with input data
                assert f.read().decode('utf-8') == u'fooñ'
            return DEFAULT

        self.popen.side_effect = check_input_file
        Filter.subprocess(['{input}'], StringIO(), data=u'fooñ')
        # No stdin was passed
        self.popen.return_value.communicate.assert_called_with(None)
        # File has been deleted
        assert not os.path.exists(intercepted['filename'])

        # {input} requires input data
        assert_raises(ValueError, Filter.subprocess, ['{input}'], StringIO())
Example #6
0
    def test_subprocess(self):
        """Instead of the ``argv`` shortcut, subclasses can also use the
        ``subprocess`` helper manually.
        """

        class Filter(ExternalTool): pass

        # Without stdin data
        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']
        out = StringIO()
        Filter.subprocess(['test'], out)
        assert out.getvalue() == 'stdout'
        self.popen.return_value.communicate.assert_called_with(None)

        # With stdin data
        self.popen.reset_mock()
        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']
        out = StringIO()
        Filter.subprocess(['test'], out, data='data')
        assert out.getvalue() == 'stdout'
        self.popen.return_value.communicate.assert_called_with('data')

        # With error
        self.popen.return_value.returncode = 1
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']
        assert_raises(FilterError, Filter.subprocess, ['test'], StringIO())
Example #7
0
    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'
Example #8
0
    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 list(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"
Example #9
0
    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'
Example #10
0
 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)
Example #11
0
 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')
Example #12
0
    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]
Example #13
0
    def test_output_var(self):
        class Filter(ExternalTool): pass
        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']

        # {input} creates an input file
        intercepted = {}
        def fake_output_file(argv,  **kw):
            intercepted['filename'] = argv[0]
            with open(argv[0], 'w') as f:
                f.write('bat')
            return DEFAULT
        self.popen.side_effect = fake_output_file
        # We get the result we generated in the hook above
        out = StringIO()
        Filter.subprocess(['{output}'], out)
        assert out.getvalue() == 'bat'
        # File has been deleted
        assert not os.path.exists(intercepted['filename'])
Example #14
0
    def test_output_var(self):
        class Filter(ExternalTool): pass
        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ['stdout', 'stderr']

        # {input} creates an input file
        intercepted = {}
        def fake_output_file(argv,  **kw):
            intercepted['filename'] = argv[0]
            with open(argv[0], 'w') as f:
                f.write('bat')
            return DEFAULT
        self.popen.side_effect = fake_output_file
        # We get the result we generated in the hook above
        out = StringIO()
        Filter.subprocess(['{output}'], out)
        assert out.getvalue() == 'bat'
        # File has been deleted
        assert not os.path.exists(intercepted['filename'])
Example #15
0
    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]
Example #16
0
 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')
Example #17
0
    def test_get_config(self):
        """Test the ``get_config`` helper.
        """
        m = Environment(None, None)
        f = Filter()
        f.set_context(ContextWrapper(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

        with os_environ_sandbox():
            # Test raising of error, and test not raising it.
            assert_raises(EnvironmentError, get_config, NAME)
            assert get_config(NAME, require=False) is 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_equal(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)
Example #18
0
    def test_output_var(self):
        class Filter(ExternalTool):
            pass

        self.popen.return_value.returncode = 0
        self.popen.return_value.communicate.return_value = ["stdout", "stderr"]

        # {output} creates an output file
        intercepted = {}

        def fake_output_file(argv, **kw):
            intercepted["filename"] = argv[0]
            with open(argv[0], "w") as f:
                f.write("bat")
            return DEFAULT

        self.popen.side_effect = fake_output_file
        # We get the result we generated in the hook above
        out = StringIO()
        Filter.subprocess(["{output}"], out)
        assert out.getvalue() == "bat"
        # File has been deleted
        assert not os.path.exists(intercepted["filename"])
Example #19
0
 def __init__(self, id):
     Filter.__init__(self)
     self.id = id
Example #20
0
 def __init__(self, id):
     Filter.__init__(self)
     self.id = id
Example #21
0
 def __init__(self, input=None, output=None, unique=True):
     Filter.__init__(self)
     self._input = input
     self._output = output
     self._unique = unique
Example #22
0
 def __init__(self, input=(None, None), output=(None, None)):
     Filter.__init__(self)
     self._input_from, self._input_to = input
     self._output_from, self._output_to = output
Example #23
0
 def __init__(self, id): Filter.__init__(self); self.id = id
 def id(self): return self.id
Example #24
0
 def __init__(self, input=None, output=None, unique=True):
     Filter.__init__(self)
     self._input = input
     self._output = output
     self._unique = unique
Example #25
0
 def __init__(self, input=(None, None), output=(None, None)):
     Filter.__init__(self)
     self._input_from, self._input_to = input
     self._output_from, self._output_to = output
Example #26
0
 def __init__(self, input=None, output=None):
     Filter.__init__(self)
     self._input = input
     self._output = output
Example #27
0
 def __init__(self, input=None, output=None):
     Filter.__init__(self)
     self._input = input
     self._output = output