adrs-core 0.7.3

Core library for managing Architecture Decision Records
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
//! Compatibility tests with npryce/adr-tools format.
//!
//! These tests verify that adrs-core can correctly parse and generate
//! ADRs in the format produced by the original adr-tools.

use adrs_core::{Adr, AdrStatus, Config, LinkKind, Parser, Repository};
use std::fs;
use tempfile::TempDir;

/// Sample ADR from adr-tools: 0001-record-architecture-decisions.md
const ADR_TOOLS_SAMPLE_1: &str = r#"# 1. Record architecture decisions

Date: 2016-02-12

## Status

Accepted

## Context

We need to record the architectural decisions made on this project.

## Decision

We will use Architecture Decision Records, as described by Michael Nygard in this article: http://thinkrelevance.com/blog/2011/11/15/documenting-architecture-decisions

## Consequences

See Michael Nygard's article, linked above.
"#;

/// Sample ADR from adr-tools: 0002-implement-as-shell-scripts.md
const ADR_TOOLS_SAMPLE_2: &str = r#"# 2. Implement as shell scripts

Date: 2016-02-12

## Status

Accepted

## Context

ADRs are plain text files stored in a subdirectory of the project.
The tool needs to create and update that directory and the files in it.

It must work on a developer's machine, which could be running any common OS.

We want to get a working tool as quickly as possible.

## Decision

We will write the tool as shell scripts that run on any POSIX
compliant operating system.

## Consequences

We will only be able to use commands that are installed on a
vanilla development machine.
"#;

/// Sample ADR with more complex structure: 0003-single-command-with-subcommands.md
const ADR_TOOLS_SAMPLE_3: &str = r#"# 3. Single command with subcommands

Date: 2016-02-12

## Status

Accepted

## Context

The tool provides a number of related commands to create
and manipulate architecture decision records.

How can the user find out about the commands that are available?

## Decision

The tool defines a single command, called `adr`.

The first argument to `adr` (the subcommand) specifies the
action to perform.  Further arguments are interpreted by the
subcommand.

Running `adr` without any arguments lists the available
subcommands.

Subcommands are implemented as scripts in the same
directory as the `adr` script.  E.g. the subcommand `new` is
implemented as the script `adr-new`, the subcommand `help`
as the script `adr-help` and so on.

Helper scripts that are part of the implementation but not
subcommands follow a different naming convention, so that
subcommands can be listed by filtering and transforming script
file names.

## Consequences

Users can more easily explore the capabilities of the tool.

Users are already used to this style of command-line tool.  For
example, Git works this way.

Each subcommand can be implemented in the most appropriate
language.
"#;

/// Superseded ADR format (from adr-tools test expectations)
const ADR_TOOLS_SUPERSEDED: &str = r#"# 1. First Record

Date: 1992-01-12

## Status

Superceded by [2. Second Record](0002-second-record.md)

## Context

First context.

## Decision

First decision.

## Consequences

First consequences.
"#;

/// Superseding ADR format (from adr-tools test expectations)
const ADR_TOOLS_SUPERSEDING: &str = r#"# 2. Second Record

Date: 1992-01-12

## Status

Accepted

Supercedes [1. First Record](0001-first-record.md)

## Context

Second context.

## Decision

Second decision.

## Consequences

Second consequences.
"#;

// ========== Parsing Tests ==========

#[test]
fn test_parse_adr_tools_sample_1() {
    let parser = Parser::new();
    let adr = parser.parse(ADR_TOOLS_SAMPLE_1).unwrap();

    assert_eq!(adr.number, 1);
    assert_eq!(adr.title, "Record architecture decisions");
    assert_eq!(adr.status, AdrStatus::Accepted);
    assert!(adr.context.contains("record the architectural decisions"));
    assert!(adr.decision.contains("Architecture Decision Records"));
    assert!(adr.consequences.contains("Michael Nygard"));
}

#[test]
fn test_parse_adr_tools_sample_2() {
    let parser = Parser::new();
    let adr = parser.parse(ADR_TOOLS_SAMPLE_2).unwrap();

    assert_eq!(adr.number, 2);
    assert_eq!(adr.title, "Implement as shell scripts");
    assert_eq!(adr.status, AdrStatus::Accepted);
    assert!(adr.context.contains("plain text files"));
    assert!(adr.decision.contains("shell scripts"));
}

