yek 0.25.5

A tool to serialize a repository into chunks of text files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::path::PathBuf;
use tempfile::TempDir;
use yek::error::{safe_ops, ErrorContext, ErrorReporter, YekError};

#[cfg(test)]
mod error_tests {
    use super::*;

    #[test]
    fn test_yek_error_display_file_system() {
        let error = YekError::FileSystem {
            operation: "read".to_string(),
            path: PathBuf::from("/test/path"),
            source: std::io::Error::new(std::io::ErrorKind::NotFound, "File not found"),
        };
        let display = format!("{}", error);
        assert!(display.contains("File system error during 'read' on '/test/path'"));
        assert!(display.contains("File not found"));
    }

    #[test]
    fn test_yek_error_display_git() {
        let error = YekError::Git {
            operation: "commit".to_string(),
            repository: PathBuf::from("/repo"),
            source: git2::Error::from_str("Invalid commit"),
        };
        let display = format!("{}", error);
        assert!(display.contains("Git error during 'commit' in repository '/repo'"));
    }

    #[test]
    fn test_yek_error_display_configuration() {
        let error = YekError::Configuration {
            field: "max_size".to_string(),
            value: "invalid".to_string(),
            reason: "Invalid format".to_string(),
        };
        let display = format!("{}", error);
        assert!(display.contains(
            "Configuration error for field 'max_size' with value 'invalid': Invalid format"
        ));
    }

    #[test]
    fn test_yek_error_display_processing() {
        let error = YekError::Processing {
            stage: "tokenization".to_string(),
            file: Some(PathBuf::from("test.txt")),
            reason: "Invalid encoding".to_string(),
        };
        let display = format!("{}", error);
        assert!(display.contains(
            "Processing error in stage 'tokenization' for file 'test.txt': Invalid encoding"
        ));
    }

    #[test]
    fn test_yek_error_display_processing_no_file() {
        let error = YekError::Processing {
            stage: "parsing".to_string(),
            file: None,
            reason: "Syntax error".to_string(),
        };
        let display = format!("{}", error);
        assert!(display.contains("Processing error in stage 'parsing': Syntax error"));
    }

    #[test]
    fn test_yek_error_display_memory() {
        let error = YekError::Memory {
            operation: "file reading".to_string(),
            requested: 1000,
            available: Some(500),
        };
        let display = format!("{}", error);
        assert!(display.contains(
            "Memory error during 'file reading' - requested: 1000 bytes, available: 500 bytes"
        ));
    }

    #[test]
    fn test_yek_error_display_memory_no_available() {
        let error = YekError::Memory {
            operation: "allocation".to_string(),
            requested: 2000,
            available: None,
        };
        let display = format!("{}", error);
        assert!(display.contains("Memory error during 'allocation' - requested: 2000 bytes"));
    }

    #[test]
    fn test_yek_error_display_security() {
        let error = YekError::Security {
            violation: "Path traversal".to_string(),
            path: PathBuf::from("../outside"),
            attempted_by: "user_input".to_string(),
        };
        let display = format!("{}", error);
        assert!(display.contains(
            "Security violation 'Path traversal' for path '../outside' attempted by: user_input"
        ));
    }

    #[test]
    fn test_yek_error_display_validation() {
        let error = YekError::Validation {
            field: "pattern".to_string(),
            value: "*".to_string(),
            constraint: "must be valid regex".to_string(),
        };
        let display = format!("{}", error);
        assert!(display.contains("Validation error for field 'pattern' with value '*': violates constraint 'must be valid regex'"));
    }

    #[test]
    fn test_yek_error_display_tokenization() {
        let error = YekError::Tokenization {
            content_type: "text".to_string(),
            size: 1024,
            reason: "Encoding error".to_string(),
        };
        let display = format!("{}", error);
        assert!(
            display.contains("Tokenization error for text content (size: 1024): Encoding error")
        );
    }

    #[test]
    fn test_yek_error_display_user_input() {
        let error = YekError::UserInput {
            input_type: "path".to_string(),
            value: "/invalid".to_string(),
            suggestion: "Use absolute paths".to_string(),
        };
        let display = format!("{}", error);
        assert!(display.contains("Invalid path input '/invalid': Use absolute paths"));
    }

    #[test]
    fn test_error_context_new() {
        let context = ErrorContext::new("test_operation");
        assert_eq!(context.operation, "test_operation");
        assert!(context.file.is_none());
        assert!(context.line.is_none());
        assert!(context.column.is_none());
        assert!(context.additional_info.is_empty());
    }

    #[test]
    fn test_error_context_with_file() {
        let context = ErrorContext::new("test").with_file("/test/file.txt");
        assert_eq!(context.file, Some(PathBuf::from("/test/file.txt")));
    }

    #[test]
    fn test_error_context_with_location() {
        let context = ErrorContext::new("test").with_location(10, 5);
        assert_eq!(context.line, Some(10));
        assert_eq!(context.column, Some(5));
    }

    #[test]
    fn test_error_context_with_info() {
        let context = ErrorContext::new("test")
            .with_info("key1", "value1")
            .with_info("key2", "value2");
        assert_eq!(context.additional_info.len(), 2);
        assert_eq!(
            context.additional_info[0],
            ("key1".to_string(), "value1".to_string())
        );
        assert_eq!(
            context.additional_info[1],
            ("key2".to_string(), "value2".to_string())
        );
    }

    #[test]
    fn test_error_context_default() {
        let context = ErrorContext::default();
        assert_eq!(context.operation, "unknown_operation");
    }

    #[test]
    fn test_error_reporter_user_friendly_message_file_system() {
        let error = YekError::FileSystem {
            operation: "read".to_string(),
            path: PathBuf::from("test.txt"),
            source: std::io::Error::new(std::io::ErrorKind::PermissionDenied, "Permission denied"),
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(message, "Failed to read file 'test.txt'");
    }

    #[test]
    fn test_error_reporter_user_friendly_message_git() {
        let error = YekError::Git {
            operation: "push".to_string(),
            repository: PathBuf::from("/repo"),
            source: git2::Error::from_str("Network error"),
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(message, "Git operation 'push' failed in repository '/repo'");
    }

    #[test]
    fn test_error_reporter_user_friendly_message_configuration() {
        let error = YekError::Configuration {
            field: "timeout".to_string(),
            value: "abc".to_string(),
            reason: "Must be a number".to_string(),
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(
            message,
            "Configuration issue with 'timeout': Must be a number"
        );
    }

    #[test]
    fn test_error_reporter_user_friendly_message_processing() {
        let error = YekError::Processing {
            stage: "compilation".to_string(),
            file: Some(PathBuf::from("main.rs")),
            reason: "Syntax error".to_string(),
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(
            message,
            "Processing failed in 'compilation' stage for 'main.rs': Syntax error"
        );
    }

    #[test]
    fn test_error_reporter_user_friendly_message_memory() {
        let error = YekError::Memory {
            operation: "buffer allocation".to_string(),
            requested: 1000000,
            available: None,
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(
            message,
            "Insufficient memory for 'buffer allocation' (requested: 1000000 bytes)"
        );
    }

    #[test]
    fn test_error_reporter_user_friendly_message_security() {
        let error = YekError::Security {
            violation: "Directory traversal".to_string(),
            path: PathBuf::from("../../etc"),
            attempted_by: "input".to_string(),
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(
            message,
            "Security violation 'Directory traversal' for path '../../etc'"
        );
    }

    #[test]
    fn test_error_reporter_user_friendly_message_validation() {
        let error = YekError::Validation {
            field: "email".to_string(),
            value: "invalid".to_string(),
            constraint: "must be valid email format".to_string(),
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(
            message,
            "Validation failed for 'email': violates 'must be valid email format'"
        );
    }

    #[test]
    fn test_error_reporter_user_friendly_message_tokenization() {
        let error = YekError::Tokenization {
            content_type: "binary".to_string(),
            size: 2048,
            reason: "Unsupported format".to_string(),
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(
            message,
            "Failed to process binary content (2048): Unsupported format"
        );
    }

    #[test]
    fn test_error_reporter_user_friendly_message_user_input() {
        let error = YekError::UserInput {
            input_type: "command".to_string(),
            value: "invalid_cmd".to_string(),
            suggestion: "Use 'help' to see available commands".to_string(),
        };
        let message = ErrorReporter::user_friendly_message(&error);
        assert_eq!(
            message,
            "Invalid command: Use 'help' to see available commands"
        );
    }

    #[test]
    fn test_safe_read_file_nonexistent() {
        let temp_dir = TempDir::new().unwrap();
        let nonexistent_path = temp_dir.path().join("nonexistent.txt");
        let context = ErrorContext::new("test_read");

        let result = safe_ops::safe_read_file(&nonexistent_path, &context, None);
        assert!(result.is_err());
        let err = result.unwrap_err();
        match &err.error {
            YekError::FileSystem { operation, .. } => assert_eq!(operation, "read"),
            _ => panic!("Expected FileSystem error"),
        }
    }

    #[test]
    fn test_safe_read_file_directory() {
        let temp_dir = TempDir::new().unwrap();
        let context = ErrorContext::new("test_read");

        let result = safe_ops::safe_read_file(temp_dir.path(), &context, None);
        assert!(result.is_err());
        let err = result.unwrap_err();
        match &err.error {
            YekError::FileSystem { operation, .. } => assert_eq!(operation, "read"),
            _ => panic!("Expected FileSystem error"),
        }
    }

    #[test]
    fn test_safe_read_file_success() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.txt");
        std::fs::write(&file_path, b"Hello, world!").unwrap();
        let context = ErrorContext::new("test_read");

        let result = safe_ops::safe_read_file(&file_path, &context, None);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), b"Hello, world!");
    }

    #[test]
    fn test_safe_read_file_size_limit() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.txt");
        std::fs::write(&file_path, b"Hello, world!").unwrap(); // 13 bytes
        let context = ErrorContext::new("test_read");

        let result = safe_ops::safe_read_file(&file_path, &context, Some(10));
        assert!(result.is_err());
        let err = result.unwrap_err();
        match &err.error {
            YekError::Memory {
                operation,
                requested,
                available,
            } => {
                assert_eq!(operation, "file reading");
                assert_eq!(*requested, 13);
                assert_eq!(*available, Some(10));
            }
            _ => panic!("Expected Memory error"),
        }
    }

    #[test]
    fn test_safe_validate_utf8_valid() {
        let bytes = b"Hello, world!";
        let context = ErrorContext::new("test_utf8");

        let result = safe_ops::safe_validate_utf8(bytes, &context);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Hello, world!");
    }

    #[test]
    fn test_safe_validate_utf8_invalid_with_replacement() {
        // Create invalid UTF-8 bytes
        let bytes = vec![0xFF, 0xFE, 0xFD]; // Invalid UTF-8 sequence
        let context = ErrorContext::new("test_utf8");

        let result = safe_ops::safe_validate_utf8(&bytes, &context);
        assert!(result.is_ok());
        let content = result.unwrap();
        // Should contain replacement character
        assert!(content.contains('\u{FFFD}'));
    }

    #[test]
    fn test_safe_validate_path_valid() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.txt");
        std::fs::write(&file_path, b"test").unwrap();
        let context = ErrorContext::new("test_path");

        // Use canonicalized paths for comparison
        let canonical_base = std::fs::canonicalize(temp_dir.path()).unwrap();
        let canonical_file = std::fs::canonicalize(&file_path).unwrap();

        let result = safe_ops::safe_validate_path(&canonical_file, &canonical_base, &context);
        assert!(result.is_ok());
    }

    #[test]
    fn test_safe_validate_path_traversal() {
        let temp_dir = TempDir::new().unwrap();
        let parent_dir = temp_dir.path().parent().unwrap();
        let traversal_path = parent_dir.join("outside.txt");
        let context = ErrorContext::new("test_path");

        // Create the traversal path outside temp_dir
        std::fs::write(&traversal_path, b"outside").unwrap();

        let canonical_base = std::fs::canonicalize(temp_dir.path()).unwrap();
        let canonical_traversal = std::fs::canonicalize(&traversal_path).unwrap();

        let result = safe_ops::safe_validate_path(&canonical_traversal, &canonical_base, &context);
        assert!(result.is_err());
        let err = result.unwrap_err();
        match &err.error {
            YekError::Security { violation, .. } => assert_eq!(violation, "Path traversal attempt"),
            _ => panic!("Expected Security error"),
        }
    }
}