<commit_before>﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Azure.Test.HttpRecorder.ProcessRecordings;

namespace Microsoft.Rest.ClientRuntime.Azure.TestFramework
{
    /// <summary>
    /// A coordinator for tracking and undoing WAML operations.  Usage pattern is
    /// using(MockContext.Create())
    /// {
    ///   maml stuff
    /// }
    /// You can also manually call the Dispose() or UndoAll() methods to undo all 'undoable' operations since the
    /// UndoContext was created.
    /// Call: MockContext.Commit() to remove all undo information
    /// </summary>
    public class MockContext : IDisposable
    {
        //prevent multiple dispose events
        protected bool disposed = false;
        private List<ResourceGroupCleaner> undoHandlers = new List<ResourceGroupCleaner>();
        private TestEnvironment _testFxEnvironment;


        static MockContext()
        {
        }

        internal TestEnvironment TestFxEnvironment
        {
            get
            {
                if(_testFxEnvironment == null)
                {
                    string envStr = Environment.GetEnvironmentVariable(TestEnvironmentFactory.TestCSMOrgIdConnectionStringKey);
                    _testFxEnvironment = new TestEnvironment(envStr);
                    OptimizeTestRecordingFile = _testFxEnvironment.OptimizeRecordedFile;
                }

                return _testFxEnvironment;
            }
        }

        internal bool OptimizeTestRecordingFile { get; set; } = false;
        
        
        /// <summary>
        /// Return a new UndoContext
        /// </summary>
        /// <returns></returns>
        public static MockContext Start(
            string className,
            [System.Runtime.CompilerServices.CallerMemberName]
            string methodName= "testframework_failed")
        {
            var context = new MockContext();
            if (HttpMockServer.FileSystemUtilsObject == null)
            {
                HttpMockServer.FileSystemUtilsObject = new Microsoft.Azure.Test.HttpRecorder.FileSystemUtils();
            }
            HttpMockServer.Initialize(className, methodName);
            if (HttpMockServer.Mode != HttpRecorderMode.Playback)
            {
                context.disposed = false;
            }

            return context;
        }

        /// <summary>
        /// Get a test environment using default options
        /// </summary>
        /// <typeparam name="T">The type of the service client to return</typeparam>
        /// <returns>A Service client using credentials and base uri from the current environment</returns>
        public T GetServiceClient<T>(bool internalBaseUri = false, params DelegatingHandler[] handlers) where T : class
        {
            return GetServiceClient<T>(TestFxEnvironment, internalBaseUri, handlers);
        }

        /// <summary>
        /// Get a test environment, allowing the test to customize the creation options
        /// </summary>
        /// <typeparam name="T"></typeparam><commit_msg>Add overload for mockcontext (#7297)<commit_after>﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Azure.Test.HttpRecorder.ProcessRecordings;

namespace Microsoft.Rest.ClientRuntime.Azure.TestFramework
{
    /// <summary>
    /// A coordinator for tracking and undoing WAML operations.  Usage pattern is
    /// using(MockContext.Create())
    /// {
    ///   maml stuff
    /// }
    /// You can also manually call the Dispose() or UndoAll() methods to undo all 'undoable' operations since the
    /// UndoContext was created.
    /// Call: MockContext.Commit() to remove all undo information
    /// </summary>
    public class MockContext : IDisposable
    {
        //prevent multiple dispose events
        protected bool disposed = false;
        private List<ResourceGroupCleaner> undoHandlers = new List<ResourceGroupCleaner>();
        private TestEnvironment _testFxEnvironment;


        static MockContext()
        {
        }

        internal TestEnvironment TestFxEnvironment
        {
            get
            {
                if(_testFxEnvironment == null)
                {
                    string envStr = Environment.GetEnvironmentVariable(TestEnvironmentFactory.TestCSMOrgIdConnectionStringKey);
                    _testFxEnvironment = new TestEnvironment(envStr);
                    OptimizeTestRecordingFile = _testFxEnvironment.OptimizeRecordedFile;
                }

                return _testFxEnvironment;
            }
        }

        internal bool OptimizeTestRecordingFile { get; set; } = false;
        
        
        /// <summary>
        /// Return a new MockContext
        /// </summary>
        /// <returns></returns>
        public static MockContext Start(
            string className,
            [System.Runtime.CompilerServices.CallerMemberName]
            string methodName= "testframework_failed")
        {
            var context = new MockContext();
            if (HttpMockServer.FileSystemUtilsObject == null)
            {
                HttpMockServer.FileSystemUtilsObject = new Microsoft.Azure.Test.HttpRecorder.FileSystemUtils();
            }
            HttpMockServer.Initialize(className, methodName);
            if (HttpMockServer.Mode != HttpRecorderMode.Playback)
            {
                context.disposed = false;
            }

            return context;
        }

        /// <summary>
        /// Return a new MockContext
        /// </summary>
        /// <returns></returns>
        public static MockContext Start(
            Type type,
            [System.Runtime.CompilerServices.CallerMemberName]
            string methodName= "testframework_failed")
        {
            return MockContext.Start(type.Name, methodName);
        }

        /// <summary>
        /// Get a test environment using default options
        /// </summary>
        /// <typeparam name="T">The type of the service client to return</typeparam>
        /// <returns>A Service client using credentials and base uri from the current environment</returns>
        public T GetServiceClient<T>(bool internalBaseUri = false, params DelegatingHandler[] handlers) where T : class
        {
            return GetServiceClient<T>(TestFxEnvironment, internalBaseUri, handlers);
        }

        /// <summary>
        /// Get a test environment, allowing the test to customize the creation options
        /// </summary>
        /// <typeparam name="T"></typeparam>
{'fraction_non_alphanumeric': 0.08920657612580414, 'fraction_numerical': 0.0005718370264474624, 'mean_word_length': 2.045711797997388, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '3959359', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1636, 'n_words': 463}