Conversation
…assertions Agent-Logs-Url: https://github.com/dotnet/extensions/sessions/d232918b-7fce-4025-b36c-84d01c01aaeb Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix flaky test SaveToAsync_WithJustFilename_SavesInCurrentDirectory
Fix flaky tests: remove process-wide Apr 24, 2026
Directory.SetCurrentDirectory and anchor cwd assertions to absolute paths
jozkee
reviewed
Apr 29, 2026
Co-authored-by: David Cantú <dacantu@microsoft.com>
jozkee
approved these changes
Apr 29, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Removes process-wide current-directory mutation from the AI Abstractions test suite and makes “saves to current directory” assertions deterministic under xUnit’s default parallel execution.
Changes:
- Eliminated
Directory.SetCurrentDirectoryusage fromDownloadToAsync_EmptyDestinationPath_UsesCurrentDirectory. - Anchored current-directory file assertions/cleanup to
cwdBefore+ filename (absolute path) rather than relying on relative-path resolution at assertion time. - Updated
DataContent.SaveToAsynctests to assert and clean up via absolute paths derived from the captured cwd.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Files/HostedFileClientExtensionsTests.cs | Removes process-wide cwd mutation; asserts download output via absolute path based on captured cwd. |
| test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/DataContentTests.cs | Reworks “current directory” save assertions/cleanup to use absolute paths based on captured cwd. |
jeffhandley
pushed a commit
that referenced
this pull request
May 1, 2026
…and anchor cwd assertions to absolute paths (#7487) * Initial plan * Fix flaky tests: remove Directory.SetCurrentDirectory and harden cwd assertions Agent-Logs-Url: https://github.com/dotnet/extensions/sessions/d232918b-7fce-4025-b36c-84d01c01aaeb Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: David Cantú <dacantu@microsoft.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com> Co-authored-by: David Cantú <dacantu@microsoft.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DownloadToAsync_EmptyDestinationPath_UsesCurrentDirectorycalledDirectory.SetCurrentDirectory, mutating process-wide state. xUnit runs test classes in parallel by default, so this raced withSaveToAsync_WithJustFilename_SavesInCurrentDirectoryandSaveToAsync_WithEmptyPath_UsesCurrentDirectoryAndContentName, both of which relied onFile.Exists(relativePath)— resolving silently against whatever cwd happened to be at check time.Changes
HostedFileClientExtensionsTests.cs—DownloadToAsync_EmptyDestinationPath_UsesCurrentDirectoryDirectory.SetCurrentDirectoryentirely; no temp directory created or deletedDirectory.GetCurrentDirectory()before the call; asserts file existence and content viaPath.Combine(cwdBefore, savedPath)finallydeletes only the single written file by absolute pathDataContentTests.cs—SaveToAsync_WithJustFilename_SavesInCurrentDirectoryandSaveToAsync_WithEmptyPath_UsesCurrentDirectoryAndContentNamePath.Combine(cwdBefore, filename)rather than a bare relative path, making the assertion independent of cwd at check timefinallyalso uses the absolute pathDirectory.SetCurrentDirectoryis no longer called anywhere in the test assembly.Original prompt
Problem
The test
SaveToAsync_WithJustFilename_SavesInCurrentDirectoryintest/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/DataContentTests.csis flaky in CI. Example failure:Root cause
The test
DownloadToAsync_EmptyDestinationPath_UsesCurrentDirectoryintest/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Files/HostedFileClientExtensionsTests.cscallsDirectory.SetCurrentDirectory(tempDir), which mutates process-wide state. xUnit runs different test classes in parallel by default, so whenDataContentTestsandHostedFileClientExtensionsTestsinterleave:SaveToAsync_WithJustFilename_SavesInCurrentDirectorywritestest_<guid>.jsonto cwd (relative path).DownloadToAsync_EmptyDestinationPath_UsesCurrentDirectoryswitches cwd to its temp dir, thenDirectory.Delete(tempDir, recursive: true)wipes the just-written file (if cwd happened to be that temp dir at write time) or moves cwd soFile.Exists(savedPath)in the other test resolves a relative path against the wrong directory.Either way
File.Exists(savedPath)returns false non-deterministically.Additionally,
SaveToAsync_WithJustFilename_SavesInCurrentDirectorydoes not actually verify the file landed in cwd — it just checksFile.Exists(savedPath)on a relative path, which is an implicit, time-of-check resolution against whatever cwd happens to be, not a real "is in cwd" assertion.Fix
Make two changes:
1. Remove
Directory.SetCurrentDirectoryfromDownloadToAsync_EmptyDestinationPath_UsesCurrentDirectoryStop mutating process-wide cwd. Instead, capture cwd before the call and assert the file exists at
Path.Combine(cwdBefore, savedPath). This preserves the "cwd-at-call-time" semantic check without racing with other tests. Use a GUID-based filename to avoid collisions, and delete just that file infinally.File:
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Files/HostedFileClientExtensionsTests.csReplace the existing test body with something like:
Do not call
Directory.SetCurrentDirectoryanywhere in this test (or anywhere else in the test assembly). NotempDircreation/deletion is needed for this test anymore.2. Tighten
SaveToAsync_WithJustFilename_SavesInCurrentDirectoryto actually check cwdFile:
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/DataContentTests.csReplace the body of
SaveToAsync_WithJustFilename_SavesInCurrentDirectorywith a version that captures cwd up front and asserts the file exists at the absolute path resolved from that captured cwd: