SPB Git

Repository Public MIT Research preview

test_lab.py 4.9 KB 101 lines python

1"""Tests du laboratoire S15 : le corrigé réussit, le point de départ échoue proprement, les copies concordent."""2from __future__ import annotations34import re5import subprocess6import sys7from pathlib import Path89SESSION = Path(__file__).resolve().parents[2]10LABO = SESSION / "labs"111213def lancer(dossier: str, *arguments: str) -> subprocess.CompletedProcess:14    return subprocess.run([sys.executable, "verifier.py", *arguments], cwd=LABO / dossier,15                          capture_output=True, text=True, encoding="utf-8")161718def test_corrige_reussit():19    r = lancer("solution")20    assert r.returncode == 0, r.stdout + r.stderr21    assert r.stdout.count("[ RÉUSSI  ] CA-") == 822    assert "8/8 critères satisfaits" in r.stdout23    assert "[ RÉUSSI  ] Défi — votre essai accepte le candidat A et refuse le candidat B" in r.stdout242526def test_point_de_depart_echoue_sans_planter():27    r = lancer("starter")28    assert r.returncode == 129    assert r.stdout.count("[ À FAIRE ]") == 9 and "0/8 critères satisfaits" in r.stdout30    assert "Traceback" not in r.stderr and "Traceback" not in r.stdout313233def test_tableau_des_candidats_suite_livree():34    r = lancer("starter", "--candidats")35    assert r.returncode == 036    lignes = {ligne.split()[0]: ligne.split()[1:] for ligne in r.stdout.splitlines() if ligne.startswith(("CA-", "total"))}37    assert lignes["CA-04"] == ["ok", "ok", "ok"] and lignes["CA-07"] == ["ok", "ok", "ÉCHEC"]38    assert lignes["total"] == ["8/8", "8/8", "7/8"]394041def test_tableau_des_candidats_avec_l_essai_du_defi():42    r = lancer("solution", "--candidats")43    lignes = {ligne.split()[0]: ligne.split()[1:] for ligne in r.stdout.splitlines() if ligne.startswith(("CA-", "total"))}44    assert lignes["CA-04"] == ["ok", "ÉCHEC", "ok"] and lignes["total"] == ["8/8", "7/8", "7/8"]454647def test_suite_unittest_lancee_comme_dans_les_consignes():48    r = subprocess.run([sys.executable, "-m", "unittest", "tests_acceptation"], cwd=LABO / "solution",49                       capture_output=True, text=True, encoding="utf-8")50    assert r.returncode == 0 and "Ran 11 tests" in r.stderr51    r = subprocess.run([sys.executable, "-m", "unittest", "tests_acceptation"], cwd=LABO / "starter",52                       capture_output=True, text=True, encoding="utf-8")53    assert r.returncode == 1 and "Ran 11 tests" in r.stderr545556def test_point_de_depart_ne_contient_pas_le_corrige():57    depart = (LABO / "starter" / "inventaire.py").read_text(encoding="utf-8")58    assert "import json" not in depart and "def retirer" not in depart and "CODE_A_FAIRE" in depart59    defi = (LABO / "starter" / "tests_defi.py").read_text(encoding="utf-8")60    assert "raise NotImplementedError" in defi and "self.refuser(\"" not in defi61    assert not (LABO / "starter" / "corrige").exists()626364def test_fichiers_communs_identiques_dans_les_deux_dossiers():65    for nom in ("verifier.py", "tests_acceptation.py", "exigences.md", "candidats/candidat_a.py",66                "candidats/candidat_b.py", "candidats/candidat_c.py"):67        assert (LABO / "starter" / nom).read_bytes() == (LABO / "solution" / nom).read_bytes(), nom686970def test_candidats_etiquetes_comme_rediges_pour_le_cours():71    for lettre in "abc":72        code = (LABO / "starter" / "candidats" / f"candidat_{lettre}.py").read_text(encoding="utf-8")73        assert "rédigée pour le cours" in code and "n'a pas été produit par un agent" in code747576def test_consignes_sans_commande_dangereuse_ni_cle():77    motifs = [r"\brm\s+-[a-zA-Z]*r", r"(curl|wget)[^\n|]*\|\s*(sudo\s+)?(ba|z)?sh", r"\bsudo\b", r"--force", r"\bsk-[A-Za-z0-9]{8,}"]78    for fichier in ("labs/instructions.md", "demos/README.md", "README.md", "labs/starter/AGENTS.md",79                    "video/script.md", "slides/speaker-notes.md"):80        texte = (SESSION / fichier).read_text(encoding="utf-8")81        for motif in motifs:82            assert not re.search(motif, texte), f"{fichier} : motif interdit {motif}"838485def test_consignes_annoncent_le_compte_la_facturation_et_les_points_d_arret():86    texte = (LABO / "instructions.md").read_text(encoding="utf-8")87    for attendu in ("compte", "facturé", "Point d'arrêt", "sans code", "Défi", "exemple rédigé pour le cours",88                    "documentation officielle", "journal-interventions.md", "Windows", "Python 3"):89        assert attendu in texte, attendu90    assert "learn.chatgpt.com/docs/pricing" in texte919293def test_pieces_du_parcours_b_identiques_aux_sorties_reelles():94    texte = (LABO / "instructions.md").read_text(encoding="utf-8")95    assert lancer("starter", "--candidats").stdout.strip() in texte96    assert lancer("starter").stdout.strip() in texte97    demo = subprocess.run([sys.executable, str(SESSION / "demos" / "src" / "atlas_s15" / "demo.py")],98                          capture_output=True, text=True, encoding="utf-8").stdout99    assert demo[demo.index("[5]"):demo.index("[6]")].rstrip() in texte100    assert demo[demo.index("[6]"):demo.index("[7]")].rstrip() in texte101
Files main
Menu

Theme