def test_normal(fs): init() dst = StringIO() Quom(Path('main.hpp'), dst) assert dst.getvalue() == RESULT_NORMAL
def test_normal_without_trim(fs): init() dst = StringIO() Quom(Path('main.hpp'), dst, trim=False) assert dst.getvalue() == RESULT_NORMAL_WITHOUT_TRIM
def test_add_line_break_in_stitched_files_if_missing_at_stitch_location(fs): init() dst = StringIO() Quom(Path('main.hpp'), dst, stitch_format='Stitch Begin') assert dst.getvalue() == RESULT_STITCH
def test_with_mismatching_include_guard_format(fs): init() dst = StringIO() Quom(Path('main.hpp'), dst, include_guard_format='FOOBAR_.+_HP') assert dst.getvalue() == RESULT_NORMAL
def test_add_line_break_in_stitched_files_if_missing(fs): init() dst = StringIO() Quom(Path('main.hpp'), dst) assert dst.getvalue() == RESULT
def test_with_missing_header_file(fs): init() Path('foo.hpp').unlink() dst = StringIO() with pytest.raises(QuomError): Quom(Path('main.hpp'), dst)
def test_with_include_guard_format(fs): init() dst = StringIO() Quom(Path('main.hpp'), dst, include_guard_format='FOOBAR_.+_HPP') assert dst.getvalue() == RESULT_WITH_INCLUDE_GUARD_FORMAT
def test_without_newline_at_end(fs): with open('main.hpp', 'w+', encoding='utf-8') as file: file.write('int a;') dst = StringIO() Quom(Path('main.hpp'), dst) assert dst.getvalue() == 'int a;'
def test_with_missing_source_file(fs): init() Path('foo.cpp').unlink() dst = StringIO() Quom(Path('main.hpp'), dst) assert dst.getvalue() == RESULT_NORMAL_WITHOUT_SOURCES
def test_with_stitch_location(fs): init() with open('main.hpp', 'w') as file: file.write(FILE_MAIN_WITH_STITCH_LOCATION_HPP) dst = StringIO() Quom(Path('main.hpp'), dst, stitch_format='~> stitch <~') assert dst.getvalue() == RESULT_WITH_STITCH_LOCATION
def test_include_directory(fs): os.makedirs('include/my_lib/core') os.makedirs('include/my_lib/util') os.makedirs('include/my_other_lib/') with open('include/my_lib/main.hpp', 'w+', encoding='utf-8') as file: file.write(FILE_MAIN_HPP) with open('include/my_lib/core/core.hpp', 'w+', encoding='utf-8') as file: file.write(FILE_CORE_HPP) with open('include/my_lib/core/core.cpp', 'w+', encoding='utf-8') as file: file.write(FILE_CORE_CPP) with open('include/my_lib/util/foo.hpp', 'w+', encoding='utf-8') as file: file.write(FILE_FOO_HPP) with open('include/my_lib/util/foo.cpp', 'w+', encoding='utf-8') as file: file.write(FILE_FOO_CPP) with open('include/my_other_lib/bar.hpp', 'w+', encoding='utf-8') as file: file.write(FILE_BAR_HPP) with open('include/my_other_lib/bar.cpp', 'w+', encoding='utf-8') as file: file.write(FILE_BAR_CPP) with open('include/my_other_lib/info.hpp', 'w+', encoding='utf-8') as file: file.write(FILE_INFO_HPP) dst = StringIO() with pytest.raises(QuomError): Quom(Path('include/my_lib/main.hpp'), dst) dst = StringIO() Quom(Path('include/my_lib/main.hpp'), dst, include_directories=['include/']) assert dst.getvalue() == RESULT
def test_source_directory(fs): os.makedirs('project/') os.chdir('project/') os.makedirs('include/') os.makedirs('src/') with open('include/main.hpp', 'w+', encoding='utf-8') as file: file.write(FILE_MAIN_HPP) with open('src/main.cpp', 'w+', encoding='utf-8') as file: file.write(FILE_MAIN_CPP) dst = StringIO() Quom(Path('include/main.hpp'), dst) assert dst.getvalue() != RESULT dst = StringIO() Quom(Path('include/main.hpp'), dst, relative_source_directories=[Path('../src')]) assert dst.getvalue() == RESULT dst = StringIO() Quom(Path('include/main.hpp'), dst, source_directories=[Path('src').resolve()]) assert dst.getvalue() == RESULT dst = StringIO() Quom(Path('include/main.hpp'), dst, source_directories=[Path('/project/src')]) assert dst.getvalue() == RESULT main(['include/main.hpp', 'result.hpp', '-S', './../src']) assert Path('result.hpp').read_text() == RESULT main(['include/main.hpp', 'result.hpp', '-S', 'src']) assert Path('result.hpp').read_text() == RESULT main(['include/main.hpp', 'result.hpp', '-S', '/project/src']) assert Path('result.hpp').read_text() == RESULT
def test_same_file_different_include(fs): os.makedirs('a') os.makedirs('b') with open('main.cpp', 'w+', encoding='utf-8') as file: file.write(FILE_MAIN_CPP) with open('a/foo.hpp', 'w+', encoding='utf-8') as file: file.write(FILE_FOO_HPP) with open('b/bar.hpp', 'w+', encoding='utf-8') as file: file.write(FILE_BAR_HPP) dst = StringIO() Quom('main.cpp', dst) assert dst.getvalue() == RESULT
def test_with_mismatching_stitch(fs): init() dst = StringIO() with pytest.raises(QuomError): Quom(Path('main.hpp'), dst, stitch_format='~ stitch ~')
def main(args: List[str]): parser = argparse.ArgumentParser( prog='quom', description='Single header generator for C/C++ libraries.') parser.add_argument('--version', action='version', version='quom {ver}'.format(ver=__version__)) parser.add_argument('input_path', metavar='input', type=Path, help='Input file path of the main file.') parser.add_argument( 'output_path', metavar='output', type=Path, help='Output file path of the generated single header file.') parser.add_argument( '--stitch', '-s', metavar='format', type=str, default=None, help= 'Format of the comment where the source files should be placed (e.g. // ~> stitch <~). \ Default: %(default)s (at the end of the main file)') parser.add_argument( '--include_guard', '-g', metavar='format', type=str, default=None, help='Regex format of the include guard. Default: %(default)s') parser.add_argument( '--trim', '-t', action='store_true', default=True, help='Reduce continuous line breaks to one. Default: %(default)s') parser.add_argument('--include_directory', '-I', type=Path, action='append', default=[], help='Add include directories for header files.') parser.add_argument( '--source_directory', '-S', type=str, action='append', default=['.'], help='Set the source directories for source files. ' 'Use ./ or .\\ in front of a path to mark as relative to the header file.' ) parser.add_argument('--encoding', '-e', type=str, default='utf-8', help='The encoding used to read and write all files.') args = parser.parse_args(args) # Transform source directories to distingue between: # - relative from header file (starting with dot) # - relative from workdir # - absolute path relative_source_directories = [] source_directories = [] for src in args.source_directory: path = Path(src) if src == '.' or src.startswith('./') or src.startswith('.\\'): relative_source_directories.append(path) else: source_directories.append(path.resolve()) with args.output_path.open('w+', encoding=args.encoding) as file: Quom(args.input_path, file, args.stitch, args.include_guard, args.trim, args.include_directory, relative_source_directories, source_directories, args.encoding)