|
| 1 | +using System.Reflection; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace ShellDocs.Tests; |
| 5 | + |
| 6 | +/* Integration tests for `shelldocs add`. Uses reflection to reach the internal |
| 7 | + AddCommand.Run entry point (matches the InitCommand test pattern). */ |
| 8 | +public class AddCommandTests : IDisposable |
| 9 | +{ |
| 10 | + private readonly string _sandbox; |
| 11 | + private readonly MethodInfo _run; |
| 12 | + |
| 13 | + public AddCommandTests() |
| 14 | + { |
| 15 | + _sandbox = Path.Combine(Path.GetTempPath(), "shelldocs-add-tests-" + Guid.NewGuid().ToString("N")[..8]); |
| 16 | + Directory.CreateDirectory(Path.Combine(_sandbox, "content")); |
| 17 | + |
| 18 | + var cli = AppDomain.CurrentDomain.GetAssemblies() |
| 19 | + .FirstOrDefault(a => a.GetName().Name == "shelldocs") |
| 20 | + ?? Assembly.Load("shelldocs"); |
| 21 | + var type = cli.GetType("ShellDocs.CLI.Commands.AddCommand", throwOnError: true)!; |
| 22 | + _run = type.GetMethod("Run", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!; |
| 23 | + } |
| 24 | + |
| 25 | + public void Dispose() |
| 26 | + { |
| 27 | + try { Directory.Delete(_sandbox, recursive: true); } catch { } |
| 28 | + } |
| 29 | + |
| 30 | + private int Add(string template, string name, bool force = false, string? dir = null) => |
| 31 | + (int)_run.Invoke(null, new object?[] { template, name, dir ?? _sandbox, force })!; |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void Add_Component_WritesToContentDocsComponents_WithSlugFilename() |
| 35 | + { |
| 36 | + var exit = Add("component", "MyBigCard"); |
| 37 | + |
| 38 | + Assert.Equal(0, exit); |
| 39 | + var expected = Path.Combine(_sandbox, "content", "docs", "components", "my-big-card.md"); |
| 40 | + Assert.True(File.Exists(expected)); |
| 41 | + var body = File.ReadAllText(expected); |
| 42 | + Assert.Contains("title: MyBigCard", body); |
| 43 | + Assert.Contains("`<MyBigCard>`", body); |
| 44 | + Assert.Contains("razor:preview", body); |
| 45 | + Assert.Contains("<TypeTable>", body); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void Add_Guide_WritesToContentDocsGuides_WithTitleCaseName() |
| 50 | + { |
| 51 | + var exit = Add("guide", "getting-started"); |
| 52 | + |
| 53 | + Assert.Equal(0, exit); |
| 54 | + var expected = Path.Combine(_sandbox, "content", "docs", "guides", "getting-started.md"); |
| 55 | + Assert.True(File.Exists(expected)); |
| 56 | + var body = File.ReadAllText(expected); |
| 57 | + Assert.Contains("title: Getting Started", body); |
| 58 | + Assert.Contains("<Steps>", body); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void Add_Page_WritesToContentDocs_WithBlankBody() |
| 63 | + { |
| 64 | + var exit = Add("page", "faq"); |
| 65 | + |
| 66 | + Assert.Equal(0, exit); |
| 67 | + var expected = Path.Combine(_sandbox, "content", "docs", "faq.md"); |
| 68 | + Assert.True(File.Exists(expected)); |
| 69 | + var body = File.ReadAllText(expected); |
| 70 | + Assert.Contains("title: Faq", body); |
| 71 | + Assert.DoesNotContain("razor:preview", body); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void Add_UnknownTemplate_ReturnsNonZero() |
| 76 | + { |
| 77 | + Assert.NotEqual(0, Add("widget", "Foo")); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void Add_MissingContentDir_ReturnsNonZero() |
| 82 | + { |
| 83 | + var stray = Path.Combine(Path.GetTempPath(), "shelldocs-add-no-content-" + Guid.NewGuid().ToString("N")[..8]); |
| 84 | + Directory.CreateDirectory(stray); |
| 85 | + try |
| 86 | + { |
| 87 | + Assert.NotEqual(0, Add("component", "Foo", dir: stray)); |
| 88 | + } |
| 89 | + finally |
| 90 | + { |
| 91 | + Directory.Delete(stray, recursive: true); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void Add_ExistingFileWithoutForce_Fails() |
| 97 | + { |
| 98 | + Add("component", "Duplicate"); |
| 99 | + Assert.NotEqual(0, Add("component", "Duplicate")); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void Add_ExistingFileWithForce_Overwrites() |
| 104 | + { |
| 105 | + Add("component", "Duplicate"); |
| 106 | + var target = Path.Combine(_sandbox, "content", "docs", "components", "duplicate.md"); |
| 107 | + File.WriteAllText(target, "custom content"); |
| 108 | + |
| 109 | + var exit = Add("component", "Duplicate", force: true); |
| 110 | + |
| 111 | + Assert.Equal(0, exit); |
| 112 | + var body = File.ReadAllText(target); |
| 113 | + Assert.Contains("`<Duplicate>`", body); |
| 114 | + Assert.DoesNotContain("custom content", body); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void Add_EmptyName_ReturnsNonZero() |
| 119 | + { |
| 120 | + Assert.NotEqual(0, Add("component", "")); |
| 121 | + } |
| 122 | +} |
0 commit comments