<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;

using JetBrains.Annotations;

using Mono.Cecil;
using Mono.Cecil.Cil;

using NUnit.Framework;

using TomsToolbox.Core;

namespace Tests
{
    internal class WeaverHelper : DefaultAssemblyResolver
    {
        [NotNull]
        private static readonly Dictionary<string, WeaverHelper> _cache = new Dictionary<string, WeaverHelper>();

        [NotNull]
        public Assembly Assembly { get; }
        [NotNull]
        public string NewAssemblyPath { get; }
        [NotNull]
        public string OriginalAssemblyPath { get; }
        [NotNull, ItemNotNull]
        public IList<string> Errors { get; } = new List<string>();
        [NotNull, ItemNotNull]
        public IList<string> Messages { get; } = new List<string>();

#if (!DEBUG)
        private const string Configuration = "Release";
#else
        private const string Configuration = "Debug";
#endif

        [NotNull]
        public static WeaverHelper Create([NotNull] string assemblyName = "AssemblyToProcess")
        {
            lock (typeof(WeaverHelper))
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                return _cache.ForceValue(assemblyName, _ => new WeaverHelper(assemblyName));
            }
        }

        private WeaverHelper([NotNull] string assemblyName)
        {
            // ReSharper disable once AssignNullToNotNullAttribute
            // ReSharper disable once PossibleNullReferenceException
            var projectDir = Path.GetFullPath(Path.Combine(TestContext.CurrentContext.TestDirectory, $@"..\..\..\..\{assemblyName}"));

            var binaryDir = Path.Combine(projectDir, @"bin", Configuration, "Net45");
            OriginalAssemblyPath = Path.Combine(binaryDir, $@"{assemblyName}.dll");

            NewAssemblyPath = OriginalAssemblyPath.Replace(".dll", "2.dll");

            RegisterAssembly(AssemblyDefinition.ReadAssembly(Path.Combine(binaryDir, "TestLibrary.dll")));
            RegisterAssembly(AssemblyDefinition.ReadAssembly(Path.Combine(binaryDir, "AutoProperties.dll")));

            using (var moduleDefinition = ModuleDefinition.ReadModule(OriginalAssemblyPath, new ReaderParameters(ReadingMode.Immediate) { ReadSymbols = true, AssemblyResolver = this }))
            {
                Debug.Assert(moduleDefinition != null, "moduleDefinition != null");

                var weavingTask = new ModuleWeaver
                {
                    ModuleDefinition = moduleDefinition,
                    // ReSharper disable once AssignNullToNotNullAttribute
                    AssemblyResolver = moduleDefinition.AssemblyResolver
                };

                weavingTask.LogErrorPoint += WeavingTask_LogErrorPoint;
                weavingTask.LogError += WeavingTask_LogError;
                weavingTask.LogInfo += WeavingTask_LogInfo;
                weavingTask.LogDebug += WeavingTask_LogDebug;

                weavingTask.Execute();
<commit_msg>Fix tests<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;

using JetBrains.Annotations;

using Mono.Cecil;
using Mono.Cecil.Cil;

using NUnit.Framework;

using TomsToolbox.Core;

namespace Tests
{
    internal class WeaverHelper : DefaultAssemblyResolver
    {
        [NotNull]
        private static readonly Dictionary<string, WeaverHelper> _cache = new Dictionary<string, WeaverHelper>();

        [NotNull]
        public Assembly Assembly { get; }
        [NotNull]
        public string NewAssemblyPath { get; }
        [NotNull]
        public string OriginalAssemblyPath { get; }
        [NotNull, ItemNotNull]
        public IList<string> Errors { get; } = new List<string>();
        [NotNull, ItemNotNull]
        public IList<string> Messages { get; } = new List<string>();

#if (!DEBUG)
        private const string Configuration = "Release";
#else
        private const string Configuration = "Debug";
#endif

        [NotNull]
        public static WeaverHelper Create([NotNull] string assemblyName = "AssemblyToProcess")
        {
            lock (typeof(WeaverHelper))
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                return _cache.ForceValue(assemblyName, _ => new WeaverHelper(assemblyName));
            }
        }

        private WeaverHelper([NotNull] string assemblyName)
        {
            // ReSharper disable once AssignNullToNotNullAttribute
            // ReSharper disable once PossibleNullReferenceException
            var projectDir = Path.GetFullPath(Path.Combine(TestContext.CurrentContext.TestDirectory, $@"..\..\..\..\{assemblyName}"));

            var binaryDir = Path.Combine(projectDir, @"bin", Configuration, "Net45");
            OriginalAssemblyPath = Path.Combine(binaryDir, $@"{assemblyName}.dll");

            NewAssemblyPath = OriginalAssemblyPath.Replace(".dll", "2.dll");

            RegisterAssembly(AssemblyDefinition.ReadAssembly(Path.Combine(binaryDir, "TestLibrary.dll")));
            RegisterAssembly(AssemblyDefinition.ReadAssembly(Path.Combine(binaryDir, "AutoProperties.dll")));

            using (var moduleDefinition = ModuleDefinition.ReadModule(OriginalAssemblyPath, new ReaderParameters { ReadSymbols = true, AssemblyResolver = this }))
            {
                Debug.Assert(moduleDefinition != null, "moduleDefinition != null");

                var weavingTask = new ModuleWeaver
                {
                    ModuleDefinition = moduleDefinition,
                    // ReSharper disable once AssignNullToNotNullAttribute
                    AssemblyResolver = moduleDefinition.AssemblyResolver
                };

                weavingTask.LogErrorPoint += WeavingTask_LogErrorPoint;
                weavingTask.LogError += WeavingTask_LogError;
                weavingTask.LogInfo += WeavingTask_LogInfo;
                weavingTask.LogDebug += WeavingTask_LogDebug;

                weavingTask.Execute();

{'fraction_non_alphanumeric': 0.09277678134681233, 'fraction_numerical': 0.0009783140388064568, 'mean_word_length': 2.5934387814879907, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2090798', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1470, 'n_words': 337}