gitprint 0.4.0

Convert git repositories into beautifully formatted, printer-friendly PDFs
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
431
432
433
434
use std::path::{Path, PathBuf};

use tempfile::TempDir;

use gitprint::types::{Config, PaperSize};

async fn git_in(dir: &str, args: &[&str]) {
    let output = tokio::process::Command::new("git")
        .args(["-C", dir])
        .args(args)
        .output()
        .await
        .unwrap();
    assert!(
        output.status.success(),
        "git {:?} failed: {}",
        args,
        String::from_utf8_lossy(&output.stderr)
    );
}

async fn create_test_repo() -> TempDir {
    let dir = TempDir::new().unwrap();
    let p = dir.path().to_str().unwrap().to_string();

    git_in(&p, &["init", "-b", "main"]).await;

    // git config writes must be sequential (both modify .git/config, git's file lock
    // is advisory and concurrent writes fail). File writes are independent so they
    // run in parallel with the sequential git config block.
    tokio::join!(
        async {
            git_in(&p, &["config", "user.email", "test@test.com"]).await;
            git_in(&p, &["config", "user.name", "Test"]).await;
        },
        async {
            tokio::try_join!(
                tokio::fs::write(
                    dir.path().join("main.rs"),
                    "fn main() {\n    println!(\"hello\");\n}\n",
                ),
                tokio::fs::write(
                    dir.path().join("lib.rs"),
                    "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n",
                ),
                tokio::fs::write(dir.path().join("README.md"), "# Test Repo\n"),
                tokio::fs::create_dir_all(dir.path().join("src")),
            )
            .unwrap();
            // src/ now exists; write util.rs after create_dir_all completes.
            tokio::fs::write(
                dir.path().join("src/util.rs"),
                "// utility\npub fn noop() {}\n",
            )
            .await
            .unwrap();
        },
    );

    git_in(&p, &["add", "."]).await;
    git_in(&p, &["commit", "-m", "initial commit"]).await;

    dir
}

fn test_config(repo_path: PathBuf, output_path: PathBuf) -> Config {
    Config {
        repo_path,
        output_path,
        include_patterns: vec![],
        exclude_patterns: vec![],
        theme: "InspiredGitHub".to_string(),
        font_size: 8.0,
        no_line_numbers: false,
        toc: true,
        file_tree: true,
        branch: None,
        commit: None,
        paper_size: PaperSize::A4,
        landscape: false,
        remote_url: None,
    }
}

// ── git module tests ──────────────────────────────────────────────

#[tokio::test]
async fn git_verify_repo_valid() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let info = gitprint::git::verify_repo(repo.path()).await?;
    assert!(info.is_git);
    assert!(info.scope.is_none());
    assert!(info.single_file.is_none());
    Ok(())
}

#[tokio::test]
async fn git_verify_repo_subdir() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let info = gitprint::git::verify_repo(&repo.path().join("src")).await?;
    assert!(info.is_git);
    assert_eq!(info.scope, Some(PathBuf::from("src")));
    assert!(info.single_file.is_none());
    Ok(())
}

#[tokio::test]
async fn git_verify_repo_single_file_in_git() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let info = gitprint::git::verify_repo(&repo.path().join("main.rs")).await?;
    assert!(info.is_git);
    assert_eq!(info.single_file, Some(PathBuf::from("main.rs")));
    assert!(info.scope.is_none());
    Ok(())
}

#[tokio::test]
async fn git_verify_repo_plain_directory() -> Result<(), Box<dyn std::error::Error>> {
    let dir = TempDir::new()?;
    let info = gitprint::git::verify_repo(dir.path()).await?;
    assert!(!info.is_git);
    assert!(info.single_file.is_none());
    Ok(())
}

#[tokio::test]
async fn git_verify_repo_plain_file() -> Result<(), Box<dyn std::error::Error>> {
    let dir = TempDir::new()?;
    tokio::fs::write(dir.path().join("hello.rs"), "fn main() {}")
        .await
        .unwrap();
    let info = gitprint::git::verify_repo(&dir.path().join("hello.rs")).await?;
    assert!(!info.is_git);
    assert_eq!(info.single_file, Some(PathBuf::from("hello.rs")));
    Ok(())
}