#[test]
fn test_parse_adr_tools_sample_3() {
    let parser = Parser::new();
    let adr = parser.parse(ADR_TOOLS_SAMPLE_3).unwrap();

    assert_eq!(adr.number, 3);
    assert_eq!(adr.title, "Single command with subcommands");
    assert_eq!(adr.status, AdrStatus::Accepted);

    // Check multiline content is preserved
    assert!(adr.context.contains("related commands"));
    assert!(adr.decision.contains("adr-new"));
    assert!(adr.consequences.contains("Git works this way"));
}

#[test]
fn test_parse_adr_tools_superseded() {
    let parser = Parser::new();
    let adr = parser.parse(ADR_TOOLS_SUPERSEDED).unwrap();

    assert_eq!(adr.number, 1);
    assert_eq!(adr.title, "First Record");
    // Note: adr-tools uses "Superceded" (typo), we treat it as Superseded
    assert_eq!(adr.status, AdrStatus::Superseded);
    assert_eq!(adr.links.len(), 1);
    assert_eq!(adr.links[0].target, 2);
    assert_eq!(adr.links[0].kind, LinkKind::SupersededBy);
}

#[test]
fn test_parse_adr_tools_superseding() {
    let parser = Parser::new();
    let adr = parser.parse(ADR_TOOLS_SUPERSEDING).unwrap();

    assert_eq!(adr.number, 2);
    assert_eq!(adr.title, "Second Record");
    assert_eq!(adr.status, AdrStatus::Accepted);
    assert_eq!(adr.links.len(), 1);
    assert_eq!(adr.links[0].target, 1);
    assert_eq!(adr.links[0].kind, LinkKind::Supersedes);
}

// ========== File Parsing Tests ==========

#[test]
fn test_parse_adr_tools_files_from_disk() {
    let parser = Parser::new();

    // These files are from the actual adr-tools repo
    let adr_tools_path = std::path::Path::new(".tmp/adr-tools/doc/adr");

    if !adr_tools_path.exists() {
        // Skip if adr-tools not available
        return;
    }

    // Parse all ADRs from adr-tools
    for entry in fs::read_dir(adr_tools_path).unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();

        if path.extension().map(|e| e == "md").unwrap_or(false) {
            let adr = parser.parse_file(&path).unwrap();

            // All should have valid numbers
            assert!(adr.number > 0, "ADR should have a number: {:?}", path);

            // All should have non-empty titles
            assert!(!adr.title.is_empty(), "ADR should have a title: {:?}", path);

            // All should have a valid status
            assert!(
                matches!(
                    adr.status,
                    AdrStatus::Proposed
                        | AdrStatus::Accepted
                        | AdrStatus::Deprecated
                        | AdrStatus::Superseded
                        | AdrStatus::Custom(_)
                ),
                "ADR should have a valid status: {:?}",
                path
            );
        }
    }
}

// ========== Repository Compatibility Tests ==========

#[test]
fn test_repository_creates_adr_tools_compatible_files() {
    let temp = TempDir::new().unwrap();
    let repo = Repository::init(temp.path(), None, false).unwrap();

    // Create a new ADR
    let (_, path) = repo.new_adr("Use PostgreSQL").unwrap();
    let content = fs::read_to_string(&path).unwrap();

    // Should follow adr-tools format
    assert!(content.starts_with("# 2. Use PostgreSQL"));
    assert!(content.contains("Date:"));
    assert!(content.contains("## Status"));
    assert!(content.contains("## Context"));
    assert!(content.contains("## Decision"));
    assert!(content.contains("## Consequences"));

    // Should NOT have frontmatter in compatible mode
    assert!(!content.starts_with("---"));
}

#[test]
fn test_repository_supersede_creates_adr_tools_format() {
    let temp = TempDir::new().unwrap();
    let repo = Repository::init(temp.path(), None, false).unwrap();

    // Supersede the initial ADR
    repo.supersede("New approach", 1).unwrap();

    // Check the superseded ADR
    let old_content = fs::read_to_string(
        repo.adr_path()
            .join("0001-record-architecture-decisions.md"),
    )
    .unwrap();
    assert!(old_content.contains("Superseded by"));
    assert!(old_content.contains("[2."));
    assert!(old_content.contains("0002-"));

    // Check the superseding ADR
    let new_content = fs::read_to_string(repo.adr_path().join("0002-new-approach.md")).unwrap();
    assert!(new_content.contains("Supersedes"));
    assert!(new_content.contains("[1."));
    assert!(new_content.contains("0001-"));
}

