Esempio n. 1
0
    def test_dependencies(self):
        conf_file = os.path.join(self.__priv_dir, 'test.json')
        with open(conf_file, 'w') as _:
            _.write('{\n'
                    '"index": "my_index.markdown",\n'
                    '"test_index": "/home/meh/test_index.markdown",\n'
                    '"test_sources": ["*.x"],\n'
                    '"test_source_filters": ["foobar.x"]\n'
                    '}\n')

        touch(os.path.join(self.__priv_dir, 'foo.x'))
        touch(os.path.join(self.__priv_dir, 'bar.x'))
        touch(os.path.join(self.__priv_dir, 'baz.x'))
        touch(os.path.join(self.__priv_dir, 'foobar.x'))

        cfg = Config(conf_file=conf_file)

        deps = set([os.path.abspath(dep) for dep in cfg.get_dependencies()])

        self.assertSetEqual(
            deps,
            set([
                os.path.join(self.__priv_dir, 'foo.x'),
                os.path.join(self.__priv_dir, 'bar.x'),
                os.path.join(self.__priv_dir, 'baz.x'), conf_file
            ]))
Esempio n. 2
0
    def test_basic(self):
        proj = Project(self)

        conf = Config({})
        with self.assertRaises(ConfigError):
            proj.parse_name_from_config(conf)
            proj.parse_config(conf)

        sm_path = self._create_sitemap('sitemap.txt', 'index.markdown\n')
        conf = Config({'sitemap': sm_path})
        with self.assertRaises(ConfigError):
            proj.parse_name_from_config(conf)
            proj.parse_config(conf)

        index_path = self._create_md_file('index.markdown', '# Project')
        conf = Config({'sitemap': sm_path,
                       'index': index_path,
                       'project_name': 'test-project',
                       'project_version': '0.1'})
        proj.parse_name_from_config(conf)
        proj.parse_config(conf)

        self.assertDictEqual(
            {key: type(val) for key, val in proj.extensions.items()},
            self.extension_classes)

        proj.setup()

        self.assertEqual(len(proj.tree.get_pages()), 1)
Esempio n. 3
0
    def test_path(self):
        conf_file = os.path.join(self.__priv_dir, 'test.json')
        with open(conf_file, 'w') as _:
            _.write('{\n' '"my_path_argument": "somewhere/plop.x"\n' '}\n')

        cli = {'my_cli_path_argument': 'elsewhere/foo.x'}

        cfg = Config(command_line_args=cli, conf_file=conf_file)
        self.assertEqual(cfg.get_path('my_path_argument'),
                         os.path.join(self.__priv_dir, 'somewhere', 'plop.x'))
        invoke_dir = os.getcwd()
        self.assertEqual(cfg.get_path('my_cli_path_argument'),
                         os.path.join(invoke_dir, 'elsewhere', 'foo.x'))
Esempio n. 4
0
    def test_dump(self):
        conf_file = os.path.join(self.__priv_dir, 'test.json')
        with open(conf_file, 'w') as _:
            _.write('{\n'
                    '"index": "my_index.markdown",\n'
                    '"test_index": "/home/meh/test_index.markdown",\n'
                    '"test_sources": ["*.x"],\n'
                    '"test_source_filters": ["foobar.x"]\n'
                    '}\n')

        here = os.path.abspath(os.path.dirname(__file__))
        invoke_dir = os.getcwd()
        relpath = os.path.relpath(here, invoke_dir)
        overriden_src_dir = os.path.join(relpath, 'overridden_sources')

        cli = {
            'index': 'another_index.markdown',
            'test_sources': ['%s/*.x' % overriden_src_dir],
            'test_source_filters': ['%s/ignored.x' % overriden_src_dir]
        }

        cfg = Config(command_line_args=cli, conf_file=conf_file)
        cfg.dump(conf_file=conf_file)
        ncfg = Config(conf_file=conf_file)
        self.assertEqual(ncfg.get_index(),
                         os.path.join(invoke_dir, 'another_index.markdown'))
        self.assertListEqual(ncfg.get('test_sources'),
                             [u'../overridden_sources/*.x'])
