Skip to content

Commit

Permalink
Add console rendering (github#11)
Browse files Browse the repository at this point in the history
* Add version command to cli

* Add in non-default command line parsing options

* Lint

* Fix tests

* Attach tty to process service

* Bump nuget versions

* Cleanup

* Fix tests

* Always publish test reports
  • Loading branch information
ethanis authored Apr 22, 2022
1 parent 4b22247 commit 0368943
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
env:
DOTNET_VERSION: "6.0.x"

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -35,6 +39,7 @@ jobs:
run: dotnet test src/Valet.UnitTests/Valet.UnitTests.csproj --no-build --no-restore --logger "junit;LogFilePath=unit-tests.xml"
- name: publish test results
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
with:
files: "**/*-tests.xml"
check_name: "Unit Test Results"
Expand Down
4 changes: 2 additions & 2 deletions src/Valet.UnitTests/Services/DockerServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public async Task ExecuteCommandAsync_InvokesDocker_ReturnsTrue()
_processService.Setup(handler =>
handler.RunAsync(
"docker",
$"run --rm -v \"{Directory.GetCurrentDirectory()}\":/data {image} {string.Join(' ', arguments)}",
$"run --rm -t -v \"{Directory.GetCurrentDirectory()}\":/data {image} {string.Join(' ', arguments)}",
Directory.GetCurrentDirectory(),
new[] { new System.ValueTuple<string, string>("MSYS_NO_PATHCONV", "1") },
true
Expand Down Expand Up @@ -168,7 +168,7 @@ public async Task ExecuteCommandAsync_InvokesDocker_WithEnvironmentVariables_Ret
_processService.Setup(handler =>
handler.RunAsync(
"docker",
$"run --rm --env GITHUB_ACCESS_TOKEN=foo --env GITHUB_INSTANCE_URL=https://github.fabrikam.com --env JENKINS_ACCESS_TOKEN=bar -v \"{Directory.GetCurrentDirectory()}\":/data {image} {string.Join(' ', arguments)}",
$"run --rm -t --env GITHUB_ACCESS_TOKEN=foo --env GITHUB_INSTANCE_URL=https://github.fabrikam.com --env JENKINS_ACCESS_TOKEN=bar -v \"{Directory.GetCurrentDirectory()}\":/data {image} {string.Join(' ', arguments)}",
Directory.GetCurrentDirectory(),
new[] { new System.ValueTuple<string, string>("MSYS_NO_PATHCONV", "1") },
true
Expand Down
11 changes: 7 additions & 4 deletions src/Valet.UnitTests/Valet.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

<ItemGroup>
<PackageReference Include="JUnitTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/Valet/Services/DockerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Task ExecuteCommandAsync(string image, params string[] arguments)
{
var valetArguments = new List<string>
{
"run --rm"
"run --rm -t"
};
valetArguments.AddRange(GetEnvironmentVariableArguments());
valetArguments.Add($"-v \"{Directory.GetCurrentDirectory()}\":/data");
Expand All @@ -66,7 +66,6 @@ public async Task VerifyDockerRunningAsync()
{
try
{
// TODO: Verify this is cross platform
await _processService.RunAsync(
"docker",
"info",
Expand Down
39 changes: 24 additions & 15 deletions src/Valet/Services/ProcessService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System.Collections.Specialized;
using System.Diagnostics;
using Valet.Interfaces;

namespace Valet.Services;

using System;
using System.Diagnostics;
using System.Threading.Tasks;

public class ProcessService : IProcessService
{
public Task RunAsync(
Expand All @@ -17,14 +13,17 @@ public Task RunAsync(
bool output = true)
{
var tcs = new TaskCompletionSource<bool>();
var cts = new CancellationTokenSource();

var startInfo = new ProcessStartInfo
{
FileName = filename,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
WorkingDirectory = cwd
WorkingDirectory = cwd,
CreateNoWindow = true
};

if (environmentVariables != null)
Expand All @@ -44,8 +43,8 @@ public Task RunAsync(
void OnProcessExited(object? sender, EventArgs args)
{
process.Exited -= OnProcessExited;
process.OutputDataReceived -= OnOutputDataReceived;

cts.Cancel();
if (process.ExitCode == 0)
{
tcs.TrySetResult(true);
Expand All @@ -59,17 +58,27 @@ void OnProcessExited(object? sender, EventArgs args)
process.Dispose();
}

void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (output) Console.WriteLine(e.Data);
}

process.OutputDataReceived += OnOutputDataReceived;
process.Exited += OnProcessExited;

process.Start();
process.BeginOutputReadLine();

ReadStream(process.StandardOutput, output, cts.Token);
ReadStream(process.StandardError, output, cts.Token);

return tcs.Task;
}

private void ReadStream(StreamReader reader, bool output, CancellationToken ctx)
{
if (!output) return;

Task.Run(() =>
{
while (!ctx.IsCancellationRequested)
{
int current;
while ((current = reader.Read()) >= 0)
Console.Write((char)current);
}
}, ctx);
}
}

0 comments on commit 0368943

Please sign in to comment.