Ejemplo n.º 1
0
    def assertTestIO(self, testio, msg=None):
        """
        Args:
            testio (test_pyline.IO):
        Kwargs:
            msg (None, str): Assertion kwargs
        """
        # print(testio)
        args, kwargs, expectedoutput = testio

        if isinstance(args, basestring):
            args = args.splitlines(True)
        iterable = args

        if hasattr(expectedoutput, 'readlines'):
            expectedoutputlist = expectedoutput.readlines()
        elif hasattr(expectedoutput, 'splitlines'): # isinstance(basestring)
            expectedoutputlist = expectedoutput.splitlines(True)
        else:
            expectedoutputlist = expectedoutput

        # output = list(pyline.pyline(args, **kwargs)) # TODO: port sort?

        output = []
        pyline.main(iterable=iterable, results=output, opts=kwargs)

        outputresults = [x.result for x in output]
        self.assertSequenceEqualSidebyside(
                expectedoutputlist,
                outputresults,
                seq_type=list, # (list, io.StringIO),
                header1='seq1',
                header2='seq2',
                msg=msg)
Ejemplo n.º 2
0
 def test_pyline_jinja__TemplateNotFound(self):
     iterable = TEST_INPUT_A0
     results = []
     with self.assertRaises(jinja2.TemplateNotFound):
         pyline.main(
             args=['-O', 'jinja:template=TemplateNotFound!.jinja'],
             results=results,
             iterable=iterable)
Ejemplo n.º 3
0
 def test_pyline_jinja__testtemplate(self):
     iterable = TEST_INPUT_A0
     template_name = 'obj-newline.jinja2'
     templatespath = os.path.realpath(os.path.join(
         os.path.dirname(__file__),
         '..',
         'pyline',
         'templates'))
     templatepath = os.path.join(templatespath, template_name)
     output_formatstr = 'jinja:template={}'.format(templatepath)
     results = []
     retcode, _results = pyline.main(
         #args=['-O', 'jinja:template=obj-newline.jinja'],
         args=['-O', output_formatstr],
         results=results,
         iterable=iterable)
     self.assertEqual(0, retcode)
     self.assertEqual(results, _results)
Ejemplo n.º 4
0
 def test_pyline_jinja__mustspecifyargs_ValueError(self):
     iterable = TEST_INPUT_A0
     with self.assertRaises(ValueError):
         pyline.main(
             args=['-O', 'jinja'],
             iterable=iterable)
Ejemplo n.º 5
0
    def test_20_pyline_main(self):
        CMDLINE_TESTS = (
            tuple(),
            ("line",),
            ("l",),
            ("l", "-n"),
            ("l and l[:5]",),
            ("words",),
            ("w",),
            ("w", "-n"),
            ("w", "--shlex"),
            ("w", '-O', 'csv'),
            ("w", '-O', 'csv', '-n'),

            ("w", '-O', 'csv', '-s', '0'),
            ("w", '-O', 'csv', '-s', '1'),
            ("w", '-O', 'csv', '-s', '1,2'),
            ("w", '-O', 'csv', '-S', '1'),
            ("w", '-O', 'csv', '-S', '1', '-n'),

            ("w", '-O', 'json'),
            ("w", '-O', 'json', '-n'),

            ("w", '-O', 'tsv'),

            ("w", '-O', 'html'),

            ("w", '-O', 'checkbox'),
            ("w", '-O', 'chk'),

            ("len(words) > 2 and words",),

            ('-r', '(.*with.*)'),
            ('-r', '(.*with.*)',            '-R', 'i'),
            ('-r', '(?P<line>.*with.*)'),
            ('-r', '(?P<line>.*with.*)',    '-O', 'json'),
            ('-r', '(?P<line>.*with.*)',    '-O', 'checkbox'),
            ('-r', '(.*with.*)', 'rgx and {"n":i, "match": rgx.groups()[0]}',
             '-O', 'json'),
            ("-r", '(.*with.*)', '_rgx.findall(line)',
             '-O', 'json'),

            ('-m',
             'os',
             'os.path.isfile(line) and (os.stat(line).st_size, line)'),
            #
            ("-p", "p and p.isfile() and (p.size, p, p.stat())")
        )

        TEST_ARGS = ('-f', self.TEST_FILE)

        for argset in CMDLINE_TESTS:
            _args = TEST_ARGS + argset
            self.log.debug("main%s" % str(_args))
            try:
                output = pyline.main(_args)
                for n in output and output or []:
                    self.log.debug(n)
            except Exception as e:
                self.log.error("cmd: %s" % repr(_args))
                self.log.exception(e)
                raise