Example #1
0
    def test_walk_simple(self):
        filenames = [path.join(self.temp, name) for name in ('z', 'a', 'b')]
        for filename in filenames:
            with open(filename, 'w'):
                pass

        entries = walk([self.temp])
        self.assertListEqual(list(entries), list(sorted(filenames)))

        entries = walk(filenames)
        self.assertListEqual(list(entries), filenames)
Example #2
0
    def test_walk_subdir(self):
        mkdir(path.join(self.temp, 'dir'))
        filenames = [path.join(self.temp, 'dir', name) for name in ('a', 'b')]
        for filename in filenames:
            with open(filename, 'w'):
                pass

        entries = walk([self.temp])
        self.assertListEqual(list(entries), filenames)
Example #3
0
    def test_walk_mixed(self):
        mkdir(path.join(self.temp, 'dir'))
        files = ['c', ('dir', 'a'), ('dir', 'b')]
        filenames = [path.join(self.temp, *p) for p in files]
        for filename in filenames:
            with open(filename, 'w'):
                pass

        entries = walk([self.temp])
        self.assertListEqual(list(entries), filenames)
Example #4
0
def _walk(file_or_dirs, log):
    """Convenience wrapper around beangulp.utils.walk()

    Log the name of the file being processed and check the input file
    size against identify.FILE_TOO_LARGE_THRESHOLD for too large input.

    """
    for filename in utils.walk(file_or_dirs):
        log(f'* {filename:}', nl=False)
        if os.path.getsize(filename) > identify.FILE_TOO_LARGE_THRESHOLD:
            log(' ... SKIP')
            continue
        yield filename
Example #5
0
 def test_walk_empty(self):
     entries = walk([self.temp])
     self.assertListEqual(list(entries), [])
Example #6
0
def _run(ctx,
         documents: List[str],
         expected: str,
         verbose: int,
         quiet: int,
         generate: bool = False,
         failfast: bool = False,
         force: bool = False):
    """Implement the test and generate commands."""

    assert len(ctx.importers) == 1
    importer = ctx.importers[0]

    verbosity = verbose - quiet
    log = utils.logger(verbosity)
    failures = 0

    for doc in utils.walk(documents):
        if doc.endswith('.beancount'):
            continue

        # Unless verbose mode is enabled, do not output a newline so
        # the test result is printed on the same line as the test
        # document filename.
        log(f'* {doc}', nl=verbosity > 0)

        # Compute the path to the expected output file.
        expected_filename = f"{doc}.beancount"
        if expected:
            expected_filename = path.join(expected,
                                          path.basename(expected_filename))

        # Run the importer's identify() method.
        if importer.identify(doc):
            account, date, name, entries = run_importer(importer, doc)
            log(f'  {expected_filename}', 1)
            if account is None:
                failures += 1
                log('  ERROR', fg='red')
                log('  ValueError: account() should not return None')
                continue
            log(
                '  {}/{:%Y-%m-%d}-{}'.format(account.replace(":", "/"), date
                                             or utils.getmdate(doc), name
                                             or path.basename(doc)), 1)
            if generate:
                try:
                    write_expected_file(expected_filename,
                                        account,
                                        date,
                                        name,
                                        entries,
                                        force=force)
                except FileExistsError as ex:
                    failures += 1
                    log('  ERROR', fg='red')
                    log('  FileExistsError: {}'.format(ex.filename))
                    continue
                log('  OK', fg='green')
                continue
            try:
                diff = compare_expected(expected_filename, account, date, name,
                                        entries)
            except FileNotFoundError:
                # The importer has positively identified a document
                # for which there is no expecred output file.
                failures += 1
                log('  ERROR', fg='red')
                log('  ExpectedOutputFileNotFound')
                continue
            if diff:
                # Test failure. Log an error.
                failures += 1
                log('  ERROR', fg='red')
                if verbosity >= 0:
                    sys.stdout.writelines(diff)
                    sys.stdout.write(os.linesep)
                    continue
            log('  PASSED', fg='green')

        elif path.exists(expected_filename):
            # The importer has not identified a document it should have.
            failures += 1
            log('  ERROR', fg='red')
            log('  DocumentNotIdentified')

        else:
            # Ignore files that are not positively identified by the
            # importer and for which there is no expected output file.
            log('  IGNORED')

        if failfast and failures:
            break

    if failures:
        sys.exit(1)