SPB Git

Repository Public MIT Research preview

test_lab.py 5.4 KB 111 lines python

1"""Tests du laboratoire S13 : le corrigé réussit, le point de départ échoue proprement, rien ne fuit."""2from __future__ import annotations34import os5import re6import subprocess7import sys8from pathlib import Path910SESSION = Path(__file__).resolve().parents[2]11LABS = SESSION / "labs"12ENV = dict(os.environ, PYTHONDONTWRITEBYTECODE="1")131415def lancer(dossier: str, *commande: str) -> subprocess.CompletedProcess:16    return subprocess.run([sys.executable, *commande], cwd=LABS / dossier, capture_output=True, text=True, env=ENV)171819def test_corrige_reussit_le_verificateur():20    r = lancer("solution", "verifier.py")21    assert r.returncode == 0, r.stdout + r.stderr22    assert "4/4 étapes obligatoires réussies" in r.stdout23    assert r.stdout.count("[ RÉUSSI  ]") == 524    assert "Défi — votre suite de tests écarte 1 correctif sur 3" in r.stdout252627def test_point_de_depart_echoue_sans_planter():28    r = lancer("starter", "verifier.py")29    assert r.returncode == 130    assert r.stdout.count("[ À FAIRE ]") == 5 and "Traceback" not in r.stderr + r.stdout31    assert "0/4 étapes obligatoires réussies" in r.stdout32    assert "n'écarte aucun des 3 correctifs" in r.stdout333435def test_suites_unittest():36    depart = lancer("starter", "-m", "unittest")37    assert depart.returncode == 1 and "Ran 5 tests" in depart.stderr and "errors=2" in depart.stderr38    corrige = lancer("solution", "-m", "unittest")39    assert corrige.returncode == 0 and "Ran 17 tests" in corrige.stderr404142def test_ligne_de_commande_du_corrige():43    r = lancer("solution", "-m", "calculatrice", "7 / 2")44    assert (r.returncode, r.stdout.strip()) == (0, "3.5")45    r = lancer("solution", "-m", "calculatrice", "5 / 0")46    assert r.returncode == 2 and "Erreur : division par zéro" in r.stderr47    r = lancer("solution", "-m", "calculatrice", "__import__('os').getcwd() + 1")48    assert r.returncode == 2    # aucun texte n'est exécuté : contrainte 2495051def test_point_de_depart_ne_contient_pas_le_corrige():52    operations = (LABS / "starter" / "calculatrice" / "operations.py").read_text(encoding="utf-8")53    expression = (LABS / "starter" / "calculatrice" / "expression.py").read_text(encoding="utf-8")54    assert operations.count("raise NotImplementedError") == 2 and expression.count("raise NotImplementedError") == 155    assert "a / b" not in operations and "sum(" not in operations and "split(" not in expression56    assert not (LABS / "starter" / "tests" / "test_expression.py").exists()57    assert "test_division_non_entiere" not in (LABS / "starter" / "tests" / "test_operations.py").read_text(encoding="utf-8")585960def test_fichiers_communs_identiques():61    for relatif in ("verifier.py", "specification.md", "journal.md", "calculatrice/__init__.py",62                    "calculatrice/erreurs.py", "calculatrice/__main__.py", "tests/__init__.py"):63        assert (LABS / "starter" / relatif).read_text(encoding="utf-8") == (LABS / "solution" / relatif).read_text(encoding="utf-8"), relatif646566def test_le_corrige_conserve_les_tests_fournis():67    """Contrainte 3 de la spécification : les tests fournis ne sont ni supprimés ni affaiblis."""68    depart = (LABS / "starter" / "tests" / "test_operations.py").read_text(encoding="utf-8")69    corrige = (LABS / "solution" / "tests" / "test_operations.py").read_text(encoding="utf-8")70    for methode in re.findall(r"(    def test_\w+\(self\):\n(?:        .*\n)+)", depart):71        assert methode in corrige, methode727374def test_essayer_concorde_avec_la_demonstration():75    sys.path.insert(0, str(SESSION / "demos" / "src"))76    from atlas_s13.evaluation import tout_evaluer7778    bilans = {b.nom: b for b in tout_evaluer()}79    for numero in (1, 2, 3):80        r = lancer("revue", "essayer.py", str(numero))81        b = bilans[f"correctif {numero}"]82        assert r.returncode == 0, r.stderr83        assert f"{b.livres[0]}/{b.livres[1]} tests réussis" in r.stdout84        for fichier in b.fichiers_modifies:85            assert f"    {fichier}\n" in r.stdout86        assert "ACCEPTER" not in r.stdout and "REFUSER" not in r.stdout   # le script ne rend aucun verdict878889def test_essayer_refuse_un_projet_deja_modifie():90    r = lancer("revue", "essayer.py", "1", "--projet", str(LABS / "solution"))91    assert r.returncode == 2 and "plus le point de départ intact" in r.stdout929394def test_fichiers_diff_etiquetes_et_complets():95    for numero in (1, 2, 3):96        texte = (LABS / "revue" / f"correctif-{numero}.diff").read_text(encoding="utf-8")97        assert texte.startswith(f"# Correctif {numero} — rédigé pour le cours ATLAS IA (S13). Ce n'est pas la sortie d'un outil.")98        assert "--- a/calculatrice/operations.py" in texte99    assert "tests/test_operations.py" in (LABS / "revue" / "correctif-2.diff").read_text(encoding="utf-8")100101102def test_consignes_sures():103    """Aucune commande d'installation par script distant, aucune élévation de privilèges, aucune clé."""104    for relatif in ("labs/instructions.md", "demos/README.md", "README.md", "labs/rubric.md"):105        texte = (SESSION / relatif).read_text(encoding="utf-8")106        assert not re.search(r"(curl|wget|irm)[^\n|]*\|", texte), relatif107        assert not re.search(r"\bsudo\b", texte), relatif108        assert not re.search(r"sk-ant-|ANTHROPIC_API_KEY\s*=\s*\S", texte), relatif109    consignes = (SESSION / "labs" / "instructions.md").read_text(encoding="utf-8")110    assert "compte" in consignes and "facturé" in consignes and "claude.com/pricing" in consignes111
Files main
Menu

Theme