Repository navigation
Expand file tree
/
Copy pathAddCommandTests.cs
More file actions
120 lines (101 loc) · 3.8 KB
/
Copy pathAddCommandTests.cs
File metadata and controls
120 lines (101 loc) · 3.8 KB
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
using System.Reflection;
using Xunit;
namespace ShellDocs.Tests;
public class AddCommandTests : IDisposable
{
private readonly string _sandbox;
private readonly MethodInfo _run;
public AddCommandTests()
{
_sandbox = Path.Combine(Path.GetTempPath(), "shelldocs-add-tests-" + Guid.NewGuid().ToString("N")[..8]);
Directory.CreateDirectory(Path.Combine(_sandbox, "content"));
var cli = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.GetName().Name == "shelldocs")
?? Assembly.Load("shelldocs");
var type = cli.GetType("ShellDocs.CLI.Commands.AddCommand", throwOnError: true)!;
_run = type.GetMethod("Run", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!;
}
public void Dispose()
{
try { Directory.Delete(_sandbox, recursive: true); } catch { }
}
private int Add(string template, string name, bool force = false, string? dir = null) =>
(int)_run.Invoke(null, new object?[] { template, name, dir ?? _sandbox, force })!;
[Fact]
public void Add_Component_WritesToContentDocsComponents_WithSlugFilename()
{
var exit = Add("component", "MyBigCard");
Assert.Equal(0, exit);
var expected = Path.Combine(_sandbox, "content", "docs", "components", "my-big-card.md");
Assert.True(File.Exists(expected));
var body = File.ReadAllText(expected);
Assert.Contains("title: MyBigCard", body);
Assert.Contains("`<MyBigCard>`", body);
Assert.Contains("razor:preview", body);
Assert.Contains("<TypeTable>", body);
}
[Fact]
public void Add_Guide_WritesToContentDocsGuides_WithTitleCaseName()
{
var exit = Add("guide", "getting-started");
Assert.Equal(0, exit);
var expected = Path.Combine(_sandbox, "content", "docs", "guides", "getting-started.md");
Assert.True(File.Exists(expected));
var body = File.ReadAllText(expected);
Assert.Contains("title: Getting Started", body);
Assert.Contains("<Steps>", body);
}
[Fact]
public void Add_Page_WritesToContentDocs_WithBlankBody()
{
var exit = Add("page", "faq");
Assert.Equal(0, exit);
var expected = Path.Combine(_sandbox, "content", "docs", "faq.md");
Assert.True(File.Exists(expected));
var body = File.ReadAllText(expected);
Assert.Contains("title: Faq", body);
Assert.DoesNotContain("razor:preview", body);
}
[Fact]
public void Add_UnknownTemplate_ReturnsNonZero()
{
Assert.NotEqual(0, Add("widget", "Foo"));
}
[Fact]
public void Add_MissingContentDir_ReturnsNonZero()
{
var stray = Path.Combine(Path.GetTempPath(), "shelldocs-add-no-content-" + Guid.NewGuid().ToString("N")[..8]);
Directory.CreateDirectory(stray);
try
{
Assert.NotEqual(0, Add("component", "Foo", dir: stray));
}
finally
{
Directory.Delete(stray, recursive: true);
}
}
[Fact]
public void Add_ExistingFileWithoutForce_Fails()
{
Add("component", "Duplicate");
Assert.NotEqual(0, Add("component", "Duplicate"));
}
[Fact]
public void Add_ExistingFileWithForce_Overwrites()
{
Add("component", "Duplicate");
var target = Path.Combine(_sandbox, "content", "docs", "components", "duplicate.md");
File.WriteAllText(target, "custom content");
var exit = Add("component", "Duplicate", force: true);
Assert.Equal(0, exit);
var body = File.ReadAllText(target);
Assert.Contains("`<Duplicate>`", body);
Assert.DoesNotContain("custom content", body);
}
[Fact]
public void Add_EmptyName_ReturnsNonZero()
{
Assert.NotEqual(0, Add("component", ""));
}
}