Browse Source

fix(bin): mask conf 相对路径按项目根解析(不依赖 cwd)

复用 dw_base.datax.entry._resolve_relative_to_base 同款思路,
sync 脚本加 _resolve_to_project_root:相对路径自动 join 项目根,
绝对路径透传。任何 cwd 跑都能找到 mask conf,与 datax-hive-import-starter
等其他 bin 入口行为一致。

加 2 条单测覆盖(绝对路径透传 / 相对路径 join 项目根)。
tianyu.chu 1 week ago
parent
commit
cea495de44
2 changed files with 28 additions and 1 deletions
  1. 17 1
      bin/datax-sync-template-gen.py
  2. 11 0
      tests/unit/datax/test_sync_template_gen.py

+ 17 - 1
bin/datax-sync-template-gen.py

@@ -97,6 +97,18 @@ def query_columns_full(conn, schema, table):
     return cur.fetchall()
 
 
+def _resolve_to_project_root(path):
+    """相对路径按项目根解析,绝对路径原样返回。
+
+    复用 dw_base.datax.entry._resolve_relative_to_base 的逻辑——
+    任何 cwd 跑此脚本都能找到 mask conf 等相对路径资源,
+    与项目其他 bin 入口(datax-hive-import-starter 等)行为一致。
+    """
+    if os.path.isabs(path):
+        return path
+    return os.path.join(project_root, path)
+
+
 def load_mask_conf(path):
     """读 mask 配置 ini,返回 {field: method} dict。
 
@@ -232,7 +244,11 @@ def main():
         conn.close()
 
     # full_rows: [(attnum, attname, comment, pg_type, pk_flag), ...]
-    mask_dict = load_mask_conf(args.mask_conf) if args.mask_conf else {}
+    if args.mask_conf:
+        mask_path = _resolve_to_project_root(args.mask_conf)
+        mask_dict = load_mask_conf(mask_path)
+    else:
+        mask_dict = {}
 
     # mask 配置含表中不存在字段时 stderr 警告(不阻断)
     pg_field_set = {r[1] for r in full_rows}

+ 11 - 0
tests/unit/datax/test_sync_template_gen.py

@@ -154,6 +154,17 @@ def test_load_mask_conf_missing_file_raises():
         GEN.load_mask_conf('/nonexistent/path/x.mask.ini')
 
 
+def test_resolve_to_project_root_absolute_pass_through():
+    abs_path = os.path.abspath('/abs/path/x.ini')
+    assert GEN._resolve_to_project_root(abs_path) == abs_path
+
+
+def test_resolve_to_project_root_relative_joins_project_root():
+    result = GEN._resolve_to_project_root('jobs/raw/trd/x.mask.ini')
+    assert os.path.isabs(result)
+    assert result.endswith('jobs/raw/trd/x.mask.ini')
+
+
 def test_render_template_empty_pk():
     out = GEN.render_template(
         ds_ref='postgresql/prod-hobby', database='db', schema='public',