1+ import os
2+ import shutil
3+ import tempfile
4+ import unittest
5+ from pathlib import Path
6+
7+ import sys
8+ sys .path .append (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
9+ from docs .compile_llms_txt import compile_llms_txt
10+
11+ class TestCompileLLMsTxt (unittest .TestCase ):
12+ def setUp (self ):
13+ # Create a temporary directory for test files
14+ self .test_dir = tempfile .mkdtemp ()
15+ self .docs_dir = Path (self .test_dir )
16+
17+ # Save current working directory
18+ self .original_cwd = os .getcwd ()
19+ os .chdir (self .docs_dir )
20+
21+ def tearDown (self ):
22+ os .chdir (self .original_cwd )
23+ shutil .rmtree (self .test_dir )
24+
25+ def create_test_mdx_file (self , path : str , content : str ):
26+ """Helper to create test MDX files"""
27+ file_path = self .docs_dir / path
28+ file_path .parent .mkdir (parents = True , exist_ok = True )
29+ file_path .write_text (content )
30+
31+ def test_basic_compilation (self ):
32+ """Test basic MDX file compilation"""
33+ # Create test MDX files
34+ self .create_test_mdx_file ("test1.mdx" , "Test content 1" )
35+ self .create_test_mdx_file ("test2.mdx" , "Test content 2" )
36+
37+ # Run compilation
38+ compile_llms_txt ()
39+
40+ # Check output file exists and contains expected content
41+ output_path = self .docs_dir / "llms.txt"
42+ self .assertTrue (output_path .exists ())
43+
44+ content = output_path .read_text ()
45+ self .assertIn ("## test1.mdx" , content )
46+ self .assertIn ("Test content 1" , content )
47+ self .assertIn ("## test2.mdx" , content )
48+ self .assertIn ("Test content 2" , content )
49+
50+ def test_excluded_directories (self ):
51+ """Test that files in excluded directories are skipped"""
52+ # Create files in both regular and excluded directories
53+ self .create_test_mdx_file ("regular/file.mdx" , "Regular content" )
54+ self .create_test_mdx_file ("tool/file.mdx" , "Tool content" )
55+
56+ compile_llms_txt ()
57+
58+ content = (self .docs_dir / "llms.txt" ).read_text ()
59+ self .assertIn ("Regular content" , content )
60+ self .assertNotIn ("Tool content" , content )
61+
62+ def test_excluded_files (self ):
63+ """Test that excluded files are skipped"""
64+ self .create_test_mdx_file ("regular.mdx" , "Regular content" )
65+ self .create_test_mdx_file ("tool.mdx" , "Tool content" )
66+
67+ compile_llms_txt ()
68+
69+ content = (self .docs_dir / "llms.txt" ).read_text ()
70+ self .assertIn ("Regular content" , content )
71+ self .assertNotIn ("Tool content" , content )
72+
73+ def test_nested_directories (self ):
74+ """Test compilation from nested directory structure"""
75+ self .create_test_mdx_file ("dir1/test1.mdx" , "Content 1" )
76+ self .create_test_mdx_file ("dir1/dir2/test2.mdx" , "Content 2" )
77+
78+ compile_llms_txt ()
79+
80+ content = (self .docs_dir / "llms.txt" ).read_text ()
81+ self .assertIn ("## dir1/test1.mdx" , content )
82+ self .assertIn ("## dir1/dir2/test2.mdx" , content )
83+ self .assertIn ("Content 1" , content )
84+ self .assertIn ("Content 2" , content )
85+
86+ def test_empty_directory (self ):
87+ """Test compilation with no MDX files"""
88+ compile_llms_txt ()
89+
90+ content = (self .docs_dir / "llms.txt" ).read_text ()
91+ self .assertEqual (content , "" )
0 commit comments