Esempio n. 5
0
    def test_subproject(self):
        proj = Project(self)
        sm_path = self._create_sitemap(
            'sitemap.txt', 'index.markdown\n\tsubproject.json')
        index_path = self._create_md_file('index.markdown', '# Project')

        sub_sm_path = self._create_sitemap(
            'subsitemap.txt', 'subindex.markdown')
        sub_index_path = self._create_md_file(
            'subindex.markdown', '# Subproject')
        self._create_conf_file('subproject.json',
                               {'index': sub_index_path,
                                'sitemap': sub_sm_path,
                                'project_name': 'subproject',
                                'project_version': '0.2'})

        conf = Config({'sitemap': sm_path,
                       'index': index_path,
                       'project_name': 'test-project',
                       'project_version': '0.1',
                       'output': self._output_dir})
        proj.parse_name_from_config(conf)
        proj.parse_config(conf)
        proj.setup()

        self.assertEqual(len(proj.tree.get_pages()), 2)
        proj.format(self.link_resolver, self.output)
Esempio n. 6
0
 def add_subproject(self, fname, conf_path):
     """Creates and adds a new subproject."""
     config = Config(conf_file=conf_path)
     proj = Project(self.app)
     proj.parse_name_from_config(config)
     proj.parse_config(config)
     proj.setup()
     self.subprojects[fname] = proj
Esempio n. 7
0
    def test_cli_overrides(self):
        conf_file = os.path.join(self.__priv_dir, 'test.json')
        with open(conf_file, 'w') as _:
            _.write('{\n'
                    '"index": "my_index.markdown",\n'
                    '"test_index": "/home/meh/test_index.markdown",\n'
                    '"test_sources": ["*.x"],\n'
                    '"test_source_filters": ["foobar.x"]\n'
                    '}\n')

        touch(os.path.join(self.__priv_dir, 'foo.x'))
        touch(os.path.join(self.__priv_dir, 'foobar.x'))

        here = os.path.abspath(os.path.dirname(__file__))
        invoke_dir = os.getcwd()
        relpath = os.path.relpath(here, invoke_dir)
        overriden_src_dir = os.path.join(relpath, 'overridden_sources')

        shutil.rmtree(overriden_src_dir, ignore_errors=True)
        os.mkdir(overriden_src_dir)
        touch(os.path.join(overriden_src_dir, 'other.x'))
        touch(os.path.join(overriden_src_dir, 'foobar.x'))
        touch(os.path.join(overriden_src_dir, 'ignored.x'))

        cli = {
            'index': 'another_index.markdown',
            'test_sources': ['%s/*.x' % overriden_src_dir],
            'test_source_filters': ['%s/ignored.x' % overriden_src_dir]
        }

        cfg = Config(command_line_args=cli, conf_file=conf_file)
        self.assertEqual(cfg.get('index'), 'another_index.markdown')

        self.assertEqual(cfg.get_index(),
                         os.path.join(invoke_dir, 'another_index.markdown'))

        overriden_abs_dir = os.path.join(invoke_dir, overriden_src_dir)

        self.assertSetEqual(
            set(cfg.get_sources('test')),
            set([
                os.path.join(overriden_abs_dir, 'other.x'),
                os.path.join(overriden_abs_dir, 'foobar.x')
            ]))

        shutil.rmtree(overriden_src_dir, ignore_errors=True)
Esempio n. 8
0
    def test_sources(self):
        conf_file = os.path.join(self.__priv_dir, 'test.json')
        with open(conf_file, 'w') as _:
            _.write('{\n'
                    '"test_sources": ["*.x"],\n'
                    '"test_source_filters": ["foobar.x"]\n'
                    '}\n')

        touch(os.path.join(self.__priv_dir, 'foo.x'))
        touch(os.path.join(self.__priv_dir, 'bar.x'))
        touch(os.path.join(self.__priv_dir, 'baz.x'))
        touch(os.path.join(self.__priv_dir, 'foobar.x'))

        cfg = Config(conf_file=conf_file)
        self.assertSetEqual(
            set(cfg.get_sources('test')),
            set([
                os.path.join(self.__priv_dir, 'foo.x'),
                os.path.join(self.__priv_dir, 'bar.x'),
                os.path.join(self.__priv_dir, 'baz.x')
            ]))
