|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using Xunit; |
| 4 | +using ImperatorToCK3.CommonUtils; |
| 5 | +using commonItems.Mods; |
| 6 | +using ImperatorToCK3.Imperator; |
| 7 | +using commonItems.Exceptions; |
| 8 | + |
| 9 | +namespace ImperatorToCK3.UnitTests.CommonUtils { |
| 10 | + public class FileHelperTests : IDisposable { |
| 11 | + private readonly string tempRoot; |
| 12 | + |
| 13 | + public FileHelperTests() { |
| 14 | + tempRoot = Path.Combine(Path.GetTempPath(), "IRToCK3Tests", Guid.NewGuid().ToString()); |
| 15 | + Directory.CreateDirectory(tempRoot); |
| 16 | + } |
| 17 | + |
| 18 | + public void Dispose() { |
| 19 | + try { |
| 20 | + Directory.Delete(tempRoot, recursive: true); |
| 21 | + } catch { /* best effort cleanup */ } |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void EnsureDirectoryExists_createsMissingPath() { |
| 26 | + var target = Path.Combine(tempRoot, "a", "b", "c"); |
| 27 | + Assert.False(Directory.Exists(target)); |
| 28 | + |
| 29 | + FileHelper.EnsureDirectoryExists(target); |
| 30 | + |
| 31 | + Assert.True(Directory.Exists(target)); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void EnsureDirectoryExists_throwsWhenFileCollision() { |
| 36 | + var target = Path.Combine(tempRoot, "collisionDir"); |
| 37 | + Directory.CreateDirectory(Path.GetDirectoryName(target)!); |
| 38 | + File.WriteAllText(target, "oops"); |
| 39 | + Assert.True(File.Exists(target)); |
| 40 | + |
| 41 | + var ex = Assert.Throws<UserErrorException>(() => FileHelper.EnsureDirectoryExists(target)); |
| 42 | + Assert.Contains("directory", ex.Message, StringComparison.OrdinalIgnoreCase); |
| 43 | + |
| 44 | + // original file must remain untouched |
| 45 | + Assert.True(File.Exists(target)); |
| 46 | + Assert.False(Directory.Exists(target)); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void OutputGuiContainer_handlesFileInPlaceOfGuiDirectory() { |
| 51 | + // prepare a fake Imperator installation with one GUI file |
| 52 | + var gameRoot = Path.Combine(tempRoot, "game"); |
| 53 | + var guiDir = Path.Combine(gameRoot, "gui"); |
| 54 | + Directory.CreateDirectory(guiDir); |
| 55 | + var topbar = Path.Combine(guiDir, "ingame_topbar.gui"); |
| 56 | + File.WriteAllText(topbar, "foo"); |
| 57 | + |
| 58 | + var modFS = new ModFilesystem(gameRoot, []); |
| 59 | + |
| 60 | + // configuration points to a separate doc path; create a collision file |
| 61 | + var docPath = Path.Combine(tempRoot, "docs"); |
| 62 | + var config = new Configuration { |
| 63 | + ImperatorDocPath = docPath |
| 64 | + }; |
| 65 | + Directory.CreateDirectory(docPath); |
| 66 | + |
| 67 | + var collisionFile = Path.Combine(docPath, "mod", "coa_export_mod", "gui"); |
| 68 | + Directory.CreateDirectory(Path.GetDirectoryName(collisionFile)!); |
| 69 | + File.WriteAllText(collisionFile, "not a directory"); |
| 70 | + |
| 71 | + // run the helper; it should not crash but will bail out early |
| 72 | + World.OutputGuiContainer(modFS, [], config); |
| 73 | + |
| 74 | + // collision file remains and no directory has been created |
| 75 | + var expectedGuiDir = Path.Combine(docPath, "mod", "coa_export_mod", "gui"); |
| 76 | + Assert.False(Directory.Exists(expectedGuiDir)); |
| 77 | + Assert.True(File.Exists(collisionFile)); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments