1818sample usage: src/etc/generate-deriving-span-tests.py
1919"""
2020
21- import sys , os , datetime , stat
21+ import sys , os , datetime , stat , re
2222
2323TEST_DIR = os .path .abspath (
2424 os .path .join (os .path .dirname (__file__ ), '../test/compile-fail' ))
@@ -87,16 +87,25 @@ def create_test_case(type, trait, super_traits, error_count):
8787def write_file (name , string ):
8888 test_file = os .path .join (TEST_DIR , 'derives-span-%s.rs' % name )
8989
90+ with open (test_file ) as f :
91+ old_str = f .read ()
92+ old_str_ignoring_date = re .sub (r'^// Copyright \d+' ,
93+ '// Copyright {year}' .format (year = YEAR ), old_str )
94+ if old_str_ignoring_date == string :
95+ # if all we're doing is updating the copyright year, ignore it
96+ return 0
97+
9098 # set write permission if file exists, so it can be changed
9199 if os .path .exists (test_file ):
92100 os .chmod (test_file , stat .S_IWUSR )
93101
94- with open (test_file , 'wt ' ) as f :
102+ with open (test_file , 'w ' ) as f :
95103 f .write (string )
96104
97105 # mark file read-only
98106 os .chmod (test_file , stat .S_IRUSR | stat .S_IRGRP | stat .S_IROTH )
99107
108+ return 1
100109
101110
102111ENUM = 1
@@ -120,11 +129,15 @@ def write_file(name, string):
120129 ('Hash' , [], 1 )]:
121130 traits [trait ] = (ALL , supers , errs )
122131
132+ files = 0
133+
123134for (trait , (types , super_traits , error_count )) in traits .items ():
124135 mk = lambda ty : create_test_case (ty , trait , super_traits , error_count )
125136 if types & ENUM :
126- write_file (trait + '-enum' , mk (ENUM_TUPLE ))
127- write_file (trait + '-enum-struct-variant' , mk (ENUM_STRUCT ))
137+ files += write_file (trait + '-enum' , mk (ENUM_TUPLE ))
138+ files += write_file (trait + '-enum-struct-variant' , mk (ENUM_STRUCT ))
128139 if types & STRUCT :
129- write_file (trait + '-struct' , mk (STRUCT_FIELDS ))
130- write_file (trait + '-tuple-struct' , mk (STRUCT_TUPLE ))
140+ files += write_file (trait + '-struct' , mk (STRUCT_FIELDS ))
141+ files += write_file (trait + '-tuple-struct' , mk (STRUCT_TUPLE ))
142+
143+ print ('Generated {files} deriving span test{}.' .format ('s' if files != 1 else '' , files = files ))
0 commit comments