From f4b288fdccaeec09a00be4f6dc57f6b5f9c56a80 Mon Sep 17 00:00:00 2001 From: Firefly <45487685+Snoopy1866@users.noreply.github.com> Date: Sun, 7 Jun 2026 11:07:33 +0800 Subject: [PATCH] test: add more tests --- src/submit/submit.py | 9 +++--- tests/conftest.py | 22 ++++++++++--- tests/test_submit.py | 76 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/src/submit/submit.py b/src/submit/submit.py index a1fd429..9ea598e 100644 --- a/src/submit/submit.py +++ b/src/submit/submit.py @@ -62,9 +62,9 @@ def _cut_code( if start_match and end_match: code = re.sub(rf"{NEGATIVE_COMMENT_BEGIN}.*?{NEGATIVE_COMMENT_END}", "", code, flags=re_flags) elif start_match is not None and end_match is None: - click.secho(f"源文件 {file.name} 中存在 NEGATIVE 模式的起始注释,但未找到对应的终止注释!", fg="red") + click.secho(f"源文件 {file.name} 中存在 NEGATIVE 模式的起始注释,但未找到对应的终止注释!", fg="red", err=True) elif start_match is None and end_match is not None: - click.secho(f"源文件 {file.name} 中存在 NEGATIVE 模式的终止注释,但未找到对应的起始注释!", fg="red") + click.secho(f"源文件 {file.name} 中存在 NEGATIVE 模式的终止注释,但未找到对应的起始注释!", fg="red", err=True) else: pass @@ -75,9 +75,9 @@ def _cut_code( code_segaments = re.findall(rf"{POSITIVE_COMMENT_BEGIN}(.*?){POSITIVE_COMMENT_END}", code, re_flags) code = "\n\n".join(code.strip() for code in code_segaments) elif start_match is not None and end_match is None: - click.secho(f"源文件 {file.name} 中存在 POSITIVE 模式的起始注释,但未找到对应的终止注释!", fg="red") + click.secho(f"源文件 {file.name} 中存在 POSITIVE 模式的起始注释,但未找到对应的终止注释!", fg="red", err=True) elif start_match is None and end_match is not None: - click.secho(f"源文件 {file.name} 中存在 POSITIVE 模式的终止注释,但未找到对应的起始注释!", fg="red") + click.secho(f"源文件 {file.name} 中存在 POSITIVE 模式的终止注释,但未找到对应的起始注释!", fg="red", err=True) else: pass @@ -198,6 +198,7 @@ def copy_directory( click.secho(f"已排除目录:{dirpath.absolute()}", fg="magenta") continue if txt_dir in dirpath.parents or dirpath == txt_dir: # 如果当前目录是目标目录或其子目录,则跳过 + click.secho(f"已跳过目录:{dirpath.absolute()},跳过原因:输出目录与输入目录是同一目录,或输出目录在输入目录内", fg="magenta") continue for file in filenames: fileabspath = dirpath / file diff --git a/tests/conftest.py b/tests/conftest.py index 843696e..99abc72 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,21 +10,35 @@ def dummy_sas_dir(tmp_path: Path) -> Path: src_dir = tmp_path / "sas_src" src_dir.mkdir() - # 1. 创建一个包含 POSITIVE 标记的文件 + # 包含 POSITIVE 标记的文件 file_1 = src_dir / "t_6_1.sas" file_1.write_text("data _null_;\n/*SUBMIT BEGIN*/\nproc print data=sashelp.class;\nrun;\n/*SUBMIT END*/\nrun;", encoding="gbk") - # 2. 创建一个包含 NEGATIVE 标记的文件 + # 包含 NEGATIVE 标记的文件 file2 = src_dir / "t_6_2.sas" file2.write_text("/*NOT SUBMIT BEGIN*/\noptions nodate;\n/*NOT SUBMIT END*/\nproc means data=test; run;", encoding="gbk") - # 3. 创建一个在子目录里的文件(用于测试 --exclude-dir 参数) + # 包含不完整 POSITIVE 标记的文件 + file3 = src_dir / "t_6_3.sas" + file3.write_text("/*SUBMIT BEGIN*/\nproc print data=sashelp.class;\nrun;", encoding="gbk") + + file4 = src_dir / "t_6_4.sas" + file4.write_text("proc print data=sashelp.class;\nrun;\n/*SUBMIT END*/", encoding="gbk") + + # 包含不完整 NEGATIVE 标记的文件 + file5 = src_dir / "t_6_5.sas" + file5.write_text("/*NOT SUBMIT BEGIN*/\noptions nodate;\nproc means data=test; run;", encoding="gbk") + + file6 = src_dir / "t_6_6.sas" + file6.write_text("proc means; run;\n/*NOT SUBMIT END*/", encoding="gbk") + + # 在子目录里的文件(用于测试 --exclude-dir 参数) sub_dir = src_dir / "sponser_only" sub_dir.mkdir() file3 = sub_dir / "t_7_1.sas" file3.write_text("proc gplot; run;", encoding="gbk") - # 4. 创建一个不需要转换的文件(用于测试 --exclude-file 参数) + # 不需要转换的文件(用于测试 --exclude-file 参数) file3 = src_dir / "deprecated_t_8_1.sas" file3.write_text("proc means; run;", encoding="gbk") diff --git a/tests/test_submit.py b/tests/test_submit.py index 6fa9cea..39a5b22 100644 --- a/tests/test_submit.py +++ b/tests/test_submit.py @@ -90,7 +90,7 @@ def test_copydir_with_exclude_dirs(dummy_sas_dir: Path, tmp_path: Path) -> None: assert not (txt_dir / "t_7_1.txt").exists() -def test_copydir_with_merge(tmp_path: Path, dummy_sas_dir: Path) -> None: +def test_copydir_with_merge(dummy_sas_dir: Path, tmp_path: Path) -> None: """测试 copydir 命令,带上 --merge 参数""" runner = CliRunner() @@ -124,3 +124,77 @@ def test_copydir_with_merge(tmp_path: Path, dummy_sas_dir: Path) -> None: # 验证合并代码中的内容是否包含特定字符串 merge_content = merge_file.read_text(encoding="gbk") assert "/*====================t_6_1.sas====================*/" in merge_content + + +def test_cutcode_incomplete_comment(dummy_sas_dir: Path, tmp_path: Path) -> None: + """测试 cutcode 对不完整标记的识别""" + + runner = CliRunner() + + txt_dir = tmp_path / "txt_out" + + result = runner.invoke( + cli, + [ + "copydir", + "-s", + str(dummy_sas_dir), + "-t", + str(txt_dir), + ], + ) + + assert result.exit_code == 0 + + assert "存在 POSITIVE 模式的起始注释,但未找到对应的终止注释" in result.stderr + assert "存在 POSITIVE 模式的终止注释,但未找到对应的起始注释" in result.stderr + assert "存在 NEGATIVE 模式的起始注释,但未找到对应的终止注释" in result.stderr + assert "存在 NEGATIVE 模式的终止注释,但未找到对应的起始注释" in result.stderr + + +def test_copydir_no_files_need_process(dummy_sas_dir: Path, tmp_path: Path) -> None: + """测试 copydir 命令,没有需要处理的文件时""" + + runner = CliRunner() + + txt_dir = tmp_path / "txt_out" + + result = runner.invoke( + cli, + [ + "copydir", + "-s", + str(dummy_sas_dir), + "-t", + str(txt_dir), + "--exclude-file", + "**/*.sas", + ], + ) + + assert result.exit_code == 0 + + assert "未找到需要处理的 .sas 文件" in result.stdout + + +def test_copydir_output_dir_inside_input_dir(dummy_sas_dir: Path, tmp_path: Path) -> None: + """测试 copydir 命令,输出目录在输入目录内""" + + runner = CliRunner() + + txt_dir = dummy_sas_dir / "sponser_only" + + result = runner.invoke( + cli, + [ + "copydir", + "-s", + str(dummy_sas_dir), + "-t", + str(txt_dir), + ], + ) + + assert result.exit_code == 0 + + assert "输出目录与输入目录是同一目录,或输出目录在输入目录内" in result.stdout