|
| 1 | +using Microsoft.Extensions.Logging.Abstractions; |
| 2 | +using Netdocs.Abstractions; |
| 3 | +using Netdocs.Core.Content; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Netdocs.Core.Tests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Covers <see cref="FileFilterSettings"/> resolution and the gating of <c>.mkdocsignore</c> in |
| 10 | +/// <see cref="ContentDiscovery"/>. The key behaviour: dev-only sections listed in |
| 11 | +/// <c>.mkdocsignore</c> stay in non-production builds and disappear only when the file-filter is |
| 12 | +/// enabled (production). |
| 13 | +/// </summary> |
| 14 | +public class FileFilterTests : IDisposable |
| 15 | +{ |
| 16 | + private readonly string _root; |
| 17 | + |
| 18 | + public FileFilterTests() |
| 19 | + { |
| 20 | + _root = Path.Combine(Path.GetTempPath(), "ndfilter-" + Guid.NewGuid().ToString("N")); |
| 21 | + Directory.CreateDirectory(Path.Combine(_root, "docs")); |
| 22 | + } |
| 23 | + |
| 24 | + public void Dispose() |
| 25 | + { |
| 26 | + try { Directory.Delete(_root, recursive: true); } catch { /* best effort */ } |
| 27 | + } |
| 28 | + |
| 29 | + private void WriteFilter(string yaml) => File.WriteAllText(Path.Combine(_root, ".file-filter.yml"), yaml); |
| 30 | + |
| 31 | + // ---- FileFilterSettings unit behaviour ------------------------------------------------- |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void NoConfig_IgnoreFileAlwaysApplies_FilterInactive() |
| 35 | + { |
| 36 | + var s = FileFilterSettings.Load(_root); |
| 37 | + |
| 38 | + Assert.False(s.Exists); |
| 39 | + Assert.False(s.IsActive(isServe: false)); |
| 40 | + Assert.True(s.AppliesMkdocsIgnore(isServe: false)); // backward compatible: honored |
| 41 | + Assert.True(s.AppliesMkdocsIgnore(isServe: true)); |
| 42 | + Assert.False(s.AppliesLabelFilter(isServe: false)); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void EnabledFalse_IgnoreFileSkipped() |
| 47 | + { |
| 48 | + WriteFilter("enabled: false\nmkdocsignore: true\n"); |
| 49 | + var s = FileFilterSettings.Load(_root); |
| 50 | + |
| 51 | + Assert.True(s.Exists); |
| 52 | + Assert.False(s.IsActive(isServe: false)); |
| 53 | + Assert.False(s.AppliesMkdocsIgnore(isServe: false)); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void EnabledTrue_IgnoreFileApplied_OnBuild_ButNotServeWhenServeDisabled() |
| 58 | + { |
| 59 | + WriteFilter("enabled: true\nenabled_on_serve: false\nmkdocsignore: true\n"); |
| 60 | + var s = FileFilterSettings.Load(_root); |
| 61 | + |
| 62 | + Assert.True(s.IsActive(isServe: false)); // production build |
| 63 | + Assert.True(s.AppliesMkdocsIgnore(isServe: false)); |
| 64 | + Assert.False(s.IsActive(isServe: true)); // serve stays off |
| 65 | + Assert.False(s.AppliesMkdocsIgnore(isServe: true)); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void EnvResolution_ProdBuildFlag_TogglesFilter() |
| 70 | + { |
| 71 | + WriteFilter("enabled: !ENV [MKDOCS_PROD_BUILD, false]\nmkdocsignore: true\n"); |
| 72 | + |
| 73 | + Environment.SetEnvironmentVariable("MKDOCS_PROD_BUILD", "false"); |
| 74 | + Assert.False(FileFilterSettings.Load(_root).AppliesMkdocsIgnore(isServe: false)); |
| 75 | + |
| 76 | + Environment.SetEnvironmentVariable("MKDOCS_PROD_BUILD", "true"); |
| 77 | + Assert.True(FileFilterSettings.Load(_root).AppliesMkdocsIgnore(isServe: false)); |
| 78 | + |
| 79 | + Environment.SetEnvironmentVariable("MKDOCS_PROD_BUILD", null); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void LabelFilter_RequiresEnabledAndExcludeTags() |
| 84 | + { |
| 85 | + WriteFilter("enabled: true\nexclude_tag:\n - draft\n"); |
| 86 | + Assert.True(FileFilterSettings.Load(_root).AppliesLabelFilter(isServe: false)); |
| 87 | + |
| 88 | + WriteFilter("enabled: true\n"); // no exclude tags |
| 89 | + Assert.False(FileFilterSettings.Load(_root).AppliesLabelFilter(isServe: false)); |
| 90 | + |
| 91 | + WriteFilter("enabled: false\nexclude_tag:\n - draft\n"); |
| 92 | + Assert.False(FileFilterSettings.Load(_root).AppliesLabelFilter(isServe: false)); |
| 93 | + } |
| 94 | + |
| 95 | + // ---- ContentDiscovery integration ------------------------------------------------------ |
| 96 | + |
| 97 | + private void Seed() |
| 98 | + { |
| 99 | + var docs = Path.Combine(_root, "docs"); |
| 100 | + File.WriteAllText(Path.Combine(docs, "index.md"), "# Home\n"); |
| 101 | + Directory.CreateDirectory(Path.Combine(docs, "teams")); |
| 102 | + File.WriteAllText(Path.Combine(docs, "teams", "index.md"), "# Teams\n"); |
| 103 | + File.WriteAllText(Path.Combine(_root, ".mkdocsignore"), "teams/\n"); |
| 104 | + } |
| 105 | + |
| 106 | + private IReadOnlyList<Page> Discover(bool isProduction) |
| 107 | + { |
| 108 | + var config = new SiteConfig { ProjectRoot = _root, DocsDir = "docs" }; |
| 109 | + var options = new BuildOptions { IsProduction = isProduction, IsServe = false }; |
| 110 | + return new ContentDiscovery(config, options, NullLogger<ContentDiscovery>.Instance).Discover(); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void DevBuild_KeepsMkdocsIgnoredSection_WhenFilterDisabled() |
| 115 | + { |
| 116 | + Seed(); |
| 117 | + WriteFilter("enabled: false\nmkdocsignore: true\n"); |
| 118 | + |
| 119 | + var pages = Discover(isProduction: false); |
| 120 | + |
| 121 | + Assert.Contains(pages, p => p.RelativePath.Replace('\\', '/') == "teams/index.md"); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void ProdBuild_HidesMkdocsIgnoredSection_WhenFilterEnabled() |
| 126 | + { |
| 127 | + Seed(); |
| 128 | + WriteFilter("enabled: true\nmkdocsignore: true\n"); |
| 129 | + |
| 130 | + var pages = Discover(isProduction: true); |
| 131 | + |
| 132 | + Assert.DoesNotContain(pages, p => p.RelativePath.Replace('\\', '/') == "teams/index.md"); |
| 133 | + Assert.Contains(pages, p => p.RelativePath.Replace('\\', '/') == "index.md"); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void NoFilterConfig_HonorsMkdocsIgnore_LikeGitignore() |
| 138 | + { |
| 139 | + Seed(); // no .file-filter.yml |
| 140 | + |
| 141 | + var pages = Discover(isProduction: false); |
| 142 | + |
| 143 | + Assert.DoesNotContain(pages, p => p.RelativePath.Replace('\\', '/') == "teams/index.md"); |
| 144 | + } |
| 145 | +} |
0 commit comments