hwp2md 0.5.0

HWP/HWPX ↔ Markdown bidirectional converter
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/// CLI integration tests — general behavior, `info`, `check`, and `convert` subcommands.
///
/// Covers:
///   - No-argument invocation, --version, --help
///   - `info` subcommand (help, nonexistent file)
///   - `check` subcommand (help, valid files, nonexistent, corrupt, unsupported)
///   - `convert` subcommand (auto-detection, overwrite protection, flags)
use tempfile::tempdir;

#[path = "common/mod.rs"]
mod common;
#[allow(dead_code)]
#[path = "fixtures/mod.rs"]
mod fixtures;

use common::cargo_bin;

// ---------------------------------------------------------------------------
// 1. No arguments → non-zero exit, stderr contains usage hint
// ---------------------------------------------------------------------------

#[test]
fn cli_no_args_shows_help() {
    let output = cargo_bin().output().expect("failed to execute hwp2md");
    assert!(
        !output.status.success(),
        "expected non-zero exit with no args"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Usage") || stderr.contains("usage"),
        "expected 'Usage' in stderr, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// 2. --version → stdout contains version string
// ---------------------------------------------------------------------------

#[test]
fn cli_version_flag() {
    let output = cargo_bin()
        .arg("--version")
        .output()
        .expect("failed to execute hwp2md --version");
    assert!(
        output.status.success(),
        "expected zero exit for --version; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    // The version is declared in Cargo.toml; clap renders it as "hwp2md X.Y.Z".
    assert!(
        stdout.contains(env!("CARGO_PKG_VERSION")),
        "version number not found in stdout: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// 3. --help → stdout contains binary name and all subcommand names
// ---------------------------------------------------------------------------

#[test]
fn cli_help_flag() {
    let output = cargo_bin()
        .arg("--help")
        .output()
        .expect("failed to execute hwp2md --help");
    assert!(
        output.status.success(),
        "expected zero exit for --help; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("hwp2md"), "binary name missing: {stdout}");
    assert!(
        stdout.contains("to-md"),
        "to-md subcommand missing: {stdout}"
    );
    assert!(
        stdout.contains("to-hwpx"),
        "to-hwpx subcommand missing: {stdout}"
    );
    assert!(stdout.contains("info"), "info subcommand missing: {stdout}");
}

// ---------------------------------------------------------------------------
// 6. info --help → shows input option
// ---------------------------------------------------------------------------

#[test]
fn cli_info_help() {
    let output = cargo_bin()
        .args(["info", "--help"])
        .output()
        .expect("failed to execute hwp2md info --help");
    assert!(
        output.status.success(),
        "expected zero exit; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("input") || stdout.contains("INPUT"),
        "input option missing: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// 9. info nonexistent.hwpx → non-zero exit, error message
// ---------------------------------------------------------------------------

#[test]
fn cli_info_nonexistent_file() {
    let output = cargo_bin()
        .args(["info", "/nonexistent/path/file.hwpx"])
        .output()
        .expect("failed to execute hwp2md info");
    assert!(
        !output.status.success(),
        "expected non-zero exit for missing file"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.is_empty(),
        "expected some error message on stderr, got empty"
    );
}

// ---------------------------------------------------------------------------
// 12. check --help → shows input option
// ---------------------------------------------------------------------------

#[test]
fn cli_check_help() {
    let output = cargo_bin()
        .args(["check", "--help"])
        .output()
        .expect("failed to execute hwp2md check --help");
    assert!(
        output.status.success(),
        "expected zero exit; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("input") || stdout.contains("INPUT"),
        "input option missing: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// 13. check on a valid .md file → exit 0, stdout contains "OK"
// ---------------------------------------------------------------------------

#[test]
fn cli_check_valid_md_exits_zero() {
    let dir = tempdir().expect("tempdir");
    let md_file = dir.path().join("valid.md");
    std::fs::write(&md_file, b"# Title\n\nSome content.\n").expect("write md");

    let output = cargo_bin()
        .args(["check", md_file.to_str().unwrap()])
        .output()
        .expect("failed to execute hwp2md check");
    assert!(
        output.status.success(),
        "expected exit 0 for valid .md; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("OK"),
        "expected 'OK' in stdout, got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// 14. check on a valid .hwpx (produced by to-hwpx) → exit 0, stdout "OK"
// ---------------------------------------------------------------------------

#[test]
fn cli_check_valid_hwpx_exits_zero() {
    let dir = tempdir().expect("tempdir");

    // Produce a valid HWPX via to-hwpx.
    let md_src = dir.path().join("src.md");
    std::fs::write(&md_src, b"# Check Test\n\nContent.\n").expect("write md");
    let hwpx_path = dir.path().join("doc.hwpx");
    let conv = cargo_bin()
        .args([
            "to-hwpx",
            md_src.to_str().unwrap(),
            "--output",
            hwpx_path.to_str().unwrap(),
        ])
        .output()
        .expect("failed to run to-hwpx");
    assert!(
        conv.status.success(),
        "to-hwpx failed; stderr: {}",
        String::from_utf8_lossy(&conv.stderr)
    );

    let output = cargo_bin()
        .args(["check", hwpx_path.to_str().unwrap()])
        .output()
        .expect("failed to execute hwp2md check");
    assert!(
        output.status.success(),
        "expected exit 0 for valid .hwpx; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("OK"),
        "expected 'OK' in stdout, got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// 15. check on a nonexistent file → exit 1, error on stderr
// ---------------------------------------------------------------------------

#[test]
fn cli_check_nonexistent_file_exits_nonzero() {
    let output = cargo_bin()
        .args(["check", "/nonexistent/path/doc.hwpx"])
        .output()
        .expect("failed to execute hwp2md check");
    assert!(
        !output.status.success(),
        "expected non-zero exit for missing file"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.is_empty(),
        "expected error message on stderr, got empty"
    );
}

// ---------------------------------------------------------------------------
// 16. check on an unsupported extension → exit 1, "Unsupported" on stderr
// ---------------------------------------------------------------------------

#[test]
fn cli_check_unsupported_extension_exits_nonzero() {
    let dir = tempdir().expect("tempdir");
    let bad_file = dir.path().join("document.pdf");
    std::fs::write(&bad_file, b"fake-pdf").expect("write file");

    let output = cargo_bin()
        .args(["check", bad_file.to_str().unwrap()])
        .output()
        .expect("failed to execute hwp2md check");
    assert!(
        !output.status.success(),
        "expected non-zero exit for unsupported extension"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Unsupported") || stderr.contains("unsupported"),
        "expected unsupported-format error, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// 17. check on a corrupt .hwpx → exit 1, error on stderr
// ---------------------------------------------------------------------------

#[test]
fn cli_check_corrupt_hwpx_exits_nonzero() {
    let dir = tempdir().expect("tempdir");
    let bad_hwpx = dir.path().join("corrupt.hwpx");
    std::fs::write(&bad_hwpx, b"not a zip file at all").expect("write file");

    let output = cargo_bin()
        .args(["check", bad_hwpx.to_str().unwrap()])
        .output()
        .expect("failed to execute hwp2md check");
    assert!(
        !output.status.success(),
        "expected non-zero exit for corrupt .hwpx"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.is_empty(),
        "expected error message on stderr, got empty"
    );
}

// ---------------------------------------------------------------------------
// Sprint 3 — `convert` subcommand: extension-based auto-detection
// ---------------------------------------------------------------------------

#[test]
fn cli_convert_help_lists_supported_pairs() {
    let output = cargo_bin()
        .args(["convert", "--help"])
        .output()
        .expect("failed to execute hwp2md convert --help");
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains(".hwp") && stdout.contains(".md"),
        "convert help must mention supported extensions: {stdout}"
    );
}

#[test]
fn cli_convert_md_to_hwpx_creates_output_and_exits_zero() {
    let dir = tempdir().expect("tempdir");
    let input = dir.path().join("note.md");
    std::fs::write(&input, "# Heading\n\nContent.\n").expect("write");
    let output = dir.path().join("note.hwpx");

    let result = cargo_bin()
        .args(["convert", input.to_str().unwrap(), output.to_str().unwrap()])
        .output()
        .expect("execute convert");
    assert!(
        result.status.success(),
        "convert failed; stderr: {}",
        String::from_utf8_lossy(&result.stderr)
    );
    assert!(output.exists(), "output hwpx not created");
}

#[test]
fn cli_convert_hwpx_to_md_creates_output_and_exits_zero() {
    let dir = tempdir().expect("tempdir");
    // Build an HWPX from a markdown source first.
    let md_in = dir.path().join("source.md");
    std::fs::write(&md_in, "# Hello\n").expect("write");
    let hwpx = dir.path().join("source.hwpx");
    let _ = cargo_bin()
        .args([
            "to-hwpx",
            md_in.to_str().unwrap(),
            "-o",
            hwpx.to_str().unwrap(),
        ])
        .output()
        .expect("seed hwpx");
    assert!(hwpx.exists(), "seed hwpx not produced");

    let md_out = dir.path().join("converted.md");
    let result = cargo_bin()
        .args(["convert", hwpx.to_str().unwrap(), md_out.to_str().unwrap()])
        .output()
        .expect("execute convert");
    assert!(
        result.status.success(),
        "convert failed; stderr: {}",
        String::from_utf8_lossy(&result.stderr)
    );
    let body = std::fs::read_to_string(&md_out).expect("read md_out");
    assert!(body.contains("Hello"), "heading lost: {body:?}");
}

#[test]
fn cli_convert_md_to_md_rejected_with_clear_error() {
    let dir = tempdir().expect("tempdir");
    let input = dir.path().join("a.md");
    let output = dir.path().join("b.md");
    std::fs::write(&input, "# x\n").expect("write");

    let result = cargo_bin()
        .args(["convert", input.to_str().unwrap(), output.to_str().unwrap()])
        .output()
        .expect("execute convert");
    assert!(!result.status.success(), "same-format conversion must fail");
    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        stderr.contains("cannot infer conversion direction"),
        "stderr should explain the rejection: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// Sprint 4 — `convert --force` overwrite protection (M-3)
// ---------------------------------------------------------------------------

#[test]
fn convert_refuses_overwrite_without_force() {
    let dir = tempdir().expect("tempdir");
    let input = dir.path().join("doc.md");
    let output = dir.path().join("doc.hwpx");
    std::fs::write(&input, "# Test").expect("write input");
    std::fs::write(&output, "existing").expect("write pre-existing output");

    let result = cargo_bin()
        .args(["convert", input.to_str().unwrap(), output.to_str().unwrap()])
        .output()
        .expect("execute convert");
    assert!(
        !result.status.success(),
        "must fail without --force when output already exists"
    );
    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        stderr.contains("already exists"),
        "stderr must mention 'already exists': {stderr}"
    );
}

#[test]
fn convert_overwrites_with_force_flag() {
    let dir = tempdir().expect("tempdir");
    let input = dir.path().join("doc.md");
    let output = dir.path().join("doc.hwpx");
    std::fs::write(&input, "# Test").expect("write input");
    std::fs::write(&output, "existing").expect("write pre-existing output");

    let result = cargo_bin()
        .args([
            "convert",
            input.to_str().unwrap(),
            output.to_str().unwrap(),
            "--force",
        ])
        .output()
        .expect("execute convert");
    assert!(
        result.status.success(),
        "must succeed with --force: {}",
        String::from_utf8_lossy(&result.stderr)
    );
    let content = std::fs::read(&output).expect("read output");
    assert_ne!(
        content, b"existing",
        "output must be overwritten by --force conversion"
    );
}

// ---------------------------------------------------------------------------
// Sprint 13 — `convert` subcommand gains --frontmatter, --style, --assets-dir
// ---------------------------------------------------------------------------

#[test]
fn convert_frontmatter_flag_adds_yaml_header() {
    let dir = tempdir().expect("tempdir");
    let hwpx = dir.path().join("doc.hwpx");
    common::make_hwpx(&hwpx);
    let md_out = dir.path().join("doc.md");

    let result = cargo_bin()
        .args([
            "convert",
            hwpx.to_str().unwrap(),
            md_out.to_str().unwrap(),
            "--frontmatter",
        ])
        .output()
        .expect("execute convert --frontmatter");
    assert!(
        result.status.success(),
        "convert --frontmatter failed: {}",
        String::from_utf8_lossy(&result.stderr)
    );
    let body = std::fs::read_to_string(&md_out).expect("read md");
    assert!(
        body.starts_with("---"),
        "expected YAML frontmatter: {body:?}"
    );
}

#[test]
fn convert_style_flag_accepted_for_md_to_hwpx() {
    let dir = tempdir().expect("tempdir");
    let input = dir.path().join("styled.md");
    std::fs::write(&input, "# Styled\n\nBody.\n").expect("write md");
    let style_yml = dir.path().join("style.yml");
    std::fs::write(&style_yml, "page:\n  width: 210\n  height: 297\n").expect("write style");
    let output = dir.path().join("styled.hwpx");

    let result = cargo_bin()
        .args([
            "convert",
            input.to_str().unwrap(),
            output.to_str().unwrap(),
            "--style",
            style_yml.to_str().unwrap(),
        ])
        .output()
        .expect("execute convert --style");
    assert!(
        result.status.success(),
        "convert --style failed: {}",
        String::from_utf8_lossy(&result.stderr)
    );
    assert!(output.exists(), "styled hwpx not created");
}

#[test]
fn convert_assets_dir_flag_accepted() {
    let dir = tempdir().expect("tempdir");
    let hwpx = dir.path().join("doc.hwpx");
    common::make_hwpx(&hwpx);
    let md_out = dir.path().join("doc.md");
    let assets = dir.path().join("assets");

    let result = cargo_bin()
        .args([
            "convert",
            hwpx.to_str().unwrap(),
            md_out.to_str().unwrap(),
            "--assets-dir",
            assets.to_str().unwrap(),
        ])
        .output()
        .expect("execute convert --assets-dir");
    assert!(
        result.status.success(),
        "convert --assets-dir failed: {}",
        String::from_utf8_lossy(&result.stderr)
    );
    assert!(md_out.exists(), "md output not created");
}

#[test]
fn convert_assets_dir_extracts_embedded_image() {
    let png_data = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
    let fixture = fixtures::HwpxFixture::new()
        .section(&fixtures::para_xml("Image doc"))
        .bin_data("logo.png", png_data.clone());
    let (dir, hwpx_path) = fixture.write_to_tempfile();

    let md_out = dir.path().join("out.md");
    let assets = dir.path().join("extracted");

    let result = cargo_bin()
        .args([
            "convert",
            hwpx_path.to_str().unwrap(),
            md_out.to_str().unwrap(),
            "--assets-dir",
            assets.to_str().unwrap(),
        ])
        .output()
        .expect("execute convert --assets-dir");
    assert!(
        result.status.success(),
        "convert --assets-dir failed: {}",
        String::from_utf8_lossy(&result.stderr)
    );
    let extracted = assets.join("logo.png");
    assert!(extracted.exists(), "image must be extracted to assets dir");
    assert_eq!(
        std::fs::read(&extracted).unwrap(),
        png_data,
        "extracted image data must match original"
    );
}