@@ -15,15 +15,13 @@ namespace Dibix.Testing
1515 internal sealed class TestRunTestResultFileComposer : TestResultFileComposer
1616 {
1717 private static readonly ConcurrentDictionary < string , Lazy < TestRunTestResultFileComposer > > Cache = new ConcurrentDictionary < string , Lazy < TestRunTestResultFileComposer > > ( ) ;
18- private readonly TestContext _testContext ;
1918 private readonly string _defaultRunDirectory ;
2019 private readonly bool _useDedicatedTestResultsDirectory ;
2120 private readonly ICollection < string > _deployedFiles ;
2221 private readonly ICollection < string > _registeredFileNames ;
2322
24- private TestRunTestResultFileComposer ( string directory , TestContext testContext , string defaultRunDirectory , bool useDedicatedTestResultsDirectory ) : base ( directory , testContext )
23+ private TestRunTestResultFileComposer ( string directory , string defaultRunDirectory , bool useDedicatedTestResultsDirectory ) : base ( directory )
2524 {
26- _testContext = testContext ;
2725 _defaultRunDirectory = defaultRunDirectory ;
2826 _deployedFiles = new HashSet < string > ( ) ;
2927 _useDedicatedTestResultsDirectory = useDedicatedTestResultsDirectory ;
@@ -36,10 +34,13 @@ public static TestRunTestResultFileComposer Resolve(TestContext testContext, boo
3634 string testRunIdentifier = testContext . TestRunDirectory ;
3735
3836 // Use Lazy<T> to ensure the test run attachments are only written to disk once when running tests in parallel
39- TestRunTestResultFileComposer instance = Cache . GetOrAdd ( testRunIdentifier , _ => new Lazy < TestRunTestResultFileComposer > ( ( ) => Create ( testContext , useDedicatedTestResultsDirectory ) ) ) . Value ;
37+ Lazy < TestRunTestResultFileComposer > value = Cache . GetOrAdd ( testRunIdentifier , _ => new Lazy < TestRunTestResultFileComposer > ( ( ) => Create ( testContext , useDedicatedTestResultsDirectory ) ) ) ;
38+ bool isFirstTest = ! value . IsValueCreated ;
39+ TestRunTestResultFileComposer instance = value . Value ;
4040
4141 // Make test run attachments available for each test method
42- instance . ImportResultFilesIfNecessary ( testContext ) ;
42+ if ( ! isFirstTest )
43+ instance . ImportResultFiles ( testContext ) ;
4344
4445 return instance ;
4546 }
@@ -56,28 +57,25 @@ public void CopyTestOutput(IEnumerable<string> resultFiles)
5657
5758 protected override void OnFileNameRegistered ( string fileName ) => _registeredFileNames . Add ( fileName ) ;
5859
59- private void Initialize ( )
60+ private void Initialize ( TestContext testContext )
6061 {
61- EnsureTestContextDump ( ) ;
62- EnsureEnvironmentDump ( ) ;
62+ EnsureTestContextDump ( testContext ) ;
63+ EnsureEnvironmentDump ( testContext ) ;
6364 }
6465
65- private void ImportResultFilesIfNecessary ( TestContext currentTestContext )
66+ private void ImportResultFiles ( TestContext testContext )
6667 {
67- if ( currentTestContext == _testContext )
68- return ;
69-
7068 foreach ( string resultFile in ResultFiles )
71- currentTestContext . AddResultFile ( resultFile ) ;
69+ testContext . AddResultFile ( resultFile ) ;
7270 }
7371
74- private void EnsureTestContextDump ( ) => AddResultFile ( "TestContext.json" , JsonConvert . SerializeObject ( _testContext , new JsonSerializerSettings
72+ private void EnsureTestContextDump ( TestContext testContext ) => AddResultFile ( "TestContext.json" , JsonConvert . SerializeObject ( testContext , new JsonSerializerSettings
7573 {
7674 Formatting = Formatting . Indented ,
7775 ContractResolver = new TestContextContractResolver ( )
78- } ) ) ;
76+ } ) , testContext ) ;
7977
80- private void EnsureEnvironmentDump ( )
78+ private void EnsureEnvironmentDump ( TestContext testContext )
8179 {
8280 IDictionary < string , object > environmentVariables = Environment . GetEnvironmentVariables ( )
8381 . Cast < DictionaryEntry > ( )
@@ -94,7 +92,7 @@ private void EnsureEnvironmentDump()
9492 }
9593
9694 string environment = sb . ToString ( ) ;
97- AddResultFile ( "Environment.txt" , environment ) ;
95+ AddResultFile ( "Environment.txt" , environment , testContext ) ;
9896 }
9997
10098 private void CopyFiles ( IEnumerable < string > files , string targetDirectory )
@@ -120,8 +118,8 @@ private static TestRunTestResultFileComposer Create(TestContext testContext, boo
120118 {
121119 string defaultRunDirectory = testContext . TestRunResultsDirectory ;
122120 string runDirectory = useDedicatedTestResultsDirectory ? CreateTestRunDirectory ( testContext ) : defaultRunDirectory ;
123- TestRunTestResultFileComposer instance = new TestRunTestResultFileComposer ( runDirectory , testContext , defaultRunDirectory , useDedicatedTestResultsDirectory ) ;
124- instance . Initialize ( ) ;
121+ TestRunTestResultFileComposer instance = new TestRunTestResultFileComposer ( runDirectory , defaultRunDirectory , useDedicatedTestResultsDirectory ) ;
122+ instance . Initialize ( testContext ) ;
125123 return instance ;
126124 }
127125
0 commit comments