1+ import unittest
2+ import os
3+ import shutil
4+ import tempfile
5+ import sys
6+ from unittest .mock import patch , ANY
7+
8+ sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' , 'scripts' )))
9+ from copy_files import process_input , main
10+
11+ class TestCopyFiles (unittest .TestCase ):
12+
13+ def setUp (self ):
14+ # 创建一个临时目录作为测试环境
15+ self .test_dir = tempfile .mkdtemp ()
16+ self .build_dir = os .path .join (self .test_dir , 'build' )
17+ os .makedirs (self .build_dir )
18+ self .source_dir = os .path .join (self .test_dir , 'source' )
19+ os .makedirs (self .source_dir )
20+
21+ # 创建一些测试文件和目录
22+ self .test_file1 = os .path .join (self .source_dir , 'file1.txt' )
23+ self .test_file2 = os .path .join (self .source_dir , 'file2.py' )
24+ self .test_subdir = os .path .join (self .source_dir , 'subdir' )
25+ os .makedirs (self .test_subdir )
26+ self .test_subfile = os .path .join (self .test_subdir , 'subfile.txt' )
27+
28+ with open (self .test_file1 , 'w' ) as f :
29+ f .write ('This is file1.' )
30+ with open (self .test_file2 , 'w' ) as f :
31+ f .write ('print("This is file2.")' )
32+ with open (self .test_subfile , 'w' ) as f :
33+ f .write ('This is a subfile.' )
34+
35+ def tearDown (self ):
36+ # 清理临时目录
37+ shutil .rmtree (self .test_dir )
38+
39+ def test_process_input_empty (self ):
40+ self .assertEqual (process_input ('' ), [])
41+
42+ def test_process_input_single_line (self ):
43+ self .assertEqual (process_input ('file.txt' ), ['file.txt' ])
44+
45+ def test_process_input_multiple_lines (self ):
46+ input_str = 'file1.txt\n file2.py\n spaced_file.txt \n \n '
47+ expected = ['file1.txt' , 'file2.py' , 'spaced_file.txt' ]
48+ self .assertEqual (process_input (input_str ), expected )
49+
50+ @patch ('sys.exit' )
51+ @patch ('builtins.print' )
52+ def test_main_missing_env_var (self , mock_print , mock_exit ):
53+ with patch .dict (os .environ , {}, clear = True ):
54+ main ()
55+ mock_exit .assert_called_once_with (1 )
56+ mock_print .assert_any_call (ANY ) # Check for any error message
57+
58+ @patch ('builtins.print' )
59+ def test_main_copy_operations (self , mock_print ):
60+ with patch .dict (os .environ , {
61+ 'BUILD_PATH' : self .build_dir ,
62+ 'INCLUDED_FILES' : f'{ self .test_file1 } \n { self .test_subdir } \n non_existent_file.txt' ,
63+ 'PYSTAND_ENTRY_FILE' : self .test_file2 ,
64+ 'APPLICATION_NAME' : 'my_app'
65+ }):
66+ # 确保在测试前 build_dir 是空的
67+ for item in os .listdir (self .build_dir ):
68+ shutil .rmtree (os .path .join (self .build_dir , item )) if os .path .isdir (os .path .join (self .build_dir , item )) else os .remove (os .path .join (self .build_dir , item ))
69+
70+ main ()
71+
72+ # 检查文件和目录是否被复制
73+ self .assertTrue (os .path .exists (os .path .join (self .build_dir , 'file1.txt' )))
74+ self .assertTrue (os .path .isdir (os .path .join (self .build_dir , 'subdir' )))
75+ self .assertTrue (os .path .exists (os .path .join (self .build_dir , 'subdir' , 'subfile.txt' )))
76+ self .assertTrue (os .path .exists (os .path .join (self .build_dir , 'my_app.py' )))
77+
78+ # 检查警告信息
79+ print_args_list = [call_args [0 ][0 ] for call_args in mock_print .call_args_list ]
80+ self .assertTrue (any ("Item does not exist, skipped." in args for args in print_args_list ))
81+ self .assertTrue (any ("non_existent_file.txt" in args for args in print_args_list ))
82+
83+ @patch ('builtins.print' )
84+ def test_main_only_entry_file (self , mock_print ):
85+ with patch .dict (os .environ , {
86+ 'BUILD_PATH' : self .build_dir ,
87+ 'INCLUDED_FILES' : '' ,
88+ 'PYSTAND_ENTRY_FILE' : self .test_file1 ,
89+ 'APPLICATION_NAME' : 'app_with_no_includes'
90+ }):
91+ # 确保在测试前 build_dir 是空的
92+ for item in os .listdir (self .build_dir ):
93+ shutil .rmtree (os .path .join (self .build_dir , item )) if os .path .isdir (os .path .join (self .build_dir , item )) else os .remove (os .path .join (self .build_dir , item ))
94+
95+ main ()
96+ self .assertTrue (os .path .exists (os .path .join (self .build_dir , 'app_with_no_includes.py' )))
97+ self .assertFalse (os .path .exists (os .path .join (self .build_dir , 'file1.txt' )))
98+
99+
100+ if __name__ == '__main__' :
101+ unittest .main ()
0 commit comments