Skip to content

Commit c14e0d9

Browse files
committed
Add tests
1 parent 2f30eef commit c14e0d9

File tree

11 files changed

+139
-41
lines changed

11 files changed

+139
-41
lines changed

docs-builder.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "docs-assembler", "src\docs-
5353
EndProject
5454
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "authoring", "tests\authoring\authoring.fsproj", "{018F959E-824B-4664-B345-066784478D24}"
5555
EndProject
56+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "docs-mover", "src\docs-mover\docs-mover.csproj", "{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}"
57+
EndProject
5658
Global
5759
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5860
Debug|Any CPU = Debug|Any CPU
@@ -95,6 +97,10 @@ Global
9597
{018F959E-824B-4664-B345-066784478D24}.Debug|Any CPU.Build.0 = Debug|Any CPU
9698
{018F959E-824B-4664-B345-066784478D24}.Release|Any CPU.ActiveCfg = Release|Any CPU
9799
{018F959E-824B-4664-B345-066784478D24}.Release|Any CPU.Build.0 = Release|Any CPU
100+
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
101+
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Debug|Any CPU.Build.0 = Debug|Any CPU
102+
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Release|Any CPU.ActiveCfg = Release|Any CPU
103+
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Release|Any CPU.Build.0 = Release|Any CPU
98104
EndGlobalSection
99105
GlobalSection(NestedProjects) = preSolution
100106
{4D198E25-C211-41DC-9E84-B15E89BD7048} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
@@ -106,5 +112,6 @@ Global
106112
{A2A34BBC-CB5E-4100-9529-A12B6ECB769C} = {245023D2-D3CA-47B9-831D-DAB91A2FFDC7}
107113
{28350800-B44B-479B-86E2-1D39E321C0B4} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
108114
{018F959E-824B-4664-B345-066784478D24} = {67B576EE-02FA-4F9B-94BC-3630BC09ECE5}
115+
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
109116
EndGlobalSection
110117
EndGlobal

docs/docset.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ toc:
9797
- file: req.md
9898
- folder: nested
9999
- file: cross-links.md
100+
- folder: mover
101+
children:
102+
- file: first-page.md
103+
- file: second-page.md

docs/testing/mover/first-page.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# First Page
2+
3+
[Link to second page](second-page.md)

