Add MSTEST0068 - CollectionAssert to Assert analyzer and code fix#8768
Open
Evangelink wants to merge 2 commits into
Open
Add MSTEST0068 - CollectionAssert to Assert analyzer and code fix#8768Evangelink wants to merge 2 commits into
Evangelink wants to merge 2 commits into
Conversation
Implements the MSTEST0068 analyzer and code fix to migrate legacy `CollectionAssert.*` calls to their modern `Assert.*` equivalents. Mappings: - AreEqual/AreNotEqual -> AreSequenceEqual/AreNotSequenceEqual - AreEquivalent/AreNotEquivalent -> AreSequenceEqual/AreNotSequenceEqual with SequenceOrder.InAnyOrder - AllItemsAreNotNull -> AreAllNotNull - AllItemsAreUnique -> AreAllDistinct - AllItemsAreInstancesOfType -> AreAllOfType (swap args) - Contains/DoesNotContain -> Contains/DoesNotContain (swap args) IComparer/IEqualityComparer<T> overloads, IsSubsetOf and IsNotSubsetOf are intentionally not migrated. Fixes microsoft#8764 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds MSTEST0068 to MSTest.Analyzers (and a corresponding C# code fix in MSTest.Analyzers.CodeFixes) to migrate supported CollectionAssert.* calls to modern Assert.* equivalents, plus unit tests and localized resources.
Changes:
- Introduce
CollectionAssertToAssertAnalyzer(MSTEST0068) with method-to-method mappings and skipped overload handling. - Add
CollectionAssertToAssertFixerimplementing argument reordering andSequenceOrder.InAnyOrderinsertion. - Add unit tests and update analyzer/code-fix resources and release tracking entries.
Show a summary per file
| File | Description |
|---|---|
| test/UnitTests/MSTest.Analyzers.UnitTests/CollectionAssertToAssertAnalyzerTests.cs | Adds analyzer+fixer unit tests covering supported mappings and some formatting/argument-order scenarios. |
| src/Analyzers/MSTest.Analyzers/Resources.resx | Adds MSTEST0068 title/message resources. |
| src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs | Reserves diagnostic ID MSTEST0068 constant. |
| src/Analyzers/MSTest.Analyzers/CollectionAssertToAssertAnalyzer.cs | Implements MSTEST0068 analyzer and mapping logic. |
| src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md | Registers MSTEST0068 in unshipped analyzer release notes. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf | Localization update for new MSTEST0068 analyzer strings. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/CodeFixResources.resx | Adds code-fix title resource for CollectionAssert→Assert migration. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/CollectionAssertToAssertFixer.cs | Implements the C# code fix for MSTEST0068 (argument normalization + rewrite strategies). |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.cs.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.de.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.es.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.fr.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.it.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.ja.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.ko.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.pl.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.pt-BR.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.ru.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.tr.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.zh-Hans.xlf | Localization update for new code-fix title resource. |
| src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.zh-Hant.xlf | Localization update for new code-fix title resource. |
Copilot's findings
- Files reviewed: 33/33 changed files
- Comments generated: 2
Member
Author
|
@copilot address review comments |
… Assert The fixer used to unconditionally replace `<qualifier>.CollectionAssert.<Method>` with the unqualified `Assert.<ProperMethod>`. That breaks fully-qualified calls like `Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(...)` in files without the `using Microsoft.VisualStudio.TestTools.UnitTesting;` directive: the rewritten `Assert` would either fail to bind or bind to a different `Assert` type in scope. Now preserve the original qualifier and only swap the trailing `CollectionAssert` identifier for `Assert`. Handles unqualified, namespace-qualified, `global::`-qualified, and `alias::`-qualified forms. Added two regression tests covering `Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.X(...)` (no using directive) and `global::Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.X(...)`. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.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.
Fixes #8764.
Adds the MSTEST0068 analyzer + code fix to migrate legacy
CollectionAssert.*calls to their modernAssert.*equivalents.Mappings
CollectionAssertAssertAreEqual/AreNotEqualAreSequenceEqual/AreNotSequenceEqualAreEquivalent/AreNotEquivalentAreSequenceEqual/AreNotSequenceEqualwithSequenceOrder.InAnyOrderAllItemsAreNotNullAreAllNotNullAllItemsAreUniqueAreAllDistinctAllItemsAreInstancesOfType(coll, type)AreAllOfType(type, coll)Contains(coll, x)Contains(x, coll)DoesNotContain(coll, x)DoesNotContain(x, coll)Skipped overloads
AreEqual/AreNotEqualoverloads taking anIComparerare not migrated (no equivalent onAssert).AreEquivalent/AreNotEquivalentoverloads taking anIEqualityComparer<T>are not migrated.IsSubsetOf/IsNotSubsetOfhave noAssertequivalent and are not migrated.Notes
MSTEST0068, categoryUsage, severityInfo.SequenceOrderinsertion are operation-based, so named/out-of-order arguments are normalized correctly.SequenceOrder.InAnyOrderis emitted fully-qualified to avoid breaking files that lack theMicrosoft.VisualStudio.TestTools.UnitTestingusing directive.AllItemsAreInstancesOfType->AreAllOfTypechanges null-element semantics:CollectionAssert.AllItemsAreInstancesOfTypeallowed nulls, whileAssert.AreAllOfTypetreats them as failures. The new behavior is arguably more correct and matches the issue mandate.