#[tokio::test]
async fn git_verify_repo_nonexistent_path() {
    assert!(
        gitprint::git::verify_repo(Path::new("/nonexistent/path"))
            .await
            .is_err()
    );
}

#[tokio::test]
async fn git_get_metadata() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let config = test_config(repo.path().to_path_buf(), PathBuf::from("/tmp/test.pdf"));
    let metadata = gitprint::git::get_metadata(repo.path(), &config, true, None).await?;

    assert!(!metadata.name.is_empty());
    assert_eq!(metadata.branch, "main");
    assert_eq!(metadata.commit_hash.len(), 40);
    assert!(metadata.commit_hash.chars().all(|c| c.is_ascii_hexdigit()));
    assert_eq!(metadata.commit_hash_short.len(), 7);
    assert_eq!(metadata.commit_message, "initial commit");
    assert!(!metadata.commit_date.is_empty());
    Ok(())
}

#[tokio::test]
async fn git_get_metadata_plain_directory() -> Result<(), Box<dyn std::error::Error>> {
    let dir = TempDir::new()?;
    let config = test_config(dir.path().to_path_buf(), PathBuf::from("/tmp/test.pdf"));
    let metadata = gitprint::git::get_metadata(dir.path(), &config, false, None).await?;

    assert!(!metadata.name.is_empty());
    assert!(metadata.branch.is_empty());
    assert!(metadata.commit_hash.is_empty());
    assert!(metadata.commit_date.is_empty());
    Ok(())
}

#[tokio::test]
async fn git_get_metadata_with_branch() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let mut config = test_config(repo.path().to_path_buf(), PathBuf::from("/tmp/test.pdf"));
    config.branch = Some("main".to_string());
    let metadata = gitprint::git::get_metadata(repo.path(), &config, true, None).await?;
    assert_eq!(metadata.branch, "main");
    Ok(())
}

#[tokio::test]
async fn git_list_tracked_files() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let config = test_config(repo.path().to_path_buf(), PathBuf::from("/tmp/test.pdf"));
    let files = gitprint::git::list_tracked_files(repo.path(), &config, true, None).await?;

    assert!(files.contains(&PathBuf::from("main.rs")));
    assert!(files.contains(&PathBuf::from("lib.rs")));
    assert!(files.contains(&PathBuf::from("src/util.rs")));
    assert!(files.contains(&PathBuf::from("README.md")));
    assert_eq!(files.len(), 4);
    Ok(())
}

#[tokio::test]
async fn git_list_files_plain_directory() -> Result<(), Box<dyn std::error::Error>> {
    let dir = TempDir::new()?;
    tokio::try_join!(
        tokio::fs::write(dir.path().join("hello.rs"), "fn main() {}"),
        tokio::fs::create_dir(dir.path().join("sub")),
    )
    .unwrap();
    tokio::fs::write(dir.path().join("sub/world.rs"), "pub fn world() {}")
        .await
        .unwrap();
    let config = test_config(dir.path().to_path_buf(), PathBuf::from("/tmp/test.pdf"));
    let files = gitprint::git::list_tracked_files(dir.path(), &config, false, None).await?;

    assert!(files.contains(&PathBuf::from("hello.rs")));
    assert!(files.contains(&PathBuf::from("sub/world.rs")));
    assert_eq!(files.len(), 2);
    Ok(())
}

#[tokio::test]
async fn git_read_file_content() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let config = test_config(repo.path().to_path_buf(), PathBuf::from("/tmp/test.pdf"));
    let content =
        gitprint::git::read_file_content(repo.path(), Path::new("main.rs"), &config).await?;

    assert!(content.contains("fn main()"));
    assert!(content.contains("println!"));
    Ok(())
}

#[tokio::test]
async fn git_read_file_content_nonexistent() {
    let repo = create_test_repo().await;
    let config = test_config(repo.path().to_path_buf(), PathBuf::from("/tmp/test.pdf"));
    let result =
        gitprint::git::read_file_content(repo.path(), Path::new("nonexistent.rs"), &config).await;
    assert!(result.is_err());
}

// ── full pipeline tests ───────────────────────────────────────────

