A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
fix(sim): loader reports unreadable files, missing rules start and empty office terms
eyalgolanff05f048331b
diff --git a/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs b/sim/Statecraft.Sim.Tests/ContentLoaderTests.csindex 90e631089..11df83c81 100644--- a/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs+++ b/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs@@ -1,4 +1,6 @@+using System;using System.Collections.Generic;+using System.IO;using Statecraft.Sim;public class ContentLoaderTests@@ -79,4 +81,52 @@ public class ContentLoaderTestsAssert.Equal(file, ex.File);Assert.Equal(field, ex.Field);}+ [Fact]+ public void UnreadableFileIsAContentError()+ {+ if (OperatingSystem.IsWindows()) return; // unix permissions only+ var dir = Fixture.Write();+ var path = Path.Combine(dir, "parties.yaml");+ File.SetUnixFileMode(path, UnixFileMode.None);+ try+ {+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(dir));+ Assert.Equal("parties.yaml", ex.File);+ Assert.Equal("(file)", ex.Field);+ }+ finally { File.SetUnixFileMode(path, UnixFileMode.UserRead | UnixFileMode.UserWrite); }+ }++ [Fact]+ public void LockedFileIsAContentError()+ {+ var dir = Fixture.Write();+ using (new FileStream(Path.Combine(dir, "parties.yaml"), FileMode.Open, FileAccess.ReadWrite, FileShare.None))+ {+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(dir));+ Assert.Equal("parties.yaml", ex.File);+ Assert.Equal("(file)", ex.Field);+ }+ }++ [Fact]+ public void RulesStartIsRequired()+ {+ var bad = Fixture.Files["rules.yaml"].Replace("start: 1948-05-15\n", "");+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = bad })));+ Assert.Equal("rules.yaml", ex.File);+ Assert.Equal("start", ex.Field);+ Assert.EndsWith("required", ex.Message);+ }++ [Theory]+ [InlineData("office:")]+ [InlineData("office: ''")]+ public void OfficeTermNeedsAnOffice(string replacement)+ {+ var bad = Fixture.Files["people.yaml"].Replace("office: finance_minister", replacement);+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["people.yaml"] = bad })));+ Assert.Equal("people.yaml", ex.File);+ Assert.Equal("people[def_two].offices", ex.Field);+ }}diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 762844aa5..49f582118 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -37,6 +37,8 @@ namespace Statecraft.Simif (!File.Exists(path)) throw new ContentException(relPath, "(file)", "missing file");try { return Yaml.Deserialize<T>(File.ReadAllText(path)) ?? throw new ContentException(relPath, "(file)", "empty file"); }catch (YamlException e) { throw new ContentException(relPath, $"line {e.Start.Line}", e.InnerException?.Message ?? e.Message); }+ catch (IOException e) { throw new ContentException(relPath, "(file)", e.Message); }+ catch (UnauthorizedAccessException e) { throw new ContentException(relPath, "(file)", e.Message); }}public static Content Load(string contentRoot)@@ -100,6 +102,7 @@ namespace Statecraft.Simif (p.Party != "" && !parties.Contains(p.Party)) throw new ContentException("people.yaml", $"people[{p.Id}].party", $"unknown party '{p.Party}'");foreach (var t in p.Offices){+ if (string.IsNullOrEmpty(t.Office)) throw new ContentException("people.yaml", $"people[{p.Id}].offices", "office term without an office");if (!offices.Contains(t.Office)) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}", "unknown office");if (t.To.HasValue && t.To.Value <= t.From) throw new ContentException("people.yaml", $"people[{p.Id}].offices.{t.Office}", $"term ends ({t.To:yyyy-MM-dd}) before it starts ({t.From:yyyy-MM-dd})");}@@ -140,6 +143,7 @@ namespace Statecraft.Simforeach (var v in c.VoterGroups)foreach (var k in v.Weights.Keys)if (!indicators.Contains(k)) throw new ContentException("model.yaml", $"voter_groups[{v.Id}].weights.{k}", "unknown indicator");+ if (c.Rules.Start == default) throw new ContentException("rules.yaml", "start", "required");if (c.Rules.RungMinMobilization.Count != 5) throw new ContentException("rules.yaml", "rung_min_mobilization", "needs exactly 5 values (rungs 1-5)");foreach (var k in c.Rules.BudgetStart.Keys)if (!offices.Contains(k)) throw new ContentException("rules.yaml", $"budget_start.{k}", "unknown office");