From 5ba673a0140857370ff34c61efe5822b7f59ff7b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Feb 2026 02:32:09 +0000 Subject: [PATCH] Add test for work directory creation failure during runner startup Co-authored-by: TingluoHuang <1750815+TingluoHuang@users.noreply.github.com> --- src/Test/L0/Listener/RunnerL0.cs | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/Test/L0/Listener/RunnerL0.cs b/src/Test/L0/Listener/RunnerL0.cs index 456f51cc9..b0b1b59e6 100644 --- a/src/Test/L0/Listener/RunnerL0.cs +++ b/src/Test/L0/Listener/RunnerL0.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using GitHub.DistributedTask.WebApi; @@ -1076,5 +1077,68 @@ namespace GitHub.Runner.Common.Tests.Listener Assert.True(hc.AllowAuthMigration); } } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Runner")] + public async Task RunCommand_ShouldReturnTerminatedError_WhenWorkDirCreationFails() + { + using (var hostCtx = new TestHostContext(this)) + { + // Setup: arrange mocks and runner instance + var runnerInstance = new Runner.Listener.Runner(); + hostCtx.SetSingleton(_configurationManager.Object); + hostCtx.SetSingleton(_jobNotification.Object); + hostCtx.SetSingleton(_messageListener.Object); + hostCtx.SetSingleton(_promptManager.Object); + hostCtx.SetSingleton(_configStore.Object); + hostCtx.SetSingleton(_runnerServer.Object); + hostCtx.EnqueueInstance(_acquireJobThrottler.Object); + + runnerInstance.Initialize(hostCtx); + + // Create a file at the work directory path to block directory creation + string workPath = hostCtx.GetDirectory(WellKnownDirectory.Work); + + // Clean up any existing directory first + if (Directory.Exists(workPath)) + { + Directory.Delete(workPath, recursive: true); + } + + // Place a file where the work directory should be - this blocks Directory.CreateDirectory + string parentPath = Path.GetDirectoryName(workPath); + Directory.CreateDirectory(parentPath); + File.WriteAllText(workPath, "blocking file content"); + + var runnerConfig = new RunnerSettings + { + PoolId = 12345, + AgentId = 67890 + }; + + _configurationManager.Setup(m => m.LoadSettings()).Returns(runnerConfig); + _configurationManager.Setup(m => m.IsConfigured()).Returns(true); + _configStore.Setup(m => m.IsServiceConfigured()).Returns(false); + + try + { + // Execute: run the command which should fail during work dir validation + var cmd = new CommandSettings(hostCtx, new string[] { "run" }); + int exitCode = await runnerInstance.ExecuteCommand(cmd); + + // Verify: should return TerminatedError code when dir creation fails + Assert.Equal(Constants.Runner.ReturnCode.TerminatedError, exitCode); + } + finally + { + // Cleanup: remove the blocking file + if (File.Exists(workPath)) + { + File.Delete(workPath); + } + } + } + } } }