#[tokio::test]
async fn full_pipeline() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let config = test_config(repo.path().to_path_buf(), output_path.clone());

    gitprint::run(&config).await?;

    assert!(output_path.exists());
    assert!(std::fs::metadata(&output_path)?.len() > 0);
    Ok(())
}

#[tokio::test]
async fn full_pipeline_with_include_filter() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path.clone());
    config.include_patterns = vec!["*.rs".to_string()];

    gitprint::run(&config).await?;

    assert!(output_path.exists());
    assert!(std::fs::metadata(&output_path)?.len() > 0);
    Ok(())
}

#[tokio::test]
async fn full_pipeline_with_exclude_filter() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path.clone());
    config.exclude_patterns = vec!["*.md".to_string()];

    gitprint::run(&config).await?;
    assert!(output_path.exists());
    Ok(())
}

#[tokio::test]
async fn full_pipeline_no_toc_no_tree() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path.clone());
    config.toc = false;
    config.file_tree = false;

    gitprint::run(&config).await?;
    assert!(output_path.exists());
    Ok(())
}

#[tokio::test]
async fn full_pipeline_no_line_numbers() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path.clone());
    config.no_line_numbers = true;

    gitprint::run(&config).await?;
    assert!(output_path.exists());
    Ok(())
}

#[tokio::test]
async fn full_pipeline_landscape() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path.clone());
    config.landscape = true;

    gitprint::run(&config).await?;
    assert!(output_path.exists());
    Ok(())
}

#[tokio::test]
async fn full_pipeline_letter_paper() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path.clone());
    config.paper_size = PaperSize::Letter;

    gitprint::run(&config).await?;
    assert!(output_path.exists());
    Ok(())
}

#[tokio::test]
async fn full_pipeline_subdir() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let config = test_config(repo.path().join("src"), output_path.clone());

    gitprint::run(&config).await?;

    assert!(output_path.exists());
    assert!(std::fs::metadata(&output_path)?.len() > 0);
    Ok(())
}

#[tokio::test]
async fn full_pipeline_single_file() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let config = test_config(repo.path().join("main.rs"), output_path.clone());

    gitprint::run(&config).await?;

    assert!(output_path.exists());
    assert!(std::fs::metadata(&output_path)?.len() > 0);
    Ok(())
}

#[tokio::test]
async fn full_pipeline_plain_directory() -> Result<(), Box<dyn std::error::Error>> {
    let dir = TempDir::new()?;
    tokio::try_join!(
        tokio::fs::write(dir.path().join("main.rs"), "fn main() {}\n"),
        tokio::fs::write(
            dir.path().join("lib.rs"),
            "pub fn add(a: i32, b: i32) -> i32 { a + b }\n",
        ),
    )
    .unwrap();
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let config = test_config(dir.path().to_path_buf(), output_path.clone());

    gitprint::run(&config).await?;

    assert!(output_path.exists());
    assert!(std::fs::metadata(&output_path)?.len() > 0);
    Ok(())
}

#[tokio::test]
async fn full_pipeline_nonexistent_repo() {
    let out_dir = TempDir::new().unwrap();
    let output_path = out_dir.path().join("output.pdf");
    let config = test_config(PathBuf::from("/nonexistent/repo"), output_path);

    assert!(gitprint::run(&config).await.is_err());
}

#[tokio::test]
async fn full_pipeline_invalid_theme() {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new().unwrap();
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path);
    config.theme = "NonExistentTheme".to_string();

    let err = gitprint::run(&config).await.unwrap_err();
    assert!(err.to_string().contains("NonExistentTheme"));
    assert!(err.to_string().contains("--list-themes"));
}

#[tokio::test]
async fn full_pipeline_include_excludes_everything() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path.clone());
    config.include_patterns = vec!["*.nonexistent".to_string()];

    gitprint::run(&config).await?;
    assert!(output_path.exists());
    Ok(())
}

#[tokio::test]
async fn full_pipeline_custom_font_size() -> Result<(), Box<dyn std::error::Error>> {
    let repo = create_test_repo().await;
    let out_dir = TempDir::new()?;
    let output_path = out_dir.path().join("output.pdf");
    let mut config = test_config(repo.path().to_path_buf(), output_path.clone());
    config.font_size = 12.0;

    gitprint::run(&config).await?;
    assert!(output_path.exists());
    Ok(())
}