PowerShellEditorServices icon indicating copy to clipboard operation
PowerShellEditorServices copied to clipboard

Add regression test for ANSI escape bug

Open andyleejordan opened this issue 3 years ago • 2 comments

Hm, well this is as far as I got with the test and I think I'm going to postpone getting a test for this so we can just get the fix in:

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Threading;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Xunit;

namespace Microsoft.PowerShell.EditorServices.Test.Console
{
    public class PSHostTests : IDisposable
    {
        private readonly PsesInternalHost _psesHost;
        public PSHostTests()
        {
            _psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
        }

        public void Dispose()
        {
            _psesHost.StopAsync().GetAwaiter().GetResult();
        }

        [Trait("Category", "LengthInBufferCells")]
        [Fact]
        public void HasLengthInBufferCells()
        {
            var script = @"[pscustomobject]@{ a = ""`e[30mtest`e[0m"" } | Format-Table @{ Width = 4; Expression = 'a' } | Out-Default";

            IReadOnlyList<string> results = _psesHost.InvokePSCommand<string>(
                new PSCommand().AddScript(script),
                new PowerShellExecutionOptions { WriteInputToHost = true, WriteOutputToHost = true, ThrowOnError = false, InterruptCurrentForeground = true },
                CancellationToken.None);
            Assert.NotEmpty(results);
        }
    }
}

Originally posted by @andschwa in https://github.com/PowerShell/PowerShellEditorServices/issues/1606#issuecomment-959910355

andyleejordan avatar Nov 03 '21 20:11 andyleejordan

The missing piece is a test PSHost that inherits EditorServicesConsolePSHost and overwrites WriteLine to something that just adds to a StringBuilder.

SeeminglyScience avatar Nov 05 '21 02:11 SeeminglyScience

Ah! _psesHost has a host that is EditorServicesConsolePSHost. I'll try to create a test harness that does what you say.

andyleejordan avatar Nov 05 '21 16:11 andyleejordan