@furongjun1999/dsh-memory 0.9.6 → 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -662
- package/codebuddy/CODEBUDDY.md +333 -204
- package/data/policy.json +38 -38
- package/docs/README.md +18 -0
- package/docs/discipline/harnesses.yaml +11 -3
- package/docs/discipline/templates/compact.txt.tmpl +20 -18
- package/docs/discipline/templates/full.md.tmpl +63 -61
- package/docs/discipline/templates/rules.mdc.tmpl +5 -3
- package/docs/discipline/templates/skill.md.tmpl +25 -23
- package/docs/discipline/templates/spec.txt.tmpl +2 -2
- package/docs/discipline/templates/zcode-user.md.tmpl +1 -1
- package/dsh/cordis.yml.example +139 -139
- package/lib/hooks.d.ts +19 -0
- package/lib/hooks.js +30 -7
- package/lib/index.js +18 -3
- package/lib/init.js +19 -4
- package/lib/lib/hook_audit.js +64 -2
- package/lib/lib/prompt_safety.js +14 -3
- package/lib/tools.d.ts +7 -0
- package/lib/tools.js +22 -1
- package/md_cg/_guard_kit.py +121 -121
- package/md_cg/anchor.py +114 -114
- package/md_cg/audit.py +15 -2
- package/md_cg/chain.py +3 -2
- package/md_cg/config_registry_guard.py +487 -487
- package/md_cg/conformance.py +747 -747
- package/md_cg/consolidate.py +1793 -1793
- package/md_cg/crypto.py +794 -794
- package/md_cg/hyperedge.py +262 -262
- package/md_cg/interface_schema.py +146 -146
- package/md_cg/lifecycle.py +426 -426
- package/md_cg/mdcos.py +12 -8
- package/md_cg/predict.py +1103 -1103
- package/md_cg/protect.py +654 -654
- package/md_cg/provenance.py +606 -606
- package/md_cg/queue_health.py +130 -130
- package/md_cg/refine.py +613 -613
- package/md_cg/retrieve.py +128 -128
- package/md_cg/revise.py +140 -140
- package/md_cg/rollback.py +657 -657
- package/md_cg/rollback_cli.py +124 -124
- package/md_cg/rotate.py +346 -346
- package/md_cg/routing.py +490 -490
- package/md_cg/self_surface.py +50 -50
- package/md_cg/semantic/canonical.py +144 -144
- package/md_cg/semantic/zh_en_atoms.py +141 -141
- package/md_cg/sources.py +1091 -1091
- package/md_cg/stg.py +3 -2
- package/md_cg/sustain.py +10 -1
- package/md_cg/test_audit_rotate_parity.py +279 -279
- package/md_cg/test_autonomy_modes.py +2013 -2013
- package/md_cg/test_cn_recall_grams.py +206 -206
- package/md_cg/test_code_kind_forbidden.py +101 -0
- package/md_cg/test_condaccept.py +156 -156
- package/md_cg/test_condgate.py +141 -141
- package/md_cg/test_heartbeat_ledger.py +9 -3
- package/md_cg/test_i50e_readside_protection.py +658 -658
- package/md_cg/test_issue111_gate_order.py +76 -76
- package/md_cg/test_issue111_scoring_contract.py +86 -86
- package/md_cg/test_issue121_calibration_bounds.py +293 -293
- package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
- package/md_cg/test_issue147_pack_id_escape.py +378 -378
- package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
- package/md_cg/test_issue149_accel_default_face.py +248 -248
- package/md_cg/test_issue150_empty_query_terms.py +328 -328
- package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
- package/md_cg/test_issue152_sealed_postings.py +309 -309
- package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
- package/md_cg/test_issue157b_index_remainder.py +344 -344
- package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
- package/md_cg/test_issue191_import_trust.py +382 -382
- package/md_cg/test_issue193_write_scope.py +378 -378
- package/md_cg/test_issue212_writeface_gaps.py +349 -349
- package/md_cg/test_issue214_reject_sensitivity.py +266 -266
- package/md_cg/test_issue215_condition_source_gap.py +272 -272
- package/md_cg/test_issue216_clash_routes.py +214 -214
- package/md_cg/test_issue217_slot_routes.py +248 -248
- package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
- package/md_cg/test_issue258_pack_sensitivity.py +468 -468
- package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
- package/md_cg/test_issue274_read_node_id_none.py +341 -341
- package/md_cg/test_issue275_layer_param.py +250 -250
- package/md_cg/test_issue277_node_id_traversal.py +384 -384
- package/md_cg/test_issue278_neg_separated_tension.py +271 -271
- package/md_cg/test_issue278_neg_window.py +294 -294
- package/md_cg/test_issue278_negative_layering.py +83 -83
- package/md_cg/test_issue284_skip_not_pass.py +291 -291
- package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
- package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
- package/md_cg/test_issue303_design_boundaries.py +262 -262
- package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
- package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
- package/md_cg/test_issue321_mcp_deep_json.py +304 -304
- package/md_cg/test_issue322_node_write_lock.py +727 -727
- package/md_cg/test_issue324_write_size_fields.py +214 -214
- package/md_cg/test_issue325_audit_poison.py +229 -229
- package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
- package/md_cg/test_issue326_key_perms.py +355 -355
- package/md_cg/test_issue326_twin_parser_parity.py +323 -323
- package/md_cg/test_issue326_vm_call_frame.py +569 -569
- package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
- package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
- package/md_cg/test_issue348_insight_verify_op.py +199 -199
- package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
- package/md_cg/test_issue350_casefold_collision.py +235 -235
- package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
- package/md_cg/test_issue352_imgaudit.py +418 -418
- package/md_cg/test_issue352_pathsjson.py +537 -537
- package/md_cg/test_issue352_refindex.py +412 -412
- package/md_cg/test_issue352_rollback.py +604 -604
- package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
- package/md_cg/test_issue354_rust_index_containment.py +571 -571
- package/md_cg/test_issue354_rust_wal_containment.py +606 -606
- package/md_cg/test_issue358_truncation_observable.py +247 -247
- package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
- package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
- package/md_cg/test_issue362_neg_condition.py +468 -468
- package/md_cg/test_issue363_lexical_idf.py +275 -275
- package/md_cg/test_issue370_readcache_assembly.py +488 -488
- package/md_cg/test_issue376_heartbeat_failed.py +293 -293
- package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
- package/md_cg/test_issue378_protected_floor.py +310 -310
- package/md_cg/test_issue383_state_event_write_gate.py +462 -462
- package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
- package/md_cg/test_issue398_toolface_op_parity.py +511 -511
- package/md_cg/test_issue399_hive_json_shape.py +317 -317
- package/md_cg/test_issue401_wisdom_codegen.py +295 -295
- package/md_cg/test_issue404_heal_receipt.py +389 -389
- package/md_cg/test_issue409_compact_index_completeness.py +509 -509
- package/md_cg/test_issue410_verify_demote_write.py +366 -366
- package/md_cg/test_issue411_scrub_idempotence.py +575 -575
- package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
- package/md_cg/test_issue413_preimage_failclosed.py +239 -239
- package/md_cg/test_issue413_preimage_twin.py +339 -339
- package/md_cg/test_issue415_deps_gate_probe.py +357 -357
- package/md_cg/test_issue416_entry_key_subset.py +525 -525
- package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
- package/md_cg/test_issue425_ingest_library_path.py +331 -331
- package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
- package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
- package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
- package/md_cg/test_issue427_serve_status_shape.py +191 -191
- package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
- package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
- package/md_cg/test_issue434_435_float_gate.py +90 -90
- package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
- package/md_cg/test_issue43_default_policy.py +874 -874
- package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
- package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
- package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
- package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
- package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
- package/md_cg/test_issue451_audit_actor.py +313 -313
- package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
- package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
- package/md_cg/test_issue466_basis_registry.py +469 -469
- package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
- package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
- package/md_cg/test_issue470_wal_event_durable.py +427 -427
- package/md_cg/test_issue471_identity_readable.py +136 -136
- package/md_cg/test_issue472_audit_chain.py +90 -90
- package/md_cg/test_issue472_prune_ledger.py +105 -105
- package/md_cg/test_issue472_rollback_degraded.py +81 -81
- package/md_cg/test_issue491_compose_sandbox.py +460 -460
- package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
- package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
- package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
- package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
- package/md_cg/test_issue525_read_null_semantics.py +52 -52
- package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
- package/md_cg/test_issue547_audit_durable.py +96 -96
- package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
- package/md_cg/test_issue559_refine_prefix.py +69 -69
- package/md_cg/test_issue562_retired_faces.py +150 -0
- package/md_cg/test_issue71_queue_backlog.py +376 -376
- package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
- package/md_cg/test_issue82_credential_rules.py +271 -271
- package/md_cg/test_issue91_verifier_sandbox.py +314 -314
- package/md_cg/test_issue93_remember_redaction.py +282 -282
- package/md_cg/test_logref.py +1141 -1141
- package/md_cg/test_md_access_parity.py +330 -330
- package/md_cg/test_md_cg_no_hive_dep.py +139 -139
- package/md_cg/test_mutation_residue.py +119 -119
- package/md_cg/test_mutation_rollback.py +835 -835
- package/md_cg/test_n123_derive_expiry_chain.py +210 -210
- package/md_cg/test_n184_keys_concurrent_provision.py +1 -1
- package/md_cg/test_n209_verify_write_face_gates.py +466 -466
- package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
- package/md_cg/test_neg_tail_honesty.py +733 -733
- package/md_cg/test_p21_tokens.py +294 -294
- package/md_cg/test_p27_docindex.py +792 -792
- package/md_cg/test_p28_refcheck.py +1053 -1053
- package/md_cg/test_p2_six_elements.py +476 -476
- package/md_cg/test_p33_ccg_wiring.py +300 -300
- package/md_cg/test_p3_legacy_closure.py +438 -438
- package/md_cg/test_p40_refine_worklist.py +244 -244
- package/md_cg/test_p4_freshness.py +781 -781
- package/md_cg/test_p9c_dedup_hints.py +288 -288
- package/md_cg/test_policy_required_ccg.py +444 -444
- package/md_cg/test_read_scope_b27.py +2 -2
- package/md_cg/test_retr_s5.py +312 -312
- package/md_cg/test_s1_ast_judge.py +109 -109
- package/md_cg/test_security_audit_v21.py +249 -249
- package/md_cg/test_state_event_op.py +521 -521
- package/md_cg/test_stgidx_index_parity.py +1024 -1024
- package/md_cg/test_sustain_bad_stamp.py +178 -0
- package/md_cg/test_sustain_bounded.py +652 -652
- package/md_cg/test_trust_edge_reachability.py +405 -405
- package/md_cg/test_w7_redlines.py +659 -659
- package/md_cg/test_write_return_shape.py +919 -919
- package/md_cg/test_writepipe.py +228 -228
- package/md_cg/trust.py +1114 -1114
- package/md_cg/twophase.py +303 -303
- package/md_cg/whitebox.py +536 -536
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
- package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
- package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
- package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
- package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
- package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
- package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
- package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
- package/md_cg/writepipe.py +1337 -1337
- package/package.json +3 -2
- package/scripts/workspace_index.py +328 -0
- package/src/hooks.ts +932 -908
- package/src/index.ts +21 -6
- package/src/init.ts +16 -4
- package/src/lib/hook_audit.ts +48 -2
- package/src/lib/mdcg_client.ts +469 -469
- package/src/lib/prompt_safety.ts +17 -3
- package/src/tools.ts +23 -1
- package/zcode/AGENTS.md +333 -204
|
@@ -1,537 +1,537 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# 功能名:paths.json 解析失败时不得无声回落默认记忆库(issue #352-D)
|
|
3
|
-
# 生效条件:python -X utf8 -m md_cg.test_issue352_pathsjson [--mutate];被测面=md_cg/datapath.py 的 _read_user_paths/_user_paths/mdcg_root/data_root/set_user_root + src/lib/datapath.ts 的 readUserPaths/mdcgRoot
|
|
4
|
-
# 子功能:A 组=UTF-8 BOM 与非法转义两类常见写坏在 Python 侧与插件侧都不得无声回落;K 组=非恒真对照(合法配置照常生效、无文件时仍回落默认库、三态口径、set_user_root 不冲掉其余键、原子写);E 组=源码面单点
|
|
5
|
-
# 执行:每个变体建独立临时沙箱,先在自定义根写一条既有记忆,再写 paths.json(合法/BOM/非法转义/错形状),用子进程读 mdcg_root()+MdCGOS;插件侧用 node 直接 import src/lib/datapath.ts
|
|
6
|
-
# 验证方式:全部 PASS 即通过;--mutate 走镜像树定点变异,变异后红项必须**恰为**预期集合
|
|
7
|
-
# 不适用条件:①**坏配置 ⇒ 插件激活失败与各 CLI 退出是刻意的 fail-closed 选择**(本处按 #161 先例:静默回落会造成「原有记忆消失 + 新写入分叉」的数据分叉,比「起不来」严重);退路(console.error + describeDataPaths/op=info 点名后回落)实现成本一处——PathsConfigError 的 code/pathsFile 已留好——**是否切换归维护者裁定**,本守卫两种口径下都成立(断言只要求「不静默」)②不判路径本身是否合法/可建(保留设备名由 _abs_host_path 另管,见 test_issue354_p2_datapath_clock)③不判 paths.json 的取值优先级(env > paths.json > 默认,既有面未改)
|
|
8
|
-
"""issue #352-D 守卫:坏 paths.json 不得无声回落默认库。
|
|
9
|
-
|
|
10
|
-
病象(修前):Python `_user_paths()` 与插件 `readUserPaths()` 都用一个「吞掉
|
|
11
|
-
一切异常的 except」把「文件不存在」与「文件存在但坏了」混成同一个 `{}`——于是
|
|
12
|
-
`mcg_root()` 无声回落到默认库:用户看到「原有记忆没了」,新写入分叉进另一个库
|
|
13
|
-
(插件侧仍按 config 走,两边就此分成两个库)。Windows 上两种常见写坏:
|
|
14
|
-
· 记事本 / PowerShell 5 的 `Set-Content -Encoding UTF8` 写出 UTF-8 BOM;
|
|
15
|
-
· 照抄资源管理器路径时反斜杠未转义——Windows 路径分隔符在 JSON 串里
|
|
16
|
-
必须写成双反斜杠或改用正斜杠,单写即非法转义序列。
|
|
17
|
-
|
|
18
|
-
处置(**响亮失败**,本仓先例 #161「打包面静默坏包 ⇒ 改为响亮失败」,Lead 本轮
|
|
19
|
-
指定按同一精神):
|
|
20
|
-
1. BOM 属编码形态而非语法错 ⇒ 两侧都剥掉(Python `utf-8-sig`、TS
|
|
21
|
-
`replace(/^\uFEFF/, '')`),**能解析就正常生效**——不是「有 BOM 就报错」
|
|
22
|
-
(K3 就是这条的口径对照,M2 证明它有判别力);
|
|
23
|
-
2. 真正解析不了(含顶层非对象)⇒ 抛 `PathsConfigError`,绝不回落;
|
|
24
|
-
3. `set_user_root` 读到坏文件时**拒绝写**(改前会用空表覆写,把用户其余键
|
|
25
|
-
一并冲掉),落盘改走 `fsutil.atomic_write`(改前非原子,崩溃留下的截断件
|
|
26
|
-
正是下一条「解析失败」的来源)。
|
|
27
|
-
|
|
28
|
-
★**过度收紧方向**:K1(合法配置照常生效)、K2(无文件时仍回落默认库)、
|
|
29
|
-
K6(TS 侧无文件时仍回落)三条盯住「修复没把正常面一起关掉」。
|
|
30
|
-
"""
|
|
31
|
-
import json
|
|
32
|
-
import os
|
|
33
|
-
import shutil
|
|
34
|
-
import subprocess
|
|
35
|
-
import sys
|
|
36
|
-
import tempfile
|
|
37
|
-
|
|
38
|
-
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
39
|
-
|
|
40
|
-
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
41
|
-
sys.path.insert(0, _REPO)
|
|
42
|
-
from md_cg import datapath as _dp # noqa: E402
|
|
43
|
-
|
|
44
|
-
FAILS = []
|
|
45
|
-
_SKIPPED = []
|
|
46
|
-
_TMP = tempfile.mkdtemp(prefix="iss352d_")
|
|
47
|
-
_NODE = shutil.which("node")
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
def _check(name, cond, detail=""):
|
|
51
|
-
if cond:
|
|
52
|
-
print(" PASS " + name)
|
|
53
|
-
else:
|
|
54
|
-
print(" FAIL " + name + " " + str(detail)[:400])
|
|
55
|
-
FAILS.append(name)
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
_SEED = (
|
|
59
|
-
"import sys; sys.path.insert(0, sys.argv[1]);"
|
|
60
|
-
"from md_cg.mdcos import MdCGOS;"
|
|
61
|
-
"c = MdCGOS(sys.argv[2], autoflush=1);"
|
|
62
|
-
"c.add('old_pref', 'PASSED 旧偏置:用户对花生过敏', layer='knowledge');"
|
|
63
|
-
" c.close()")
|
|
64
|
-
|
|
65
|
-
_CHILD = '''
|
|
66
|
-
import json, sys
|
|
67
|
-
sys.path.insert(0, sys.argv[1])
|
|
68
|
-
out = {"raised": None, "root": None, "configured": None, "seen": None,
|
|
69
|
-
"msg_ok": None}
|
|
70
|
-
try:
|
|
71
|
-
from md_cg import datapath
|
|
72
|
-
except Exception as e:
|
|
73
|
-
out["raised"] = "import:" + type(e).__name__
|
|
74
|
-
print(json.dumps(out, ensure_ascii=False)); raise SystemExit(0)
|
|
75
|
-
try:
|
|
76
|
-
out["root"] = datapath.mdcg_root()
|
|
77
|
-
out["configured"] = datapath.mdcg_root_configured()
|
|
78
|
-
from md_cg.mdcos import MdCGOS
|
|
79
|
-
cg = MdCGOS(out["root"], autoflush=1)
|
|
80
|
-
out["seen"] = bool(cg.get("old_pref"))
|
|
81
|
-
cg.close()
|
|
82
|
-
except datapath.PathsConfigError as e:
|
|
83
|
-
out["raised"] = "PathsConfigError"
|
|
84
|
-
out["msg_ok"] = ("paths.json" in str(e))
|
|
85
|
-
except Exception as e:
|
|
86
|
-
out["raised"] = type(e).__name__
|
|
87
|
-
print(json.dumps(out, ensure_ascii=False))
|
|
88
|
-
'''
|
|
89
|
-
|
|
90
|
-
_NODEJS = '''
|
|
91
|
-
import { pathToFileURL } from 'node:url'
|
|
92
|
-
const mod = await import(pathToFileURL(process.argv[2]).href)
|
|
93
|
-
const out = { raised: null, root: null, msg_ok: null }
|
|
94
|
-
try {
|
|
95
|
-
out.root = mod.mdcgRoot('')
|
|
96
|
-
} catch (e) {
|
|
97
|
-
out.raised = e.name
|
|
98
|
-
out.msg_ok = String(e.message).includes('paths.json')
|
|
99
|
-
}
|
|
100
|
-
console.log(JSON.stringify(out))
|
|
101
|
-
'''
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def _py_env(t, extra=None):
|
|
105
|
-
env = {k: v for k, v in os.environ.items()
|
|
106
|
-
if not k.startswith(("MDCG_", "DSH_", "PYTHON"))}
|
|
107
|
-
env.update({"HOME": t, "MDCG_STATE_ROOT": os.path.join(t, "state"),
|
|
108
|
-
# 末段不得是 Windows 保留设备名(aux/con/nul…)——_abs_host_path
|
|
109
|
-
# 会据此 fail-closed 拒启(test_issue354_p2_datapath_clock 同一判据)。
|
|
110
|
-
"MDCG_AUX_ROOT": os.path.join(t, "auxstore"), "MDCG_SLEEP": "0",
|
|
111
|
-
"PYTHONUTF8": "1", "PYTHONPATH": _REPO})
|
|
112
|
-
if extra:
|
|
113
|
-
env.update(extra)
|
|
114
|
-
return env
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
def _py_case(tag, writer, seed=True):
|
|
118
|
-
"""跑一个 Python 变体:返回子进程读数 dict(含 testdir)。"""
|
|
119
|
-
t = tempfile.mkdtemp(prefix="iss352d_%s_" % tag, dir=_TMP)
|
|
120
|
-
state = os.path.join(t, "state")
|
|
121
|
-
os.makedirs(state, exist_ok=True)
|
|
122
|
-
custom = os.path.join(t, "MyMemory")
|
|
123
|
-
env = _py_env(t)
|
|
124
|
-
if seed:
|
|
125
|
-
subprocess.run([sys.executable, "-c", _SEED, _REPO, custom], env=env,
|
|
126
|
-
check=True, capture_output=True)
|
|
127
|
-
writer(os.path.join(state, "paths.json"), custom)
|
|
128
|
-
p = subprocess.run([sys.executable, "-c", _CHILD, _REPO], env=env,
|
|
129
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
130
|
-
errors="replace")
|
|
131
|
-
try:
|
|
132
|
-
out = json.loads(p.stdout.strip().splitlines()[-1])
|
|
133
|
-
except Exception: # noqa: BLE001
|
|
134
|
-
out = {"raised": "NOJSON", "stdout": (p.stdout or "")[-160:],
|
|
135
|
-
"stderr": (p.stderr or "")[-160:]}
|
|
136
|
-
out["custom"] = custom
|
|
137
|
-
out["testdir"] = t
|
|
138
|
-
return out
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
def _w_raw(data, name="paths.json"):
|
|
142
|
-
def _w(pf, custom):
|
|
143
|
-
with open(pf, "wb") as f:
|
|
144
|
-
f.write(data(custom) if callable(data) else data)
|
|
145
|
-
return _w
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
def _w_obj(fn):
|
|
149
|
-
def _w(pf, custom):
|
|
150
|
-
with open(pf, "w", encoding="utf-8") as f:
|
|
151
|
-
json.dump(fn(custom), f, ensure_ascii=False)
|
|
152
|
-
return _w
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
def _node_case(tag, mkpaths):
|
|
156
|
-
"""跑一个 TS 变体:node 直接 import src/lib/datapath.ts。"""
|
|
157
|
-
if _NODE is None:
|
|
158
|
-
return None
|
|
159
|
-
t = tempfile.mkdtemp(prefix="iss352d_%s_" % tag, dir=_TMP)
|
|
160
|
-
state = os.path.join(t, "state")
|
|
161
|
-
os.makedirs(state, exist_ok=True)
|
|
162
|
-
custom = os.path.join(t, "MyMemory")
|
|
163
|
-
js = os.path.join(_TMP, "ts_%s.mjs" % tag)
|
|
164
|
-
with open(js, "w", encoding="utf-8") as f:
|
|
165
|
-
f.write(_NODEJS)
|
|
166
|
-
if mkpaths is not None:
|
|
167
|
-
mkpaths(os.path.join(state, "paths.json"), custom)
|
|
168
|
-
env = {k: v for k, v in os.environ.items()
|
|
169
|
-
if k not in ("MDCG_ROOT", "MDCG_DATA_ROOT")}
|
|
170
|
-
env["MDCG_STATE_ROOT"] = state
|
|
171
|
-
p = subprocess.run([_NODE, js, os.path.join(_REPO, "src", "lib",
|
|
172
|
-
"datapath.ts")],
|
|
173
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
174
|
-
errors="replace", env=env, cwd=_REPO)
|
|
175
|
-
try:
|
|
176
|
-
out = json.loads(p.stdout.strip().splitlines()[-1])
|
|
177
|
-
except Exception: # noqa: BLE001
|
|
178
|
-
out = {"raised": "NOJSON", "stdout": (p.stdout or "")[-160:],
|
|
179
|
-
"stderr": (p.stderr or "")[-200:]}
|
|
180
|
-
out["custom"] = custom
|
|
181
|
-
return out
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
def _same(a, b):
|
|
185
|
-
return bool(a) and os.path.normcase(os.path.abspath(a)) == \
|
|
186
|
-
os.path.normcase(os.path.abspath(b))
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
_BOM = lambda c: b"\xef\xbb\xbf" + json.dumps({"root": c}).encode("utf-8")
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
def _bad_escape(c):
|
|
193
|
-
# 未转义反斜杠:\M 非法转义(照抄资源管理器路径的常见形态)
|
|
194
|
-
return ('{"root": "%s"}' % c.replace(os.sep, "\\")).encode("utf-8")
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
print("== A 组:坏/带 BOM 的 paths.json 不得无声回落 ==")
|
|
198
|
-
|
|
199
|
-
# --- D1:UTF-8 BOM + 合法 JSON ⇒ BOM 属编码形态,应正常生效(不是「有 BOM 就报错」)
|
|
200
|
-
_d1 = _py_case("d1", _w_raw(_BOM))
|
|
201
|
-
print(" [读数] D1 BOM 合法配置 → raised=%s root==custom=%s 既有记忆可读=%s"
|
|
202
|
-
% (_d1.get("raised"), _same(_d1.get("root"), _d1["custom"]),
|
|
203
|
-
_d1.get("seen")))
|
|
204
|
-
_check("D1 UTF-8 BOM 的 paths.json:正常生效(root==自定义根、既有记忆读得到),"
|
|
205
|
-
"不回落默认库",
|
|
206
|
-
_d1.get("raised") is None and _same(_d1.get("root"), _d1["custom"])
|
|
207
|
-
and _d1.get("configured") is True and _d1.get("seen") is True, _d1)
|
|
208
|
-
|
|
209
|
-
# --- D2:非法转义(真语法错)⇒ 响亮失败,不静默回落
|
|
210
|
-
_d2 = _py_case("d2", _w_raw(_bad_escape))
|
|
211
|
-
print(" [读数] D2 非法转义 → raised=%s 消息含 paths.json=%s root=%s"
|
|
212
|
-
% (_d2.get("raised"), _d2.get("msg_ok"), _d2.get("root")))
|
|
213
|
-
_check("D2 非法转义的 paths.json:响亮失败(PathsConfigError,消息点名 paths.json),"
|
|
214
|
-
"**不**静默回落到默认库",
|
|
215
|
-
_d2.get("raised") == "PathsConfigError" and _d2.get("msg_ok") is True
|
|
216
|
-
and _d2.get("root") is None, _d2)
|
|
217
|
-
|
|
218
|
-
# --- D4:合法 JSON、错形状(顶层数组)⇒ 同一口径响亮失败
|
|
219
|
-
_d4 = _py_case("d4", _w_raw(b'["not", "an", "object"]'))
|
|
220
|
-
print(" [读数] D4 顶层数组 → raised=%s" % _d4.get("raised"))
|
|
221
|
-
_check("D4 顶层非 JSON 对象:同一口径响亮失败(不按「解析成功」放行)",
|
|
222
|
-
_d4.get("raised") == "PathsConfigError" and _d4.get("root") is None, _d4)
|
|
223
|
-
|
|
224
|
-
# --- D3/D5/K6:插件侧(node 直接 import src/lib/datapath.ts)
|
|
225
|
-
_ts_bom = _node_case("tsbom", _w_raw(_BOM))
|
|
226
|
-
_ts_bad = _node_case("tsbad", _w_raw(b'{"root": '))
|
|
227
|
-
_ts_none = _node_case("tsnone", None)
|
|
228
|
-
if _ts_bom is None:
|
|
229
|
-
_SKIPPED.append("D3/D5/K6")
|
|
230
|
-
print(" [跳过] 插件侧腿:本机无 node")
|
|
231
|
-
else:
|
|
232
|
-
print(" [读数] D3 TS BOM → raised=%s root==custom=%s"
|
|
233
|
-
% (_ts_bom.get("raised"), _same(_ts_bom.get("root"), _ts_bom["custom"])))
|
|
234
|
-
_check("D3 插件侧 UTF-8 BOM 的 paths.json:正常生效(root==自定义根)",
|
|
235
|
-
_ts_bom.get("raised") is None
|
|
236
|
-
and _same(_ts_bom.get("root"), _ts_bom["custom"]), _ts_bom)
|
|
237
|
-
print(" [读数] D5 TS 语法错 → raised=%s 消息含 paths.json=%s"
|
|
238
|
-
% (_ts_bad.get("raised"), _ts_bad.get("msg_ok")))
|
|
239
|
-
_check("D5 插件侧无法解析的 paths.json:抛 PathsConfigError(不无声回落默认根)",
|
|
240
|
-
_ts_bad.get("raised") == "PathsConfigError"
|
|
241
|
-
and _ts_bad.get("msg_ok") is True and _ts_bad.get("root") is None,
|
|
242
|
-
_ts_bad)
|
|
243
|
-
|
|
244
|
-
# ========================================================= K 组:非恒真对照
|
|
245
|
-
print("== K 组:非恒真对照(含**过度收紧**方向)==")
|
|
246
|
-
|
|
247
|
-
# --- K1:合法 paths.json 正常生效(不过度收紧)
|
|
248
|
-
_k1 = _py_case("k1", _w_obj(lambda c: {"root": c}))
|
|
249
|
-
print(" [读数] K1 合法配置 → root==custom=%s configured=%s seen=%s"
|
|
250
|
-
% (_same(_k1.get("root"), _k1["custom"]), _k1.get("configured"),
|
|
251
|
-
_k1.get("seen")))
|
|
252
|
-
_check("K1 合法 paths.json 照常生效(root==自定义根、既有记忆读得到、无异常)",
|
|
253
|
-
_k1.get("raised") is None and _same(_k1.get("root"), _k1["custom"])
|
|
254
|
-
and _k1.get("configured") is True and _k1.get("seen") is True, _k1)
|
|
255
|
-
|
|
256
|
-
# --- K2:**没有** paths.json 时必须照旧回落默认库(过度收紧方向)
|
|
257
|
-
_k2 = _py_case("k2", lambda pf, custom: None, seed=False)
|
|
258
|
-
_k2_dflt = os.path.join(_k2["testdir"], "state", "data", "mdcg")
|
|
259
|
-
print(" [读数] K2 无配置文件 → raised=%s root=%s"
|
|
260
|
-
% (_k2.get("raised"), _k2.get("root")))
|
|
261
|
-
_check("K2 无 paths.json(文件不存在)时仍回落默认库——不把「没配过」也判成坏配置",
|
|
262
|
-
_k2.get("raised") is None and _same(_k2.get("root"), _k2_dflt)
|
|
263
|
-
and _k2.get("configured") is False, (_k2, _k2_dflt))
|
|
264
|
-
|
|
265
|
-
# --- K3:三态读取单点(missing / ok / bad 三分,不再混成 {})
|
|
266
|
-
def _states():
|
|
267
|
-
res = {}
|
|
268
|
-
t = tempfile.mkdtemp(prefix="iss352d_k3_", dir=_TMP)
|
|
269
|
-
state = os.path.join(t, "state")
|
|
270
|
-
os.makedirs(state)
|
|
271
|
-
old = os.environ.get("MDCG_STATE_ROOT")
|
|
272
|
-
os.environ["MDCG_STATE_ROOT"] = state
|
|
273
|
-
try:
|
|
274
|
-
res["missing"] = _dp._read_user_paths()[1]
|
|
275
|
-
with open(os.path.join(state, "paths.json"), "wb") as f:
|
|
276
|
-
f.write(_BOM(os.path.join(t, "My")))
|
|
277
|
-
res["bom_ok"] = _dp._read_user_paths()[1]
|
|
278
|
-
with open(os.path.join(state, "paths.json"), "wb") as f:
|
|
279
|
-
f.write(_bad_escape(os.path.join(t, "My")))
|
|
280
|
-
res["bad_escape"] = _dp._read_user_paths()[1]
|
|
281
|
-
res["raises"] = None
|
|
282
|
-
try:
|
|
283
|
-
_dp._user_paths()
|
|
284
|
-
except _dp.PathsConfigError:
|
|
285
|
-
res["raises"] = "PathsConfigError"
|
|
286
|
-
finally:
|
|
287
|
-
if old is None:
|
|
288
|
-
os.environ.pop("MDCG_STATE_ROOT", None)
|
|
289
|
-
else:
|
|
290
|
-
os.environ["MDCG_STATE_ROOT"] = old
|
|
291
|
-
return res
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
_k3 = _states()
|
|
295
|
-
print(" [读数] K3 三态=%s 严格读抛=%s" % (_k3, _k3["raises"]))
|
|
296
|
-
_check("K3 三态口径单点:missing / ok(BOM 剥得掉)/ bad(真语法错)三分,"
|
|
297
|
-
"且 strict 读对 bad 抛错",
|
|
298
|
-
_k3["missing"] == "missing" and _k3["bom_ok"] == "ok"
|
|
299
|
-
and _k3["bad_escape"] == "bad"
|
|
300
|
-
and _k3["raises"] == "PathsConfigError", _k3)
|
|
301
|
-
|
|
302
|
-
# --- K4:set_user_root 对坏文件拒写且**不冲掉**其余键
|
|
303
|
-
_k4t = tempfile.mkdtemp(prefix="iss352d_k4_", dir=_TMP)
|
|
304
|
-
_k4state = os.path.join(_k4t, "state")
|
|
305
|
-
os.makedirs(_k4state)
|
|
306
|
-
_k4pf = os.path.join(_k4state, "paths.json")
|
|
307
|
-
_k4raw = ('{"root": "%s", "extra": "KEEP-ME"}'
|
|
308
|
-
% os.path.join(_k4t, "My")).replace(os.sep, "\\")
|
|
309
|
-
with open(_k4pf, "wb") as f:
|
|
310
|
-
f.write(_k4raw.encode("utf-8"))
|
|
311
|
-
_old = os.environ.get("MDCG_STATE_ROOT")
|
|
312
|
-
os.environ["MDCG_STATE_ROOT"] = _k4state
|
|
313
|
-
try:
|
|
314
|
-
try:
|
|
315
|
-
_dp.set_user_root("E:/new", "data_root")
|
|
316
|
-
_k4r = "写了"
|
|
317
|
-
except _dp.PathsConfigError:
|
|
318
|
-
_k4r = "PathsConfigError"
|
|
319
|
-
finally:
|
|
320
|
-
if _old is None:
|
|
321
|
-
os.environ.pop("MDCG_STATE_ROOT", None)
|
|
322
|
-
else:
|
|
323
|
-
os.environ["MDCG_STATE_ROOT"] = _old
|
|
324
|
-
_k4after = open(_k4pf, "rb").read().decode("utf-8")
|
|
325
|
-
print(" [读数] K4 坏文件 set_user_root=%s 原文未动=%s"
|
|
326
|
-
% (_k4r, _k4after == _k4raw))
|
|
327
|
-
_check("K4 set_user_root 读到坏文件时拒绝写,且**不冲掉**用户其余键(字节不变)",
|
|
328
|
-
_k4r == "PathsConfigError" and _k4after == _k4raw, (_k4r, _k4after[:60]))
|
|
329
|
-
|
|
330
|
-
# --- K5:set_user_root 对好文件正常写入、原子落盘可读回
|
|
331
|
-
_k5t = tempfile.mkdtemp(prefix="iss352d_k5_", dir=_TMP)
|
|
332
|
-
_k5state = os.path.join(_k5t, "state")
|
|
333
|
-
os.makedirs(_k5state)
|
|
334
|
-
_old = os.environ.get("MDCG_STATE_ROOT")
|
|
335
|
-
os.environ["MDCG_STATE_ROOT"] = _k5state
|
|
336
|
-
try:
|
|
337
|
-
_w = _dp.set_user_root(os.path.join(_k5t, "My"), "root")
|
|
338
|
-
_d = json.load(open(_w, encoding="utf-8"))
|
|
339
|
-
finally:
|
|
340
|
-
if _old is None:
|
|
341
|
-
os.environ.pop("MDCG_STATE_ROOT", None)
|
|
342
|
-
else:
|
|
343
|
-
os.environ["MDCG_STATE_ROOT"] = _old
|
|
344
|
-
print(" [读数] K5 写入路径在用户级=%s 键齐=%s"
|
|
345
|
-
% (_w == os.path.join(_k5state, "paths.json"), sorted(_d)))
|
|
346
|
-
_check("K5 set_user_root 对好文件正常写入(用户级位置、含 _comment 与目标键、"
|
|
347
|
-
"可解析回来)",
|
|
348
|
-
_w == os.path.join(_k5state, "paths.json")
|
|
349
|
-
and _d.get("root") == os.path.join(_k5t, "My").replace(os.sep, "/")
|
|
350
|
-
and "_comment" in _d, (_w, sorted(_d)))
|
|
351
|
-
|
|
352
|
-
if _ts_none is not None:
|
|
353
|
-
_ts_dflt = os.path.join(_ts_none["custom"], "..", "state", "data", "mdcg")
|
|
354
|
-
print(" [读数] K6 TS 无配置文件 → raised=%s root=%s"
|
|
355
|
-
% (_ts_none.get("raised"), _ts_none.get("root")))
|
|
356
|
-
_check("K6 插件侧无 paths.json 时仍回落默认根(过度收紧方向)",
|
|
357
|
-
_ts_none.get("raised") is None
|
|
358
|
-
and _same(_ts_none.get("root"), _ts_dflt), _ts_none)
|
|
359
|
-
|
|
360
|
-
# ========================================================= E 组:源码面单点
|
|
361
|
-
print("== E 组:源码面单点 ==")
|
|
362
|
-
_dp_src = open(os.path.join(_REPO, "md_cg", "datapath.py"),
|
|
363
|
-
encoding="utf-8").read()
|
|
364
|
-
_ts_src = open(os.path.join(_REPO, "src", "lib", "datapath.ts"),
|
|
365
|
-
encoding="utf-8").read()
|
|
366
|
-
_check("E1 三态读取是唯一读取点(_user_paths 走 _read_user_paths,两者分开)",
|
|
367
|
-
"def _read_user_paths():" in _dp_src
|
|
368
|
-
and "d, st = _read_user_paths()" in _dp_src
|
|
369
|
-
and "class PathsConfigError(RuntimeError):" in _dp_src)
|
|
370
|
-
_check("E2 BOM 口径:Python 侧以 utf-8-sig 读 paths.json",
|
|
371
|
-
'open(pf, encoding="utf-8-sig")' in _dp_src)
|
|
372
|
-
_check("E3 插件侧:BOM 剥离 + PathsConfigError 抛出(与 Python 侧同一口径)",
|
|
373
|
-
"replace(/^\\uFEFF/, '')" in _ts_src
|
|
374
|
-
and "export class PathsConfigError extends Error" in _ts_src)
|
|
375
|
-
_check("E4 set_user_root 原子落盘(fsutil.atomic_write,不再裸 open('w'))",
|
|
376
|
-
"fsutil.atomic_write(pf," in _dp_src)
|
|
377
|
-
|
|
378
|
-
shutil.rmtree(_TMP, ignore_errors=True)
|
|
379
|
-
|
|
380
|
-
print("")
|
|
381
|
-
if FAILS:
|
|
382
|
-
print("issue #352-D 结果:%d 失败 → %s" % (len(FAILS), ", ".join(FAILS)))
|
|
383
|
-
else:
|
|
384
|
-
print("issue #352-D 结果:全部通过(跳过腿:%s)"
|
|
385
|
-
% (",".join(_SKIPPED) or "无"))
|
|
386
|
-
|
|
387
|
-
# ------------------------------------------------------------- 定点变异(镜像树)
|
|
388
|
-
_MUTATIONS = (
|
|
389
|
-
("M1 摘掉 Python 侧坏配置的响亮失败(回到静默返回 {})",
|
|
390
|
-
(("md_cg/datapath.py",
|
|
391
|
-
[' if st == "bad":',
|
|
392
|
-
' if strict:',
|
|
393
|
-
' raise PathsConfigError(_paths_config_message())',
|
|
394
|
-
' return {}'],
|
|
395
|
-
[' if st == "bad":',
|
|
396
|
-
' return {}']),),
|
|
397
|
-
# K3 也红:它断言 strict 读对 bad 抛错(三态单点的行为面)。
|
|
398
|
-
("D2", "D4", "K3")),
|
|
399
|
-
("M2 摘掉 BOM 兼容(utf-8-sig 回 utf-8)——把「编码形态」误判成坏配置",
|
|
400
|
-
(("md_cg/datapath.py",
|
|
401
|
-
[' with open(pf, encoding="utf-8-sig") as f:'],
|
|
402
|
-
[' with open(pf, encoding="utf-8") as f:']),),
|
|
403
|
-
("D1", "K3", "E2")),
|
|
404
|
-
("M3 摘掉 missing 分支(把「文件不存在」也判成坏配置)——过度收紧方向",
|
|
405
|
-
(("md_cg/datapath.py",
|
|
406
|
-
[' if not os.path.isfile(pf):',
|
|
407
|
-
' return {}, "missing"'],
|
|
408
|
-
[' if False:',
|
|
409
|
-
' return {}, "missing"']),),
|
|
410
|
-
# K3 也红:它的 missing 腿(文件不存在 ⇒ "missing")被这条变异一起改掉。
|
|
411
|
-
("K2", "K3")),
|
|
412
|
-
("M4 摘掉 set_user_root 对坏文件的拒写(回到用空表覆写、冲掉其余键)",
|
|
413
|
-
(("md_cg/datapath.py",
|
|
414
|
-
[' if st == "bad":',
|
|
415
|
-
' raise PathsConfigError('],
|
|
416
|
-
[' if False:',
|
|
417
|
-
' raise PathsConfigError(']),),
|
|
418
|
-
("K4",)),
|
|
419
|
-
("M5 摘掉插件侧 BOM 剥离",
|
|
420
|
-
(("src/lib/datapath.ts",
|
|
421
|
-
[" parsed = JSON.parse(raw.replace(/^\\uFEFF/, ''))"],
|
|
422
|
-
[" parsed = JSON.parse(raw)"]),),
|
|
423
|
-
("D3", "E3")),
|
|
424
|
-
("M6 摘掉插件侧「文件不存在」短路(把没配过也判成坏配置)——过度收紧方向",
|
|
425
|
-
(("src/lib/datapath.ts",
|
|
426
|
-
[" if (!existsSync(pf)) return {}"],
|
|
427
|
-
[" if (false) return {}"]),),
|
|
428
|
-
("K6",)),
|
|
429
|
-
("M7 摘掉插件侧解析失败的抛出(回到静默空表回落)",
|
|
430
|
-
(("src/lib/datapath.ts",
|
|
431
|
-
[" throw new PathsConfigError(pathsConfigMessage(pf, String(e)), pf)"],
|
|
432
|
-
[" parsed = {}"]),),
|
|
433
|
-
("D5",)),
|
|
434
|
-
)
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
def _mirror_build():
|
|
438
|
-
dst = tempfile.mkdtemp(prefix="iss352d_mut_")
|
|
439
|
-
for name in ("md_cg", "scripts", "src"):
|
|
440
|
-
src = os.path.join(_REPO, name)
|
|
441
|
-
if os.path.isdir(src):
|
|
442
|
-
shutil.copytree(src, os.path.join(dst, name),
|
|
443
|
-
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
|
444
|
-
for name in sorted(os.listdir(_REPO)):
|
|
445
|
-
p = os.path.join(_REPO, name)
|
|
446
|
-
if os.path.isfile(p) and (name.endswith(".py")
|
|
447
|
-
or name in ("package.json", "tsconfig.json")):
|
|
448
|
-
shutil.copy2(p, os.path.join(dst, name))
|
|
449
|
-
return dst
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
def _nl_of(path):
|
|
453
|
-
with open(path, "rb") as fh:
|
|
454
|
-
raw = fh.read(8192)
|
|
455
|
-
return "\r\n" if b"\r\n" in raw else "\n"
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
def _run_in(tree):
|
|
459
|
-
env = dict(os.environ)
|
|
460
|
-
env["PYTHONPATH"] = tree
|
|
461
|
-
env["PYTHONUTF8"] = "1"
|
|
462
|
-
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
463
|
-
pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
|
|
464
|
-
"md_cg.test_issue352_pathsjson"],
|
|
465
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
466
|
-
errors="replace", cwd=tree, env=env, timeout=1800)
|
|
467
|
-
out = (pr.stdout or "") + (pr.stderr or "")
|
|
468
|
-
reds = [ln.split("FAIL ", 1)[1].strip()
|
|
469
|
-
for ln in out.splitlines() if "FAIL " in ln]
|
|
470
|
-
return pr.returncode, reds, out
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
def _mutate():
|
|
474
|
-
print("!! 定点变异自证:把修复点改回改动前形态,守卫必须转红")
|
|
475
|
-
tree = _mirror_build()
|
|
476
|
-
try:
|
|
477
|
-
rc, reds, out = _run_in(tree)
|
|
478
|
-
print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
|
|
479
|
-
if rc != 0 or reds:
|
|
480
|
-
print(chr(10).join(out.splitlines()[-30:]))
|
|
481
|
-
for ln in out.splitlines():
|
|
482
|
-
if "[读数]" in ln:
|
|
483
|
-
print(" 修后读数(未变异镜像):" + ln.strip())
|
|
484
|
-
okc, bad = 0, []
|
|
485
|
-
for desc, edits, exp in _MUTATIONS:
|
|
486
|
-
backups, miss = [], []
|
|
487
|
-
for rel, oldp, newp in edits:
|
|
488
|
-
p = os.path.join(tree, rel)
|
|
489
|
-
nl = _nl_of(p)
|
|
490
|
-
old, new = nl.join(oldp), nl.join(newp)
|
|
491
|
-
with open(p, encoding="utf-8", newline="") as fh:
|
|
492
|
-
t = fh.read()
|
|
493
|
-
if t.count(old) != 1:
|
|
494
|
-
miss.append("%s —— 锚点命中 %d 次" % (rel, t.count(old)))
|
|
495
|
-
continue
|
|
496
|
-
backups.append((p, t))
|
|
497
|
-
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
498
|
-
fh.write(t.replace(old, new, 1))
|
|
499
|
-
if miss:
|
|
500
|
-
bad.append("%s —— %s" % (desc, ";".join(miss)))
|
|
501
|
-
print(" ANCHOR-MISS " + desc)
|
|
502
|
-
for p, t in backups:
|
|
503
|
-
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
504
|
-
fh.write(t)
|
|
505
|
-
continue
|
|
506
|
-
rc2, reds2, out2 = _run_in(tree)
|
|
507
|
-
for p, t in backups:
|
|
508
|
-
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
509
|
-
fh.write(t)
|
|
510
|
-
hit = sorted({e for r in reds2 for e in exp if r.startswith(e + " ")})
|
|
511
|
-
if rc2 != 0 and hit == sorted(exp) and len(reds2) == len(exp):
|
|
512
|
-
okc += 1
|
|
513
|
-
print(" OK %s 红项=%d 指定命中=%d/%d"
|
|
514
|
-
% (desc, len(reds2), len(hit), len(exp)))
|
|
515
|
-
else:
|
|
516
|
-
bad.append(desc)
|
|
517
|
-
print(" MISMATCH %s 红项=%d 指定命中=%d/%d rc=%d"
|
|
518
|
-
% (desc, len(reds2), len(hit), len(exp), rc2))
|
|
519
|
-
for r in reds2[:14]:
|
|
520
|
-
print(" FAIL " + r)
|
|
521
|
-
print("定点变异自证:%s(%d 条不合格)"
|
|
522
|
-
% ("PASS" if not bad else "FAIL", len(bad)))
|
|
523
|
-
for b in bad:
|
|
524
|
-
print(" 不合格:" + b)
|
|
525
|
-
finally:
|
|
526
|
-
shutil.rmtree(tree, ignore_errors=True)
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
def main():
|
|
530
|
-
if "--mutate" in sys.argv:
|
|
531
|
-
_mutate()
|
|
532
|
-
return 1 if FAILS else 0
|
|
533
|
-
return 0 if not FAILS else 1
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
if __name__ == "__main__":
|
|
537
|
-
sys.exit(main())
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# 功能名:paths.json 解析失败时不得无声回落默认记忆库(issue #352-D)
|
|
3
|
+
# 生效条件:python -X utf8 -m md_cg.test_issue352_pathsjson [--mutate];被测面=md_cg/datapath.py 的 _read_user_paths/_user_paths/mdcg_root/data_root/set_user_root + src/lib/datapath.ts 的 readUserPaths/mdcgRoot
|
|
4
|
+
# 子功能:A 组=UTF-8 BOM 与非法转义两类常见写坏在 Python 侧与插件侧都不得无声回落;K 组=非恒真对照(合法配置照常生效、无文件时仍回落默认库、三态口径、set_user_root 不冲掉其余键、原子写);E 组=源码面单点
|
|
5
|
+
# 执行:每个变体建独立临时沙箱,先在自定义根写一条既有记忆,再写 paths.json(合法/BOM/非法转义/错形状),用子进程读 mdcg_root()+MdCGOS;插件侧用 node 直接 import src/lib/datapath.ts
|
|
6
|
+
# 验证方式:全部 PASS 即通过;--mutate 走镜像树定点变异,变异后红项必须**恰为**预期集合
|
|
7
|
+
# 不适用条件:①**坏配置 ⇒ 插件激活失败与各 CLI 退出是刻意的 fail-closed 选择**(本处按 #161 先例:静默回落会造成「原有记忆消失 + 新写入分叉」的数据分叉,比「起不来」严重);退路(console.error + describeDataPaths/op=info 点名后回落)实现成本一处——PathsConfigError 的 code/pathsFile 已留好——**是否切换归维护者裁定**,本守卫两种口径下都成立(断言只要求「不静默」)②不判路径本身是否合法/可建(保留设备名由 _abs_host_path 另管,见 test_issue354_p2_datapath_clock)③不判 paths.json 的取值优先级(env > paths.json > 默认,既有面未改)
|
|
8
|
+
"""issue #352-D 守卫:坏 paths.json 不得无声回落默认库。
|
|
9
|
+
|
|
10
|
+
病象(修前):Python `_user_paths()` 与插件 `readUserPaths()` 都用一个「吞掉
|
|
11
|
+
一切异常的 except」把「文件不存在」与「文件存在但坏了」混成同一个 `{}`——于是
|
|
12
|
+
`mcg_root()` 无声回落到默认库:用户看到「原有记忆没了」,新写入分叉进另一个库
|
|
13
|
+
(插件侧仍按 config 走,两边就此分成两个库)。Windows 上两种常见写坏:
|
|
14
|
+
· 记事本 / PowerShell 5 的 `Set-Content -Encoding UTF8` 写出 UTF-8 BOM;
|
|
15
|
+
· 照抄资源管理器路径时反斜杠未转义——Windows 路径分隔符在 JSON 串里
|
|
16
|
+
必须写成双反斜杠或改用正斜杠,单写即非法转义序列。
|
|
17
|
+
|
|
18
|
+
处置(**响亮失败**,本仓先例 #161「打包面静默坏包 ⇒ 改为响亮失败」,Lead 本轮
|
|
19
|
+
指定按同一精神):
|
|
20
|
+
1. BOM 属编码形态而非语法错 ⇒ 两侧都剥掉(Python `utf-8-sig`、TS
|
|
21
|
+
`replace(/^\uFEFF/, '')`),**能解析就正常生效**——不是「有 BOM 就报错」
|
|
22
|
+
(K3 就是这条的口径对照,M2 证明它有判别力);
|
|
23
|
+
2. 真正解析不了(含顶层非对象)⇒ 抛 `PathsConfigError`,绝不回落;
|
|
24
|
+
3. `set_user_root` 读到坏文件时**拒绝写**(改前会用空表覆写,把用户其余键
|
|
25
|
+
一并冲掉),落盘改走 `fsutil.atomic_write`(改前非原子,崩溃留下的截断件
|
|
26
|
+
正是下一条「解析失败」的来源)。
|
|
27
|
+
|
|
28
|
+
★**过度收紧方向**:K1(合法配置照常生效)、K2(无文件时仍回落默认库)、
|
|
29
|
+
K6(TS 侧无文件时仍回落)三条盯住「修复没把正常面一起关掉」。
|
|
30
|
+
"""
|
|
31
|
+
import json
|
|
32
|
+
import os
|
|
33
|
+
import shutil
|
|
34
|
+
import subprocess
|
|
35
|
+
import sys
|
|
36
|
+
import tempfile
|
|
37
|
+
|
|
38
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
39
|
+
|
|
40
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
41
|
+
sys.path.insert(0, _REPO)
|
|
42
|
+
from md_cg import datapath as _dp # noqa: E402
|
|
43
|
+
|
|
44
|
+
FAILS = []
|
|
45
|
+
_SKIPPED = []
|
|
46
|
+
_TMP = tempfile.mkdtemp(prefix="iss352d_")
|
|
47
|
+
_NODE = shutil.which("node")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _check(name, cond, detail=""):
|
|
51
|
+
if cond:
|
|
52
|
+
print(" PASS " + name)
|
|
53
|
+
else:
|
|
54
|
+
print(" FAIL " + name + " " + str(detail)[:400])
|
|
55
|
+
FAILS.append(name)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
_SEED = (
|
|
59
|
+
"import sys; sys.path.insert(0, sys.argv[1]);"
|
|
60
|
+
"from md_cg.mdcos import MdCGOS;"
|
|
61
|
+
"c = MdCGOS(sys.argv[2], autoflush=1);"
|
|
62
|
+
"c.add('old_pref', 'PASSED 旧偏置:用户对花生过敏', layer='knowledge');"
|
|
63
|
+
" c.close()")
|
|
64
|
+
|
|
65
|
+
_CHILD = '''
|
|
66
|
+
import json, sys
|
|
67
|
+
sys.path.insert(0, sys.argv[1])
|
|
68
|
+
out = {"raised": None, "root": None, "configured": None, "seen": None,
|
|
69
|
+
"msg_ok": None}
|
|
70
|
+
try:
|
|
71
|
+
from md_cg import datapath
|
|
72
|
+
except Exception as e:
|
|
73
|
+
out["raised"] = "import:" + type(e).__name__
|
|
74
|
+
print(json.dumps(out, ensure_ascii=False)); raise SystemExit(0)
|
|
75
|
+
try:
|
|
76
|
+
out["root"] = datapath.mdcg_root()
|
|
77
|
+
out["configured"] = datapath.mdcg_root_configured()
|
|
78
|
+
from md_cg.mdcos import MdCGOS
|
|
79
|
+
cg = MdCGOS(out["root"], autoflush=1)
|
|
80
|
+
out["seen"] = bool(cg.get("old_pref"))
|
|
81
|
+
cg.close()
|
|
82
|
+
except datapath.PathsConfigError as e:
|
|
83
|
+
out["raised"] = "PathsConfigError"
|
|
84
|
+
out["msg_ok"] = ("paths.json" in str(e))
|
|
85
|
+
except Exception as e:
|
|
86
|
+
out["raised"] = type(e).__name__
|
|
87
|
+
print(json.dumps(out, ensure_ascii=False))
|
|
88
|
+
'''
|
|
89
|
+
|
|
90
|
+
_NODEJS = '''
|
|
91
|
+
import { pathToFileURL } from 'node:url'
|
|
92
|
+
const mod = await import(pathToFileURL(process.argv[2]).href)
|
|
93
|
+
const out = { raised: null, root: null, msg_ok: null }
|
|
94
|
+
try {
|
|
95
|
+
out.root = mod.mdcgRoot('')
|
|
96
|
+
} catch (e) {
|
|
97
|
+
out.raised = e.name
|
|
98
|
+
out.msg_ok = String(e.message).includes('paths.json')
|
|
99
|
+
}
|
|
100
|
+
console.log(JSON.stringify(out))
|
|
101
|
+
'''
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _py_env(t, extra=None):
|
|
105
|
+
env = {k: v for k, v in os.environ.items()
|
|
106
|
+
if not k.startswith(("MDCG_", "DSH_", "PYTHON"))}
|
|
107
|
+
env.update({"HOME": t, "MDCG_STATE_ROOT": os.path.join(t, "state"),
|
|
108
|
+
# 末段不得是 Windows 保留设备名(aux/con/nul…)——_abs_host_path
|
|
109
|
+
# 会据此 fail-closed 拒启(test_issue354_p2_datapath_clock 同一判据)。
|
|
110
|
+
"MDCG_AUX_ROOT": os.path.join(t, "auxstore"), "MDCG_SLEEP": "0",
|
|
111
|
+
"PYTHONUTF8": "1", "PYTHONPATH": _REPO})
|
|
112
|
+
if extra:
|
|
113
|
+
env.update(extra)
|
|
114
|
+
return env
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _py_case(tag, writer, seed=True):
|
|
118
|
+
"""跑一个 Python 变体:返回子进程读数 dict(含 testdir)。"""
|
|
119
|
+
t = tempfile.mkdtemp(prefix="iss352d_%s_" % tag, dir=_TMP)
|
|
120
|
+
state = os.path.join(t, "state")
|
|
121
|
+
os.makedirs(state, exist_ok=True)
|
|
122
|
+
custom = os.path.join(t, "MyMemory")
|
|
123
|
+
env = _py_env(t)
|
|
124
|
+
if seed:
|
|
125
|
+
subprocess.run([sys.executable, "-c", _SEED, _REPO, custom], env=env,
|
|
126
|
+
check=True, capture_output=True)
|
|
127
|
+
writer(os.path.join(state, "paths.json"), custom)
|
|
128
|
+
p = subprocess.run([sys.executable, "-c", _CHILD, _REPO], env=env,
|
|
129
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
130
|
+
errors="replace")
|
|
131
|
+
try:
|
|
132
|
+
out = json.loads(p.stdout.strip().splitlines()[-1])
|
|
133
|
+
except Exception: # noqa: BLE001
|
|
134
|
+
out = {"raised": "NOJSON", "stdout": (p.stdout or "")[-160:],
|
|
135
|
+
"stderr": (p.stderr or "")[-160:]}
|
|
136
|
+
out["custom"] = custom
|
|
137
|
+
out["testdir"] = t
|
|
138
|
+
return out
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _w_raw(data, name="paths.json"):
|
|
142
|
+
def _w(pf, custom):
|
|
143
|
+
with open(pf, "wb") as f:
|
|
144
|
+
f.write(data(custom) if callable(data) else data)
|
|
145
|
+
return _w
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _w_obj(fn):
|
|
149
|
+
def _w(pf, custom):
|
|
150
|
+
with open(pf, "w", encoding="utf-8") as f:
|
|
151
|
+
json.dump(fn(custom), f, ensure_ascii=False)
|
|
152
|
+
return _w
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _node_case(tag, mkpaths):
|
|
156
|
+
"""跑一个 TS 变体:node 直接 import src/lib/datapath.ts。"""
|
|
157
|
+
if _NODE is None:
|
|
158
|
+
return None
|
|
159
|
+
t = tempfile.mkdtemp(prefix="iss352d_%s_" % tag, dir=_TMP)
|
|
160
|
+
state = os.path.join(t, "state")
|
|
161
|
+
os.makedirs(state, exist_ok=True)
|
|
162
|
+
custom = os.path.join(t, "MyMemory")
|
|
163
|
+
js = os.path.join(_TMP, "ts_%s.mjs" % tag)
|
|
164
|
+
with open(js, "w", encoding="utf-8") as f:
|
|
165
|
+
f.write(_NODEJS)
|
|
166
|
+
if mkpaths is not None:
|
|
167
|
+
mkpaths(os.path.join(state, "paths.json"), custom)
|
|
168
|
+
env = {k: v for k, v in os.environ.items()
|
|
169
|
+
if k not in ("MDCG_ROOT", "MDCG_DATA_ROOT")}
|
|
170
|
+
env["MDCG_STATE_ROOT"] = state
|
|
171
|
+
p = subprocess.run([_NODE, js, os.path.join(_REPO, "src", "lib",
|
|
172
|
+
"datapath.ts")],
|
|
173
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
174
|
+
errors="replace", env=env, cwd=_REPO)
|
|
175
|
+
try:
|
|
176
|
+
out = json.loads(p.stdout.strip().splitlines()[-1])
|
|
177
|
+
except Exception: # noqa: BLE001
|
|
178
|
+
out = {"raised": "NOJSON", "stdout": (p.stdout or "")[-160:],
|
|
179
|
+
"stderr": (p.stderr or "")[-200:]}
|
|
180
|
+
out["custom"] = custom
|
|
181
|
+
return out
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _same(a, b):
|
|
185
|
+
return bool(a) and os.path.normcase(os.path.abspath(a)) == \
|
|
186
|
+
os.path.normcase(os.path.abspath(b))
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
_BOM = lambda c: b"\xef\xbb\xbf" + json.dumps({"root": c}).encode("utf-8")
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _bad_escape(c):
|
|
193
|
+
# 未转义反斜杠:\M 非法转义(照抄资源管理器路径的常见形态)
|
|
194
|
+
return ('{"root": "%s"}' % c.replace(os.sep, "\\")).encode("utf-8")
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
print("== A 组:坏/带 BOM 的 paths.json 不得无声回落 ==")
|
|
198
|
+
|
|
199
|
+
# --- D1:UTF-8 BOM + 合法 JSON ⇒ BOM 属编码形态,应正常生效(不是「有 BOM 就报错」)
|
|
200
|
+
_d1 = _py_case("d1", _w_raw(_BOM))
|
|
201
|
+
print(" [读数] D1 BOM 合法配置 → raised=%s root==custom=%s 既有记忆可读=%s"
|
|
202
|
+
% (_d1.get("raised"), _same(_d1.get("root"), _d1["custom"]),
|
|
203
|
+
_d1.get("seen")))
|
|
204
|
+
_check("D1 UTF-8 BOM 的 paths.json:正常生效(root==自定义根、既有记忆读得到),"
|
|
205
|
+
"不回落默认库",
|
|
206
|
+
_d1.get("raised") is None and _same(_d1.get("root"), _d1["custom"])
|
|
207
|
+
and _d1.get("configured") is True and _d1.get("seen") is True, _d1)
|
|
208
|
+
|
|
209
|
+
# --- D2:非法转义(真语法错)⇒ 响亮失败,不静默回落
|
|
210
|
+
_d2 = _py_case("d2", _w_raw(_bad_escape))
|
|
211
|
+
print(" [读数] D2 非法转义 → raised=%s 消息含 paths.json=%s root=%s"
|
|
212
|
+
% (_d2.get("raised"), _d2.get("msg_ok"), _d2.get("root")))
|
|
213
|
+
_check("D2 非法转义的 paths.json:响亮失败(PathsConfigError,消息点名 paths.json),"
|
|
214
|
+
"**不**静默回落到默认库",
|
|
215
|
+
_d2.get("raised") == "PathsConfigError" and _d2.get("msg_ok") is True
|
|
216
|
+
and _d2.get("root") is None, _d2)
|
|
217
|
+
|
|
218
|
+
# --- D4:合法 JSON、错形状(顶层数组)⇒ 同一口径响亮失败
|
|
219
|
+
_d4 = _py_case("d4", _w_raw(b'["not", "an", "object"]'))
|
|
220
|
+
print(" [读数] D4 顶层数组 → raised=%s" % _d4.get("raised"))
|
|
221
|
+
_check("D4 顶层非 JSON 对象:同一口径响亮失败(不按「解析成功」放行)",
|
|
222
|
+
_d4.get("raised") == "PathsConfigError" and _d4.get("root") is None, _d4)
|
|
223
|
+
|
|
224
|
+
# --- D3/D5/K6:插件侧(node 直接 import src/lib/datapath.ts)
|
|
225
|
+
_ts_bom = _node_case("tsbom", _w_raw(_BOM))
|
|
226
|
+
_ts_bad = _node_case("tsbad", _w_raw(b'{"root": '))
|
|
227
|
+
_ts_none = _node_case("tsnone", None)
|
|
228
|
+
if _ts_bom is None:
|
|
229
|
+
_SKIPPED.append("D3/D5/K6")
|
|
230
|
+
print(" [跳过] 插件侧腿:本机无 node")
|
|
231
|
+
else:
|
|
232
|
+
print(" [读数] D3 TS BOM → raised=%s root==custom=%s"
|
|
233
|
+
% (_ts_bom.get("raised"), _same(_ts_bom.get("root"), _ts_bom["custom"])))
|
|
234
|
+
_check("D3 插件侧 UTF-8 BOM 的 paths.json:正常生效(root==自定义根)",
|
|
235
|
+
_ts_bom.get("raised") is None
|
|
236
|
+
and _same(_ts_bom.get("root"), _ts_bom["custom"]), _ts_bom)
|
|
237
|
+
print(" [读数] D5 TS 语法错 → raised=%s 消息含 paths.json=%s"
|
|
238
|
+
% (_ts_bad.get("raised"), _ts_bad.get("msg_ok")))
|
|
239
|
+
_check("D5 插件侧无法解析的 paths.json:抛 PathsConfigError(不无声回落默认根)",
|
|
240
|
+
_ts_bad.get("raised") == "PathsConfigError"
|
|
241
|
+
and _ts_bad.get("msg_ok") is True and _ts_bad.get("root") is None,
|
|
242
|
+
_ts_bad)
|
|
243
|
+
|
|
244
|
+
# ========================================================= K 组:非恒真对照
|
|
245
|
+
print("== K 组:非恒真对照(含**过度收紧**方向)==")
|
|
246
|
+
|
|
247
|
+
# --- K1:合法 paths.json 正常生效(不过度收紧)
|
|
248
|
+
_k1 = _py_case("k1", _w_obj(lambda c: {"root": c}))
|
|
249
|
+
print(" [读数] K1 合法配置 → root==custom=%s configured=%s seen=%s"
|
|
250
|
+
% (_same(_k1.get("root"), _k1["custom"]), _k1.get("configured"),
|
|
251
|
+
_k1.get("seen")))
|
|
252
|
+
_check("K1 合法 paths.json 照常生效(root==自定义根、既有记忆读得到、无异常)",
|
|
253
|
+
_k1.get("raised") is None and _same(_k1.get("root"), _k1["custom"])
|
|
254
|
+
and _k1.get("configured") is True and _k1.get("seen") is True, _k1)
|
|
255
|
+
|
|
256
|
+
# --- K2:**没有** paths.json 时必须照旧回落默认库(过度收紧方向)
|
|
257
|
+
_k2 = _py_case("k2", lambda pf, custom: None, seed=False)
|
|
258
|
+
_k2_dflt = os.path.join(_k2["testdir"], "state", "data", "mdcg")
|
|
259
|
+
print(" [读数] K2 无配置文件 → raised=%s root=%s"
|
|
260
|
+
% (_k2.get("raised"), _k2.get("root")))
|
|
261
|
+
_check("K2 无 paths.json(文件不存在)时仍回落默认库——不把「没配过」也判成坏配置",
|
|
262
|
+
_k2.get("raised") is None and _same(_k2.get("root"), _k2_dflt)
|
|
263
|
+
and _k2.get("configured") is False, (_k2, _k2_dflt))
|
|
264
|
+
|
|
265
|
+
# --- K3:三态读取单点(missing / ok / bad 三分,不再混成 {})
|
|
266
|
+
def _states():
|
|
267
|
+
res = {}
|
|
268
|
+
t = tempfile.mkdtemp(prefix="iss352d_k3_", dir=_TMP)
|
|
269
|
+
state = os.path.join(t, "state")
|
|
270
|
+
os.makedirs(state)
|
|
271
|
+
old = os.environ.get("MDCG_STATE_ROOT")
|
|
272
|
+
os.environ["MDCG_STATE_ROOT"] = state
|
|
273
|
+
try:
|
|
274
|
+
res["missing"] = _dp._read_user_paths()[1]
|
|
275
|
+
with open(os.path.join(state, "paths.json"), "wb") as f:
|
|
276
|
+
f.write(_BOM(os.path.join(t, "My")))
|
|
277
|
+
res["bom_ok"] = _dp._read_user_paths()[1]
|
|
278
|
+
with open(os.path.join(state, "paths.json"), "wb") as f:
|
|
279
|
+
f.write(_bad_escape(os.path.join(t, "My")))
|
|
280
|
+
res["bad_escape"] = _dp._read_user_paths()[1]
|
|
281
|
+
res["raises"] = None
|
|
282
|
+
try:
|
|
283
|
+
_dp._user_paths()
|
|
284
|
+
except _dp.PathsConfigError:
|
|
285
|
+
res["raises"] = "PathsConfigError"
|
|
286
|
+
finally:
|
|
287
|
+
if old is None:
|
|
288
|
+
os.environ.pop("MDCG_STATE_ROOT", None)
|
|
289
|
+
else:
|
|
290
|
+
os.environ["MDCG_STATE_ROOT"] = old
|
|
291
|
+
return res
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
_k3 = _states()
|
|
295
|
+
print(" [读数] K3 三态=%s 严格读抛=%s" % (_k3, _k3["raises"]))
|
|
296
|
+
_check("K3 三态口径单点:missing / ok(BOM 剥得掉)/ bad(真语法错)三分,"
|
|
297
|
+
"且 strict 读对 bad 抛错",
|
|
298
|
+
_k3["missing"] == "missing" and _k3["bom_ok"] == "ok"
|
|
299
|
+
and _k3["bad_escape"] == "bad"
|
|
300
|
+
and _k3["raises"] == "PathsConfigError", _k3)
|
|
301
|
+
|
|
302
|
+
# --- K4:set_user_root 对坏文件拒写且**不冲掉**其余键
|
|
303
|
+
_k4t = tempfile.mkdtemp(prefix="iss352d_k4_", dir=_TMP)
|
|
304
|
+
_k4state = os.path.join(_k4t, "state")
|
|
305
|
+
os.makedirs(_k4state)
|
|
306
|
+
_k4pf = os.path.join(_k4state, "paths.json")
|
|
307
|
+
_k4raw = ('{"root": "%s", "extra": "KEEP-ME"}'
|
|
308
|
+
% os.path.join(_k4t, "My")).replace(os.sep, "\\")
|
|
309
|
+
with open(_k4pf, "wb") as f:
|
|
310
|
+
f.write(_k4raw.encode("utf-8"))
|
|
311
|
+
_old = os.environ.get("MDCG_STATE_ROOT")
|
|
312
|
+
os.environ["MDCG_STATE_ROOT"] = _k4state
|
|
313
|
+
try:
|
|
314
|
+
try:
|
|
315
|
+
_dp.set_user_root("E:/new", "data_root")
|
|
316
|
+
_k4r = "写了"
|
|
317
|
+
except _dp.PathsConfigError:
|
|
318
|
+
_k4r = "PathsConfigError"
|
|
319
|
+
finally:
|
|
320
|
+
if _old is None:
|
|
321
|
+
os.environ.pop("MDCG_STATE_ROOT", None)
|
|
322
|
+
else:
|
|
323
|
+
os.environ["MDCG_STATE_ROOT"] = _old
|
|
324
|
+
_k4after = open(_k4pf, "rb").read().decode("utf-8")
|
|
325
|
+
print(" [读数] K4 坏文件 set_user_root=%s 原文未动=%s"
|
|
326
|
+
% (_k4r, _k4after == _k4raw))
|
|
327
|
+
_check("K4 set_user_root 读到坏文件时拒绝写,且**不冲掉**用户其余键(字节不变)",
|
|
328
|
+
_k4r == "PathsConfigError" and _k4after == _k4raw, (_k4r, _k4after[:60]))
|
|
329
|
+
|
|
330
|
+
# --- K5:set_user_root 对好文件正常写入、原子落盘可读回
|
|
331
|
+
_k5t = tempfile.mkdtemp(prefix="iss352d_k5_", dir=_TMP)
|
|
332
|
+
_k5state = os.path.join(_k5t, "state")
|
|
333
|
+
os.makedirs(_k5state)
|
|
334
|
+
_old = os.environ.get("MDCG_STATE_ROOT")
|
|
335
|
+
os.environ["MDCG_STATE_ROOT"] = _k5state
|
|
336
|
+
try:
|
|
337
|
+
_w = _dp.set_user_root(os.path.join(_k5t, "My"), "root")
|
|
338
|
+
_d = json.load(open(_w, encoding="utf-8"))
|
|
339
|
+
finally:
|
|
340
|
+
if _old is None:
|
|
341
|
+
os.environ.pop("MDCG_STATE_ROOT", None)
|
|
342
|
+
else:
|
|
343
|
+
os.environ["MDCG_STATE_ROOT"] = _old
|
|
344
|
+
print(" [读数] K5 写入路径在用户级=%s 键齐=%s"
|
|
345
|
+
% (_w == os.path.join(_k5state, "paths.json"), sorted(_d)))
|
|
346
|
+
_check("K5 set_user_root 对好文件正常写入(用户级位置、含 _comment 与目标键、"
|
|
347
|
+
"可解析回来)",
|
|
348
|
+
_w == os.path.join(_k5state, "paths.json")
|
|
349
|
+
and _d.get("root") == os.path.join(_k5t, "My").replace(os.sep, "/")
|
|
350
|
+
and "_comment" in _d, (_w, sorted(_d)))
|
|
351
|
+
|
|
352
|
+
if _ts_none is not None:
|
|
353
|
+
_ts_dflt = os.path.join(_ts_none["custom"], "..", "state", "data", "mdcg")
|
|
354
|
+
print(" [读数] K6 TS 无配置文件 → raised=%s root=%s"
|
|
355
|
+
% (_ts_none.get("raised"), _ts_none.get("root")))
|
|
356
|
+
_check("K6 插件侧无 paths.json 时仍回落默认根(过度收紧方向)",
|
|
357
|
+
_ts_none.get("raised") is None
|
|
358
|
+
and _same(_ts_none.get("root"), _ts_dflt), _ts_none)
|
|
359
|
+
|
|
360
|
+
# ========================================================= E 组:源码面单点
|
|
361
|
+
print("== E 组:源码面单点 ==")
|
|
362
|
+
_dp_src = open(os.path.join(_REPO, "md_cg", "datapath.py"),
|
|
363
|
+
encoding="utf-8").read()
|
|
364
|
+
_ts_src = open(os.path.join(_REPO, "src", "lib", "datapath.ts"),
|
|
365
|
+
encoding="utf-8").read()
|
|
366
|
+
_check("E1 三态读取是唯一读取点(_user_paths 走 _read_user_paths,两者分开)",
|
|
367
|
+
"def _read_user_paths():" in _dp_src
|
|
368
|
+
and "d, st = _read_user_paths()" in _dp_src
|
|
369
|
+
and "class PathsConfigError(RuntimeError):" in _dp_src)
|
|
370
|
+
_check("E2 BOM 口径:Python 侧以 utf-8-sig 读 paths.json",
|
|
371
|
+
'open(pf, encoding="utf-8-sig")' in _dp_src)
|
|
372
|
+
_check("E3 插件侧:BOM 剥离 + PathsConfigError 抛出(与 Python 侧同一口径)",
|
|
373
|
+
"replace(/^\\uFEFF/, '')" in _ts_src
|
|
374
|
+
and "export class PathsConfigError extends Error" in _ts_src)
|
|
375
|
+
_check("E4 set_user_root 原子落盘(fsutil.atomic_write,不再裸 open('w'))",
|
|
376
|
+
"fsutil.atomic_write(pf," in _dp_src)
|
|
377
|
+
|
|
378
|
+
shutil.rmtree(_TMP, ignore_errors=True)
|
|
379
|
+
|
|
380
|
+
print("")
|
|
381
|
+
if FAILS:
|
|
382
|
+
print("issue #352-D 结果:%d 失败 → %s" % (len(FAILS), ", ".join(FAILS)))
|
|
383
|
+
else:
|
|
384
|
+
print("issue #352-D 结果:全部通过(跳过腿:%s)"
|
|
385
|
+
% (",".join(_SKIPPED) or "无"))
|
|
386
|
+
|
|
387
|
+
# ------------------------------------------------------------- 定点变异(镜像树)
|
|
388
|
+
_MUTATIONS = (
|
|
389
|
+
("M1 摘掉 Python 侧坏配置的响亮失败(回到静默返回 {})",
|
|
390
|
+
(("md_cg/datapath.py",
|
|
391
|
+
[' if st == "bad":',
|
|
392
|
+
' if strict:',
|
|
393
|
+
' raise PathsConfigError(_paths_config_message())',
|
|
394
|
+
' return {}'],
|
|
395
|
+
[' if st == "bad":',
|
|
396
|
+
' return {}']),),
|
|
397
|
+
# K3 也红:它断言 strict 读对 bad 抛错(三态单点的行为面)。
|
|
398
|
+
("D2", "D4", "K3")),
|
|
399
|
+
("M2 摘掉 BOM 兼容(utf-8-sig 回 utf-8)——把「编码形态」误判成坏配置",
|
|
400
|
+
(("md_cg/datapath.py",
|
|
401
|
+
[' with open(pf, encoding="utf-8-sig") as f:'],
|
|
402
|
+
[' with open(pf, encoding="utf-8") as f:']),),
|
|
403
|
+
("D1", "K3", "E2")),
|
|
404
|
+
("M3 摘掉 missing 分支(把「文件不存在」也判成坏配置)——过度收紧方向",
|
|
405
|
+
(("md_cg/datapath.py",
|
|
406
|
+
[' if not os.path.isfile(pf):',
|
|
407
|
+
' return {}, "missing"'],
|
|
408
|
+
[' if False:',
|
|
409
|
+
' return {}, "missing"']),),
|
|
410
|
+
# K3 也红:它的 missing 腿(文件不存在 ⇒ "missing")被这条变异一起改掉。
|
|
411
|
+
("K2", "K3")),
|
|
412
|
+
("M4 摘掉 set_user_root 对坏文件的拒写(回到用空表覆写、冲掉其余键)",
|
|
413
|
+
(("md_cg/datapath.py",
|
|
414
|
+
[' if st == "bad":',
|
|
415
|
+
' raise PathsConfigError('],
|
|
416
|
+
[' if False:',
|
|
417
|
+
' raise PathsConfigError(']),),
|
|
418
|
+
("K4",)),
|
|
419
|
+
("M5 摘掉插件侧 BOM 剥离",
|
|
420
|
+
(("src/lib/datapath.ts",
|
|
421
|
+
[" parsed = JSON.parse(raw.replace(/^\\uFEFF/, ''))"],
|
|
422
|
+
[" parsed = JSON.parse(raw)"]),),
|
|
423
|
+
("D3", "E3")),
|
|
424
|
+
("M6 摘掉插件侧「文件不存在」短路(把没配过也判成坏配置)——过度收紧方向",
|
|
425
|
+
(("src/lib/datapath.ts",
|
|
426
|
+
[" if (!existsSync(pf)) return {}"],
|
|
427
|
+
[" if (false) return {}"]),),
|
|
428
|
+
("K6",)),
|
|
429
|
+
("M7 摘掉插件侧解析失败的抛出(回到静默空表回落)",
|
|
430
|
+
(("src/lib/datapath.ts",
|
|
431
|
+
[" throw new PathsConfigError(pathsConfigMessage(pf, String(e)), pf)"],
|
|
432
|
+
[" parsed = {}"]),),
|
|
433
|
+
("D5",)),
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def _mirror_build():
|
|
438
|
+
dst = tempfile.mkdtemp(prefix="iss352d_mut_")
|
|
439
|
+
for name in ("md_cg", "scripts", "src"):
|
|
440
|
+
src = os.path.join(_REPO, name)
|
|
441
|
+
if os.path.isdir(src):
|
|
442
|
+
shutil.copytree(src, os.path.join(dst, name),
|
|
443
|
+
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
|
444
|
+
for name in sorted(os.listdir(_REPO)):
|
|
445
|
+
p = os.path.join(_REPO, name)
|
|
446
|
+
if os.path.isfile(p) and (name.endswith(".py")
|
|
447
|
+
or name in ("package.json", "tsconfig.json")):
|
|
448
|
+
shutil.copy2(p, os.path.join(dst, name))
|
|
449
|
+
return dst
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def _nl_of(path):
|
|
453
|
+
with open(path, "rb") as fh:
|
|
454
|
+
raw = fh.read(8192)
|
|
455
|
+
return "\r\n" if b"\r\n" in raw else "\n"
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def _run_in(tree):
|
|
459
|
+
env = dict(os.environ)
|
|
460
|
+
env["PYTHONPATH"] = tree
|
|
461
|
+
env["PYTHONUTF8"] = "1"
|
|
462
|
+
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
463
|
+
pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
|
|
464
|
+
"md_cg.test_issue352_pathsjson"],
|
|
465
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
466
|
+
errors="replace", cwd=tree, env=env, timeout=1800)
|
|
467
|
+
out = (pr.stdout or "") + (pr.stderr or "")
|
|
468
|
+
reds = [ln.split("FAIL ", 1)[1].strip()
|
|
469
|
+
for ln in out.splitlines() if "FAIL " in ln]
|
|
470
|
+
return pr.returncode, reds, out
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
def _mutate():
|
|
474
|
+
print("!! 定点变异自证:把修复点改回改动前形态,守卫必须转红")
|
|
475
|
+
tree = _mirror_build()
|
|
476
|
+
try:
|
|
477
|
+
rc, reds, out = _run_in(tree)
|
|
478
|
+
print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
|
|
479
|
+
if rc != 0 or reds:
|
|
480
|
+
print(chr(10).join(out.splitlines()[-30:]))
|
|
481
|
+
for ln in out.splitlines():
|
|
482
|
+
if "[读数]" in ln:
|
|
483
|
+
print(" 修后读数(未变异镜像):" + ln.strip())
|
|
484
|
+
okc, bad = 0, []
|
|
485
|
+
for desc, edits, exp in _MUTATIONS:
|
|
486
|
+
backups, miss = [], []
|
|
487
|
+
for rel, oldp, newp in edits:
|
|
488
|
+
p = os.path.join(tree, rel)
|
|
489
|
+
nl = _nl_of(p)
|
|
490
|
+
old, new = nl.join(oldp), nl.join(newp)
|
|
491
|
+
with open(p, encoding="utf-8", newline="") as fh:
|
|
492
|
+
t = fh.read()
|
|
493
|
+
if t.count(old) != 1:
|
|
494
|
+
miss.append("%s —— 锚点命中 %d 次" % (rel, t.count(old)))
|
|
495
|
+
continue
|
|
496
|
+
backups.append((p, t))
|
|
497
|
+
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
498
|
+
fh.write(t.replace(old, new, 1))
|
|
499
|
+
if miss:
|
|
500
|
+
bad.append("%s —— %s" % (desc, ";".join(miss)))
|
|
501
|
+
print(" ANCHOR-MISS " + desc)
|
|
502
|
+
for p, t in backups:
|
|
503
|
+
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
504
|
+
fh.write(t)
|
|
505
|
+
continue
|
|
506
|
+
rc2, reds2, out2 = _run_in(tree)
|
|
507
|
+
for p, t in backups:
|
|
508
|
+
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
509
|
+
fh.write(t)
|
|
510
|
+
hit = sorted({e for r in reds2 for e in exp if r.startswith(e + " ")})
|
|
511
|
+
if rc2 != 0 and hit == sorted(exp) and len(reds2) == len(exp):
|
|
512
|
+
okc += 1
|
|
513
|
+
print(" OK %s 红项=%d 指定命中=%d/%d"
|
|
514
|
+
% (desc, len(reds2), len(hit), len(exp)))
|
|
515
|
+
else:
|
|
516
|
+
bad.append(desc)
|
|
517
|
+
print(" MISMATCH %s 红项=%d 指定命中=%d/%d rc=%d"
|
|
518
|
+
% (desc, len(reds2), len(hit), len(exp), rc2))
|
|
519
|
+
for r in reds2[:14]:
|
|
520
|
+
print(" FAIL " + r)
|
|
521
|
+
print("定点变异自证:%s(%d 条不合格)"
|
|
522
|
+
% ("PASS" if not bad else "FAIL", len(bad)))
|
|
523
|
+
for b in bad:
|
|
524
|
+
print(" 不合格:" + b)
|
|
525
|
+
finally:
|
|
526
|
+
shutil.rmtree(tree, ignore_errors=True)
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def main():
|
|
530
|
+
if "--mutate" in sys.argv:
|
|
531
|
+
_mutate()
|
|
532
|
+
return 1 if FAILS else 0
|
|
533
|
+
return 0 if not FAILS else 1
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
if __name__ == "__main__":
|
|
537
|
+
sys.exit(main())
|