Repository Public MIT Research preview
1"""Le point de départ ne contient aucune solution, échoue proprement, et reste synchronisé avec le corrigé."""2from __future__ import annotations34import ast5import subprocess6import sys7import unittest89from pathlib import Path1011exec((Path(__file__).resolve().parent / "_chemin.py").read_text(encoding="utf-8"))1213import generer_starter141516_ce02_restaurer()1718class TestStarter(unittest.TestCase):19 def test_starter_synchronise_avec_le_corrige(self):20 for chemin, texte in generer_starter.attendu().items():21 self.assertEqual(chemin.read_text(encoding="utf-8"), texte, f"{chemin.name} périmé : python3 generer_starter.py")2223 def test_aucune_fonction_du_starter_ne_contient_de_solution(self):24 n = 025 for nom in generer_starter.FICHIERS:26 arbre = ast.parse((LABS / "starter" / nom).read_text(encoding="utf-8"))27 for noeud in arbre.body:28 if isinstance(noeud, ast.FunctionDef):29 n += 130 self.assertFalse(noeud.name.startswith("_"), f"{nom} : fonction privée {noeud.name} du corrigé")31 corps = [x for x in noeud.body if not (isinstance(x, ast.Expr) and isinstance(x.value, ast.Constant))]32 self.assertEqual(len(corps), 1, f"{nom}:{noeud.name} contient du code")33 self.assertIsInstance(corps[0], ast.Raise, f"{nom}:{noeud.name}")34 self.assertIsNotNone(ast.get_docstring(noeud), f"{nom}:{noeud.name} sans spécification")35 self.assertEqual(n, 27, "nombre de fonctions à écrire par les apprenants")3637 def test_le_corrige_n_est_pas_dans_le_dossier_des_apprenants(self):38 fichiers = {p.name for p in (LABS / "starter").iterdir() if p.is_file()}39 self.assertEqual(fichiers - {"README.md"}, set(generer_starter.FICHIERS))40 for p in LABS.rglob("*"):41 if p.is_file() and p.suffix in {".py", ".md"}:42 self.assertNotIn("corrige", p.name.lower(), f"{p} : un corrigé dans labs/")4344 def _verifier(self, impl: str) -> subprocess.CompletedProcess:45 return subprocess.run([sys.executable, str(LABS / "verifier.py"), "--impl", impl], capture_output=True, text=True)4647 def test_le_verificateur_echoue_proprement_sur_le_starter(self):48 r = self._verifier("starter")49 self.assertEqual(r.returncode, 1)50 self.assertNotIn("Traceback", r.stdout + r.stderr)51 self.assertEqual(r.stdout.count("À FAIRE"), 14)52 self.assertIn("0 contrôle(s) réussi(s) sur 14", r.stdout)5354 def test_le_verificateur_reussit_sur_le_corrige(self):55 r = self._verifier("solution")56 self.assertEqual(r.returncode, 0, r.stdout + r.stderr)57 self.assertIn("14 contrôle(s) réussi(s) sur 14", r.stdout)585960if __name__ == "__main__":61 unittest.main()62