Aller au contenu
Skillpedia
Tests5.8k · #14632

migrate-static-to-wrapper

Ce qu’il fait

Cette compétence fait passer du code C# des appels statiques directs, comme la lecture de l'heure actuelle ou d'un fichier, vers des wrappers injectés ou des abstractions intégrées. Elle s'adresse aux développeurs qui rendent leur code testable par petites étapes contrôlées.

Quand l’utiliser

  • ✓Quand vous voulez remplacer DateTime.UtcNow ou DateTime.Now par TimeProvider dans un projet.
  • ✓Quand les appels directs à File doivent passer par un wrapper de système de fichiers existant dans tout un espace de noms.
  • ✓Quand une classe a besoin d'une injection par constructeur pour une nouvelle abstraction et que ses tests ont besoin de faux objets.

Lien du skill

https://github.com/dotnet/skills/blob/main/plugins/dotnet-test/skills/migrate-static-to-wrapper/SKILL.md

Copiez-le et collez-le dans votre IA (Claude, ChatGPT ou Cursor) en lui demandant de suivre les instructions.

Comment l’utiliser

Le plus simple : copiez le message et collez-le dans votre IA. Elle lit le skill et suit les instructions.

Message prêt à coller

Utilise ce skill pour m’aider : https://github.com/dotnet/skills/blob/main/plugins/dotnet-test/skills/migrate-static-to-wrapper/SKILL.md Lis le fichier SKILL.md de ce lien et suis ses instructions pour ma tâche.
Envie que votre IA trouve seule des skills comme celui-ci ?Installez le MCP gratuit de Skillpedia : une ligne, et elle cherche parmi 15 000+ skills avant chaque tâche.Installer en 1 minute →

Exemple de demande à votre IA

“Remplace les appels directs à DateTime.UtcNow dans mon projet Orders par TimeProvider, et mets à jour les tests pour utiliser une fausse horloge.”

5 nouveaux skills par semaine. 1 minute de lecture.

Comment ça marche

L'agent limite le travail au périmètre que vous indiquez, modifie les sites d'appel et les constructeurs pour utiliser l'abstraction injectée, puis met à jour les tests concernés avec de faux objets.

Contenu technique (SKILL.md original)

Texte original de l’auteur, en anglais, directement depuis GitHub.

Migrate Static to Wrapper

Perform mechanical, codemod-style replacement of static dependency call sites with calls to injected wrapper interfaces or built-in abstractions. Operates on a bounded scope (single file, project, or namespace) so migrations can be done incrementally.

When to Use

  • After wrappers have been generated (via generate-testability-wrappers) or built-in abstractions identified
  • Migrating DateTime.UtcNow → TimeProvider.GetUtcNow() across a project
  • Migrating File.* → IFileSystem.File.* across a namespace
  • Adding constructor injection for the new abstraction to affected classes
  • Making a static utility class testable by adding an ambient seam (Step 3) while its existing call sites keep compiling unchanged
  • Incremental migration: one project or namespace at a time
  • Updating affected tests with fakes when the requested migration names the replacement abstraction

When Not to Use

  • No wrapper or abstraction exists yet and one must be designed from scratch (use generate-testability-wrappers first). A built-in abstraction such as TimeProvider or IFileSystem always counts as existing.
  • The user wants to detect statics, not migrate them (use detect-static-dependencies)
  • Migrating between test frameworks (use the appropriate migration skill)
  • The user primarily asks for a deterministic behavior test and has not selected the production seam (use testability-obstacle)

A class that is static, or a project with no DI container, is not a reason to skip this skill — that is exactly what the ambient seam in Step 3 is for. Use it whenever the call sites must keep compiling unchanged.

Inputs

Input Required Description
Static pattern No Infer from the request and discovered call sites (e.g., DateTime.UtcNow, File.ReadAllText)
Replacement abstraction No Infer from the request and existing project abstractions; stop only when no named/existing abstraction is available
Scope No Infer from the requested file/project/namespace, otherwise discover the narrowest relevant workspace scope
Injection strategy No constructor (default), primary-constructor, or ambient

Workflow

Non-negotiable migration boundaries

  • Missing abstraction means stop. If the named interface/package is absent and the request only authorizes call-site replacement, do not add a package, invent a local lookalike interface, or edit production code. Report the exact missing prerequisite and the authorization needed to continue.
  • One source read stays one replacement read. Do not hoist or coalesce calls, even when sharing a captured timestamp looks cleaner.
  • The requested scope is exhaustive and exclusive. Replace every named call in scope and no adjacent member or file.
  • Repository-backed requests require repository work. Start by discovering files from the current workspace. Do not claim the repository is unavailable or ask the user for a path or file contents until workspace-relative discovery found no target. Do not say work was implemented unless the diff proves it.
  • Discovered workspace files must be completed in this turn when permitted. Use a host-native shell reader (sed/cat or Get-Content) only after a confirmed reader availability, transport, or path-normalization failure and only after verifying the canonical path remains inside the current workspace. Stop on content-exclusion, permission/policy, workspace-boundary, or unknown read failures. Use a shell edit fallback only for a confirmed editor availability, transport, or path-normalization failure, never for a stale context, concurrent change, permission/policy denial, or path-boundary error. Before fallback, resolve the canonical path inside the current workspace, freshly read the file, and require an anchored replacement with the expected old text and exact match count; abort if either changed. Then re-open the file, inspect the diff, and validate. Do not ask the user to paste a readable discovered file or report a proposed patch as completed work.

Step 1: Verify prerequisites

Before modifying any code:

  1. Confirm the wrapper/abstraction exists: Check that the interface or built-in abstraction is available in the project. For TimeProvider, verify the target framework is .NET 8+ or Microsoft.Bcl.TimeProvider is referenced. For System.IO.Abstractions, v
Questions fréquentes

Crée-t-elle aussi le wrapper ?

Non. Si aucun wrapper n'existe, la compétence qui génère les wrappers de testabilité doit d'abord le créer.

Peut-on traiter toute la solution d'un coup ?

La compétence travaille sur un périmètre délimité, comme un fichier, un projet ou un espace de noms, afin que les modifications se fassent étape par étape.

Repère-t-elle seulement les appels statiques ?

Non. Leur repérage relève d'une compétence de détection distincte. Celle-ci effectue le remplacement.

Détails

Description originale de l’auteur

ALWAYS USE when asked to migrate, replace, or make testable existing C# static calls with a named wrapper or built-in abstraction: DateTime.UtcNow/Now or DateTimeOffset.UtcNow to TimeProvider/IClock, File.* to IFileSystem or an existing store such as ITextFileStore, and Environment.* to an existing reader such as IEnvironmentReader. Covers scoped files/projects, constructor injection, replacing temp-file or process-environment tests with fakes, "already registered" abstractions, and static classes whose callers/signatures must stay unchanged. Preserves DateTimeKind and call count. DO NOT USE f

repo: dotnet/skills

Popularité

Étoiles GitHub
5.8k
Position au classement
#14632
Licence
MIT
Mis à jour
02/11/2026

Ce skill vous est-il utile ?

0

Connectez-vous pour voter.

Commentaires

  • …

Autres skills de ce dépôt

Skills similaires