Esempio n. 9
0
    def setUp(self):
        here = os.path.dirname(__file__)
        self.__md_dir = os.path.abspath(os.path.join(
            here, 'tmp-markdown-files'))
        self.private_folder = os.path.abspath(os.path.join(
            here, 'tmp-private'))
        self.__src_dir = os.path.abspath(os.path.join(
            here, 'tmp-src-files'))
        self.__output_dir = os.path.abspath(os.path.join(
            here, 'tmp-output'))
        self.__remove_tmp_dirs()
        os.mkdir(self.__md_dir)
        os.mkdir(self.private_folder)
        os.mkdir(self.__src_dir)
        os.mkdir(self.get_generated_doc_folder())
        self.include_paths = OrderedSet([self.__md_dir])
        self.include_paths.add(self.get_generated_doc_folder())

        self.dependency_map = {}

        # Using the real doc database is too costly, tests should be lightning
        # fast (and they are)
        self.database = Database(self.private_folder)
        self.link_resolver = LinkResolver(self.database)

        self.change_tracker = ChangeTracker()

        self.sitemap_parser = SitemapParser()

        self.project_name = 'test-project'
        self.sanitized_name = 'test-project-0.1'
        self.incremental = False

        self.tree = Tree(self, self)

        self.test_ext = TestExtension(self, self)
        self.core_ext = CoreExtension(self, self)

        cfg = Config()

        self.test_ext.parse_toplevel_config(cfg)
        self.test_ext.parse_config(cfg)
        self.core_ext.parse_toplevel_config(cfg)
        self.core_ext.parse_config(cfg)
        self.subprojects = {}
        self.is_toplevel = True
