@@ -3681,6 +3681,163 @@ def test_cli_converters_no_converters(self):
36813681 f .write ("/*[clinic input]\n [clinic start generated code]*/\n " )
36823682 self .assertEqual (self .expect_success ("--converters" , fn ), "" )
36833683
3684+ LIST_CODE = dedent ("""
3685+ /*[clinic input]
3686+ func
3687+ a: int
3688+ /
3689+
3690+ Docstring.
3691+ [clinic start generated code]*/
3692+
3693+ /*[clinic input]
3694+ cloned = func
3695+ [clinic start generated code]*/
3696+
3697+ /*[clinic input]
3698+ module m
3699+ class m.C "void *" ""
3700+ class m.C.D "void *" ""
3701+ [clinic start generated code]*/
3702+
3703+ /*[clinic input]
3704+ m.C.meth
3705+ self: self(type="void *")
3706+ a: object
3707+ [
3708+ b: object
3709+ ]
3710+ /
3711+
3712+ Docstring.
3713+ [clinic start generated code]*/
3714+
3715+ /*[clinic input]
3716+ @classmethod
3717+ m.C.__new__
3718+ a: object
3719+
3720+ Docstring.
3721+ [clinic start generated code]*/
3722+
3723+ /*[clinic input]
3724+ @getter
3725+ m.C.prop
3726+ [clinic start generated code]*/
3727+
3728+ /*[clinic input]
3729+ @setter
3730+ m.C.prop
3731+ [clinic start generated code]*/
3732+
3733+ /*[clinic input]
3734+ m.C.D.meth
3735+ self: self(type="void *")
3736+
3737+ Docstring.
3738+ [clinic start generated code]*/
3739+ """ )
3740+
3741+ def make_list_file (self , tmp_dir ):
3742+ fn = os .path .join (tmp_dir , "test.c" )
3743+ with open (fn , "w" , encoding = "utf-8" ) as f :
3744+ f .write (self .LIST_CODE )
3745+ return fn
3746+
3747+ LIST_OUTPUT = [
3748+ " func($module, a, /)" ,
3749+ " cloned($module, a, /)" ,
3750+ " module m" ,
3751+ " class m.C" ,
3752+ # A signature with an option group is only for the docstring.
3753+ " m.C.meth(a, [b])" ,
3754+ " m.C(a)" ,
3755+ " getter m.C.prop" ,
3756+ " setter m.C.prop" ,
3757+ " class m.C.D" ,
3758+ " m.C.D.meth($self, /)" ,
3759+ ]
3760+
3761+ def test_cli_list (self ):
3762+ with os_helper .temp_dir () as tmp_dir :
3763+ fn = self .make_list_file (tmp_dir )
3764+ pre_mtime = os .stat (fn ).st_mtime_ns
3765+ out = self .expect_success ("--list" , fn )
3766+ self .assertEqual (out .splitlines (), [fn ] + self .LIST_OUTPUT )
3767+ # Nothing is written.
3768+ with open (fn , encoding = "utf-8" ) as f :
3769+ self .assertEqual (f .read (), self .LIST_CODE )
3770+ self .assertEqual (os .stat (fn ).st_mtime_ns , pre_mtime )
3771+ self .assertEqual (os .listdir (tmp_dir ), ["test.c" ])
3772+
3773+ def test_cli_list_no_clinic_block (self ):
3774+ with os_helper .temp_dir () as tmp_dir :
3775+ fn = os .path .join (tmp_dir , "test.c" )
3776+ with open (fn , "w" , encoding = "utf-8" ) as f :
3777+ f .write ("int x;\n " )
3778+ self .assertEqual (self .expect_success ("--list" , fn ), "" )
3779+
3780+ def test_cli_list_no_definitions (self ):
3781+ with os_helper .temp_dir () as tmp_dir :
3782+ fn = os .path .join (tmp_dir , "test.c" )
3783+ with open (fn , "w" , encoding = "utf-8" ) as f :
3784+ f .write ("/*[clinic input]\n [clinic start generated code]*/\n " )
3785+ self .assertEqual (self .expect_success ("--list" , fn ), "" )
3786+
3787+ def test_cli_list_make (self ):
3788+ with os_helper .temp_dir () as tmp_dir :
3789+ fn = self .make_list_file (tmp_dir )
3790+ out = self .expect_success ("--list" , "--make" , "--srcdir" , tmp_dir )
3791+ self .assertEqual (out .splitlines (), [fn ] + self .LIST_OUTPUT )
3792+ self .assertEqual (os .listdir (tmp_dir ), ["test.c" ])
3793+
3794+ def test_cli_list_verbose (self ):
3795+ with os_helper .temp_dir () as tmp_dir :
3796+ fn = self .make_list_file (tmp_dir )
3797+ # The progress does not mix with the report.
3798+ out , err , code = self .run_clinic ("-v" , "--list" , fn )
3799+ self .assertEqual (code , 0 )
3800+ self .assertEqual (err .splitlines (), [fn ])
3801+ self .assertEqual (out .splitlines (), [fn ] + self .LIST_OUTPUT )
3802+
3803+ def test_cli_list_checksum_mismatch (self ):
3804+ with os_helper .temp_dir () as tmp_dir :
3805+ fn = self .make_list_file (tmp_dir )
3806+ with open (fn , "a" , encoding = "utf-8" ) as f :
3807+ f .write ("/*[clinic end generated code: "
3808+ "output=0123456789abcdef input=fedcba9876543210]*/\n " )
3809+ _ , err = self .expect_failure ("--list" , fn )
3810+ self .assertIn ("Checksum mismatch!" , err )
3811+ # The check is skipped with --force.
3812+ out = self .expect_success ("-f" , "--list" , fn )
3813+ self .assertEqual (out .splitlines (), [fn ] + self .LIST_OUTPUT )
3814+ self .assertEqual (os .listdir (tmp_dir ), ["test.c" ])
3815+
3816+ def test_cli_list_external (self ):
3817+ # A file which uses getters, setters and nested classes.
3818+ source = support .findfile ('clinic.test.c' )
3819+ out = self .expect_success ("--list" , source )
3820+ lines = out .splitlines ()
3821+ self .assertEqual (lines [0 ], source )
3822+ for line in (" class Test" ,
3823+ " getter Test.property" ,
3824+ " setter Test.property" ,
3825+ " Test.class_method($type, /)" ,
3826+ " module m" ,
3827+ " class m.T" ):
3828+ with self .subTest (line = line ):
3829+ self .assertIn (line , lines )
3830+
3831+ def test_cli_fail_list_and_dry_run (self ):
3832+ for opt in "--dry-run" , "--diff" :
3833+ with self .subTest (opt = opt ):
3834+ _ , err = self .expect_failure ("--list" , opt , "test.c" )
3835+ self .assertIn ("can't use --dry-run or --diff with --list" , err )
3836+
3837+ def test_cli_fail_list_and_converters (self ):
3838+ _ , err = self .expect_failure ("--list" , "--converters" , "test.c" )
3839+ self .assertIn ("can't use --converters with --list" , err )
3840+
36843841 def test_cli_fail_directory (self ):
36853842 with os_helper .temp_dir () as tmp_dir :
36863843 subdir = os .path .join (tmp_dir , "test.c" )
0 commit comments