Coverage for tests\test_lmcat_2.py: 74%

87 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-29 16:57 -0700

1import os 

2from pathlib import Path 

3 

4import pytest 

5 

6from lmcat.lmcat import ( 

7 LMCatConfig, 

8 walk_and_collect, 

9 assemble_summary, 

10) 

11 

12# Base test directory 

13TEMP_PATH: Path = Path("tests/_temp") 

14 

15 

16def test_unicode_file_handling(): 

17 """Test handling of Unicode in filenames and content""" 

18 test_dir = TEMP_PATH / "unicode_test" 

19 test_dir.mkdir(parents=True, exist_ok=True) 

20 

21 # Create directories 

22 (test_dir / "привет").mkdir() 

23 (test_dir / "emoji_📁").mkdir() 

24 

25 # Create files 

26 (test_dir / "hello_世界.txt").write_text( 

27 "Hello 世界\nこんにちは\n", encoding="utf-8" 

28 ) 

29 (test_dir / "привет/мир.txt").write_text("Привет мир!\n", encoding="utf-8") 

30 (test_dir / "emoji_📁/test_🔧.txt").write_text( 

31 "Test with emojis 🎉\n", encoding="utf-8" 

32 ) 

33 (test_dir / ".gitignore").write_text("*.tmp\n") 

34 (test_dir / "unicode_temp_⚡.tmp").write_text("should be ignored", encoding="utf-8") 

35 

36 config = LMCatConfig() 

37 

38 # Test walking 

39 tree_output, file_list = walk_and_collect(test_dir, config) 

40 tree_str = "\n".join(tree_output) 

41 

42 # Check filenames in tree 

43 assert "hello_世界.txt" in tree_str 

44 assert "мир.txt" in tree_str 

45 assert "test_🔧.txt" in tree_str 

46 assert "unicode_temp_⚡.tmp" not in tree_str # Should be ignored 

47 

48 # Check content handling 

49 summary = assemble_summary(test_dir, config) 

50 assert "Hello 世界" in summary 

51 assert "Привет мир!" in summary 

52 assert "Test with emojis 🎉" in summary 

53 

54 

55def test_large_file_handling(): 

56 """Test handling of large files""" 

57 test_dir = TEMP_PATH / "large_file_test" 

58 test_dir.mkdir(parents=True, exist_ok=True) 

59 

60 # Create regular files 

61 (test_dir / "small.txt").write_text("small content\n") 

62 (test_dir / "medium.txt").write_text("medium " * 1000) 

63 

64 # Create large file 

65 with (test_dir / "large.txt").open("w") as f: 

66 f.write("x" * (1024 * 1024)) 

67 

68 config = LMCatConfig() 

69 tree_output, file_list = walk_and_collect(test_dir, config) 

70 

71 # Check stats in tree output 

72 tree_str = "\n".join(tree_output) 

73 assert "small.txt" in tree_str 

74 assert "medium.txt" in tree_str 

75 assert "large.txt" in tree_str 

76 

77 # Check that files are readable in summary 

78 summary = assemble_summary(test_dir, config) 

79 assert "small content" in summary 

80 assert "medium " * 10 in summary # Check start of medium file 

81 assert "x" * 100 in summary # Check start of large file 

82 

83 

84@pytest.mark.skip(reason="symlinks are weird, ill get back to this later") 

85def test_symlink_handling(): 

86 """Test handling of symlinks in directory structure""" 

87 test_dir = TEMP_PATH / "symlink_test" 

88 test_dir.mkdir(parents=True, exist_ok=True) 

89 

90 # Create directories and files 

91 (test_dir / "src").mkdir() 

92 (test_dir / "docs").mkdir() 

93 (test_dir / "src/module.py").write_text("print('original')\n") 

94 (test_dir / "docs/readme.md").write_text("# Documentation\n") 

95 

96 try: 

97 # Create symlinks 

98 (test_dir / "src/linked.py").symlink_to(test_dir / "src/module.py") 

99 (test_dir / "docs_link").symlink_to(test_dir / "docs") 

100 

101 config = LMCatConfig() 

102 tree_output, file_list = walk_and_collect(test_dir, config) 

103 tree_str = "\n".join(tree_output) 

104 

105 # Check if symlinks are handled 

106 assert "linked.py" in tree_str 

107 assert "docs_link" in tree_str 

108 

109 # Verify symlink contents are included 

110 summary = assemble_summary(test_dir, config) 

111 assert "print('original')" in summary 

112 assert "# Documentation" in summary 

113 

114 except OSError: 

115 pytest.skip("Symlink creation not supported") 

116 

117 

118def test_error_handling(): 

119 """Test error handling for various filesystem conditions""" 

120 test_dir = TEMP_PATH / "error_test" 

121 test_dir.mkdir(parents=True, exist_ok=True) 

122 

123 # Create test files 

124 (test_dir / "readable.txt").write_text("can read this\n") 

125 (test_dir / "binary.bin").write_bytes(b"\x00\x01\x02\x03") 

126 (test_dir / "unreadable.txt").write_text("secret") 

127 

128 try: 

129 os.chmod(test_dir / "unreadable.txt", 0o000) 

130 with open(test_dir / "unreadable.txt", "r") as f: 

131 f.read() 

132 except PermissionError: 

133 pytest.skip("Cannot create unreadable file") 

134 

135 config = LMCatConfig() 

136 tree_output, file_list = walk_and_collect(test_dir, config) 

137 tree_str = "\n".join(tree_output) 

138 

139 # Check that readable files are included 

140 assert "readable.txt" in tree_str 

141 assert "binary.bin" in tree_str 

142 

143 # Check content 

144 summary = assemble_summary(test_dir, config) 

145 assert "can read this" in summary 

146 

147 # Restore permissions for cleanup 

148 try: 

149 os.chmod(test_dir / "unreadable.txt", 0o666) 

150 except OSError: 

151 pass