|
| 1 | +using System.Reflection; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace ShellDocs.Tests; |
| 5 | + |
| 6 | +/* We don't shell out to `dotnet publish` in tests (slow + fragile). We test |
| 7 | + the two deterministic post-processing helpers in isolation: RewriteBaseHref |
| 8 | + and the recursive CopyDirectory. Everything else in BuildCommand.Run is |
| 9 | + glue around Process.Start, which is best verified by hand. */ |
| 10 | +public class BuildCommandTests : IDisposable |
| 11 | +{ |
| 12 | + private readonly string _tempDir; |
| 13 | + private readonly MethodInfo _rewrite; |
| 14 | + private readonly MethodInfo _copy; |
| 15 | + |
| 16 | + public BuildCommandTests() |
| 17 | + { |
| 18 | + _tempDir = Path.Combine(Path.GetTempPath(), "shelldocs-build-" + Guid.NewGuid().ToString("N")); |
| 19 | + Directory.CreateDirectory(_tempDir); |
| 20 | + |
| 21 | + var cli = AppDomain.CurrentDomain.GetAssemblies() |
| 22 | + .FirstOrDefault(a => a.GetName().Name == "shelldocs") |
| 23 | + ?? Assembly.Load("shelldocs"); |
| 24 | + var type = cli.GetType("ShellDocs.CLI.Commands.BuildCommand", throwOnError: true)!; |
| 25 | + _rewrite = type.GetMethod("RewriteBaseHref", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)!; |
| 26 | + _copy = type.GetMethod("CopyDirectory", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)!; |
| 27 | + } |
| 28 | + |
| 29 | + public void Dispose() |
| 30 | + { |
| 31 | + try { Directory.Delete(_tempDir, recursive: true); } catch { } |
| 32 | + } |
| 33 | + |
| 34 | + private void RewriteBaseHref(string indexPath, string href) => |
| 35 | + _rewrite.Invoke(null, new object[] { indexPath, href }); |
| 36 | + |
| 37 | + private void CopyDirectory(string source, string dest) => |
| 38 | + _copy.Invoke(null, new object[] { source, dest }); |
| 39 | + |
| 40 | + [Theory] |
| 41 | + [InlineData("<base href=\"/\" />", "/repo/", "<base href=\"/repo/\" />")] |
| 42 | + [InlineData("<base href='/'/>", "/repo/", "<base href=\"/repo/\" />")] |
| 43 | + [InlineData("<base href=/ />", "/repo/", "<base href=\"/repo/\" />")] |
| 44 | + [InlineData("<BASE HREF=\"/old/\" />", "/new/", "<base href=\"/new/\" />")] |
| 45 | + public void RewriteBaseHref_HandlesQuoteVariants(string original, string href, string expected) |
| 46 | + { |
| 47 | + var path = Path.Combine(_tempDir, "index.html"); |
| 48 | + File.WriteAllText(path, $"<html><head>{original}</head></html>"); |
| 49 | + RewriteBaseHref(path, href); |
| 50 | + Assert.Contains(expected, File.ReadAllText(path)); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void RewriteBaseHref_LeavesOtherMarkupUntouched() |
| 55 | + { |
| 56 | + var path = Path.Combine(_tempDir, "index.html"); |
| 57 | + var input = "<html><head><title>App</title><base href=\"/\" /><meta /></head><body></body></html>"; |
| 58 | + File.WriteAllText(path, input); |
| 59 | + RewriteBaseHref(path, "/x/"); |
| 60 | + var output = File.ReadAllText(path); |
| 61 | + Assert.Contains("<title>App</title>", output); |
| 62 | + Assert.Contains("<meta />", output); |
| 63 | + Assert.Contains("<base href=\"/x/\" />", output); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void CopyDirectory_CopiesNestedFiles() |
| 68 | + { |
| 69 | + var src = Path.Combine(_tempDir, "src"); |
| 70 | + var dst = Path.Combine(_tempDir, "dst"); |
| 71 | + Directory.CreateDirectory(Path.Combine(src, "sub", "deep")); |
| 72 | + File.WriteAllText(Path.Combine(src, "root.txt"), "root"); |
| 73 | + File.WriteAllText(Path.Combine(src, "sub", "mid.txt"), "mid"); |
| 74 | + File.WriteAllText(Path.Combine(src, "sub", "deep", "leaf.txt"), "leaf"); |
| 75 | + |
| 76 | + CopyDirectory(src, dst); |
| 77 | + |
| 78 | + Assert.Equal("root", File.ReadAllText(Path.Combine(dst, "root.txt"))); |
| 79 | + Assert.Equal("mid", File.ReadAllText(Path.Combine(dst, "sub", "mid.txt"))); |
| 80 | + Assert.Equal("leaf", File.ReadAllText(Path.Combine(dst, "sub", "deep", "leaf.txt"))); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public void CopyDirectory_OverwritesExistingFiles() |
| 85 | + { |
| 86 | + var src = Path.Combine(_tempDir, "src"); |
| 87 | + var dst = Path.Combine(_tempDir, "dst"); |
| 88 | + Directory.CreateDirectory(src); |
| 89 | + Directory.CreateDirectory(dst); |
| 90 | + File.WriteAllText(Path.Combine(src, "a.txt"), "new"); |
| 91 | + File.WriteAllText(Path.Combine(dst, "a.txt"), "old"); |
| 92 | + |
| 93 | + CopyDirectory(src, dst); |
| 94 | + |
| 95 | + Assert.Equal("new", File.ReadAllText(Path.Combine(dst, "a.txt"))); |
| 96 | + } |
| 97 | +} |
0 commit comments