Esempio n. 10
0
    def test_subproject_extra_assets(self):
        proj = Project(self)
        self.project = proj
        sm_path = self._create_sitemap('sitemap.txt',
                                       'index.markdown\n\tsubproject.json')
        index_path = self._create_md_file('index.markdown', '# Project')

        sub_sm_path = self._create_sitemap('subsitemap.txt',
                                           'subindex.markdown')
        sub_index_path = self._create_md_file('subindex.markdown',
                                              '# Subproject')
        sub_asset = self._create_md_file('subassets/fake_asset.md', 'Fakery')
        self._create_conf_file(
            'subproject.json', {
                'index': sub_index_path,
                'sitemap': sub_sm_path,
                'project_name': 'subproject',
                'extra_assets': [os.path.dirname(sub_asset)],
                'project_version': '0.2'
            })

        extra_assets = self._create_md_file('extra_assets/fake_asset.md',
                                            'Main fake')
        conf = Config({
            'sitemap': sm_path,
            'index': index_path,
            'project_name': 'test-project',
            'project_version': '0.1',
            'extra_assets': [os.path.dirname(extra_assets)],
            'output': self._output_dir
        })
        proj.parse_name_from_config(conf)
        proj.parse_config(conf, toplevel=True)
        proj.setup()

        self.assertEqual(len(proj.tree.get_pages()), 2)
        proj.format(self.link_resolver, self.output)
        proj.write_out(self.output)

        # FIXME: reenable with a different testing strategy
        # pylint: disable=pointless-string-statement
        '''
Esempio n. 11
0
    def test_index(self):
        conf_file = os.path.join(self.__priv_dir, 'test.json')
        with open(conf_file, 'w') as _:
            _.write('{\n'
                    '"index": "my_index.markdown",\n'
                    '"test_index": "/home/meh/test_index.markdown",\n'
                    '"test_other_index": "other_index.markdown"\n'
                    '}\n')

        cfg = Config(conf_file=conf_file)

        # A relative path was passed, the parser should return an
        # absolute path with the path to the conf file as root.
        self.assertEqual(cfg.get_index(),
                         os.path.join(self.__priv_dir, 'my_index.markdown'))

        # An absolute path was passed, and must thus be retrieved
        self.assertEqual(cfg.get_index('test'),
                         '/home/meh/test_index.markdown')

        self.assertIsNone(cfg.get_index('invalid_prefix'))

        self.assertEqual(cfg.get_index('test_other'),
                         os.path.join(self.__priv_dir, 'other_index.markdown'))
Esempio n. 12
0
 def _create_project_config(self, name, **kwargs):
     return Config(
         conf_file=self._create_project_config_file(name, **kwargs))
Esempio n. 13
0
def run(args, verbose=False):
    """
    Banana banana
    """

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter, add_help=False)
    parser.add_argument("--extra-extension-path",
                        action="append",
                        default=[],
                        dest="extra_extension_path",
                        help="An extra extension path to use")
    parser.add_argument('--conf-file',
                        help='Path to the config file',
                        dest='conf_file')
    tmpargs, _args = parser.parse_known_args(args)

    json_conf = None
    if tmpargs.conf_file:
        json_conf = load_config_json(tmpargs.conf_file)
        tmpargs.extra_extension_path += json_conf.get('extra_extension_path',
                                                      [])

    # We only get these once, doing this now means all
    # installed extensions will show up as Configurable subclasses.
    try:
        ext_classes = get_extension_classes(
            sort=True, extra_extension_paths=tmpargs.extra_extension_path)
    except HotdocException:
        return 1

    parser.add_argument('command',
                        action="store",
                        choices=('run', 'conf', 'init', 'help'),
                        nargs="?")
    parser.add_argument('--output-conf-file',
                        help='Path where to save the updated conf'
                        ' file',
                        dest='output_conf_file')
    parser.add_argument('--init-dir',
                        help='Directory to initialize',
                        dest='init_dir')
    parser.add_argument('--version',
                        help="Print version and exit",
                        action="store_true")
    parser.add_argument('--makefile-path',
                        help="Print path to includable "
                        "Makefile and exit",
                        action="store_true")
    parser.add_argument("--get-conf-key",
                        action="store",
                        help="print the value for a configuration "
                        "key")
    parser.add_argument("--get-conf-path",
                        action="store",
                        help="print the value for a configuration "
                        "path")
    parser.add_argument("--get-private-folder",
                        action="store_true",
                        help="get the path to hotdoc's private "
                        "folder")
    parser.add_argument("--has-extension",
                        action="append",
                        dest="has_extensions",
                        default=[],
                        help="Check if a given extension is available")
    parser.add_argument("--list-extensions",
                        action="store_true",
                        dest="list_extensions",
                        help="Print "
                        "available extensions")
    parser.add_argument("-",
                        action="store_true",
                        help="Separator to allow finishing a list"
                        " of arguments before a command",
                        dest="whatever")

    add_args_methods = set()

    for klass in all_subclasses(Configurable):
        if klass.add_arguments not in add_args_methods:
            klass.add_arguments(parser)
            add_args_methods.add(klass.add_arguments)

    known_args, _ = parser.parse_known_args(args)

    defaults = {}
    actual_args = {}
    for key, value in list(dict(vars(known_args)).items()):
        if value != parser.get_default(key):
            actual_args[key] = value
        if parser.get_default(key) is not None:
            defaults[key] = value

    if known_args.has_extensions:
        res = 0
        for extension_name in known_args.has_extensions:
            found = False
            for klass in ext_classes:
                if klass.extension_name == extension_name:
                    found = True
                    if verbose:
                        print("Extension '%s'... FOUND." % extension_name)
            if not found:
                if verbose:
                    print("Extension '%s'... NOT FOUND." % extension_name)
                res = 1
        return res

    if known_args.list_extensions:
        print("Extensions:")
        extensions = [e.extension_name for e in ext_classes]
        for extension in sorted(extensions):
            print(" - %s " % extension)
        return 0

    if known_args.command != 'init':
        conf_file = actual_args.get('conf_file')
        if conf_file is None and os.path.exists('hotdoc.json'):
            conf_file = 'hotdoc.json'
    else:
        conf_file = ''

    config = Config(command_line_args=actual_args,
                    conf_file=conf_file,
                    defaults=defaults,
                    json_conf=json_conf)

    Logger.parse_config(config)

    return execute_command(parser, config, ext_classes)
Esempio n. 14
0
 def __make_config(self, conf):
     return Config(conf_file=os.path.join(self.__test_dir, 'hotdoc.json'),
                   json_conf=conf)