From d469a12ba9e4e7aa33760dd08ec64dd058a980ce Mon Sep 17 00:00:00 2001 From: Mario Toffia Date: Thu, 23 Jun 2022 17:01:23 +0200 Subject: [PATCH] fixes issue #257 --- .../FluentDockerTestBase.cs | 41 ++++++++++++++++++- .../FluentDockerTestBase.cs | 41 ++++++++++++++++++- global.json | 2 +- 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs b/Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs index 968539c4..66f81adc 100644 --- a/Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs +++ b/Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs @@ -14,13 +14,20 @@ public abstract class FluentDockerTestBase [TestInitialize] public void Initialize() { - Container = Build().Build(); + var containerBuilder = Build(); + + this.OnBeforeContainerBuild(containerBuilder); + + Container = containerBuilder.Build(); + try { + this.OnBeforeContainerStart(); Container.Start(); } - catch + catch (Exception ex) { + this.OnBeforeDispose(Container, ex); Container.Dispose(); throw; } @@ -37,6 +44,7 @@ public void TeardownContainer() Container = null; try { + this.OnBeforeDispose(c, null); c?.Dispose(); } catch @@ -52,6 +60,35 @@ protected virtual void OnContainerTearDown() { } + /// + /// Invoked just before the container is built. + /// + /// The that is about to be built. + protected virtual void OnBeforeContainerBuild(ContainerBuilder containerBuilder) + { + } + + /// + /// Invoked just after the container is built and before starting it. + /// + protected virtual void OnBeforeContainerStart() + { + } + + /// + /// Invoked just before the container is ed. + /// + /// The that is about to be disposed. + /// The that caused the container to be disposed (when starting up). + /// + /// This method is invoked either when the container fails to start. In such situation the + /// is not null. It is also called when the test is cleaning up and thus the + /// is null. The is always set since the teardown + /// will clear the field. Note that the may still be null! + /// + protected virtual void OnBeforeDispose(IContainerService container, Exception throwable) + { + } /// /// Invoked after a container has been created and started. /// diff --git a/Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs b/Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs index 543585eb..5f7da3ba 100644 --- a/Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs +++ b/Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs @@ -12,13 +12,20 @@ public abstract class FluentDockerTestBase : IDisposable public FluentDockerTestBase() { - Container = Build().Build(); + var containerBuilder = Build(); + + this.OnBeforeContainerBuild(containerBuilder); + + Container = containerBuilder.Build(); + try { + this.OnBeforeContainerStart(); Container.Start(); } - catch + catch (Exception ex) { + this.OnBeforeDispose(Container, ex); Container.Dispose(); throw; } @@ -34,6 +41,7 @@ public void Dispose() Container = null; try { + this.OnBeforeDispose(c, null); c?.Dispose(); } catch @@ -49,6 +57,35 @@ protected virtual void OnContainerTearDown() { } + /// + /// Invoked just before the container is built. + /// + /// The that is about to be built. + protected virtual void OnBeforeContainerBuild(ContainerBuilder containerBuilder) + { + } + + /// + /// Invoked just after the container is built and before starting it. + /// + protected virtual void OnBeforeContainerStart() + { + } + + /// + /// Invoked just before the container is ed. + /// + /// The that is about to be disposed. + /// The that caused the container to be disposed (when starting up). + /// + /// This method is invoked either when the container fails to start. In such situation the + /// is not null. It is also called when the test is cleaning up and thus the + /// is null. The is always set since the teardown + /// will clear the field. Note that the may still be null! + /// + protected virtual void OnBeforeDispose(IContainerService container, Exception throwable) + { + } /// /// Invoked after a container has been created and started. /// diff --git a/global.json b/global.json index e7d5479b..13b8b145 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/global", "sdk": { - "version": "6.0.100", + "version": "5.0.200", "allowPrerelease": false, "rollForward": "latestMinor" }