#!/usr/bin/env python3

# Copyright © 2014 Jakub Wilk <jwilk@jwilk.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import io
import os
import sys

import nose
import nose.plugins.cover

class Coverage(nose.plugins.cover.Coverage):

    stream = None

    def report(self, stream):
        return super().report(self.stream)

basedir = os.path.join(
    os.path.dirname(__file__),
    os.pardir,
)

def main():
    argv = [
        sys.argv[0],
        '--with-coverage',
        '--cover-package=lib',
        '--cover-erase',
    ]
    path = os.path.join(
        'tests',
        'coverage'
    )
    plugin = Coverage()
    report_stream = plugin.stream = io.StringIO()
    print(
        'Generated automatically by private/update-coverage. '
        'Do not edit.\n',
        file=report_stream
    )
    ok = nose.run(argv=argv, plugins=[plugin])
    if not ok:
        sys.exit(1)
    report_stream.seek(0)
    with open(path + '.tmp', 'wt', encoding='ASCII') as file:
        for line in report_stream:
            line = line.rstrip()
            print(line, file=file)
    os.rename(path + '.tmp', path)

if __name__ == '__main__':
    main()

# vim:ts=4 sts=4 sw=4 et
