<commit_before>// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.TestFramework.Assertions;
using Microsoft.DotNet.TestFramework.Commands;
using static Microsoft.DotNet.TestFramework.Commands.MSBuildTest;
using Newtonsoft.Json.Linq;

namespace Microsoft.DotNet.TestFramework
{
    public class TestAsset : TestDirectory
    {
        // made tolower because the rest of the class works with normalized tolower strings
        private static readonly IEnumerable<string> BuildArtifactBlackList = new List<string>()
        {
          ".IncrementalCache",
          ".SDKVersion"
        }.Select(s => s.ToLower()).ToArray();

        private readonly string _testAssetRoot;
        private readonly string _buildVersion;

        public string TestRoot => Path;

        internal TestAsset(string testAssetRoot, string testDestination, string buildVersion) : base(testDestination)
        {
            if (string.IsNullOrEmpty(testAssetRoot))
            {
                throw new ArgumentException("testAssetRoot");
            }

            _testAssetRoot = testAssetRoot;
            _buildVersion = buildVersion;
        }

        public TestAsset WithSource()
        {
            var sourceDirs = Directory.GetDirectories(_testAssetRoot, "*", SearchOption.AllDirectories)
              .Where(dir => !IsBinOrObjFolder(dir));

            foreach (string sourceDir in sourceDirs)
            {
                Directory.CreateDirectory(sourceDir.Replace(_testAssetRoot, Path));
            }

            var sourceFiles = Directory.GetFiles(_testAssetRoot, "*.*", SearchOption.AllDirectories)
                                  .Where(file =>
                                  {
                                      return !IsLockFile(file) && !IsInBinOrObjFolder(file);
                                  });

            foreach (string srcFile in sourceFiles)
            {
                string destFile = srcFile.Replace(_testAssetRoot, Path);
                // For project.json, we need to replace the version of the Microsoft.DotNet.Core.Sdk with the actual build version
                if (srcFile.EndsWith("project.json"))
                {
                    var projectJson = JObject.Parse(File.ReadAllText(srcFile));
                    var dependencies = (JObject)projectJson.Property("dependencies").Value;
                    var coreSdkDependency = dependencies.Property("Microsoft.DotNet.Core.Sdk");
                    coreSdkDependency.Value = _buildVersion;
                    File.WriteAllText(destFile, projectJson.ToString());
                }
                else
                {
                    File.Copy(srcFile, destFile, true);
                }
            }

            return this;
        }

        public TestAsset Restore(params string[] args)
        {
            var restoreCommand = new RestoreCommand(Stage0MSBuild, TestRoot);
            var commandResult = restoreCommand.Execute(args);

            commandResult.Should().Pass();

            return this;
        }

        private bool IsLockFile(string file)<commit_msg>Added null checks to make sure those who don't have the Microsoft.DotNet.Core.Sdk dependency don't cause a null pointer exception<commit_after>// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.TestFramework.Assertions;
using Microsoft.DotNet.TestFramework.Commands;
using static Microsoft.DotNet.TestFramework.Commands.MSBuildTest;
using Newtonsoft.Json.Linq;

namespace Microsoft.DotNet.TestFramework
{
    public class TestAsset : TestDirectory
    {
        // made tolower because the rest of the class works with normalized tolower strings
        private static readonly IEnumerable<string> BuildArtifactBlackList = new List<string>()
        {
          ".IncrementalCache",
          ".SDKVersion"
        }.Select(s => s.ToLower()).ToArray();

        private readonly string _testAssetRoot;
        private readonly string _buildVersion;

        public string TestRoot => Path;

        internal TestAsset(string testAssetRoot, string testDestination, string buildVersion) : base(testDestination)
        {
            if (string.IsNullOrEmpty(testAssetRoot))
            {
                throw new ArgumentException("testAssetRoot");
            }

            _testAssetRoot = testAssetRoot;
            _buildVersion = buildVersion;
        }

        public TestAsset WithSource()
        {
            var sourceDirs = Directory.GetDirectories(_testAssetRoot, "*", SearchOption.AllDirectories)
              .Where(dir => !IsBinOrObjFolder(dir));

            foreach (string sourceDir in sourceDirs)
            {
                Directory.CreateDirectory(sourceDir.Replace(_testAssetRoot, Path));
            }

            var sourceFiles = Directory.GetFiles(_testAssetRoot, "*.*", SearchOption.AllDirectories)
                                  .Where(file =>
                                  {
                                      return !IsLockFile(file) && !IsInBinOrObjFolder(file);
                                  });

            foreach (string srcFile in sourceFiles)
            {
                string destFile = srcFile.Replace(_testAssetRoot, Path);
                // For project.json, we need to replace the version of the Microsoft.DotNet.Core.Sdk with the actual build version
                if (srcFile.EndsWith("project.json"))
                {
                    var projectJson = JObject.Parse(File.ReadAllText(srcFile));
                    var dependencies = projectJson.Property("dependencies")?.Value as JObject;
                    var coreSdkDependency = dependencies?.Property("Microsoft.DotNet.Core.Sdk");
                    if (coreSdkDependency != null)
                        coreSdkDependency.Value = _buildVersion;
                    File.WriteAllText(destFile, projectJson.ToString());
                }
                else
                {
                    File.Copy(srcFile, destFile, true);
                }
            }

            return this;
        }

        public TestAsset Restore(params string[] args)
        {
            var restoreCommand = new RestoreCommand(Stage0MSBuild, TestRoot);
            var commandResult = restoreCommand.Execute(args);

            commandResult.Should().Pass();

            return this;
        }

        private bool IsLockFile(string file)
{'fraction_non_alphanumeric': 0.07994719084641339, 'fraction_numerical': 0.00029338418659234265, 'mean_word_length': 1.9413287316652286, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1880668', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1564, 'n_words': 406}