Skip to content

Commit

Permalink
Improve test reliability and fix workstation domain trust issue
Browse files Browse the repository at this point in the history
  • Loading branch information
domsleee committed May 20, 2024
1 parent 5193ca3 commit 6cf3bdf
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 53 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,32 @@ jobs:
run: dotnet --info

- name: Pack
run: dotnet pack -c release /p:ContinuousIntegrationBuild=true
run: |
dotnet pack -c release /p:ContinuousIntegrationBuild=true
cp -r nupkg nupkg_pack
dotnet clean
- name: Build (AOT)
run: dotnet publish -c release /p:UseAot=1 /p:ContinuousIntegrationBuild=true
run: |
dotnet publish -c release /p:UseAot=1 /p:ContinuousIntegrationBuild=true
cp -r bin bin_aot
dotnet clean
- name: Build (release)
run: |
dotnet publish -c release /p:ContinuousIntegrationBuild=true
cp -r nupkg nupkg_release
- name: Create release
if: ${{ github.ref == 'refs/heads/main' && startsWith(github.event.head_commit.message, 'chore(release)') }}
uses: softprops/action-gh-release@v1
with:
draft: true
name: "${{ steps.get_version.outputs.version }}"
files: ForceOps/bin/Release/net8.0/win-x64/publish/ForceOps.exe
files: ForceOps/bin_aot/Release/net8.0/win-x64/publish/ForceOps.exe
tag_name: "${{ steps.get_version.outputs.version }}"

- name: Publish NuGet
if: ${{ github.ref == 'refs/heads/main' && startsWith(github.event.head_commit.message, 'chore(release)') }}
run: |
dotnet nuget push -k ${{ secrets.NUGET_AUTH_TOKEN }} -s https://api.nuget.org/v3/index.json ForceOps/nupkg/ForceOps.${{ steps.get_version.outputs.version }}.nupkg ForceOps.Lib/nupkg/ForceOps.Lib.${{ steps.get_version.outputs.version }}.nupkg --skip-duplicate
dotnet nuget push -k ${{ secrets.NUGET_AUTH_TOKEN }} -s https://api.nuget.org/v3/index.json ForceOps/nupkg_pack/ForceOps.${{ steps.get_version.outputs.version }}.nupkg ForceOps.Lib/nupkg_release/ForceOps.Lib.${{ steps.get_version.outputs.version }}.nupkg --skip-duplicate
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Authors>Dom Slee</Authors>
<PackageTags>lock file directory force delete</PackageTags>
<PackageReleaseNotes>https://github.com/domsleee/forceops/blob/main/CHANGELOG.md</PackageReleaseNotes>
<IsPackable>false</IsPackable>

<TargetFramework>net8.0</TargetFramework>
<RollForward>LatestMajor</RollForward>
Expand Down
2 changes: 1 addition & 1 deletion ForceOps.Lib/ForceOps.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<None Include="..\LICENSE.txt" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="LockChecker" Version="0.9.30-g4f01a64a51" />
<PackageReference Include="LockChecker" Version="0.9.33-gf4fccad662" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion ForceOps.Test/ForceOps.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

Expand Down
4 changes: 3 additions & 1 deletion ForceOps.Test/src/ListFileOrDirectoryLocksTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using ForceOps.Lib;
using static ForceOps.Test.TestUtil;
using static ForceOps.Test.TestUtilStdout;

namespace ForceOps.Test;

Expand All @@ -14,7 +15,7 @@ public class ListFileOrDirectoryLocksTest : IDisposable
public void WorksForDirectory()
{
var testContext = new TestContext();
using var launchedProcess = LaunchPowershellWithCommand(workingDirectory: tempDirectoryPath);
using var launchedProcess = LaunchProcessInDirectory(workingDirectory: tempDirectoryPath);
new ListFileOrDirectoryLocks(testContext.forceOpsContext).PrintLocks(tempDirectoryPath);

var stdoutString = GetStdoutString(stdoutStringBuilder);
Expand All @@ -30,6 +31,7 @@ public void WorksForFile()
new ListFileOrDirectoryLocks(testContext.forceOpsContext).PrintLocks(tempFilePath);

var stdoutString = GetStdoutString(stdoutStringBuilder);
Assert.False(launchedProcess.process.HasExited);
Assert.Equal($"ProcessId,ExecutableName,ApplicationName\r\n{launchedProcess.process.Id},powershell.exe,powershell.exe\r\n", stdoutString);
}

Expand Down
4 changes: 3 additions & 1 deletion ForceOps.Test/src/ProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ForceOps.Lib;
using Moq;
using static ForceOps.Test.TestUtil;
using static ForceOps.Test.TestUtilStdout;

namespace ForceOps.Test;

