|
| 1 | +using Packer.Core.Model; |
| 2 | +using Packer.Core.Model.Abstract; |
| 3 | +using Packer.Core.Model.Configuration; |
| 4 | +using Packer.Core.Model.PackerPolicys; |
| 5 | + |
| 6 | +namespace Packer.Core.Tests.IntegrationTests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// 使用 projects/packer-example 的数据做真实集成测试。 |
| 10 | +/// 在临时目录重建项目结构(CWD 已切换到临时项目根),跑完整打包流程。 |
| 11 | +/// </summary> |
| 12 | +public class PackerExampleTests : IDisposable |
| 13 | +{ |
| 14 | + private readonly string _originalDir = Environment.CurrentDirectory; |
| 15 | + private readonly TempDir _tmp = new(); |
| 16 | + |
| 17 | + public PackerExampleTests() |
| 18 | + { |
| 19 | + Environment.CurrentDirectory = _tmp.Path; |
| 20 | + } |
| 21 | + |
| 22 | + public void Dispose() |
| 23 | + { |
| 24 | + Environment.CurrentDirectory = _originalDir; |
| 25 | + _tmp.Dispose(); |
| 26 | + } |
| 27 | + |
| 28 | + // 从 AppContext.BaseDirectory(bin/Debug/net10.0/)回溯到仓库根目录 |
| 29 | + static readonly string _exampleRoot = Path.GetFullPath( |
| 30 | + Path.Combine(AppContext.BaseDirectory, |
| 31 | + "..", "..", "..", "..", "..", |
| 32 | + "projects", "packer-example", "assets")); |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void DirectPolicy_IncludesZhCn_ExcludesEnUs() |
| 36 | + { |
| 37 | + CreateProject("example-direct"); |
| 38 | + var config = LoadTestConfig(); |
| 39 | + var ns = GetNamespace("example-direct", "direct"); |
| 40 | + |
| 41 | + var providers = ns.PackerPolicies |
| 42 | + .CreateProviders(ns, config) |
| 43 | + .ToList(); |
| 44 | + |
| 45 | + Assert.Contains(providers, p => p.Destination.EndsWith("direct/lang/zh_cn.json")); |
| 46 | + Assert.DoesNotContain(providers, p => p.Destination.EndsWith("direct/lang/en_us.json")); |
| 47 | + Assert.DoesNotContain(providers, p => p.Destination.EndsWith("direct/other.txt")); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void IndirectPolicy_RewritesNamespace() |
| 52 | + { |
| 53 | + CreateProject("example-direct", "example-indirect"); |
| 54 | + var config = LoadTestConfig(); |
| 55 | + var ns = GetNamespace("example-indirect", "indirect"); |
| 56 | + |
| 57 | + var providers = ns.PackerPolicies |
| 58 | + .CreateProviders(ns, config) |
| 59 | + .ToList(); |
| 60 | + |
| 61 | + Assert.NotEmpty(providers); |
| 62 | + foreach (var p in providers) |
| 63 | + Assert.Contains("indirect", p.Destination); |
| 64 | + Assert.Contains(providers, p => p.Destination.EndsWith("lang/zh_cn.json")); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void CompositePolicy_MergesMultipleSources() |
| 69 | + { |
| 70 | + CreateProject("example-direct", "example-indirect", "example-composition", "example-composite"); |
| 71 | + var config = LoadTestConfig(); |
| 72 | + var ns = GetNamespace("example-composite", "composite"); |
| 73 | + |
| 74 | + var providers = ns.PackerPolicies |
| 75 | + .CreateProviders(ns, config) |
| 76 | + .ToList(); |
| 77 | + |
| 78 | + var zhCn = providers.Where(p => p.Destination.Contains("composite") && p.Destination.EndsWith("lang/zh_cn.json")) |
| 79 | + .Cast<KVPFile>().ToList(); |
| 80 | + Assert.Equal(2, zhCn.Count); |
| 81 | + |
| 82 | + // 第一个来自 composite 自身(Direct),key3 被覆盖为新值 |
| 83 | + // 第二个来自 indirect(→direct),包含 direct 的全部 18 个 key |
| 84 | + var compositeEntries = zhCn[0].Entries; |
| 85 | + var directEntries = zhCn[1].Entries; |
| 86 | + Assert.Equal("new value3", compositeEntries["key3"]); |
| 87 | + Assert.Equal("value1", directEntries["key1"]); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void CompositionPolicy_ExpandsCartesianProduct() |
| 92 | + { |
| 93 | + CreateProject("example-composition"); |
| 94 | + var config = LoadTestConfig(); |
| 95 | + var ns = GetNamespace("example-composition", "composition"); |
| 96 | + |
| 97 | + var providers = ns.PackerPolicies |
| 98 | + .CreateProviders(ns, config) |
| 99 | + .ToList(); |
| 100 | + |
| 101 | + Assert.Single(providers); |
| 102 | + var provider = providers[0] as KVPFile; |
| 103 | + Assert.NotNull(provider); |
| 104 | + |
| 105 | + Assert.Equal(200, provider.Entries.Count); |
| 106 | + Assert.Equal("value_0_00", provider.Entries["key_0_00"]); |
| 107 | + Assert.Equal("altvalue_9_09", provider.Entries["altkey_9_09"]); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void FullPipeline_ProducesAllExpectedDestinations() |
| 112 | + { |
| 113 | + CreateProject("example-direct", "example-indirect", "example-composite", "example-composition"); |
| 114 | + var config = LoadTestConfig(); |
| 115 | + |
| 116 | + var mods = new[] { "example-direct", "example-indirect", "example-composite", "example-composition" }; |
| 117 | + var allNamespaces = mods.Select(m => GetNamespace(m, m.Split('-').Last())); |
| 118 | + |
| 119 | + var allProviders = allNamespaces |
| 120 | + .SelectMany(ns => ns.PackerPolicies.CreateProviders(ns, config)) |
| 121 | + .ToLookup(p => p.Destination); |
| 122 | + |
| 123 | + var dests = allProviders.Select(g => g.Key).ToList(); |
| 124 | + Assert.Contains(dests, d => d.Contains("direct/lang/zh_cn.json")); |
| 125 | + Assert.Contains(dests, d => d.Contains("indirect/lang/zh_cn.json")); |
| 126 | + Assert.Contains(dests, d => d.Contains("composite/lang/zh_cn.json")); |
| 127 | + Assert.Contains(dests, d => d.Contains("composition/lang/zh_cn.json")); |
| 128 | + } |
| 129 | + |
| 130 | + // ── 辅助方法 ── |
| 131 | + |
| 132 | + private void CreateProject(params string[] modNames) |
| 133 | + { |
| 134 | + foreach (var mod in modNames) |
| 135 | + { |
| 136 | + var src = Path.Combine(_exampleRoot, mod); |
| 137 | + foreach (var nsDir in Directory.EnumerateDirectories(src)) |
| 138 | + { |
| 139 | + var ns = Path.GetFileName(nsDir); |
| 140 | + var dest = Path.Combine(_tmp.Path, "projects", "assets", mod, "packer-example", ns); |
| 141 | + CopyDirectory(nsDir, dest); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private static Config LoadTestConfig() |
| 147 | + { |
| 148 | + return new Config( |
| 149 | + new BaseConfig( |
| 150 | + Version: "packer-example", |
| 151 | + TargetLanguages: ["zh_cn"], |
| 152 | + McMetaTemplate: "", |
| 153 | + McMetaParameters: [], |
| 154 | + ReadmeTemplate: "", |
| 155 | + ReadmeParameters: [], |
| 156 | + ExclusionMods: [], |
| 157 | + ExclusionNamespaces: [] |
| 158 | + ), |
| 159 | + new FloatingConfig( |
| 160 | + InclusionDomains: [], |
| 161 | + ExclusionDomains: [], |
| 162 | + ExclusionPaths: ["local-config.json", "packer-policy.json"], |
| 163 | + InclusionPaths: [], |
| 164 | + CharacterReplacement: new Dictionary<string, string> |
| 165 | + { |
| 166 | + ["REPLACEMENT"] = "被替换的内容" |
| 167 | + }, |
| 168 | + DestinationReplacement: new Dictionary<string, string>() |
| 169 | + ) |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + private INamespaceResource GetNamespace(string modName, string nsName) |
| 174 | + { |
| 175 | + var nsDir = Path.Combine(_tmp.Path, "projects", "assets", modName, "packer-example", nsName); |
| 176 | + return new AssetsNamespaceResource(nsDir, nsName, modName, "packer-example"); |
| 177 | + } |
| 178 | + |
| 179 | + private static void CopyDirectory(string src, string dest) |
| 180 | + { |
| 181 | + Directory.CreateDirectory(dest); |
| 182 | + foreach (var file in Directory.EnumerateFiles(src, "*", SearchOption.AllDirectories)) |
| 183 | + { |
| 184 | + var relative = Path.GetRelativePath(src, file); |
| 185 | + var target = Path.Combine(dest, relative); |
| 186 | + Directory.CreateDirectory(Path.GetDirectoryName(target)!); |
| 187 | + File.Copy(file, target); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments