Skip to content

Commit

Permalink
Merge pull request #3 from Cyberboss/CodeCoverage
Browse files Browse the repository at this point in the history
Code coverage
  • Loading branch information
Cyberboss authored Nov 14, 2023
2 parents 0d00d8b + 9239f08 commit af9d910
Show file tree
Hide file tree
Showing 7 changed files with 846 additions and 12 deletions.
15 changes: 13 additions & 2 deletions Content.Tests/DMTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NUnit.Framework;
using OpenDreamRuntime;
using OpenDreamRuntime.Objects;
using OpenDreamRuntime.Procs.DebugAdapter;
using OpenDreamRuntime.Rendering;
using OpenDreamShared.Rendering;
using Robust.Shared.Asynchronous;
Expand All @@ -29,6 +30,7 @@ public sealed class DMTests : ContentUnitTest {
[Dependency] private readonly DreamManager _dreamMan = default!;
[Dependency] private readonly DreamObjectTree _objectTree = default!;
[Dependency] private readonly ITaskManager _taskManager = default!;
[Dependency] private readonly IDreamDebugManager _debugManager = default!;

[Flags]
public enum DMTestFlags {
Expand Down Expand Up @@ -71,7 +73,7 @@ private static void Cleanup(string? compiledFile) {
}

[Test, TestCaseSource(nameof(GetTests))]
public void TestFiles(string sourceFile, DMTestFlags testFlags) {
public void TestFiles(string sourceFile, string coverageFile, DMTestFlags testFlags) {
string initialDirectory = Directory.GetCurrentDirectory();
TestContext.WriteLine($"--- TEST {sourceFile} | Flags: {testFlags}");
try {
Expand All @@ -85,10 +87,15 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags) {

Assert.IsTrue(compiledFile is not null && File.Exists(compiledFile), "Failed to compile DM source file");
Assert.IsTrue(_dreamMan.LoadJson(compiledFile), $"Failed to load {compiledFile}");

_debugManager.InitializeCoverage(coverageFile);

_dreamMan.StartWorld();

(bool successfulRun, DreamValue? returned, Exception? exception) = RunTest();

_debugManager.Shutdown();

if (testFlags.HasFlag(DMTestFlags.NoReturn)) {
Assert.IsFalse(returned.HasValue, "proc returned unexpectedly");
} else {
Expand Down Expand Up @@ -137,8 +144,11 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags) {
var watch = new Stopwatch();
watch.Start();

// hack to hopefully force spawned calls to finish
var iterationsRemaining = 100;

// Tick until our inner call has finished
while (!callTask.IsCompleted) {
while (!callTask.IsCompleted || iterationsRemaining-- > 0) {
_dreamMan.Update();
_taskManager.ProcessPendingTasks();

Expand All @@ -163,6 +173,7 @@ private static IEnumerable<object[]> GetTests()

yield return new object[] {
sourceFile2,
Path.GetFullPath($"{sourceFile[..^2]}coverage.xml"),
testFlags
};
}
Expand Down
7 changes: 6 additions & 1 deletion OpenDreamRuntime/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,18 @@ public override void Init() {
public override void PostInit() {
_commandSystem = _entitySystemManager.GetEntitySystem<DreamCommandSystem>();

var codeCoverageOutputFile = _configManager.GetCVar(OpenDreamCVars.CodeCoverage);
if (!String.IsNullOrWhiteSpace(codeCoverageOutputFile)) {
_debugManager.InitializeCoverage(codeCoverageOutputFile);
}

int debugAdapterPort = _configManager.GetCVar(OpenDreamCVars.DebugAdapterLaunched);
if (debugAdapterPort == 0) {
_dreamManager.PreInitialize(_configManager.GetCVar<string>(OpenDreamCVars.JsonPath));
_dreamManager.StartWorld();
} else {
// The debug manager is responsible for running _dreamManager.PreInitialize() and .StartWorld()
_debugManager.Initialize(debugAdapterPort);
_debugManager.InitializeDebugging(debugAdapterPort);
}
}

Expand Down
13 changes: 7 additions & 6 deletions OpenDreamRuntime/Objects/DreamObjectTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,20 @@ public void LoadJson(DreamCompiledJson json) {

Strings = json.Strings ?? new();

if (json.GlobalInitProc is { } initProcDef) {
GlobalInitProc = new DMProc(0, DreamPath.Root, initProcDef, "<global init>", _dreamManager, _atomManager, _dreamMapManager, _dreamDebugManager, _dreamResourceManager, this, _procScheduler);
} else {
GlobalInitProc = null;
}

var types = json.Types ?? Array.Empty<DreamTypeJson>();
var procs = json.Procs;
var globalProcs = json.GlobalProcs;

// Load procs first so types can set their init proc's super proc
LoadProcsFromJson(types, procs, globalProcs);
LoadTypesFromJson(types);

if (json.GlobalInitProc is { } initProcDef) {
GlobalInitProc = new DMProc(Procs.Count, DreamPath.Root, initProcDef, "<global init>", _dreamManager, _atomManager, _dreamMapManager, _dreamDebugManager, _dreamResourceManager, this, _procScheduler);
Procs.Add(GlobalInitProc);
} else {
GlobalInitProc = null;
}
}

public TreeEntry GetTreeEntry(DreamPath path) {
Expand Down
Loading

0 comments on commit af9d910

Please sign in to comment.