docs/testing/mover/second-page.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Second Page
2+
3+
[Link to first page](first-page.md)
4+
5+
[Absolut link to first page](/testing/mover/first-page.md)
6+
```

src/docs-builder/Cli/Commands.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ConsoleAppFramework;
88
using Documentation.Builder.Diagnostics.Console;
99
using Documentation.Builder.Http;
10+
using Documentation.Mover;
1011
using Elastic.Markdown;
1112
using Elastic.Markdown.IO;
1213
using Microsoft.Extensions.Logging;
@@ -128,7 +129,7 @@ public async Task<int> Move(
128129
};
129130
var set = new DocumentationSet(context);
130131

131-
var moveCommand = new Move(fileSystem, set, logger);
132+
var moveCommand = new Move(fileSystem, fileSystem, set, logger);
132133
return await moveCommand.Execute(source, target, dryRun ?? false, ctx);
133134
}
134135
}

src/docs-builder/docs-builder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
</ItemGroup>
3131

3232
<ItemGroup>
33+
<ProjectReference Include="..\docs-mover\docs-mover.csproj" />
3334
<ProjectReference Include="..\Elastic.Markdown\Elastic.Markdown.csproj" />
3435
</ItemGroup>
3536
</Project>

src/docs-builder/Cli/Move.cs renamed to src/docs-mover/Move.cs

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
// Licensed to Elasticsearch B.V under one or more agreements.
1+
// Licensed to Elasticsearch B.V under one or more agreements.
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.Collections.ObjectModel;
56
using System.IO.Abstractions;
67
using System.Text.RegularExpressions;
78
using Elastic.Markdown.IO;
89
using Microsoft.Extensions.Logging;
910

10-
namespace Documentation.Builder.Cli;
11+
namespace Documentation.Mover;
1112

12-
internal class Move(IFileSystem fileSystem, DocumentationSet documentationSet, ILoggerFactory loggerFactory)
13+
public class Move(IFileSystem readFileSystem, IFileSystem writeFileSystem, DocumentationSet documentationSet, ILoggerFactory loggerFactory)
1314
{
1415
private readonly ILogger _logger = loggerFactory.CreateLogger<Move>();
1516
private readonly List<(string filePath, string originalContent, string newContent)> _changes = [];
17+
private readonly List<LinkModification> _linkModifications = [];
1618
private const string ChangeFormatString = "Change \e[31m{0}\e[0m to \e[32m{1}\e[0m at \e[34m{2}:{3}:{4}\e[0m";
1719

20+
public record LinkModification(string OldLink, string NewLink, string SourceFile, int LineNumber, int ColumnNumber);
21+
22+
23+
public ReadOnlyCollection<LinkModification> LinkModifications => _linkModifications.AsReadOnly();
24+
1825
public async Task<int> Execute(string? source, string? target, bool isDryRun, Cancel ctx = default)
1926
{
27+
_linkModifications.Clear();
2028
if (isDryRun)
2129
_logger.LogInformation("Running in dry-run mode");
2230

@@ -25,12 +33,13 @@ public async Task<int> Execute(string? source, string? target, bool isDryRun, Ca
2533
return 1;
2634
}
2735

36+
2837
var sourcePath = Path.GetFullPath(source!);
2938
var targetPath = Path.GetFullPath(target!);
3039

3140
var (_, sourceMarkdownFile) = documentationSet.MarkdownFiles.Single(i => i.Value.FilePath == sourcePath);
3241

33-
var sourceContent = await fileSystem.File.ReadAllTextAsync(sourceMarkdownFile.FilePath, ctx);
42+
var sourceContent = await readFileSystem.File.ReadAllTextAsync(sourceMarkdownFile.FilePath, ctx);
3443

3544
var markdownLinkRegex = new Regex(@"\[([^\]]*)\]\(((?:\.{0,2}\/)?[^:)]+\.md(?:#[^)]*)?)\)", RegexOptions.Compiled);
3645

@@ -51,16 +60,13 @@ public async Task<int> Execute(string? source, string? target, bool isDryRun, Ca
5160
var newLink = $"[{match.Groups[1].Value}]({newPath})";
5261
var lineNumber = sourceContent.Substring(0, match.Index).Count(c => c == '\n') + 1;
5362
var columnNumber = match.Index - sourceContent.LastIndexOf('\n', match.Index);
54-
_logger.LogInformation(
55-
string.Format(
56-
ChangeFormatString,
57-
match.Value,
58-
newLink,
59-
sourceMarkdownFile.SourceFile.FullName,
60-
lineNumber,
61-
columnNumber
62-
)
63-
);
63+
_linkModifications.Add(new LinkModification(
64+
match.Value,
65+
newLink,
66+
sourceMarkdownFile.SourceFile.FullName,
67+
lineNumber,
68+
columnNumber
69+
));
6470
return newLink;
6571
});
6672

@@ -76,22 +82,34 @@ await ProcessMarkdownFile(
7682
);
7783
}
7884

85+
foreach (var (oldLink, newLink, sourceFile, lineNumber, columnNumber) in LinkModifications)
86+
{
87+
_logger.LogInformation(string.Format(
88+
ChangeFormatString,
89+
oldLink,
90+
newLink,
91+
sourceFile,
92+
lineNumber,
93+
columnNumber
94+
));
95+
}
96+
7997
if (isDryRun)
8098
return 0;
8199

82100
var targetDirectory = Path.GetDirectoryName(targetPath);
83-
fileSystem.Directory.CreateDirectory(targetDirectory!);
84-
fileSystem.File.Move(sourcePath, targetPath);
101+
readFileSystem.Directory.CreateDirectory(targetDirectory!);
102+
readFileSystem.File.Move(sourcePath, targetPath);
85103
try
86104
{
87105
foreach (var (filePath, _, newContent) in _changes)
88-
await fileSystem.File.WriteAllTextAsync(filePath, newContent, ctx);
106+
await writeFileSystem.File.WriteAllTextAsync(filePath, newContent, ctx);
89107
}
90108
catch (Exception)
91109
{
92110
foreach (var (filePath, originalContent, _) in _changes)
93-
await fileSystem.File.WriteAllTextAsync(filePath, originalContent, ctx);
94-
fileSystem.File.Move(targetPath, sourcePath);
111+
await writeFileSystem.File.WriteAllTextAsync(filePath, originalContent, ctx);
112+
writeFileSystem.File.Move(targetPath, sourcePath);
95113
throw;
96114
}
97115
return 0;
@@ -124,13 +142,13 @@ private bool ValidateInputs(string? source, string? target)
124142
return false;
125143
}
126144

127-
if (!fileSystem.File.Exists(source))
145+
if (!readFileSystem.File.Exists(source))
128146
{
129147
_logger.LogError($"Source file {source} does not exist");
130148
return false;
131149
}
132150

133-
if (fileSystem.File.Exists(target))
151+
if (readFileSystem.File.Exists(target))
134152
{
135153
_logger.LogError($"Target file {target} already exists");
136154
return false;
@@ -145,7 +163,7 @@ private async Task ProcessMarkdownFile(
145163
MarkdownFile value,
146164
Cancel ctx)
147165
{
148-
var content = await fileSystem.File.ReadAllTextAsync(value.FilePath, ctx);
166+
var content = await readFileSystem.File.ReadAllTextAsync(value.FilePath, ctx);
149167
var currentDir = Path.GetDirectoryName(value.FilePath)!;
150168
var pathInfo = GetPathInfo(currentDir, source, target);
151169
var linkPattern = BuildLinkPattern(pathInfo);
@@ -199,30 +217,25 @@ MarkdownFile value
199217
: "";
200218

201219
string newLink;
202-
if (originalPath.StartsWith("/"))
220+
if (originalPath.StartsWith('/'))
203221
{
204-
// Absolute style link
205222
newLink = $"[{match.Groups[1].Value}]({absoluteStyleTarget}{anchor})";
206223
}
207224
else
208225
{
209-
// Relative link
210226
var relativeTarget = Path.GetRelativePath(Path.GetDirectoryName(value.FilePath)!, target);
211227
newLink = $"[{match.Groups[1].Value}]({relativeTarget}{anchor})";
212228
}
213229

214230
var lineNumber = content.Substring(0, match.Index).Count(c => c == '\n') + 1;
215231
var columnNumber = match.Index - content.LastIndexOf('\n', match.Index);
216-
_logger.LogInformation(
217-
string.Format(
218-
ChangeFormatString,
219-
match.Value,
220-
newLink,
221-
value.SourceFile.FullName,
222-
lineNumber,
223-
columnNumber
224-
)
225-
);
232+
_linkModifications.Add(new LinkModification(
233+
match.Value,
234+
newLink,
235+
value.SourceFile.FullName,
236+
lineNumber,
237+
columnNumber
238+
));
226239
return newLink;
227240
});
228241
}

src/docs-mover/docs-mover.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<RootNamespace>Documentation.Mover</RootNamespace>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<AssemblyName>Documentation.Mover</AssemblyName>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\Elastic.Markdown\Elastic.Markdown.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
17+
</ItemGroup>
18+
19+
</Project>

tests/Elastic.Markdown.Tests/DocSet/NavigationTestsBase.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Elastic.Markdown.IO;
99
using Elastic.Markdown.IO.Configuration;
1010
using FluentAssertions;
11+
using Microsoft.Extensions.Logging;
1112
using Xunit.Abstractions;
1213

1314
namespace Elastic.Markdown.Tests.DocSet;
@@ -16,14 +17,14 @@ public class NavigationTestsBase : IAsyncLifetime
1617
{
1718
protected NavigationTestsBase(ITestOutputHelper output)
1819
{
19-
var logger = new TestLoggerFactory(output);
20+
LoggerFactory = new TestLoggerFactory(output);
2021
ReadFileSystem = new FileSystem(); //use real IO to read docs.
21-
var writeFs = new MockFileSystem(new MockFileSystemOptions //use in memory mock fs to test generation
22+
WriteFileSystem = new MockFileSystem(new MockFileSystemOptions //use in memory mock fs to test generation
2223
{
2324
CurrentDirectory = Paths.Root.FullName
2425
});
2526
var collector = new TestDiagnosticsCollector(output);
26-
var context = new BuildContext(ReadFileSystem, writeFs)
27+
var context = new BuildContext(ReadFileSystem, WriteFileSystem)
2728
{
2829
Force = false,
2930
UrlPathPrefix = null,
@@ -33,11 +34,13 @@ protected NavigationTestsBase(ITestOutputHelper output)
3334
Set = new DocumentationSet(context);
3435

3536
Set.Files.Should().HaveCountGreaterThan(10);
36-
Generator = new DocumentationGenerator(Set, logger);
37-
37+
Generator = new DocumentationGenerator(Set, LoggerFactory);
3838
}
3939

40+
protected ILoggerFactory LoggerFactory { get; }
41+
4042
protected FileSystem ReadFileSystem { get; set; }
43+
protected IFileSystem WriteFileSystem { get; set; }
4144
protected DocumentationSet Set { get; }
4245
protected DocumentationGenerator Generator { get; }
4346
protected ConfigurationFile Configuration { get; set; } = default!;

tests/Elastic.Markdown.Tests/Elastic.Markdown.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24+
<ProjectReference Include="..\..\src\docs-mover\docs-mover.csproj" />
2425
<ProjectReference Include="..\..\src\Elastic.Markdown\Elastic.Markdown.csproj"/>
2526
</ItemGroup>
2627

0 commit comments

Comments
 (0)