from pathlib import Path from docforge.cli.main import cli def test_mkdocs_build_full_flow( cli_runner, mock_mkdocs_build, mock_mkdocs_load_config, tmp_path, ): # This test covers what used to be generate + mkdocs + build with cli_runner.isolated_filesystem(): cwd = Path.cwd() pkg = cwd / "testpkg" pkg.mkdir() (pkg / "__init__.py").write_text("") (pkg / "mod.py").write_text("def f(): ...\n") nav_file = cwd / "docforge.nav.yml" nav_file.write_text("home: lib/testpkg/index.md\ngroups: {}\n") result = cli_runner.invoke( cli, [ "build", "--mkdocs", "--module", "testpkg", "--site-name", "Test Site", ], ) assert result.exit_code == 0 assert mock_mkdocs_build() is True config = cwd / "docs" / "mkdocs.lib.yml" assert config.exists() assert (cwd / "docs" / "lib" / "testpkg" / "mod.md").exists() content = config.read_text(encoding="utf-8") assert "docs_dir: lib" in content assert "site_dir: ../site/lib" in content assert "Home: testpkg/index.md" in content def test_mkdocs_build_missing_module_fails(cli_runner): result = cli_runner.invoke(cli, ["build", "--mkdocs", "--site-name", "Test"]) assert result.exit_code != 0 assert "--module is required" in result.output def test_mkdocs_build_without_site_name_uses_module_as_default_full_flow( cli_runner, mock_mkdocs_build, mock_mkdocs_load_config, ): # Full integration test: real generation, real config, mocked mkdocs build with cli_runner.isolated_filesystem(): cwd = Path.cwd() # Create a minimal Python package pkg = cwd / "testpkg" pkg.mkdir() (pkg / "__init__.py").write_text("") (pkg / "mod.py").write_text("def f(): ...\n") # Create nav spec expected by build_lib_nav nav_file = cwd / "docforge.nav.yml" nav_file.write_text( "home: lib/testpkg/index.md\ngroups: {}\n", encoding="utf-8", ) result = cli_runner.invoke( cli, ["build", "--mkdocs", "--module", "testpkg"], ) assert result.exit_code == 0 assert mock_mkdocs_build() is True # The per-kind MkDocs config must exist config = cwd / "docs" / "mkdocs.lib.yml" assert config.exists() # Site name must default to module name content = config.read_text(encoding="utf-8") assert "site_name: testpkg" in content # Docs must be generated under the nested docs/lib dir assert (cwd / "docs" / "lib" / "testpkg" / "mod.md").exists() def test_existing_lib_config_used_verbatim_without_refresh( cli_runner, mock_mkdocs_build, mock_mkdocs_load_config, ): with cli_runner.isolated_filesystem(): cwd = Path.cwd() pkg = cwd / "testpkg" pkg.mkdir() (pkg / "__init__.py").write_text("") (pkg / "mod.py").write_text("def f(): ...\n") nav_file = cwd / "docforge.nav.yml" nav_file.write_text( "home: lib/testpkg/index.md\ngroups: {}\n", encoding="utf-8", ) config = cwd / "docs" / "mkdocs.lib.yml" config.parent.mkdir(parents=True) sentinel = "site_name: Custom Lib\nnav: []\n" config.write_text(sentinel, encoding="utf-8") result = cli_runner.invoke( cli, ["build", "--mkdocs", "--module", "testpkg"], ) assert result.exit_code == 0, result.output assert mock_mkdocs_build() is True assert config.read_text(encoding="utf-8") == sentinel assert "Using existing MkDocs config" in result.output def test_refresh_rebases_existing_lib_config( cli_runner, mock_mkdocs_build, mock_mkdocs_load_config, ): with cli_runner.isolated_filesystem(): cwd = Path.cwd() pkg = cwd / "testpkg" pkg.mkdir() (pkg / "__init__.py").write_text("") (pkg / "mod.py").write_text("def f(): ...\n") nav_file = cwd / "docforge.nav.yml" nav_file.write_text( "home: lib/testpkg/index.md\ngroups: {}\n", encoding="utf-8", ) config = cwd / "docs" / "mkdocs.lib.yml" config.parent.mkdir(parents=True) config.write_text("site_name: Stale\nnav: []\n", encoding="utf-8") result = cli_runner.invoke( cli, ["build", "--mkdocs", "--module", "testpkg", "--refresh"], ) assert result.exit_code == 0, result.output assert mock_mkdocs_build() is True content = config.read_text(encoding="utf-8") assert "site_name: testpkg" in content assert "docs_dir: lib" in content assert "site_dir: ../site/lib" in content assert "Home: testpkg/index.md" in content