#[test]
fn test_roundtrip_adr_tools_format() {
    let temp = TempDir::new().unwrap();
    let adr_path = temp.path().join("doc/adr");
    fs::create_dir_all(&adr_path).unwrap();

    // Write an adr-tools format file
    fs::write(adr_path.join("0001-test-decision.md"), ADR_TOOLS_SAMPLE_1).unwrap();

    // Write the .adr-dir config
    fs::write(temp.path().join(".adr-dir"), "doc/adr").unwrap();

    // Open and read with our library
    let repo = Repository::open(temp.path()).unwrap();
    let adrs = repo.list().unwrap();

    assert_eq!(adrs.len(), 1);
    assert_eq!(adrs[0].number, 1);
    assert_eq!(adrs[0].title, "Record architecture decisions");
    assert_eq!(adrs[0].status, AdrStatus::Accepted);
}

#[test]
fn test_config_file_compatibility() {
    let temp = TempDir::new().unwrap();

    // Create .adr-dir file like adr-tools does
    fs::create_dir_all(temp.path().join("custom/path")).unwrap();
    fs::write(temp.path().join(".adr-dir"), "custom/path\n").unwrap();

    let config = Config::load(temp.path()).unwrap();
    assert_eq!(config.adr_dir, std::path::PathBuf::from("custom/path"));
}

#[test]
fn test_filename_format_compatibility() {
    // adr-tools uses: NNNN-slug.md format
    let adr = Adr::new(1, "Use Rust for Implementation");
    assert_eq!(adr.filename(), "0001-use-rust-for-implementation.md");

    let adr = Adr::new(42, "API v2.0 Design");
    assert_eq!(adr.filename(), "0042-api-v2-0-design.md");

    let adr = Adr::new(999, "Final Decision");
    assert_eq!(adr.filename(), "0999-final-decision.md");

    let adr = Adr::new(9999, "Max Four Digits");
    assert_eq!(adr.filename(), "9999-max-four-digits.md");
}

// ========== Edge Cases from adr-tools tests ==========

#[test]
fn test_funny_characters_in_title() {
    // From adr-tools test: funny-characters.sh
    let temp = TempDir::new().unwrap();
    let repo = Repository::init(temp.path(), None, false).unwrap();

    // Test various special characters
    let (adr, path) = repo.new_adr("Use a \"Alarm Clock\" for alerts").unwrap();
    assert!(path.exists());
    assert_eq!(adr.title, "Use a \"Alarm Clock\" for alerts");

    let (adr, path) = repo.new_adr("Use the 'Strategy' pattern").unwrap();
    assert!(path.exists());
    assert_eq!(adr.title, "Use the 'Strategy' pattern");
}

#[test]
fn test_alternative_adr_directory() {
    // From adr-tools test: alternative-adr-directory.sh
    let temp = TempDir::new().unwrap();

    // Use a non-default directory
    let _repo = Repository::init(temp.path(), Some("decisions".into()), false).unwrap();

    assert!(temp.path().join("decisions").exists());

    // .adr-dir should contain the custom path
    let config_content = fs::read_to_string(temp.path().join(".adr-dir")).unwrap();
    assert_eq!(config_content, "decisions");
}

#[test]
fn test_create_multiple_records() {
    // From adr-tools test: create-multiple-records.sh
    let temp = TempDir::new().unwrap();
    let repo = Repository::init(temp.path(), None, false).unwrap();

    // Create multiple ADRs
    let _ = repo.new_adr("First ADR").unwrap();
    let _ = repo.new_adr("Second ADR").unwrap();
    let _ = repo.new_adr("Third ADR").unwrap();

    let adrs = repo.list().unwrap();
    assert_eq!(adrs.len(), 4); // Including the init ADR

    // Check numbering
    assert_eq!(adrs[0].number, 1);
    assert_eq!(adrs[1].number, 2);
    assert_eq!(adrs[2].number, 3);
    assert_eq!(adrs[3].number, 4);
}

// ========== Status Parsing Compatibility ==========

#[test]
fn test_parse_various_status_formats() {
    let parser = Parser::new();

    // Standard statuses
    for (status_text, expected) in [
        ("Proposed", AdrStatus::Proposed),
        ("Accepted", AdrStatus::Accepted),
        ("Deprecated", AdrStatus::Deprecated),
        ("Superseded", AdrStatus::Superseded),
        // adr-tools typo variant
        ("Superceded", AdrStatus::Superseded),
    ] {
        let content = format!(
            r#"# 1. Test

## Status

{status_text}

## Context

Context.

## Decision

Decision.

## Consequences

Consequences.
"#
        );

        let adr = parser.parse(&content).unwrap();
        assert_eq!(
            adr.status, expected,
            "Failed to parse status: {status_text}"
        );
    }
}