]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Improve NoSavedPostMapInitTest
authorElectroJr <leonsfriedrich@gmail.com>
Mon, 23 Dec 2024 23:27:33 +0000 (12:27 +1300)
committerElectroJr <leonsfriedrich@gmail.com>
Mon, 23 Dec 2024 23:27:33 +0000 (12:27 +1300)
Content.IntegrationTests/Tests/PostMapInitTest.cs

index 0a48730d54b2c88c1aed3d136c43919e94eb20f3..80e8e7b3182c2b62704c80843cb3dfdcb21cab40 100644 (file)
@@ -18,6 +18,7 @@ using Robust.Shared.Prototypes;
 using Content.Shared.Station.Components;
 using Robust.Shared.EntitySerialization;
 using Robust.Shared.EntitySerialization.Systems;
+using Robust.Shared.IoC;
 using Robust.Shared.Utility;
 using YamlDotNet.RepresentationModel;
 
@@ -157,29 +158,54 @@ namespace Content.IntegrationTests.Tests
             var deps = server.ResolveDependency<IEntitySystemManager>().DependencyCollection;
             foreach (var map in v7Maps)
             {
-                if (!loader.TryReadFile(map, out var data))
-                {
-                    Assert.Fail($"Failed to read {map}");
-                    continue;
-                }
+                Assert.That(IsPreInit(map, loader, deps));
+            }
 
-                var reader = new EntityDeserializer(deps, data, DeserializationOptions.Default);
-                if (!reader.TryProcessData())
-                {
-                    Assert.Fail($"Failed to process {map}");
-                    continue;
-                }
+            // Check that the test actually does manage to catch post-init maps and isn't just blindly passing everything.
+            // To that end, create a new post-init map and try verify it.
+            var mapSys = server.System<SharedMapSystem>();
+            MapId id = default;
+            await server.WaitPost(() => mapSys.CreateMap(out id, runMapInit: false));
+            await server.WaitPost(() => server.EntMan.Spawn(null, new MapCoordinates(0, 0, id)));
 
-                foreach (var mapId in reader.MapYamlIds)
-                {
-                    var mapData = reader.YamlEntities[mapId];
-                    Assert.That(!mapData.PostInit, $"Map {map.Filename} contains a postmapinit map with yaml id: {mapId}");
-                }
-            }
+            // First check that a pre-init version passes
+            var path = new ResPath($"{nameof(NoSavedPostMapInitTest)}.yml");
+            loader.SaveMap(id, path);
+            Assert.That(IsPreInit(path, loader, deps));
+
+            // and the post-init version fails.
+            await server.WaitPost(() => mapSys.InitializeMap(id));
+            loader.SaveMap(id, path);
+            Assert.That(IsPreInit(path, loader, deps), Is.False);
 
             await pair.CleanReturnAsync();
         }
 
+        private bool IsPreInit(ResPath map, MapLoaderSystem loader, IDependencyCollection deps)
+        {
+            if (!loader.TryReadFile(map, out var data))
+            {
+                Assert.Fail($"Failed to read {map}");
+                return false;
+            }
+
+            var reader = new EntityDeserializer(deps, data, DeserializationOptions.Default);
+            if (!reader.TryProcessData())
+            {
+                Assert.Fail($"Failed to process {map}");
+                return false;
+            }
+
+            foreach (var mapId in reader.MapYamlIds)
+            {
+                var mapData = reader.YamlEntities[mapId];
+                if (mapData.PostInit)
+                    return false;
+            }
+
+            return true;
+        }
+
         [Test, TestCaseSource(nameof(GameMaps))]
         public async Task GameMapsLoadableTest(string mapProto)
         {