From 46658f102e08614e927cff7f38f030a6f055e5fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20P=C3=B6tzl?= Date: Wed, 18 Nov 2026 01:07:37 +0100 Subject: [PATCH 1/2] Fix Clear() in CodeCoverageTracker for null/empty module name Clear(string codeModuleName = null) threw a NullReferenceException for the default value. Null or empty now clears all modules. Clear(name) also removes the module from the tracker, so the report no longer lists it. --- .../CodeCoverage/CodeCoverageTracker.cs | 7 +++- ...essCodeLib.AccUnit.Assertions.Tests.csproj | 4 +++ .../CodeCoverageTrackerTests.cs | 34 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 source/Tests/AccUnit.Assertions.Tests/CodeCoverageTrackerTests.cs diff --git a/source/AccUnit/CodeCoverage/CodeCoverageTracker.cs b/source/AccUnit/CodeCoverage/CodeCoverageTracker.cs index d1d5191..79884e3 100644 --- a/source/AccUnit/CodeCoverage/CodeCoverageTracker.cs +++ b/source/AccUnit/CodeCoverage/CodeCoverageTracker.cs @@ -20,9 +20,14 @@ public CodeCoverageTracker(VBProject vbProject) public void Clear(string codeModuleName = null) { - if (codeModuleName.Length > 0) + if (!string.IsNullOrEmpty(codeModuleName)) { RemoveCodeCoverageTracker(codeModuleName); + if (_codeModules.TryGetValue(codeModuleName, out var codeModuleTracker)) + { + codeModuleTracker.Procedures.Clear(); + _codeModules.Remove(codeModuleName); + } } else { diff --git a/source/Tests/AccUnit.Assertions.Tests/AccessCodeLib.AccUnit.Assertions.Tests.csproj b/source/Tests/AccUnit.Assertions.Tests/AccessCodeLib.AccUnit.Assertions.Tests.csproj index 660bc3c..c03c8a2 100644 --- a/source/Tests/AccUnit.Assertions.Tests/AccessCodeLib.AccUnit.Assertions.Tests.csproj +++ b/source/Tests/AccUnit.Assertions.Tests/AccessCodeLib.AccUnit.Assertions.Tests.csproj @@ -105,6 +105,9 @@ prompt + + ..\..\libvbide\Microsoft.Vbe.Interop.dll + ..\..\packages\NUnit.3.13.3\lib\net45\nunit.framework.dll @@ -114,6 +117,7 @@ + diff --git a/source/Tests/AccUnit.Assertions.Tests/CodeCoverageTrackerTests.cs b/source/Tests/AccUnit.Assertions.Tests/CodeCoverageTrackerTests.cs new file mode 100644 index 0000000..20e99aa --- /dev/null +++ b/source/Tests/AccUnit.Assertions.Tests/CodeCoverageTrackerTests.cs @@ -0,0 +1,34 @@ +using AccessCodeLib.AccUnit.CodeCoverage; +using NUnit.Framework; + +namespace AccessCodeLib.AccUnit.Assertions.Tests +{ + public class CodeCoverageTrackerTests + { + // An empty tracker needs no VBProject: nothing has to be removed from a code module. + + [Test] + public void Clear_WithoutArgument_ClearsAllModulesWithoutException() + { + var tracker = new CodeCoverageTracker(null); + + Assert.DoesNotThrow(() => tracker.Clear()); + } + + [Test] + public void Clear_WithNull_ClearsAllModulesWithoutException() + { + var tracker = new CodeCoverageTracker(null); + + Assert.DoesNotThrow(() => tracker.Clear(null)); + } + + [Test] + public void Clear_WithEmptyString_ClearsAllModulesWithoutException() + { + var tracker = new CodeCoverageTracker(null); + + Assert.DoesNotThrow(() => tracker.Clear("")); + } + } +} From abfb07efcc8d047e552f580f43b1e78bde1c8452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20P=C3=B6tzl?= Date: Wed, 18 Nov 2026 01:07:37 +0100 Subject: [PATCH 2/2] Throw instead of returning null when test suite init fails TestSuiteManager.InitTestSuite only showed the exception and returned a null suite, so VBA failed later with a meaningless message. The exception is now logged and rethrown as InvalidOperationException with the original message; UI callers already show exceptions themselves. TestSuiteInitialized is raised only on success. --- .../Properties/AssemblyInfo.cs | 2 ++ .../TestExplorer/TestListAndResultManager.cs | 9 ++++++- .../AccUnit.VbeAddIn/TestSuiteManager.cs | 11 ++++---- .../TestSuiteManagerTests.cs | 25 +++++++++++++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 vbe-add-In/Tests/AccUnit.VbeAddIn.Tests/TestSuiteManagerTests.cs diff --git a/vbe-add-In/AccUnit.VbeAddIn/Properties/AssemblyInfo.cs b/vbe-add-In/AccUnit.VbeAddIn/Properties/AssemblyInfo.cs index fd11951..e6c478d 100644 --- a/vbe-add-In/AccUnit.VbeAddIn/Properties/AssemblyInfo.cs +++ b/vbe-add-In/AccUnit.VbeAddIn/Properties/AssemblyInfo.cs @@ -1,4 +1,5 @@ using System.Reflection; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // Allgemeine Informationen über eine Assembly werden über die folgenden @@ -16,6 +17,7 @@ // Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly // für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von // COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: InternalsVisibleTo("AccUnit.VbeAddIn.Tests")] [assembly: ComVisible(false)] // Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird diff --git a/vbe-add-In/AccUnit.VbeAddIn/TestExplorer/TestListAndResultManager.cs b/vbe-add-In/AccUnit.VbeAddIn/TestExplorer/TestListAndResultManager.cs index 4a36963..2cfe321 100644 --- a/vbe-add-In/AccUnit.VbeAddIn/TestExplorer/TestListAndResultManager.cs +++ b/vbe-add-In/AccUnit.VbeAddIn/TestExplorer/TestListAndResultManager.cs @@ -79,7 +79,14 @@ public VbideUserControl TestListAndResultWindow { _testListAndResultVbideControl = new VbideUserControl(AddIn, Resources.UserControls.TestResultUserControlInfoCaption, TestListAndResultViewUserControlInfo.PositionGuid); _testListAndResultView = _testListAndResultVbideControl.Control; - _testListAndResultView.RunBreakOnErrorMenuItemEnabled = (TestSuiteManager.TestSuite is AccessTestSuite); + try + { + _testListAndResultView.RunBreakOnErrorMenuItemEnabled = (TestSuiteManager.TestSuite is AccessTestSuite); + } + catch (Exception ex) + { + UITools.ShowException(ex); + } _testListAndResultView.RunTests += TestListAndResultViewRunTests; _testListAndResultView.ShowSourceCodeInvoked += TestListAndResultViewShowSourceCodeInvoked; diff --git a/vbe-add-In/AccUnit.VbeAddIn/TestSuiteManager.cs b/vbe-add-In/AccUnit.VbeAddIn/TestSuiteManager.cs index 4caa2c2..b0e5638 100644 --- a/vbe-add-In/AccUnit.VbeAddIn/TestSuiteManager.cs +++ b/vbe-add-In/AccUnit.VbeAddIn/TestSuiteManager.cs @@ -61,14 +61,15 @@ private void InitTestSuite() try { _vbaTestSuite = CreateVbaTestSuite(OfficeApplicationHelper); + TestSuiteInitialized?.Invoke(_vbaTestSuite); } catch (Exception ex) { - UITools.ShowException(ex); - } - finally - { - TestSuiteInitialized?.Invoke(_vbaTestSuite); + // Do not hand out a silent null: VBA would fail later with a meaningless message. + // The exception reaches VBA as COM error (Err.Description contains the message); + // UI callers show it themselves. + Logger.Log(ex); + throw new InvalidOperationException("AccUnit test suite could not be initialized: " + ex.Message, ex); } } } diff --git a/vbe-add-In/Tests/AccUnit.VbeAddIn.Tests/TestSuiteManagerTests.cs b/vbe-add-In/Tests/AccUnit.VbeAddIn.Tests/TestSuiteManagerTests.cs new file mode 100644 index 0000000..ffb0b23 --- /dev/null +++ b/vbe-add-In/Tests/AccUnit.VbeAddIn.Tests/TestSuiteManagerTests.cs @@ -0,0 +1,25 @@ +using NUnit.Framework; +using System; + +namespace AccessCodeLib.AccUnit.VbeAddIn.Tests +{ + public class TestSuiteManagerTests + { + [Test] + public void TestSuite_WhenInitializationFails_ThrowsWithOriginalMessageInsteadOfReturningNull() + { + using (var manager = new TestSuiteManager()) + { + manager.TestResultReporterRequest += (ref System.Collections.Generic.IList reporters) => + throw new InvalidOperationException("reporter failure"); + // no OfficeApplicationHelper => the suite cannot be created + + var exception = Assert.Throws(() => { var _ = manager.TestSuite; }); + + Assert.That(exception.Message, Does.StartWith("AccUnit test suite could not be initialized")); + Assert.That(exception.InnerException, Is.Not.Null); + Assert.That(exception.Message, Does.Contain(exception.InnerException.Message)); + } + } + } +}