Expand Down Expand Up @@ -146,7 +147,8 @@ public void RelaunchedProgramUsesForceDelete()
testContext.forceOpsContext.relaunchAsElevated = new RelaunchAsElevated() { verb = "", exeNameOverride = exeNameOverride };
var forceOps = new ForceOps(new[] { "delete", pathThatCanBeDeleted, tempDirectoryPath }, testContext.forceOpsContext);
forceOps.extraRelaunchArgs = new List<string>() { "--disable-elevate" };
Assert.True(0 == forceOps.Run(), forceOps.caughtException?.ToString());
var stdoutString = GetStdoutString(stdoutStringBuilder);
Assert.True(0 == forceOps.Run(), BuildFailMessage(testContext, forceOps, stdoutString));
Assert.True(!Directory.Exists(tempDirectoryPath), "Deleted by relaunch");
Assert.Contains("Unable to perform operation as an unelevated process. Retrying as elevated and logging to", testContext.fakeLoggerFactory.GetAllLogsString());
}
Expand Down
46 changes: 3 additions & 43 deletions ForceOps.Test/src/TestUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Diagnostics;
using System.Reactive.Disposables;
using System.Text;

namespace ForceOps.Test;

Expand All @@ -13,38 +12,18 @@ public static WrappedProcess LaunchProcessInDirectory(string workingDirectory)

public static WrappedProcess HoldLockOnFileUsingPowershell(string filePath)
{
return LaunchPowershellWithCommand(command: $"[System.IO.File]::Open('{filePath}', 'OpenOrCreate')");
return LaunchPowershellWithCommand(command: $"$file = [System.IO.File]::Open('{filePath}', 'CreateNew')");
}

public static WrappedProcess LaunchPowershellWithCommand(string command = "", string workingDirectory = "")
static WrappedProcess LaunchPowershellWithCommand(string command = "", string workingDirectory = "")
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "powershell",
WorkingDirectory = workingDirectory,
Arguments = $"-NoProfile -Command \"{command}; echo 'process has been loaded'; sleep 10000\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};

StartProcessUntilProcessHasBeenLoadedMessage(process);

return new WrappedProcess(process);
}

public static WrappedProcess LaunchCmdWithCommand(string command = "", string workingDirectory = "")
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd",
WorkingDirectory = workingDirectory,
Arguments = $"/c \"{command}; echo process has been loaded && timeout /t 10 /nobreak\"",
Arguments = $"-NoProfile -Command \"$ErrorActionPreference='stop'; {command}; echo 'process has been loaded'; sleep 10000\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
Expand Down Expand Up @@ -95,12 +74,6 @@ public static string GetTemporaryFileName()
return Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString());
}

public static string GetStdoutString(StringBuilder stdoutStringBuilder)
{
Console.Out.Flush();
return stdoutStringBuilder.ToString();
}

public static IDisposable CreateTemporaryDirectory(string directory)
{
Directory.CreateDirectory(directory);
Expand All @@ -113,17 +86,4 @@ public static IDisposable CreateTemporaryDirectory(string directory)
catch { }
});
}

public static IDisposable RedirectStdout(StringBuilder stringBuilder)
{
var originalConsoleOut = Console.Out;
Console.Out.Flush();
Console.SetOut(new StringWriter(stringBuilder));

return Disposable.Create(() =>
{
Console.SetOut(originalConsoleOut);
Console.Out.Flush();
});
}
}
26 changes: 26 additions & 0 deletions ForceOps.Test/src/TestUtilStdout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Reactive.Disposables;
using System.Text;

namespace ForceOps.Test;

public static class TestUtilStdout
{
public static string GetStdoutString(StringBuilder stdoutStringBuilder)
{
Console.Out.Flush();
return stdoutStringBuilder.ToString();
}

public static IDisposable RedirectStdout(StringBuilder stringBuilder)
{
var originalConsoleOut = Console.Out;
Console.Out.Flush();
Console.SetOut(new StringWriter(stringBuilder));

return Disposable.Create(() =>
{
Console.SetOut(originalConsoleOut);
Console.Out.Flush();
});
}
}
2 changes: 1 addition & 1 deletion ForceOps.Test/src/WrappedProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public WrappedProcess(Process process)
void IDisposable.Dispose()
{
process.Kill();
process.WaitForExit(1);
process.WaitForExit();
}
}
2 changes: 1 addition & 1 deletion ForceOps/src/ForceOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void RunWithRelaunchAsElevated(Action action, Func<List<string>> buildArgsForRel
var childProcessExitCode = forceOpsContext.relaunchAsElevated.RelaunchAsElevated(args, childOutputFile);
if (childProcessExitCode != 0)
{
throw new AggregateException($"Child process failed with {childProcessExitCode}.");
throw new AggregateException($"Child process failed with exit code {childProcessExitCode}.");
}
else
{
Expand Down

0 comments on commit 6cf3bdf

Please sign in to comment.