From 05670d6d7f47c99ad5435c31b9e013f65e682a99 Mon Sep 17 00:00:00 2001
From: Livar <licavalc@microsoft.com>
Date: Fri, 18 Nov 2016 19:28:38 -0800
Subject: [PATCH] Workaround for empty restore.txt file under .tam (#4771)

* For some reason, specially in CI, the restore.txt file under .tam is ending up empty. As a workaround for it, I am going to retry the restore if that happens.

* Change how we diff the existing files in the TAM by using the explicit list from the .txt file.

* Using Nuget ReplaceWithLock utility to get around a concurrency issue on windows.
---
 .../TestAssetInfo.cs                          | 62 ++++++++++++-------
 1 file changed, 38 insertions(+), 24 deletions(-)

diff --git a/src/Microsoft.DotNet.TestFramework/TestAssetInfo.cs b/src/Microsoft.DotNet.TestFramework/TestAssetInfo.cs
index 0a431660a..4fbd935ca 100644
--- a/src/Microsoft.DotNet.TestFramework/TestAssetInfo.cs
+++ b/src/Microsoft.DotNet.TestFramework/TestAssetInfo.cs
@@ -9,6 +9,7 @@ using System.Runtime.CompilerServices;
 using System.Threading.Tasks;
 using Microsoft.DotNet.Cli.Utils;
 using Microsoft.DotNet.PlatformAbstractions;
+using NuGet.Common;
 
 namespace Microsoft.DotNet.TestFramework
 {
@@ -102,24 +103,22 @@ namespace Microsoft.DotNet.TestFramework
 
         private void SaveInventory(FileInfo file, IEnumerable<FileInfo> inventory)
         {
-            StreamWriter writer;
-
-            if (file.Exists)
-            {
-                writer = file.AppendText();
-            }
-            else
-            {
-                writer = file.CreateText();
-            }
-
-            using(writer)
-            {
-                foreach (var path in inventory.Select(i => i.FullName))
+            FileUtility.ReplaceWithLock(
+                filePath =>
                 {
-                    writer.WriteLine(path);
-                }
-            }
+                    using (var stream =
+                        new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
+                    {
+                        using (var writer = new StreamWriter(stream))
+                        {
+                            foreach (var path in inventory.Select(i => i.FullName))
+                            {
+                                writer.WriteLine(path);
+                            }
+                        }
+                    }
+                },
+                file.FullName);
         }
 
         private IEnumerable<FileInfo> GetFileList()
@@ -141,14 +140,31 @@ namespace Microsoft.DotNet.TestFramework
 
         internal IEnumerable<FileInfo> GetBuildFiles()
         {
-            return GetInventory(_inventoryFiles.Build, GetRestoreFiles, DoBuild);
+            return GetInventory(
+                _inventoryFiles.Build,
+                () =>
+                {
+                    var preInventory = new List<FileInfo>(GetRestoreFiles());
+                    preInventory.AddRange(GetSourceFiles());
+                    return preInventory;
+                },
+                DoBuild);
         }
 
-        private IEnumerable<FileInfo> GetInventory(FileInfo file, Func<IEnumerable<FileInfo>> beforeAction, Action action)
+        private IEnumerable<FileInfo> GetInventory(
+            FileInfo file,
+            Func<IEnumerable<FileInfo>> beforeAction,
+            Action action)
         {
+            var inventory = Enumerable.Empty<FileInfo>();
             if (file.Exists)
             {
-                return LoadInventory(file);
+                inventory = LoadInventory(file);
+            }
+
+            if(inventory.Any())
+            {
+                return inventory;
             }
 
             IEnumerable<FileInfo> preInventory;
@@ -159,14 +175,12 @@ namespace Microsoft.DotNet.TestFramework
             }
             else
             {
-                beforeAction();
-
-                preInventory = GetFileList();
+                preInventory = beforeAction();
             }
 
             action();
 
-            var inventory = GetFileList().Where(i => !preInventory.Select(p => p.FullName).Contains(i.FullName));
+            inventory = GetFileList().Where(i => !preInventory.Select(p => p.FullName).Contains(i.FullName));
 
             SaveInventory(file, inventory);