<commit_before>import sys
import os
if sys.version_info[0] < 3:
    from StringIO import StringIO
else:
    from io import StringIO
import unittest

from yara import cli 

TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
RULES_ROOT = os.path.join(TEST_ROOT, 'rules')
BIRD_YAR = os.path.join(RULES_ROOT, 'bird', 'meta.yar')
DOG_YAR = os.path.join(RULES_ROOT, 'dog', 'meta.yar')
EXTERN_YAR = os.path.join(RULES_ROOT, 'extern.yar')

def run_main(*args):
    sys.stdout = StringIO()
    sys.stderr = StringIO()
    try:
        try:
            ret = cli.main(args)
        finally:
            sys.stdout.seek(0)
            sys.stderr.seek(0)
            stdout = sys.stdout.read().strip()
            stderr = sys.stderr.read().strip()
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
    except Exception as exc:
        exc.stdout = stdout
        exc.stderr = stderr
        raise 
    return (ret, stdout, stderr)

class TestScan(unittest.TestCase):

    def test_help(self):
        ret, stdout, stderr = run_main('--help')
        self.assertEqual(ret, 0)
        self.assertTrue(stdout.startswith('NAME yara-ctypes'))

    def test_broken_opt(self):
        ret, stdout, stderr = run_main('--no-an-opt-opt')
        self.assertEqual(ret, -1)
        self.assertTrue("Getopt error:" in stderr)

    def test_list(self):
        ret, stdout, stderr = run_main('--list')
        self.assertEqual(ret, 0)
        self.assertTrue("example.packer_rules" in stdout)

    def test_select_yarafile(self):
        ret, stdout, stderr = run_main('-r', BIRD_YAR, '.')
        self.assertTrue("does not exist" not in stderr, msg="got %s" % stderr)
        self.assertEqual(ret, 0)

        ret, stdout, stderr = run_main('--rule=doesnotexist')
        self.assertEqual(ret, -1)
        self.assertTrue("does not exist" in stderr, msg="got %s" % stdout)

    def test_format(self):
        for fmt in ['pickle', 'json', 'pprint', 'marshal', 'dict']:
            try:
                ret, stdout, stderr = run_main('-r', BIRD_YAR, 
                    '--fmt=%s' % fmt, BIRD_YAR)
            except Exception as exc:
                print(exc.stdout)
                print(exc.stderr)
                print(exc.error)
                raise
            self.assertEqual(ret, 0)<commit_msg>logical seperation<commit_after>import sys
import os
if sys.version_info[0] < 3:
    from StringIO import StringIO
else:
    from io import StringIO
import unittest

from yara import cli 

TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
RULES_ROOT = os.path.join(TEST_ROOT, 'rules')
BIRD_YAR = os.path.join(RULES_ROOT, 'bird', 'meta.yar')
DOG_YAR = os.path.join(RULES_ROOT, 'dog', 'meta.yar')
EXTERN_YAR = os.path.join(RULES_ROOT, 'extern.yar')

def run_main(*args):
    sys.stdout = StringIO()
    sys.stderr = StringIO()
    try:
        try:
            ret = cli.main(args)
        finally:
            sys.stdout.seek(0)
            sys.stderr.seek(0)
            stdout = sys.stdout.read().strip()
            stderr = sys.stderr.read().strip()
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
    except Exception as exc:
        exc.stdout = stdout
        exc.stderr = stderr
        raise 
    return (ret, stdout, stderr)


class TestCLI(unittest.TestCase):
    def test_help(self):
        ret, stdout, stderr = run_main('--help')
        self.assertEqual(ret, 0)
        self.assertTrue(stdout.startswith('NAME yara-ctypes'))

    def test_broken_opt(self):
        ret, stdout, stderr = run_main('--no-an-opt-opt')
        self.assertEqual(ret, -1)
        self.assertTrue("Getopt error:" in stderr)

    def test_list(self):
        ret, stdout, stderr = run_main('--list')
        self.assertEqual(ret, 0)
        self.assertTrue("example.packer_rules" in stdout)


class TestScan(unittest.TestCase):
    def test_select_yarafile(self):
        ret, stdout, stderr = run_main('-r', BIRD_YAR, '.')
        self.assertTrue("does not exist" not in stderr, msg="got %s" % stderr)
        self.assertEqual(ret, 0)

        ret, stdout, stderr = run_main('--rule=doesnotexist')
        self.assertEqual(ret, -1)
        self.assertTrue("does not exist" in stderr, msg="got %s" % stdout)

    def test_format(self):
        for fmt in ['pickle', 'json', 'pprint', 'marshal', 'dict']:
            try:
                ret, stdout, stderr = run_main('-r', BIRD_YAR, 
                    '--fmt=%s' % fmt, BIRD_YAR)
            except Exception as exc:
                print(exc.stdout)
                print(exc.stderr)
                print(exc.error)
                raise
            self.assertEqual(ret, 0)
{'fraction_non_alphanumeric': 0.1309931506849315, 'fraction_numerical': 0.004280821917808219, 'mean_word_length': 2.267832167832168, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5250113', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1553, 'n_words': 338}